Reimplemented RSSHandler

This commit is contained in:
daniel oeh 2012-06-08 22:48:25 +02:00
parent 1cf9cf889f
commit 06688a139a
6 changed files with 64 additions and 104 deletions

View File

@ -20,11 +20,12 @@ public class Feed extends FeedFile{
public Feed() {
super();
items = new ArrayList<FeedItem>();
}
public Feed(String url) {
super();
this();
this.download_url = url;
}

View File

@ -12,19 +12,7 @@ import org.xml.sax.SAXException;
import de.podfetcher.feed.Feed;
public class FeedHandler {
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 Feed parseFeed(Feed feed) {
TypeGetter tg = new TypeGetter();
@ -42,6 +30,6 @@ public class FeedHandler {
e.printStackTrace();
}
return handler.feed;
return handler.state.feed;
}
}

View File

@ -17,8 +17,8 @@ public class HandlerState {
/** Namespaces that have been defined so far. */
protected HashMap<String, Namespace> namespaces;
public HandlerState() {
feed = new Feed();
public HandlerState(Feed feed) {
this.feed = feed;
tagstack = new Stack<String>();
namespaces = new HashMap<String, Namespace>();
}

View File

@ -27,28 +27,49 @@ public class RSSHandler extends SyndHandler {
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;
public Feed feed;
public String active_root_element; // channel or item or image
public String active_sub_element; // Not channel or item
public RSSHandler(Feed f) {
super();
this.feed = f;
public RSSHandler(Feed feed) {
super(feed);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (active_sub_element != null) {
strBuilder.append(ch, start, length);
if (state.tagstack.size() >= 2) {
String content = new String(ch, start, length);
String top = state.tagstack.pop();
String second = state.tagstack.peek();
state.tagstack.push(top);
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);
}
}
}
@ -56,76 +77,30 @@ public class RSSHandler extends SyndHandler {
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equals(ITEM)) {
currentItem.setFeed(feed);
items.add(currentItem);
} else if (localName.equals(TITLE)) {
if (active_root_element.equals(CHANNEL)) {
feed.setTitle(strBuilder.toString());
} else if(active_root_element.equals(ITEM)) {
currentItem.setTitle(strBuilder.toString());
} else if(active_root_element.equals(IMAGE)) {
feed.getImage().setTitle(strBuilder.toString());
}
} else if (localName.equals(DESCR)) {
if (active_root_element.equals(CHANNEL)) {
feed.setDescription(strBuilder.toString());
} else {
currentItem.setDescription(strBuilder.toString());
}
} else if (localName.equals(LINK)) {
if (active_root_element.equals(CHANNEL)) {
feed.setLink(strBuilder.toString());
} else if(active_root_element.equals(ITEM)){
currentItem.setLink(strBuilder.toString());
}
} else if (localName.equals(PUBDATE)) {
if (active_root_element.equals(ITEM)) {
currentItem.setPubDate(strBuilder.toString());
}
} else if (localName.equals(URL)) {
if(active_root_element.equals(IMAGE)) {
feed.getImage().setDownload_url(strBuilder.toString());
}
} else if(localName.equals(IMAGE)) {
active_root_element = CHANNEL;
state.currentItem = null;
}
active_sub_element = null;
strBuilder = new StringBuilder();
super.endElement(uri, localName, qName);
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals(CHANNEL)) {
if(feed == null) {
feed = new Feed();
}
active_root_element = localName;
} else if (localName.equals(ITEM)) {
currentItem = new FeedItem();
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;
if (localName.equals(ITEM)) {
state.currentItem = new FeedItem();
state.feed.getItems().add(state.currentItem);
state.currentItem.setFeed(state.feed);
} else if (localName.equals(ENCLOSURE)) {
currentItem.setMedia(new FeedMedia(currentItem,
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 = localName;
} else if(localName.equals(URL)) {
active_sub_element = qName;
state.currentItem
.setMedia(new FeedMedia(state.currentItem, attributes
.getValue(ENC_URL), Long.parseLong(attributes
.getValue(ENC_LEN)), attributes.getValue(ENC_TYPE)));
} else if (localName.equals(IMAGE)) {
state.feed.setImage(new FeedImage());
}
super.startElement(uri, localName, qName, attributes);
}
}

View File

@ -4,14 +4,20 @@ import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
import de.podfetcher.feed.Feed;
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{
private static final String TAG = "SyndHandler";
protected HandlerState state;
public SyndHandler(Feed feed) {
state = new HandlerState(feed);
}
@Override
public void startElement(String uri, String localName, String qName,
@ -45,24 +51,13 @@ public abstract class SyndHandler extends DefaultHandler{
@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
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(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

@ -14,7 +14,7 @@ import android.util.Log;
import de.podfetcher.feed.Feed;
/** Gets the type of a specific feed. */
/** Gets the type of a specific feed by reading the root element. */
public class TypeGetter {
private static final String TAG = "TypeGetter";
@ -33,6 +33,7 @@ public class TypeGetter {
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(createReader(feed));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
String tag = xpp.getName();