Converted RSSHandler into RSS 2.0 Namespace

This commit is contained in:
daniel oeh 2012-06-09 12:34:18 +02:00
parent b19afaa82c
commit a0237d6035
7 changed files with 127 additions and 91 deletions

View File

@ -17,7 +17,7 @@ public class FeedHandler {
public Feed parseFeed(Feed feed) {
TypeGetter tg = new TypeGetter();
tg.getType(feed);
RSSHandler handler = new RSSHandler(feed);
SyndHandler handler = new SyndHandler(feed);
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);

View File

@ -34,6 +34,18 @@ public class HandlerState {
public Stack<SyndElement> getTagstack() {
return tagstack;
}
public void setFeed(Feed feed) {
this.feed = feed;
}
public void setCurrentItem(FeedItem currentItem) {
this.currentItem = currentItem;
}
}

View File

@ -10,49 +10,64 @@ import de.podfetcher.feed.Feed;
import de.podfetcher.syndication.namespace.Namespace;
import de.podfetcher.syndication.namespace.SyndElement;
import de.podfetcher.syndication.namespace.atom.NSAtom;
import de.podfetcher.syndication.namespace.rss20.NSRSS20;
// TODO implement default namespace
/** Superclass for all SAX Handlers which process Syndication formats */
public abstract class SyndHandler extends DefaultHandler{
public class SyndHandler extends DefaultHandler {
private static final String TAG = "SyndHandler";
protected HandlerState state;
public SyndHandler(Feed feed) {
state = new HandlerState(feed);
state.namespaces.put("", new NSRSS20()); // TODO remove later
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
state.tagstack.push(new SyndElement(qName));
Namespace handler = state.namespaces.get(uri);
if (handler != null) {
handler.handleElement(localName, state, attributes);
handler.handleElementStart(localName, state, attributes);
state.tagstack.push(new SyndElement(localName, handler));
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
SyndElement top = state.tagstack.peek();
if (top.getNamespace() != null) {
top.getNamespace().handleCharacters(state, ch, start, length);
}
// ignore element otherwise
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
state.tagstack.pop();
Namespace handler = state.namespaces.get(uri);
if (handler != null) {
handler.handleElementEnd(localName, state);
state.tagstack.pop();
}
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
// TODO remove Namespace
}
@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
Log.d(TAG, "Found Prefix Mapping with prefix " + prefix + " and uri " + uri);
Log.d(TAG, "Found Prefix Mapping with prefix " + prefix + " and uri "
+ uri);
// Find the right namespace
if (prefix.equals(NSAtom.NSTAG) || uri.equals(NSAtom.NSURI)) {
state.namespaces.put(uri, new NSAtom());
@ -62,5 +77,5 @@ public abstract class SyndHandler extends DefaultHandler{
public HandlerState getState() {
return state;
}
}

View File

@ -10,10 +10,17 @@ public abstract class Namespace {
public static final String NSTAG = null;
public static final String NSURI = null;
/** Called by a Feedhandler when in startElement and it detects a namespace element */
public abstract void handleElement(String localName, HandlerState state, Attributes attributes);
/** Called by a Feedhandler when in startElement and it detects a namespace element
* @return true if namespace handled the element, false if it ignored it
* */
public abstract void handleElementStart(String localName, HandlerState state, Attributes attributes);
/** Called by a Feedhandler when in characters and it detects a namespace element */
public abstract void handleCharacters(String localName, Feed feed, char ch[], int start, int length);
public abstract void handleCharacters(HandlerState state, char ch[], int start, int length);
/** Called by a Feedhandler when in endElement and it detects a namespace element
* @return true if namespace handled the element, false if it ignored it
* */
public abstract void handleElementEnd(String localName, HandlerState state);
}

View File

@ -6,12 +6,12 @@ public class SyndElement {
protected Namespace namespace;
public SyndElement(String name, Namespace namespace) {
super();
this.name = name;
this.namespace = namespace;
}
public Namespace getNamespace() {
return null;
return namespace;
}
public String getName() {

View File

@ -9,24 +9,29 @@ import de.podfetcher.syndication.namespace.Namespace;
public class NSAtom extends Namespace {
public static final String NSTAG = "atom";
public static final String NSURI = "http://www.w3.org/2005/Atom";
private static final String TITLE = "title";
private static final String LINK = "link";
private static final String UPDATED = "updated";
private static final String AUTHOR = "author";
@Override
public void handleElement(String localName, HandlerState state, Attributes attributes) {
public void handleElementStart(String localName, HandlerState state,
Attributes attributes) {
if (localName.equals(TITLE)) {
}
}
@Override
public void handleCharacters(HandlerState state, char[] ch, int start,
int length) {
}
@Override
public void handleCharacters(String localName, Feed feed, char[] ch,
int start, int length) {
public void handleElementEnd(String localName, HandlerState state) {
// TODO Auto-generated method stub
}
}

View File

@ -1,4 +1,4 @@
package de.podfetcher.syndication.handler;
package de.podfetcher.syndication.namespace.rss20;
import java.util.ArrayList;
@ -6,6 +6,9 @@ import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedImage;
import de.podfetcher.feed.FeedItem;
import de.podfetcher.feed.FeedMedia;
import de.podfetcher.syndication.handler.HandlerState;
import de.podfetcher.syndication.handler.SyndHandler;
import de.podfetcher.syndication.namespace.Namespace;
import de.podfetcher.syndication.namespace.SyndElement;
import org.xml.sax.Attributes;
@ -18,7 +21,10 @@ import org.xml.sax.helpers.DefaultHandler;
* @author daniel
*
*/
public class RSSHandler extends SyndHandler {
public class NSRSS20 extends Namespace {
public static final String NSTAG = "rss";
public static final String NSURI = "";
public final static String CHANNEL = "channel";
public final static String ITEM = "item";
public final static String TITLE = "title";
@ -33,76 +39,67 @@ public class RSSHandler extends SyndHandler {
public final static String ENC_LEN = "length";
public final static String ENC_TYPE = "type";
public RSSHandler(Feed feed) {
super(feed);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (state.tagstack.size() >= 2) {
String content = new String(ch, start, length);
SyndElement topElement = state.tagstack.pop();
String top = topElement.getName();
String second = state.tagstack.peek().getName();
state.tagstack.push(topElement);
if (top.equals(TITLE)) {
if (second.equals(ITEM)) {
state.currentItem.setTitle(content);
} else if (second.equals(CHANNEL)) {
state.feed.setTitle(content);
} else if (second.equals(IMAGE)) {
state.feed.getImage().setTitle(IMAGE);
}
} else if (top.equals(DESCR)) {
if (second.equals(CHANNEL)) {
state.feed.setDescription(content);
} else if (second.equals(ITEM)) {
state.feed.setDescription(content);
}
} else if (top.equals(LINK)) {
if (second.equals(CHANNEL)) {
state.feed.setLink(content);
} else if (second.equals(ITEM)) {
state.currentItem.setLink(content);
}
} else if (top.equals(PUBDATE) && second.equals(ITEM)) {
state.currentItem.setPubDate(content);
} else if (top.equals(URL) && second.equals(IMAGE)) {
state.feed.getImage().setDownload_url(content);
}
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
public void handleElementStart(String localName, HandlerState state,
Attributes attributes) {
if (localName.equals(ITEM)) {
state.currentItem = null;
}
super.endElement(uri, localName, qName);
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals(ITEM)) {
state.currentItem = new FeedItem();
state.feed.getItems().add(state.currentItem);
state.currentItem.setFeed(state.feed);
state.setCurrentItem(new FeedItem());
state.getFeed().getItems().add(state.getCurrentItem());
state.getCurrentItem().setFeed(state.getFeed());
} else if (localName.equals(ENCLOSURE)) {
state.currentItem
.setMedia(new FeedMedia(state.currentItem, attributes
state.getCurrentItem()
.setMedia(new FeedMedia(state.getCurrentItem(), attributes
.getValue(ENC_URL), Long.parseLong(attributes
.getValue(ENC_LEN)), attributes.getValue(ENC_TYPE)));
} else if (localName.equals(IMAGE)) {
state.feed.setImage(new FeedImage());
state.getFeed().setImage(new FeedImage());
}
}
super.startElement(uri, localName, qName, attributes);
@Override
public void handleCharacters(HandlerState state, char[] ch, int start,
int length) {
if (state.getTagstack().size() >= 2) {
String content = new String(ch, start, length);
SyndElement topElement = state.getTagstack().pop();
String top = topElement.getName();
String second = state.getTagstack().peek().getName();
state.getTagstack().push(topElement);
if (top.equals(TITLE)) {
if (second.equals(ITEM)) {
state.getCurrentItem().setTitle(content);
} else if (second.equals(CHANNEL)) {
state.getFeed().setTitle(content);
} else if (second.equals(IMAGE)) {
state.getFeed().getImage().setTitle(IMAGE);
}
} else if (top.equals(DESCR)) {
if (second.equals(CHANNEL)) {
state.getFeed().setDescription(content);
} else if (second.equals(ITEM)) {
state.getFeed().setDescription(content);
}
} else if (top.equals(LINK)) {
if (second.equals(CHANNEL)) {
state.getFeed().setLink(content);
} else if (second.equals(ITEM)) {
state.getCurrentItem().setLink(content);
}
} else if (top.equals(PUBDATE) && second.equals(ITEM)) {
state.getCurrentItem().setPubDate(content);
} else if (top.equals(URL) && second.equals(IMAGE)) {
state.getFeed().getImage().setDownload_url(content);
}
}
}
@Override
public void handleElementEnd(String localName, HandlerState state) {
if (localName.equals(ITEM)) {
state.setCurrentItem(null);
}
}
}