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) {
super();
this.context = context;
requester = DownloadRequester.getInstance();
}
@Override

View File

@ -47,6 +47,14 @@ public class HandlerState {
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.rss20.NSRSS20;
/** Superclass for all SAX Handlers which process Syndication formats */
public class SyndHandler extends DefaultHandler {
private static final String TAG = "SyndHandler";
@ -32,7 +31,8 @@ public class SyndHandler extends DefaultHandler {
Namespace handler = getHandlingNamespace(uri);
if (handler != null) {
SyndElement element = handler.handleElementStart(localName, state, attributes);
SyndElement element = handler.handleElementStart(localName, state,
attributes);
state.tagstack.push(element);
}
@ -41,10 +41,11 @@ public class SyndHandler extends DefaultHandler {
@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);
if (!state.tagstack.empty()) {
SyndElement top = state.tagstack.peek();
if (top.getNamespace() != null) {
top.getNamespace().handleCharacters(state, ch, start, length);
}
}
}
@ -80,7 +81,7 @@ public class SyndHandler extends DefaultHandler {
private Namespace getHandlingNamespace(String 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();
}
return handler;

View File

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

View File

@ -3,6 +3,7 @@ package de.podfetcher.syndication.namespace.atom;
import org.xml.sax.Attributes;
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;
@ -20,6 +21,9 @@ public class NSAtom extends Namespace {
private static final String UPDATED = "updated";
private static final String AUTHOR = "author";
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";
// Link
@ -34,6 +38,8 @@ public class NSAtom extends Namespace {
private static final String LINK_REL_RELATED = "related";
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
public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) {
@ -41,29 +47,29 @@ public class NSAtom extends Namespace {
state.setCurrentItem(new FeedItem());
state.getFeed().getItems().add(state.getCurrentItem());
state.getCurrentItem().setFeed(state.getFeed());
} else if (localName.equals(TITLE) || localName.equals(CONTENT)) {
String type = attributes.getValue(null, TEXT_TYPE);
} else if (localName.matches(isText)) {
String type = attributes.getValue(TEXT_TYPE);
return new AtomText(localName, this, type);
} else if (localName.equals(LINK)) {
String href = attributes.getValue(null, LINK_HREF);
String rel = attributes.getValue(null, LINK_REL);
String href = attributes.getValue(LINK_HREF);
String rel = attributes.getValue(LINK_REL);
SyndElement parent = state.getTagstack().peek();
if (parent.getName().equals(ENTRY)) {
if (rel == null || rel.equals(LINK_REL_ALTERNATE)) {
state.getCurrentItem().setLink(href);
} else if (rel.equals(LINK_REL_ENCLOSURE)) {
long size = Long.parseLong(attributes.getValue(null,
LINK_LENGTH));
String type = attributes.getValue(null, LINK_TYPE);
String download_url = attributes.getValue(null,
LINK_REL_ENCLOSURE);
String strSize = attributes.getValue(LINK_LENGTH);
long size = 0;
if (strSize != null) size = Long.parseLong(strSize);
String type = attributes.getValue(LINK_TYPE);
String download_url = attributes.getValue(LINK_REL_ENCLOSURE);
state.getCurrentItem().setMedia(
new FeedMedia(state.getCurrentItem(), download_url,
size, type));
}
} else if (parent.getName().equals(FEED)) {
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
public void handleCharacters(HandlerState state, char[] ch, int start,
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
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) {
if (state.getTagstack().size() >= 2) {
String content = new String(ch, start, length);
SyndElement topElement = state.getTagstack().pop();
SyndElement topElement = state.getTagstack().peek();
String top = topElement.getName();
String second = state.getTagstack().peek().getName();
state.getTagstack().push(topElement);
SyndElement secondElement = state.getSecondTag();
String second = secondElement.getName();
if (top.equals(TITLE)) {
if (second.equals(ITEM)) {
state.getCurrentItem().setTitle(content);

View File

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