Catch NumberFormatException when parsing size attribute

This commit is contained in:
daniel oeh 2013-09-21 12:32:08 +02:00
parent 0b0ea77df1
commit aa3675bc83
1 changed files with 155 additions and 147 deletions

View File

@ -1,7 +1,7 @@
package de.danoeh.antennapod.syndication.namespace.atom; package de.danoeh.antennapod.syndication.namespace.atom;
import org.xml.sax.Attributes; import android.util.Log;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.feed.FeedImage; import de.danoeh.antennapod.feed.FeedImage;
import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.feed.FeedMedia; import de.danoeh.antennapod.feed.FeedMedia;
@ -11,168 +11,176 @@ import de.danoeh.antennapod.syndication.namespace.Namespace;
import de.danoeh.antennapod.syndication.namespace.SyndElement; import de.danoeh.antennapod.syndication.namespace.SyndElement;
import de.danoeh.antennapod.syndication.util.SyndDateUtils; import de.danoeh.antennapod.syndication.util.SyndDateUtils;
import de.danoeh.antennapod.syndication.util.SyndTypeUtils; import de.danoeh.antennapod.syndication.util.SyndTypeUtils;
import org.xml.sax.Attributes;
public class NSAtom extends Namespace { public class NSAtom extends Namespace {
private static final String TAG = "NSAtom"; private static final String TAG = "NSAtom";
public static final String NSTAG = "atom"; public static final String NSTAG = "atom";
public static final String NSURI = "http://www.w3.org/2005/Atom"; public static final String NSURI = "http://www.w3.org/2005/Atom";
private static final String FEED = "feed"; private static final String FEED = "feed";
private static final String ID = "id"; private static final String ID = "id";
private static final String TITLE = "title"; private static final String TITLE = "title";
private static final String ENTRY = "entry"; private static final String ENTRY = "entry";
private static final String LINK = "link"; private static final String LINK = "link";
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 IMAGE = "logo";
private static final String SUBTITLE = "subtitle"; private static final String SUBTITLE = "subtitle";
private static final String PUBLISHED = "published"; private static final String PUBLISHED = "published";
private static final String TEXT_TYPE = "type"; private static final String TEXT_TYPE = "type";
// Link // Link
private static final String LINK_HREF = "href"; private static final String LINK_HREF = "href";
private static final String LINK_REL = "rel"; private static final String LINK_REL = "rel";
private static final String LINK_TYPE = "type"; private static final String LINK_TYPE = "type";
private static final String LINK_TITLE = "title"; private static final String LINK_TITLE = "title";
private static final String LINK_LENGTH = "length"; private static final String LINK_LENGTH = "length";
// rel-values // rel-values
private static final String LINK_REL_ALTERNATE = "alternate"; private static final String LINK_REL_ALTERNATE = "alternate";
private static final String LINK_REL_ENCLOSURE = "enclosure"; private static final String LINK_REL_ENCLOSURE = "enclosure";
private static final String LINK_REL_PAYMENT = "payment"; private static final String LINK_REL_PAYMENT = "payment";
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";
// type-values // type-values
private static final String LINK_TYPE_ATOM = "application/atom+xml"; private static final String LINK_TYPE_ATOM = "application/atom+xml";
private static final String LINK_TYPE_HTML = "text/html"; private static final String LINK_TYPE_HTML = "text/html";
private static final String LINK_TYPE_XHTML = "application/xml+xhtml"; private static final String LINK_TYPE_XHTML = "application/xml+xhtml";
private static final String LINK_TYPE_RSS = "application/rss+xml"; private static final String LINK_TYPE_RSS = "application/rss+xml";
/** Regexp to test whether an Element is a Text Element. */ /**
private static final String isText = TITLE + "|" + CONTENT + "|" + "|" * Regexp to test whether an Element is a Text Element.
+ SUBTITLE; */
private static final String isText = TITLE + "|" + CONTENT + "|" + "|"
+ SUBTITLE;
public static final String isFeed = FEED + "|" + NSRSS20.CHANNEL; public static final String isFeed = FEED + "|" + NSRSS20.CHANNEL;
public static final String isFeedItem = ENTRY + "|" + NSRSS20.ITEM; public static final String isFeedItem = ENTRY + "|" + NSRSS20.ITEM;
@Override @Override
public SyndElement handleElementStart(String localName, HandlerState state, public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) { Attributes attributes) {
if (localName.equals(ENTRY)) { if (localName.equals(ENTRY)) {
state.setCurrentItem(new FeedItem()); state.setCurrentItem(new FeedItem());
state.getItems().add(state.getCurrentItem()); state.getItems().add(state.getCurrentItem());
state.getCurrentItem().setFeed(state.getFeed()); state.getCurrentItem().setFeed(state.getFeed());
} else if (localName.matches(isText)) { } else if (localName.matches(isText)) {
String type = attributes.getValue(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(LINK_HREF); String href = attributes.getValue(LINK_HREF);
String rel = attributes.getValue(LINK_REL); String rel = attributes.getValue(LINK_REL);
SyndElement parent = state.getTagstack().peek(); SyndElement parent = state.getTagstack().peek();
if (parent.getName().matches(isFeedItem)) { if (parent.getName().matches(isFeedItem)) {
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)) {
String strSize = attributes.getValue(LINK_LENGTH); String strSize = attributes.getValue(LINK_LENGTH);
long size = 0; long size = 0;
if (strSize != null) try {
size = Long.parseLong(strSize); if (strSize != null) {
String type = attributes.getValue(LINK_TYPE); size = Long.parseLong(strSize);
if (SyndTypeUtils.enclosureTypeValid(type) }
|| (type = SyndTypeUtils } catch (NumberFormatException e) {
.getValidMimeTypeFromUrl(href)) != null) { if (AppConfig.DEBUG) Log.d(TAG, "Length attribute could not be parsed.");
state.getCurrentItem().setMedia( }
new FeedMedia(state.getCurrentItem(), href, String type = attributes.getValue(LINK_TYPE);
size, type)); if (SyndTypeUtils.enclosureTypeValid(type)
} || (type = SyndTypeUtils
} else if (rel.equals(LINK_REL_PAYMENT)) { .getValidMimeTypeFromUrl(href)) != null) {
state.getCurrentItem().setPaymentLink(href); state.getCurrentItem().setMedia(
} new FeedMedia(state.getCurrentItem(), href,
} else if (parent.getName().matches(isFeed)) { size, type));
if (rel == null || rel.equals(LINK_REL_ALTERNATE)) { }
String type = attributes.getValue(LINK_TYPE); } else if (rel.equals(LINK_REL_PAYMENT)) {
/* state.getCurrentItem().setPaymentLink(href);
}
} else if (parent.getName().matches(isFeed)) {
if (rel == null || rel.equals(LINK_REL_ALTERNATE)) {
String type = attributes.getValue(LINK_TYPE);
/*
* Use as link if a) no type-attribute is given and * Use as link if a) no type-attribute is given and
* feed-object has no link yet b) type of link is * feed-object has no link yet b) type of link is
* LINK_TYPE_HTML or LINK_TYPE_XHTML * LINK_TYPE_HTML or LINK_TYPE_XHTML
*/ */
if ((type == null && state.getFeed().getLink() == null) if ((type == null && state.getFeed().getLink() == null)
|| (type != null && (type.equals(LINK_TYPE_HTML) || type.equals(LINK_TYPE_XHTML)))) { || (type != null && (type.equals(LINK_TYPE_HTML) || type.equals(LINK_TYPE_XHTML)))) {
state.getFeed().setLink(href); state.getFeed().setLink(href);
} }
} else if (rel.equals(LINK_REL_PAYMENT)) { } else if (rel.equals(LINK_REL_PAYMENT)) {
state.getFeed().setPaymentLink(href); state.getFeed().setPaymentLink(href);
} }
} }
} }
return new SyndElement(localName, this); return new SyndElement(localName, this);
} }
@Override @Override
public void handleElementEnd(String localName, HandlerState state) { public void handleElementEnd(String localName, HandlerState state) {
if (localName.equals(ENTRY)) { if (localName.equals(ENTRY)) {
state.setCurrentItem(null); state.setCurrentItem(null);
} }
if (state.getTagstack().size() >= 2) { if (state.getTagstack().size() >= 2) {
AtomText textElement = null; AtomText textElement = null;
String content; String content;
if (state.getContentBuf() != null) { if (state.getContentBuf() != null) {
content = state.getContentBuf().toString(); content = state.getContentBuf().toString();
} else { } else {
content = ""; content = "";
} }
SyndElement topElement = state.getTagstack().peek(); SyndElement topElement = state.getTagstack().peek();
String top = topElement.getName(); String top = topElement.getName();
SyndElement secondElement = state.getSecondTag(); SyndElement secondElement = state.getSecondTag();
String second = secondElement.getName(); String second = secondElement.getName();
if (top.matches(isText)) { if (top.matches(isText)) {
textElement = (AtomText) topElement; textElement = (AtomText) topElement;
textElement.setContent(content); textElement.setContent(content);
} }
if (top.equals(ID)) { if (top.equals(ID)) {
if (second.equals(FEED)) { if (second.equals(FEED)) {
state.getFeed().setFeedIdentifier(content); state.getFeed().setFeedIdentifier(content);
} else if (second.equals(ENTRY)) { } else if (second.equals(ENTRY)) {
state.getCurrentItem().setItemIdentifier(content); state.getCurrentItem().setItemIdentifier(content);
} }
} else if (top.equals(TITLE)) { } else if (top.equals(TITLE)) {
if (second.equals(FEED)) { if (second.equals(FEED)) {
state.getFeed().setTitle(textElement.getProcessedContent()); state.getFeed().setTitle(textElement.getProcessedContent());
} else if (second.equals(ENTRY)) { } else if (second.equals(ENTRY)) {
state.getCurrentItem().setTitle( state.getCurrentItem().setTitle(
textElement.getProcessedContent()); textElement.getProcessedContent());
} }
} else if (top.equals(SUBTITLE)) { } else if (top.equals(SUBTITLE)) {
if (second.equals(FEED)) { if (second.equals(FEED)) {
state.getFeed().setDescription( state.getFeed().setDescription(
textElement.getProcessedContent()); textElement.getProcessedContent());
} }
} else if (top.equals(CONTENT)) { } else if (top.equals(CONTENT)) {
if (second.equals(ENTRY)) { if (second.equals(ENTRY)) {
state.getCurrentItem().setDescription( state.getCurrentItem().setDescription(
textElement.getProcessedContent()); textElement.getProcessedContent());
} }
} else if (top.equals(UPDATED)) { } else if (top.equals(UPDATED)) {
if (second.equals(ENTRY) if (second.equals(ENTRY)
&& state.getCurrentItem().getPubDate() == null) { && state.getCurrentItem().getPubDate() == null) {
state.getCurrentItem().setPubDate( state.getCurrentItem().setPubDate(
SyndDateUtils.parseRFC3339Date(content)); SyndDateUtils.parseRFC3339Date(content));
} }
} else if (top.equals(PUBLISHED)) { } else if (top.equals(PUBLISHED)) {
if (second.equals(ENTRY)) { if (second.equals(ENTRY)) {
state.getCurrentItem().setPubDate( state.getCurrentItem().setPubDate(
SyndDateUtils.parseRFC3339Date(content)); SyndDateUtils.parseRFC3339Date(content));
} }
} else if (top.equals(IMAGE)) { } else if (top.equals(IMAGE)) {
state.getFeed().setImage(new FeedImage(content, null)); state.getFeed().setImage(new FeedImage(content, null));
} }
} }
} }
} }