diff options
Diffstat (limited to 'src/de/danoeh/antennapod/syndication')
6 files changed, 134 insertions, 74 deletions
diff --git a/src/de/danoeh/antennapod/syndication/handler/FeedHandler.java b/src/de/danoeh/antennapod/syndication/handler/FeedHandler.java index 5576603b6..aafa1c209 100644 --- a/src/de/danoeh/antennapod/syndication/handler/FeedHandler.java +++ b/src/de/danoeh/antennapod/syndication/handler/FeedHandler.java @@ -14,7 +14,7 @@ import java.io.Reader; public class FeedHandler { - public Feed parseFeed(Feed feed) throws SAXException, IOException, + public FeedHandlerResult parseFeed(Feed feed) throws SAXException, IOException, ParserConfigurationException, UnsupportedFeedtypeException { TypeGetter tg = new TypeGetter(); TypeGetter.Type type = tg.getType(feed); @@ -29,6 +29,6 @@ public class FeedHandler { saxParser.parse(inputSource, handler); inputStreamReader.close(); - return handler.state.feed; + return new FeedHandlerResult(handler.state.feed, handler.state.alternateUrls); } } diff --git a/src/de/danoeh/antennapod/syndication/handler/FeedHandlerResult.java b/src/de/danoeh/antennapod/syndication/handler/FeedHandlerResult.java new file mode 100644 index 000000000..41aa29b52 --- /dev/null +++ b/src/de/danoeh/antennapod/syndication/handler/FeedHandlerResult.java @@ -0,0 +1,19 @@ +package de.danoeh.antennapod.syndication.handler; + +import de.danoeh.antennapod.feed.Feed; + +import java.util.Map; + +/** + * Container for results returned by the Feed parser + */ +public class FeedHandlerResult { + + public Feed feed; + public Map<String, String> alternateFeedUrls; + + public FeedHandlerResult(Feed feed, Map<String, String> alternateFeedUrls) { + this.feed = feed; + this.alternateFeedUrls = alternateFeedUrls; + } +} diff --git a/src/de/danoeh/antennapod/syndication/handler/HandlerState.java b/src/de/danoeh/antennapod/syndication/handler/HandlerState.java index a9a8bb934..17f84724f 100644 --- a/src/de/danoeh/antennapod/syndication/handler/HandlerState.java +++ b/src/de/danoeh/antennapod/syndication/handler/HandlerState.java @@ -5,9 +5,7 @@ import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.syndication.namespace.Namespace; import de.danoeh.antennapod.syndication.namespace.SyndElement; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Stack; +import java.util.*; /** * Contains all relevant information to describe the current state of a @@ -15,70 +13,86 @@ import java.util.Stack; */ public class HandlerState { - /** Feed that the Handler is currently processing. */ - protected Feed feed; - protected ArrayList<FeedItem> items; - protected FeedItem currentItem; - protected Stack<SyndElement> tagstack; - /** Namespaces that have been defined so far. */ - protected HashMap<String, Namespace> namespaces; - protected Stack<Namespace> defaultNamespaces; - /** Buffer for saving characters. */ - protected StringBuffer contentBuf; - - public HandlerState(Feed feed) { - this.feed = feed; - items = new ArrayList<FeedItem>(); - tagstack = new Stack<SyndElement>(); - namespaces = new HashMap<String, Namespace>(); - defaultNamespaces = new Stack<Namespace>(); - } - - public Feed getFeed() { - return feed; - } - - public ArrayList<FeedItem> getItems() { - return items; - } - - public FeedItem getCurrentItem() { - return currentItem; - } - - public Stack<SyndElement> getTagstack() { - return tagstack; - } - - public void setFeed(Feed feed) { - this.feed = feed; - } - - public void setCurrentItem(FeedItem 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; - } - - public SyndElement getThirdTag() { - SyndElement top = tagstack.pop(); - SyndElement second = tagstack.pop(); - SyndElement third = tagstack.peek(); - tagstack.push(second); - tagstack.push(top); - return third; - } - - public StringBuffer getContentBuf() { - return contentBuf; - } + /** + * Feed that the Handler is currently processing. + */ + protected Feed feed; + /** + * Contains links to related feeds, e.g. feeds with enclosures in other formats. The key of the map is the + * URL of the feed, the value is the title + */ + protected Map<String, String> alternateUrls; + protected ArrayList<FeedItem> items; + protected FeedItem currentItem; + protected Stack<SyndElement> tagstack; + /** + * Namespaces that have been defined so far. + */ + protected HashMap<String, Namespace> namespaces; + protected Stack<Namespace> defaultNamespaces; + /** + * Buffer for saving characters. + */ + protected StringBuffer contentBuf; + + public HandlerState(Feed feed) { + this.feed = feed; + alternateUrls = new LinkedHashMap<String, String>(); + items = new ArrayList<FeedItem>(); + tagstack = new Stack<SyndElement>(); + namespaces = new HashMap<String, Namespace>(); + defaultNamespaces = new Stack<Namespace>(); + } + + public Feed getFeed() { + return feed; + } + + public ArrayList<FeedItem> getItems() { + return items; + } + + public FeedItem getCurrentItem() { + return currentItem; + } + + public Stack<SyndElement> getTagstack() { + return tagstack; + } + + public void setFeed(Feed feed) { + this.feed = feed; + } + + public void setCurrentItem(FeedItem 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; + } + + public SyndElement getThirdTag() { + SyndElement top = tagstack.pop(); + SyndElement second = tagstack.pop(); + SyndElement third = tagstack.peek(); + tagstack.push(second); + tagstack.push(top); + return third; + } + + public StringBuffer getContentBuf() { + return contentBuf; + } + + public void addAlternateFeedUrl(String title, String url) { + alternateUrls.put(url, title); + } } diff --git a/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java b/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java index 0ac1b7ae2..5ed9ff2b0 100644 --- a/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java +++ b/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java @@ -17,7 +17,7 @@ import java.io.Reader; public class TypeGetter { private static final String TAG = "TypeGetter"; - enum Type { + public enum Type { RSS20, RSS091, ATOM, INVALID } diff --git a/src/de/danoeh/antennapod/syndication/namespace/atom/NSAtom.java b/src/de/danoeh/antennapod/syndication/namespace/atom/NSAtom.java index 383b29fc8..2c8e232ff 100644 --- a/src/de/danoeh/antennapod/syndication/namespace/atom/NSAtom.java +++ b/src/de/danoeh/antennapod/syndication/namespace/atom/NSAtom.java @@ -92,7 +92,8 @@ public class NSAtom extends Namespace { .getValidMimeTypeFromUrl(href)) != null) { state.getCurrentItem().setMedia( new FeedMedia(state.getCurrentItem(), href, - size, type)); + size, type) + ); } } else if (rel.equals(LINK_REL_PAYMENT)) { state.getCurrentItem().setPaymentLink(href); @@ -101,13 +102,20 @@ public class NSAtom extends Namespace { 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 * LINK_TYPE_HTML or LINK_TYPE_XHTML */ if ((type == null && state.getFeed().getLink() == null) || (type != null && (type.equals(LINK_TYPE_HTML) || type.equals(LINK_TYPE_XHTML)))) { state.getFeed().setLink(href); + } else if (type != null && (type.equals(LINK_TYPE_ATOM) || type.equals(LINK_TYPE_RSS))) { + // treat as podlove alternate feed + String title = attributes.getValue(LINK_TITLE); + if (title == null) { + title = href; + } + state.addAlternateFeedUrl(title, href); } } else if (rel.equals(LINK_REL_PAYMENT)) { state.getFeed().setPaymentLink(href); diff --git a/src/de/danoeh/antennapod/syndication/util/SyndDateUtils.java b/src/de/danoeh/antennapod/syndication/util/SyndDateUtils.java index 2c1cff914..3138f087a 100644 --- a/src/de/danoeh/antennapod/syndication/util/SyndDateUtils.java +++ b/src/de/danoeh/antennapod/syndication/util/SyndDateUtils.java @@ -79,7 +79,7 @@ public class SyndDateUtils { second = date.substring(date.indexOf("-")); } } - + date = first + second; } if (isLocal) { @@ -131,4 +131,23 @@ public class SyndDateUtils { result += (Float.valueOf(parts[idx])) * 1000L; return result; } + + public static String formatRFC822Date(Date date) { + SimpleDateFormat format = RFC822Formatter.get(); + return format.format(date); + } + + public static String formatRFC3339Local(Date date) { + SimpleDateFormat format = RFC3339Formatter.get(); + format.applyPattern(RFC3339LOCAL); + String result = format.format(date); + format.applyPattern(RFC3339UTC); + return result; + } + + public static String formatRFC3339UTC(Date date) { + SimpleDateFormat format = RFC3339Formatter.get(); + format.applyPattern(RFC3339UTC); + return format.format(date); + } } |