Implemented Atom namespace

This commit is contained in:
daniel oeh 2012-06-11 11:19:08 +02:00
parent 19053b2b59
commit 105039dd9f
7 changed files with 86 additions and 29 deletions

View File

@ -33,6 +33,7 @@ public class DownloadObserver extends AsyncTask<FeedFile, DownloadObserver.Downl
public DownloadObserver(Context context) { public DownloadObserver(Context context) {
super(); super();
this.context = context; this.context = context;
requester = DownloadRequester.getInstance();
} }
@Override @Override

View File

@ -47,6 +47,14 @@ public class HandlerState {
this.currentItem = currentItem; this.currentItem = currentItem;
} }
/** Returns the SyndElement that comes after the top element of the tagstack. */
public SyndElement getSecondTag() {
SyndElement top = tagstack.pop();
SyndElement second = tagstack.peek();
tagstack.push(top);
return second;
}

View File

@ -12,7 +12,6 @@ import de.podfetcher.syndication.namespace.SyndElement;
import de.podfetcher.syndication.namespace.atom.NSAtom; import de.podfetcher.syndication.namespace.atom.NSAtom;
import de.podfetcher.syndication.namespace.rss20.NSRSS20; import de.podfetcher.syndication.namespace.rss20.NSRSS20;
/** Superclass for all SAX Handlers which process Syndication formats */ /** Superclass for all SAX Handlers which process Syndication formats */
public class SyndHandler extends DefaultHandler { public class SyndHandler extends DefaultHandler {
private static final String TAG = "SyndHandler"; private static final String TAG = "SyndHandler";
@ -32,7 +31,8 @@ public class SyndHandler extends DefaultHandler {
Namespace handler = getHandlingNamespace(uri); Namespace handler = getHandlingNamespace(uri);
if (handler != null) { if (handler != null) {
SyndElement element = handler.handleElementStart(localName, state, attributes); SyndElement element = handler.handleElementStart(localName, state,
attributes);
state.tagstack.push(element); state.tagstack.push(element);
} }
@ -41,10 +41,11 @@ public class SyndHandler extends DefaultHandler {
@Override @Override
public void characters(char[] ch, int start, int length) public void characters(char[] ch, int start, int length)
throws SAXException { throws SAXException {
if (!state.tagstack.empty()) {
SyndElement top = state.tagstack.peek(); SyndElement top = state.tagstack.peek();
if (top.getNamespace() != null) { if (top.getNamespace() != null) {
top.getNamespace().handleCharacters(state, ch, start, length); top.getNamespace().handleCharacters(state, ch, start, length);
}
} }
} }
@ -80,7 +81,7 @@ public class SyndHandler extends DefaultHandler {
private Namespace getHandlingNamespace(String uri) { private Namespace getHandlingNamespace(String uri) {
Namespace handler = state.namespaces.get(uri); Namespace handler = state.namespaces.get(uri);
if (handler == null && uri.equals(DEFAULT_PREFIX) && !state.defaultNamespaces.empty()) { if (handler == null && !state.defaultNamespaces.empty()) {
handler = state.defaultNamespaces.peek(); handler = state.defaultNamespaces.peek();
} }
return handler; return handler;

View File

@ -38,7 +38,7 @@ public class TypeGetter {
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {
String tag = xpp.getName(); String tag = xpp.getName();
if (tag.equals(ATOM_ROOT)) { if (tag.equals(ATOM_ROOT)) {
Log.d(TAG, "Recongnized type Atom"); Log.d(TAG, "Recognized type Atom");
return Type.ATOM; return Type.ATOM;
} else if (tag.equals(RSS_ROOT) } else if (tag.equals(RSS_ROOT)
&& (xpp.getAttributeValue(null, "version") && (xpp.getAttributeValue(null, "version")
@ -46,6 +46,7 @@ public class TypeGetter {
Log.d(TAG, "Recognized type RSS 2.0"); Log.d(TAG, "Recognized type RSS 2.0");
return Type.RSS20; return Type.RSS20;
} else { } else {
Log.d(TAG, "Type is invalid");
return Type.INVALID; return Type.INVALID;
} }
} else { } else {
@ -58,6 +59,7 @@ public class TypeGetter {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
Log.d(TAG, "Type is invalid");
return Type.INVALID; return Type.INVALID;
} }

View File

@ -3,6 +3,7 @@ package de.podfetcher.syndication.namespace.atom;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import de.podfetcher.feed.Feed; import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedImage;
import de.podfetcher.feed.FeedItem; import de.podfetcher.feed.FeedItem;
import de.podfetcher.feed.FeedMedia; import de.podfetcher.feed.FeedMedia;
import de.podfetcher.syndication.handler.HandlerState; import de.podfetcher.syndication.handler.HandlerState;
@ -20,6 +21,9 @@ public class NSAtom extends Namespace {
private static final String UPDATED = "updated"; private static final String UPDATED = "updated";
private static final String AUTHOR = "author"; private static final String AUTHOR = "author";
private static final String CONTENT = "content"; private static final String CONTENT = "content";
private static final String IMAGE = "logo";
private static final String SUBTITLE = "subtitle";
private static final String PUBLISHED = "published";
private static final String TEXT_TYPE = "type"; private static final String TEXT_TYPE = "type";
// Link // Link
@ -34,6 +38,8 @@ public class NSAtom extends Namespace {
private static final String LINK_REL_RELATED = "related"; private static final String LINK_REL_RELATED = "related";
private static final String LINK_REL_SELF = "self"; private static final String LINK_REL_SELF = "self";
/** Regexp to test whether an Element is a Text Element. */
private static final String isText = TITLE + "|" + CONTENT + "|" + "|" + SUBTITLE;
@Override @Override
public SyndElement handleElementStart(String localName, HandlerState state, public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) { Attributes attributes) {
@ -41,29 +47,29 @@ public class NSAtom extends Namespace {
state.setCurrentItem(new FeedItem()); state.setCurrentItem(new FeedItem());
state.getFeed().getItems().add(state.getCurrentItem()); state.getFeed().getItems().add(state.getCurrentItem());
state.getCurrentItem().setFeed(state.getFeed()); state.getCurrentItem().setFeed(state.getFeed());
} else if (localName.equals(TITLE) || localName.equals(CONTENT)) { } else if (localName.matches(isText)) {
String type = attributes.getValue(null, TEXT_TYPE); String type = attributes.getValue(TEXT_TYPE);
return new AtomText(localName, this, type); return new AtomText(localName, this, type);
} else if (localName.equals(LINK)) { } else if (localName.equals(LINK)) {
String href = attributes.getValue(null, LINK_HREF); String href = attributes.getValue(LINK_HREF);
String rel = attributes.getValue(null, LINK_REL); String rel = attributes.getValue(LINK_REL);
SyndElement parent = state.getTagstack().peek(); SyndElement parent = state.getTagstack().peek();
if (parent.getName().equals(ENTRY)) { if (parent.getName().equals(ENTRY)) {
if (rel == null || rel.equals(LINK_REL_ALTERNATE)) { if (rel == null || rel.equals(LINK_REL_ALTERNATE)) {
state.getCurrentItem().setLink(href); state.getCurrentItem().setLink(href);
} else if (rel.equals(LINK_REL_ENCLOSURE)) { } else if (rel.equals(LINK_REL_ENCLOSURE)) {
long size = Long.parseLong(attributes.getValue(null, String strSize = attributes.getValue(LINK_LENGTH);
LINK_LENGTH)); long size = 0;
String type = attributes.getValue(null, LINK_TYPE); if (strSize != null) size = Long.parseLong(strSize);
String download_url = attributes.getValue(null, String type = attributes.getValue(LINK_TYPE);
LINK_REL_ENCLOSURE); String download_url = attributes.getValue(LINK_REL_ENCLOSURE);
state.getCurrentItem().setMedia( state.getCurrentItem().setMedia(
new FeedMedia(state.getCurrentItem(), download_url, new FeedMedia(state.getCurrentItem(), download_url,
size, type)); size, type));
} }
} else if (parent.getName().equals(FEED)) { } else if (parent.getName().equals(FEED)) {
if (rel == null || rel.equals(LINK_REL_ALTERNATE)) { if (rel == null || rel.equals(LINK_REL_ALTERNATE)) {
state.getCurrentItem().setLink(href); state.getFeed().setLink(href);
} }
} }
} }
@ -73,12 +79,50 @@ public class NSAtom extends Namespace {
@Override @Override
public void handleCharacters(HandlerState state, char[] ch, int start, public void handleCharacters(HandlerState state, char[] ch, int start,
int length) { int length) {
if (state.getTagstack().size() >= 2) {
AtomText textElement = null;
String content = new String(ch, start, length);
SyndElement topElement = state.getTagstack().peek();
String top = topElement.getName();
SyndElement secondElement = state.getSecondTag();
String second = secondElement.getName();
if (top.matches(isText)) {
textElement = (AtomText) topElement;
textElement.setContent(content);
}
if (top.equals(TITLE)) {
if (second.equals(FEED)) {
state.getFeed().setTitle(textElement.getProcessedContent());
} else if (second.equals(ENTRY)) {
state.getCurrentItem().setTitle(textElement.getProcessedContent());
}
} else if (top.equals(SUBTITLE)) {
if (second.equals(FEED)) {
state.getFeed().setDescription(textElement.getProcessedContent());
}
} else if (top.equals(CONTENT)) {
if (second.equals(ENTRY)) {
state.getCurrentItem().setDescription(textElement.getProcessedContent());
}
} else if (top.equals(PUBLISHED)) {
if (second.equals(ENTRY)) {
state.getCurrentItem().setPubDate(content);
}
} else if (top.equals(IMAGE)) {
state.getFeed().setImage(new FeedImage(content, null));
}
}
} }
@Override @Override
public void handleElementEnd(String localName, HandlerState state) { public void handleElementEnd(String localName, HandlerState state) {
// TODO Auto-generated method stub if (localName.equals(ENTRY)) {
state.setCurrentItem(null);
}
} }
} }

View File

@ -63,10 +63,10 @@ public class NSRSS20 extends Namespace {
int length) { int length) {
if (state.getTagstack().size() >= 2) { if (state.getTagstack().size() >= 2) {
String content = new String(ch, start, length); String content = new String(ch, start, length);
SyndElement topElement = state.getTagstack().pop(); SyndElement topElement = state.getTagstack().peek();
String top = topElement.getName(); String top = topElement.getName();
String second = state.getTagstack().peek().getName(); SyndElement secondElement = state.getSecondTag();
state.getTagstack().push(topElement); String second = secondElement.getName();
if (top.equals(TITLE)) { if (top.equals(TITLE)) {
if (second.equals(ITEM)) { if (second.equals(ITEM)) {
state.getCurrentItem().setTitle(content); state.getCurrentItem().setTitle(content);

View File

@ -7,6 +7,7 @@ public class HtmlUnescaper {
private static HashMap<String, String> symbols; private static HashMap<String, String> symbols;
static { static {
symbols = new HashMap<String, String>();
symbols.put("&nbsp", " "); symbols.put("&nbsp", " ");
symbols.put("&quot", "\""); symbols.put("&quot", "\"");
symbols.put("&amp", "&"); symbols.put("&amp", "&");