Implemented more methods in SyndFeed and more

This commit is contained in:
daniel oeh 2012-06-08 22:06:06 +02:00
parent b1dbb0f45e
commit 1cf9cf889f
6 changed files with 128 additions and 66 deletions

View File

@ -1,6 +1,7 @@
package de.podfetcher.syndication.handler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;
import de.podfetcher.syndication.namespace.Namespace;
@ -14,7 +15,14 @@ public class HandlerState {
protected FeedItem currentItem;
protected Stack<String> tagstack;
/** Namespaces that have been defined so far. */
protected ArrayList<Namespace> namespaces;
protected HashMap<String, Namespace> namespaces;
public HandlerState() {
feed = new Feed();
tagstack = new Stack<String>();
namespaces = new HashMap<String, Namespace>();
}
public Feed getFeed() {
return feed;

View File

@ -17,7 +17,21 @@ import org.xml.sax.helpers.DefaultHandler;
* @author daniel
*
*/
public class RSSHandler extends DefaultHandler {
public class RSSHandler extends SyndHandler {
public final static String CHANNEL = "channel";
public final static String ITEM = "item";
public final static String TITLE = "title";
public final static String LINK = "link";
public final static String DESCR = "description";
public final static String PUBDATE = "pubDate";
public final static String ENCLOSURE = "enclosure";
public final static String IMAGE = "image";
public final static String URL = "url";
public final static String ENC_URL = "url";
public final static String ENC_LEN = "length";
public final static String ENC_TYPE = "type";
public ArrayList<FeedItem> items;
public FeedItem currentItem;
public StringBuilder strBuilder;
@ -38,90 +52,80 @@ public class RSSHandler extends DefaultHandler {
}
}
@Override
public void endDocument() throws SAXException {
feed.setItems(items);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase(FeedHandler.ITEM)) {
if (localName.equals(ITEM)) {
currentItem.setFeed(feed);
items.add(currentItem);
} else if (qName.equalsIgnoreCase(FeedHandler.TITLE)) {
if (active_root_element.equalsIgnoreCase(FeedHandler.CHANNEL)) {
} else if (localName.equals(TITLE)) {
if (active_root_element.equals(CHANNEL)) {
feed.setTitle(strBuilder.toString());
} else if(active_root_element.equalsIgnoreCase(FeedHandler.ITEM)) {
} else if(active_root_element.equals(ITEM)) {
currentItem.setTitle(strBuilder.toString());
} else if(active_root_element.equalsIgnoreCase(FeedHandler.IMAGE)) {
} else if(active_root_element.equals(IMAGE)) {
feed.getImage().setTitle(strBuilder.toString());
}
} else if (qName.equalsIgnoreCase(FeedHandler.DESCR)) {
if (active_root_element.equalsIgnoreCase(FeedHandler.CHANNEL)) {
} else if (localName.equals(DESCR)) {
if (active_root_element.equals(CHANNEL)) {
feed.setDescription(strBuilder.toString());
} else {
currentItem.setDescription(strBuilder.toString());
}
} else if (qName.equalsIgnoreCase(FeedHandler.LINK)) {
if (active_root_element.equalsIgnoreCase(FeedHandler.CHANNEL)) {
} else if (localName.equals(LINK)) {
if (active_root_element.equals(CHANNEL)) {
feed.setLink(strBuilder.toString());
} else if(active_root_element.equalsIgnoreCase(FeedHandler.ITEM)){
} else if(active_root_element.equals(ITEM)){
currentItem.setLink(strBuilder.toString());
}
} else if (qName.equalsIgnoreCase(FeedHandler.PUBDATE)) {
if (active_root_element.equalsIgnoreCase(FeedHandler.ITEM)) {
} else if (localName.equals(PUBDATE)) {
if (active_root_element.equals(ITEM)) {
currentItem.setPubDate(strBuilder.toString());
}
} else if (qName.equalsIgnoreCase(FeedHandler.URL)) {
if(active_root_element.equalsIgnoreCase(FeedHandler.IMAGE)) {
} else if (localName.equals(URL)) {
if(active_root_element.equals(IMAGE)) {
feed.getImage().setDownload_url(strBuilder.toString());
}
} else if(qName.equalsIgnoreCase(FeedHandler.IMAGE)) {
active_root_element = FeedHandler.CHANNEL;
} else if(localName.equals(IMAGE)) {
active_root_element = CHANNEL;
}
active_sub_element = null;
strBuilder = new StringBuilder();
}
@Override
public void startDocument() throws SAXException {
items = new ArrayList<FeedItem>();
strBuilder = new StringBuilder();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase(FeedHandler.CHANNEL)) {
if (localName.equals(CHANNEL)) {
if(feed == null) {
feed = new Feed();
}
active_root_element = qName;
} else if (qName.equalsIgnoreCase(FeedHandler.ITEM)) {
active_root_element = localName;
} else if (localName.equals(ITEM)) {
currentItem = new FeedItem();
active_root_element = qName;
} else if (qName.equalsIgnoreCase(FeedHandler.TITLE)) {
active_sub_element = qName;
} else if (qName.equalsIgnoreCase(FeedHandler.DESCR)) {
active_sub_element = qName;
} else if (qName.equalsIgnoreCase(FeedHandler.LINK)) {
active_sub_element = qName;
} else if (qName.equalsIgnoreCase(FeedHandler.PUBDATE)) {
active_sub_element = qName;
} else if (qName.equalsIgnoreCase(FeedHandler.ENCLOSURE)) {
active_root_element = localName;
} else if (localName.equals(TITLE)) {
active_sub_element = localName;
} else if (localName.equals(DESCR)) {
active_sub_element = localName;
} else if (localName.equals(LINK)) {
active_sub_element = localName;
} else if (localName.equals(PUBDATE)) {
active_sub_element = localName;
} else if (localName.equals(ENCLOSURE)) {
currentItem.setMedia(new FeedMedia(currentItem,
attributes.getValue(FeedHandler.ENC_URL),
Long.parseLong(attributes.getValue(FeedHandler.ENC_LEN)),
attributes.getValue(FeedHandler.ENC_TYPE)));
} else if(qName.equalsIgnoreCase(FeedHandler.IMAGE)) {
attributes.getValue(ENC_URL),
Long.parseLong(attributes.getValue(ENC_LEN)),
attributes.getValue(ENC_TYPE)));
} else if(localName.equals(IMAGE)) {
feed.setImage(new FeedImage());
active_root_element = qName;
} else if(qName.equalsIgnoreCase(FeedHandler.URL)) {
active_root_element = localName;
} else if(localName.equals(URL)) {
active_sub_element = qName;
}
super.startElement(uri, localName, qName, attributes);
}
}

View File

@ -1,14 +1,68 @@
package de.podfetcher.syndication.handler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedItem;
import de.podfetcher.syndication.namespace.Namespace;
import de.podfetcher.syndication.namespace.atom.NSAtom;
/** Superclass for all SAX Handlers which process Syndication formats */
public abstract class SyndHandler extends DefaultHandler{
protected HandlerState state;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
state.tagstack.push(qName);
String[] parts = qName.split(":");
Namespace handler = state.namespaces.get(parts[0]);
if (handler != null) {
handler.handleElement(localName, state, attributes);
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
state.tagstack.pop();
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
state.namespaces.remove(prefix);
}
@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
// Find the right namespace
if (prefix.equals(NSAtom.NSTAG) || uri.equals(NSAtom.NSURI)) {
state.namespaces.put(prefix, new NSAtom());
}
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void startDocument() throws SAXException {
state = new HandlerState();
}
public HandlerState getState() {
return state;
}

View File

@ -3,9 +3,10 @@ package de.podfetcher.syndication.namespace;
import org.xml.sax.Attributes;
import de.podfetcher.feed.Feed;
import de.podfetcher.syndication.handler.HandlerState;
/** Defines a XML Element of a specific namespace */
public abstract class NSElement {
/** Called by its namespace if the processing of the element gets more complex */
public abstract void handleElement(String localName, Feed feed, Attributes attributes);
public abstract void handleElement(String localName, HandlerState state, Attributes attributes);
}

View File

@ -3,16 +3,17 @@ package de.podfetcher.syndication.namespace;
import org.xml.sax.Attributes;
import de.podfetcher.feed.Feed;
import de.podfetcher.syndication.handler.HandlerState;
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, Feed feed, Attributes attributes);
public abstract void handleElement(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 String getNsTag();
public abstract String getNsURI();
}

View File

@ -3,16 +3,20 @@ package de.podfetcher.syndication.namespace.atom;
import org.xml.sax.Attributes;
import de.podfetcher.feed.Feed;
import de.podfetcher.syndication.handler.HandlerState;
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, Feed feed, Attributes attributes) {
public void handleElement(String localName, HandlerState state, Attributes attributes) {
if (localName.equals(TITLE)) {
}
@ -25,14 +29,4 @@ public class NSAtom extends Namespace {
}
@Override
public String getNsTag() {
return "atom";
}
@Override
public String getNsURI() {
return "http://www.w3.org/2005/Atom";
}
}