diff options
Diffstat (limited to 'src/de/danoeh/antennapod/syndication')
17 files changed, 0 insertions, 1249 deletions
diff --git a/src/de/danoeh/antennapod/syndication/handler/FeedHandler.java b/src/de/danoeh/antennapod/syndication/handler/FeedHandler.java deleted file mode 100644 index aafa1c209..000000000 --- a/src/de/danoeh/antennapod/syndication/handler/FeedHandler.java +++ /dev/null @@ -1,34 +0,0 @@ -package de.danoeh.antennapod.syndication.handler; - -import de.danoeh.antennapod.feed.Feed; -import org.apache.commons.io.input.XmlStreamReader; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; -import java.io.File; -import java.io.IOException; -import java.io.Reader; - -public class FeedHandler { - - public FeedHandlerResult parseFeed(Feed feed) throws SAXException, IOException, - ParserConfigurationException, UnsupportedFeedtypeException { - TypeGetter tg = new TypeGetter(); - TypeGetter.Type type = tg.getType(feed); - SyndHandler handler = new SyndHandler(feed, type); - - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(true); - SAXParser saxParser = factory.newSAXParser(); - File file = new File(feed.getFile_url()); - Reader inputStreamReader = new XmlStreamReader(file); - InputSource inputSource = new InputSource(inputStreamReader); - - saxParser.parse(inputSource, handler); - inputStreamReader.close(); - 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 deleted file mode 100644 index 41aa29b52..000000000 --- a/src/de/danoeh/antennapod/syndication/handler/FeedHandlerResult.java +++ /dev/null @@ -1,19 +0,0 @@ -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 deleted file mode 100644 index 17f84724f..000000000 --- a/src/de/danoeh/antennapod/syndication/handler/HandlerState.java +++ /dev/null @@ -1,98 +0,0 @@ -package de.danoeh.antennapod.syndication.handler; - -import de.danoeh.antennapod.feed.Feed; -import de.danoeh.antennapod.feed.FeedItem; -import de.danoeh.antennapod.syndication.namespace.Namespace; -import de.danoeh.antennapod.syndication.namespace.SyndElement; - -import java.util.*; - -/** - * Contains all relevant information to describe the current state of a - * SyndHandler. - */ -public class HandlerState { - - /** - * 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/SyndHandler.java b/src/de/danoeh/antennapod/syndication/handler/SyndHandler.java deleted file mode 100644 index 15dc94d65..000000000 --- a/src/de/danoeh/antennapod/syndication/handler/SyndHandler.java +++ /dev/null @@ -1,126 +0,0 @@ -package de.danoeh.antennapod.syndication.handler; - -import android.util.Log; -import de.danoeh.antennapod.BuildConfig; -import de.danoeh.antennapod.feed.Feed; -import de.danoeh.antennapod.syndication.namespace.*; -import de.danoeh.antennapod.syndication.namespace.atom.NSAtom; -import org.xml.sax.Attributes; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.DefaultHandler; - -/** Superclass for all SAX Handlers which process Syndication formats */ -public class SyndHandler extends DefaultHandler { - private static final String TAG = "SyndHandler"; - private static final String DEFAULT_PREFIX = ""; - protected HandlerState state; - - public SyndHandler(Feed feed, TypeGetter.Type type) { - state = new HandlerState(feed); - if (type == TypeGetter.Type.RSS20 || type == TypeGetter.Type.RSS091) { - state.defaultNamespaces.push(new NSRSS20()); - } - } - - @Override - public void startElement(String uri, String localName, String qName, - Attributes attributes) throws SAXException { - state.contentBuf = new StringBuffer(); - Namespace handler = getHandlingNamespace(uri, qName); - if (handler != null) { - SyndElement element = handler.handleElementStart(localName, state, - attributes); - state.tagstack.push(element); - - } - } - - @Override - public void characters(char[] ch, int start, int length) - throws SAXException { - if (!state.tagstack.empty()) { - if (state.getTagstack().size() >= 2) { - if (state.contentBuf != null) { - state.contentBuf.append(ch, start, length); - } - } - } - } - - @Override - public void endElement(String uri, String localName, String qName) - throws SAXException { - Namespace handler = getHandlingNamespace(uri, qName); - if (handler != null) { - handler.handleElementEnd(localName, state); - state.tagstack.pop(); - - } - state.contentBuf = null; - - } - - @Override - public void endPrefixMapping(String prefix) throws SAXException { - if (state.defaultNamespaces.size() > 1 && prefix.equals(DEFAULT_PREFIX)) { - state.defaultNamespaces.pop(); - } - } - - @Override - public void startPrefixMapping(String prefix, String uri) - throws SAXException { - // Find the right namespace - if (!state.namespaces.containsKey(uri)) { - if (uri.equals(NSAtom.NSURI)) { - if (prefix.equals(DEFAULT_PREFIX)) { - state.defaultNamespaces.push(new NSAtom()); - } else if (prefix.equals(NSAtom.NSTAG)) { - state.namespaces.put(uri, new NSAtom()); - if (BuildConfig.DEBUG) - Log.d(TAG, "Recognized Atom namespace"); - } - } else if (uri.equals(NSContent.NSURI) - && prefix.equals(NSContent.NSTAG)) { - state.namespaces.put(uri, new NSContent()); - if (BuildConfig.DEBUG) - Log.d(TAG, "Recognized Content namespace"); - } else if (uri.equals(NSITunes.NSURI) - && prefix.equals(NSITunes.NSTAG)) { - state.namespaces.put(uri, new NSITunes()); - if (BuildConfig.DEBUG) - Log.d(TAG, "Recognized ITunes namespace"); - } else if (uri.equals(NSSimpleChapters.NSURI) - && prefix.matches(NSSimpleChapters.NSTAG)) { - state.namespaces.put(uri, new NSSimpleChapters()); - if (BuildConfig.DEBUG) - Log.d(TAG, "Recognized SimpleChapters namespace"); - } else if (uri.equals(NSMedia.NSURI) - && prefix.equals(NSMedia.NSTAG)) { - state.namespaces.put(uri, new NSMedia()); - if (BuildConfig.DEBUG) - Log.d(TAG, "Recognized media namespace"); - } - } - } - - private Namespace getHandlingNamespace(String uri, String qName) { - Namespace handler = state.namespaces.get(uri); - if (handler == null && !state.defaultNamespaces.empty() - && !qName.contains(":")) { - handler = state.defaultNamespaces.peek(); - } - return handler; - } - - @Override - public void endDocument() throws SAXException { - super.endDocument(); - state.getFeed().setItems(state.getItems()); - } - - public HandlerState getState() { - return state; - } - -} diff --git a/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java b/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java deleted file mode 100644 index 2496e112a..000000000 --- a/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java +++ /dev/null @@ -1,112 +0,0 @@ -package de.danoeh.antennapod.syndication.handler; - -import android.util.Log; -import de.danoeh.antennapod.BuildConfig; -import de.danoeh.antennapod.feed.Feed; -import org.apache.commons.io.input.XmlStreamReader; -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; -import org.xmlpull.v1.XmlPullParserFactory; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.Reader; - -/** Gets the type of a specific feed by reading the root element. */ -public class TypeGetter { - private static final String TAG = "TypeGetter"; - - public enum Type { - RSS20, RSS091, ATOM, INVALID - } - - private static final String ATOM_ROOT = "feed"; - private static final String RSS_ROOT = "rss"; - - public Type getType(Feed feed) throws UnsupportedFeedtypeException { - XmlPullParserFactory factory; - if (feed.getFile_url() != null) { - try { - factory = XmlPullParserFactory.newInstance(); - factory.setNamespaceAware(true); - 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(); - if (tag.equals(ATOM_ROOT)) { - feed.setType(Feed.TYPE_ATOM1); - if (BuildConfig.DEBUG) - Log.d(TAG, "Recognized type Atom"); - return Type.ATOM; - } else if (tag.equals(RSS_ROOT)) { - String strVersion = xpp.getAttributeValue(null, - "version"); - if (strVersion != null) { - - if (strVersion.equals("2.0")) { - feed.setType(Feed.TYPE_RSS2); - if (BuildConfig.DEBUG) - Log.d(TAG, "Recognized type RSS 2.0"); - return Type.RSS20; - } else if (strVersion.equals("0.91") - || strVersion.equals("0.92")) { - if (BuildConfig.DEBUG) - Log.d(TAG, - "Recognized type RSS 0.91/0.92"); - return Type.RSS091; - } - } - throw new UnsupportedFeedtypeException(Type.INVALID); - } else { - if (BuildConfig.DEBUG) - Log.d(TAG, "Type is invalid"); - throw new UnsupportedFeedtypeException(Type.INVALID, tag); - } - } else { - eventType = xpp.next(); - } - } - - } catch (XmlPullParserException e) { - e.printStackTrace(); - // XML document might actually be a HTML document -> try to parse as HTML - String rootElement = null; - try { - if (Jsoup.parse(new File(feed.getFile_url()), null) != null) { - rootElement = "html"; - } - } catch (IOException e1) { - e1.printStackTrace(); - } finally { - throw new UnsupportedFeedtypeException(Type.INVALID, rootElement); - } - - } catch (IOException e) { - e.printStackTrace(); - } - } - if (BuildConfig.DEBUG) - Log.d(TAG, "Type is invalid"); - throw new UnsupportedFeedtypeException(Type.INVALID); - } - - private Reader createReader(Feed feed) { - Reader reader; - try { - reader = new XmlStreamReader(new File(feed.getFile_url())); - } catch (FileNotFoundException e) { - e.printStackTrace(); - return null; - } catch (IOException e) { - e.printStackTrace(); - return null; - } - return reader; - } -} diff --git a/src/de/danoeh/antennapod/syndication/handler/UnsupportedFeedtypeException.java b/src/de/danoeh/antennapod/syndication/handler/UnsupportedFeedtypeException.java deleted file mode 100644 index 605dad2fb..000000000 --- a/src/de/danoeh/antennapod/syndication/handler/UnsupportedFeedtypeException.java +++ /dev/null @@ -1,38 +0,0 @@ -package de.danoeh.antennapod.syndication.handler; - -import de.danoeh.antennapod.syndication.handler.TypeGetter.Type; - -public class UnsupportedFeedtypeException extends Exception { - private static final long serialVersionUID = 9105878964928170669L; - private TypeGetter.Type type; - private String rootElement; - - public UnsupportedFeedtypeException(Type type) { - super(); - this.type = type; - } - - public UnsupportedFeedtypeException(Type type, String rootElement) { - this.type = type; - this.rootElement = rootElement; - } - - public TypeGetter.Type getType() { - return type; - } - - public String getRootElement() { - return rootElement; - } - - @Override - public String getMessage() { - if (type == TypeGetter.Type.INVALID) { - return "Invalid type"; - } else { - return "Type " + type + " not supported"; - } - } - - -} diff --git a/src/de/danoeh/antennapod/syndication/namespace/NSContent.java b/src/de/danoeh/antennapod/syndication/namespace/NSContent.java deleted file mode 100644 index 9ad3026be..000000000 --- a/src/de/danoeh/antennapod/syndication/namespace/NSContent.java +++ /dev/null @@ -1,25 +0,0 @@ -package de.danoeh.antennapod.syndication.namespace; - -import de.danoeh.antennapod.syndication.handler.HandlerState; -import org.xml.sax.Attributes; - -public class NSContent extends Namespace { - public static final String NSTAG = "content"; - public static final String NSURI = "http://purl.org/rss/1.0/modules/content/"; - - private static final String ENCODED = "encoded"; - - @Override - public SyndElement handleElementStart(String localName, HandlerState state, - Attributes attributes) { - return new SyndElement(localName, this); - } - - @Override - public void handleElementEnd(String localName, HandlerState state) { - if (localName.equals(ENCODED)) { - state.getCurrentItem().setContentEncoded(state.getContentBuf().toString()); - } - } - -} diff --git a/src/de/danoeh/antennapod/syndication/namespace/NSITunes.java b/src/de/danoeh/antennapod/syndication/namespace/NSITunes.java deleted file mode 100644 index d8cbe040b..000000000 --- a/src/de/danoeh/antennapod/syndication/namespace/NSITunes.java +++ /dev/null @@ -1,51 +0,0 @@ -package de.danoeh.antennapod.syndication.namespace; - -import de.danoeh.antennapod.feed.FeedImage; -import de.danoeh.antennapod.syndication.handler.HandlerState; -import org.xml.sax.Attributes; - -public class NSITunes extends Namespace { - public static final String NSTAG = "itunes"; - public static final String NSURI = "http://www.itunes.com/dtds/podcast-1.0.dtd"; - - private static final String IMAGE = "image"; - private static final String IMAGE_TITLE = "image"; - private static final String IMAGE_HREF = "href"; - - private static final String AUTHOR = "author"; - - - @Override - public SyndElement handleElementStart(String localName, HandlerState state, - Attributes attributes) { - if (localName.equals(IMAGE)) { - FeedImage image = new FeedImage(); - image.setTitle(IMAGE_TITLE); - image.setDownload_url(attributes.getValue(IMAGE_HREF)); - - if (state.getCurrentItem() != null) { - // this is an items image - image.setTitle(state.getCurrentItem().getTitle() + IMAGE_TITLE); - state.getCurrentItem().setImage(image); - - } else { - // this is the feed image - if (state.getFeed().getImage() == null) { - state.getFeed().setImage(image); - } - } - - } - - return new SyndElement(localName, this); - } - - @Override - public void handleElementEnd(String localName, HandlerState state) { - if (localName.equals(AUTHOR)) { - state.getFeed().setAuthor(state.getContentBuf().toString()); - } - - } - -} diff --git a/src/de/danoeh/antennapod/syndication/namespace/NSMedia.java b/src/de/danoeh/antennapod/syndication/namespace/NSMedia.java deleted file mode 100644 index cc23167c1..000000000 --- a/src/de/danoeh/antennapod/syndication/namespace/NSMedia.java +++ /dev/null @@ -1,68 +0,0 @@ -package de.danoeh.antennapod.syndication.namespace; - -import android.util.Log; -import de.danoeh.antennapod.BuildConfig; -import de.danoeh.antennapod.feed.FeedMedia; -import de.danoeh.antennapod.syndication.handler.HandlerState; -import de.danoeh.antennapod.syndication.util.SyndTypeUtils; -import org.xml.sax.Attributes; - -import java.util.concurrent.TimeUnit; - -/** Processes tags from the http://search.yahoo.com/mrss/ namespace. */ -public class NSMedia extends Namespace { - private static final String TAG = "NSMedia"; - - public static final String NSTAG = "media"; - public static final String NSURI = "http://search.yahoo.com/mrss/"; - - private static final String CONTENT = "content"; - private static final String DOWNLOAD_URL = "url"; - private static final String SIZE = "fileSize"; - private static final String MIME_TYPE = "type"; - private static final String DURATION = "duration"; - - @Override - public SyndElement handleElementStart(String localName, HandlerState state, - Attributes attributes) { - if (localName.equals(CONTENT)) { - String url = attributes.getValue(DOWNLOAD_URL); - String type = attributes.getValue(MIME_TYPE); - if (state.getCurrentItem().getMedia() == null - && url != null - && (SyndTypeUtils.enclosureTypeValid(type) || ((type = SyndTypeUtils - .getValidMimeTypeFromUrl(url)) != null))) { - - long size = 0; - try { - size = Long.parseLong(attributes.getValue(SIZE)); - } catch (NumberFormatException e) { - if (BuildConfig.DEBUG) - Log.d(TAG, "Length attribute could not be parsed."); - } - - int duration = 0; - try { - String durationStr = attributes.getValue(DURATION); - if (durationStr != null) { - duration = (int) TimeUnit.MILLISECONDS.convert( - Long.parseLong(durationStr), TimeUnit.SECONDS); - } - } catch (NumberFormatException e) { - if (BuildConfig.DEBUG) - Log.d(TAG, "Duration attribute could not be parsed"); - } - - state.getCurrentItem().setMedia( - new FeedMedia(state.getCurrentItem(), url, size, type)); - } - } - return new SyndElement(localName, this); - } - - @Override - public void handleElementEnd(String localName, HandlerState state) { - - } - -} diff --git a/src/de/danoeh/antennapod/syndication/namespace/NSRSS20.java b/src/de/danoeh/antennapod/syndication/namespace/NSRSS20.java deleted file mode 100644 index 9572f87ae..000000000 --- a/src/de/danoeh/antennapod/syndication/namespace/NSRSS20.java +++ /dev/null @@ -1,141 +0,0 @@ -package de.danoeh.antennapod.syndication.namespace; - -import android.util.Log; -import de.danoeh.antennapod.BuildConfig; -import de.danoeh.antennapod.feed.FeedImage; -import de.danoeh.antennapod.feed.FeedItem; -import de.danoeh.antennapod.feed.FeedMedia; -import de.danoeh.antennapod.syndication.handler.HandlerState; -import de.danoeh.antennapod.syndication.util.SyndDateUtils; -import de.danoeh.antennapod.syndication.util.SyndTypeUtils; -import org.xml.sax.Attributes; - -/** - * SAX-Parser for reading RSS-Feeds - * - * @author daniel - * - */ -public class NSRSS20 extends Namespace { - private static final String TAG = "NSRSS20"; - public static final String NSTAG = "rss"; - public static final String NSURI = ""; - - public final static String CHANNEL = "channel"; - public final static String ITEM = "item"; - public final static String GUID = "guid"; - 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 LANGUAGE = "language"; - - public final static String ENC_URL = "url"; - public final static String ENC_LEN = "length"; - public final static String ENC_TYPE = "type"; - - @Override - public SyndElement handleElementStart(String localName, HandlerState state, - Attributes attributes) { - if (localName.equals(ITEM)) { - state.setCurrentItem(new FeedItem()); - state.getItems().add(state.getCurrentItem()); - state.getCurrentItem().setFeed(state.getFeed()); - - } else if (localName.equals(ENCLOSURE)) { - String type = attributes.getValue(ENC_TYPE); - String url = attributes.getValue(ENC_URL); - if (state.getCurrentItem().getMedia() == null - && (SyndTypeUtils.enclosureTypeValid(type) || ((type = SyndTypeUtils - .getValidMimeTypeFromUrl(url)) != null))) { - - long size = 0; - try { - size = Long.parseLong(attributes.getValue(ENC_LEN)); - } catch (NumberFormatException e) { - if (BuildConfig.DEBUG) - Log.d(TAG, "Length attribute could not be parsed."); - } - state.getCurrentItem().setMedia( - new FeedMedia(state.getCurrentItem(), url, size, type)); - } - - } else if (localName.equals(IMAGE)) { - if (state.getTagstack().size() >= 1) { - String parent = state.getTagstack().peek().getName(); - if (parent.equals(CHANNEL)) { - state.getFeed().setImage(new FeedImage()); - } - } - } - return new SyndElement(localName, this); - } - - @Override - public void handleElementEnd(String localName, HandlerState state) { - if (localName.equals(ITEM)) { - if (state.getCurrentItem() != null) { - // the title tag is optional in RSS 2.0. The description is used - // as a - // title if the item has no title-tag. - if (state.getCurrentItem().getTitle() == null) { - state.getCurrentItem().setTitle( - state.getCurrentItem().getDescription()); - } - } - state.setCurrentItem(null); - } else if (state.getTagstack().size() >= 2 - && state.getContentBuf() != null) { - String content = state.getContentBuf().toString(); - SyndElement topElement = state.getTagstack().peek(); - String top = topElement.getName(); - SyndElement secondElement = state.getSecondTag(); - String second = secondElement.getName(); - String third = null; - if (state.getTagstack().size() >= 3) { - third = state.getThirdTag().getName(); - } - - if (top.equals(GUID) && second.equals(ITEM)) { - // some feed creators include an empty or non-standard guid-element in their feed, which should be ignored - if (!content.isEmpty()) { - state.getCurrentItem().setItemIdentifier(content); - } - } else if (top.equals(TITLE)) { - if (second.equals(ITEM)) { - state.getCurrentItem().setTitle(content); - } else if (second.equals(CHANNEL)) { - state.getFeed().setTitle(content); - } else if (second.equals(IMAGE) && third != null - && third.equals(CHANNEL)) { - state.getFeed().getImage().setTitle(content); - } - } else if (top.equals(LINK)) { - if (second.equals(CHANNEL)) { - state.getFeed().setLink(content); - } else if (second.equals(ITEM)) { - state.getCurrentItem().setLink(content); - } - } else if (top.equals(PUBDATE) && second.equals(ITEM)) { - state.getCurrentItem().setPubDate( - SyndDateUtils.parseRFC822Date(content)); - } else if (top.equals(URL) && second.equals(IMAGE) && third != null - && third.equals(CHANNEL)) { - state.getFeed().getImage().setDownload_url(content); - } else if (localName.equals(DESCR)) { - if (second.equals(CHANNEL)) { - state.getFeed().setDescription(content); - } else if (second.equals(ITEM)) { - state.getCurrentItem().setDescription(content); - } - - } else if (localName.equals(LANGUAGE)) { - state.getFeed().setLanguage(content.toLowerCase()); - } - } - } - -} diff --git a/src/de/danoeh/antennapod/syndication/namespace/NSSimpleChapters.java b/src/de/danoeh/antennapod/syndication/namespace/NSSimpleChapters.java deleted file mode 100644 index b45793b6b..000000000 --- a/src/de/danoeh/antennapod/syndication/namespace/NSSimpleChapters.java +++ /dev/null @@ -1,51 +0,0 @@ -package de.danoeh.antennapod.syndication.namespace; - -import android.util.Log; - -import de.danoeh.antennapod.BuildConfig; -import de.danoeh.antennapod.feed.Chapter; -import de.danoeh.antennapod.feed.SimpleChapter; -import de.danoeh.antennapod.syndication.handler.HandlerState; -import de.danoeh.antennapod.syndication.util.SyndDateUtils; -import org.xml.sax.Attributes; - -import java.util.ArrayList; - -public class NSSimpleChapters extends Namespace { - private static final String TAG = "NSSimpleChapters"; - - public static final String NSTAG = "psc|sc"; - public static final String NSURI = "http://podlove.org/simple-chapters"; - - public static final String CHAPTERS = "chapters"; - public static final String CHAPTER = "chapter"; - public static final String START = "start"; - public static final String TITLE = "title"; - public static final String HREF = "href"; - - @Override - public SyndElement handleElementStart(String localName, HandlerState state, - Attributes attributes) { - if (localName.equals(CHAPTERS)) { - state.getCurrentItem().setChapters(new ArrayList<Chapter>()); - } else if (localName.equals(CHAPTER)) { - try { - state.getCurrentItem() - .getChapters() - .add(new SimpleChapter(SyndDateUtils - .parseTimeString(attributes.getValue(START)), - attributes.getValue(TITLE), state.getCurrentItem(), - attributes.getValue(HREF))); - } catch (NumberFormatException e) { - if (BuildConfig.DEBUG) Log.w(TAG, "Unable to read chapter", e); - } - } - - return new SyndElement(localName, this); - } - - @Override - public void handleElementEnd(String localName, HandlerState state) { - } - -} diff --git a/src/de/danoeh/antennapod/syndication/namespace/Namespace.java b/src/de/danoeh/antennapod/syndication/namespace/Namespace.java deleted file mode 100644 index 910131feb..000000000 --- a/src/de/danoeh/antennapod/syndication/namespace/Namespace.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.danoeh.antennapod.syndication.namespace; - -import de.danoeh.antennapod.syndication.handler.HandlerState; -import org.xml.sax.Attributes; - - -public abstract class Namespace { - public static final String NSTAG = null; - public static final String NSURI = null; - - /** Called by a Feedhandler when in startElement and it detects a namespace element - * @return The SyndElement to push onto the stack - * */ - public abstract SyndElement handleElementStart(String localName, HandlerState state, Attributes attributes); - - /** Called by a Feedhandler when in endElement and it detects a namespace element - * @return true if namespace handled the element, false if it ignored it - * */ - public abstract void handleElementEnd(String localName, HandlerState state); - -} diff --git a/src/de/danoeh/antennapod/syndication/namespace/SyndElement.java b/src/de/danoeh/antennapod/syndication/namespace/SyndElement.java deleted file mode 100644 index 187312c9e..000000000 --- a/src/de/danoeh/antennapod/syndication/namespace/SyndElement.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.danoeh.antennapod.syndication.namespace; - -/** Defines a XML Element that is pushed on the tagstack */ -public class SyndElement { - protected String name; - protected Namespace namespace; - - public SyndElement(String name, Namespace namespace) { - this.name = name; - this.namespace = namespace; - } - - public Namespace getNamespace() { - return namespace; - } - - public String getName() { - return name; - } - - -} diff --git a/src/de/danoeh/antennapod/syndication/namespace/atom/AtomText.java b/src/de/danoeh/antennapod/syndication/namespace/atom/AtomText.java deleted file mode 100644 index 86b80d2ed..000000000 --- a/src/de/danoeh/antennapod/syndication/namespace/atom/AtomText.java +++ /dev/null @@ -1,46 +0,0 @@ -package de.danoeh.antennapod.syndication.namespace.atom; - -import de.danoeh.antennapod.syndication.namespace.Namespace; -import de.danoeh.antennapod.syndication.namespace.SyndElement; -import org.apache.commons.lang3.StringEscapeUtils; - -/** Represents Atom Element which contains text (content, title, summary). */ -public class AtomText extends SyndElement { - public static final String TYPE_TEXT = "text"; - public static final String TYPE_HTML = "html"; - public static final String TYPE_XHTML = "xhtml"; - - private String type; - private String content; - - public AtomText(String name, Namespace namespace, String type) { - super(name, namespace); - this.type = type; - } - - /** Processes the content according to the type and returns it. */ - public String getProcessedContent() { - if (type == null) { - return content; - } else if (type.equals(TYPE_HTML)) { - return StringEscapeUtils.unescapeHtml4(content); - } else if (type.equals(TYPE_XHTML)) { - return content; - } else { // Handle as text by default - return content; - } - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - public String getType() { - return type; - } - -} diff --git a/src/de/danoeh/antennapod/syndication/namespace/atom/NSAtom.java b/src/de/danoeh/antennapod/syndication/namespace/atom/NSAtom.java deleted file mode 100644 index 2c8e232ff..000000000 --- a/src/de/danoeh/antennapod/syndication/namespace/atom/NSAtom.java +++ /dev/null @@ -1,194 +0,0 @@ -package de.danoeh.antennapod.syndication.namespace.atom; - -import android.util.Log; -import de.danoeh.antennapod.BuildConfig; -import de.danoeh.antennapod.feed.FeedImage; -import de.danoeh.antennapod.feed.FeedItem; -import de.danoeh.antennapod.feed.FeedMedia; -import de.danoeh.antennapod.syndication.handler.HandlerState; -import de.danoeh.antennapod.syndication.namespace.NSRSS20; -import de.danoeh.antennapod.syndication.namespace.Namespace; -import de.danoeh.antennapod.syndication.namespace.SyndElement; -import de.danoeh.antennapod.syndication.util.SyndDateUtils; -import de.danoeh.antennapod.syndication.util.SyndTypeUtils; -import org.xml.sax.Attributes; - -public class NSAtom extends Namespace { - private static final String TAG = "NSAtom"; - public static final String NSTAG = "atom"; - public static final String NSURI = "http://www.w3.org/2005/Atom"; - - private static final String FEED = "feed"; - private static final String ID = "id"; - private static final String TITLE = "title"; - private static final String ENTRY = "entry"; - private static final String LINK = "link"; - 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 - private static final String LINK_HREF = "href"; - private static final String LINK_REL = "rel"; - private static final String LINK_TYPE = "type"; - private static final String LINK_TITLE = "title"; - private static final String LINK_LENGTH = "length"; - // rel-values - private static final String LINK_REL_ALTERNATE = "alternate"; - private static final String LINK_REL_ENCLOSURE = "enclosure"; - private static final String LINK_REL_PAYMENT = "payment"; - private static final String LINK_REL_RELATED = "related"; - private static final String LINK_REL_SELF = "self"; - // type-values - 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_XHTML = "application/xml+xhtml"; - - 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 + "|" + "|" - + SUBTITLE; - - public static final String isFeed = FEED + "|" + NSRSS20.CHANNEL; - public static final String isFeedItem = ENTRY + "|" + NSRSS20.ITEM; - - @Override - public SyndElement handleElementStart(String localName, HandlerState state, - Attributes attributes) { - if (localName.equals(ENTRY)) { - state.setCurrentItem(new FeedItem()); - state.getItems().add(state.getCurrentItem()); - state.getCurrentItem().setFeed(state.getFeed()); - } 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(LINK_HREF); - String rel = attributes.getValue(LINK_REL); - SyndElement parent = state.getTagstack().peek(); - if (parent.getName().matches(isFeedItem)) { - if (rel == null || rel.equals(LINK_REL_ALTERNATE)) { - state.getCurrentItem().setLink(href); - } else if (rel.equals(LINK_REL_ENCLOSURE)) { - String strSize = attributes.getValue(LINK_LENGTH); - long size = 0; - try { - if (strSize != null) { - size = Long.parseLong(strSize); - } - } catch (NumberFormatException e) { - if (BuildConfig.DEBUG) Log.d(TAG, "Length attribute could not be parsed."); - } - String type = attributes.getValue(LINK_TYPE); - if (SyndTypeUtils.enclosureTypeValid(type) - || (type = SyndTypeUtils - .getValidMimeTypeFromUrl(href)) != null) { - state.getCurrentItem().setMedia( - new FeedMedia(state.getCurrentItem(), href, - size, 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 - * 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); - } - } - } - return new SyndElement(localName, this); - } - - @Override - public void handleElementEnd(String localName, HandlerState state) { - if (localName.equals(ENTRY)) { - state.setCurrentItem(null); - } - - if (state.getTagstack().size() >= 2) { - AtomText textElement = null; - String content; - if (state.getContentBuf() != null) { - content = state.getContentBuf().toString(); - } else { - content = ""; - } - 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(ID)) { - if (second.equals(FEED)) { - state.getFeed().setFeedIdentifier(content); - } else if (second.equals(ENTRY)) { - state.getCurrentItem().setItemIdentifier(content); - } - } else 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(UPDATED)) { - if (second.equals(ENTRY) - && state.getCurrentItem().getPubDate() == null) { - state.getCurrentItem().setPubDate( - SyndDateUtils.parseRFC3339Date(content)); - } - } else if (top.equals(PUBLISHED)) { - if (second.equals(ENTRY)) { - state.getCurrentItem().setPubDate( - SyndDateUtils.parseRFC3339Date(content)); - } - } else if (top.equals(IMAGE)) { - state.getFeed().setImage(new FeedImage(content, null)); - } - - } - } - -} diff --git a/src/de/danoeh/antennapod/syndication/util/SyndDateUtils.java b/src/de/danoeh/antennapod/syndication/util/SyndDateUtils.java deleted file mode 100644 index 56687ac2e..000000000 --- a/src/de/danoeh/antennapod/syndication/util/SyndDateUtils.java +++ /dev/null @@ -1,161 +0,0 @@ -package de.danoeh.antennapod.syndication.util; - -import android.util.Log; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; - -/** - * Parses several date formats. - */ -public class SyndDateUtils { - private static final String TAG = "DateUtils"; - - private static final String[] RFC822DATES = {"dd MMM yy HH:mm:ss Z",}; - - /** - * RFC 3339 date format for UTC dates. - */ - public static final String RFC3339UTC = "yyyy-MM-dd'T'HH:mm:ss'Z'"; - - /** - * RFC 3339 date format for localtime dates with offset. - */ - public static final String RFC3339LOCAL = "yyyy-MM-dd'T'HH:mm:ssZ"; - - private static ThreadLocal<SimpleDateFormat> RFC822Formatter = new ThreadLocal<SimpleDateFormat>() { - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat(RFC822DATES[0], Locale.US); - } - - }; - - private static ThreadLocal<SimpleDateFormat> RFC3339Formatter = new ThreadLocal<SimpleDateFormat>() { - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat(RFC3339UTC, Locale.US); - } - - }; - - public static Date parseRFC822Date(String date) { - Date result = null; - if (date.contains("PDT")) { - date = date.replace("PDT", "PST8PDT"); - } - if (date.contains(",")) { - // Remove day of the week - date = date.substring(date.indexOf(",") + 1).trim(); - } - SimpleDateFormat format = RFC822Formatter.get(); - for (int i = 0; i < RFC822DATES.length; i++) { - try { - format.applyPattern(RFC822DATES[i]); - result = format.parse(date); - break; - } catch (ParseException e) { - e.printStackTrace(); - } - } - if (result == null) { - Log.e(TAG, "Unable to parse feed date correctly"); - } - - return result; - } - - public static Date parseRFC3339Date(String date) { - Date result = null; - SimpleDateFormat format = RFC3339Formatter.get(); - boolean isLocal = date.endsWith("Z"); - if (date.contains(".")) { - // remove secfrac - int fracIndex = date.indexOf("."); - String first = date.substring(0, fracIndex); - String second = null; - if (isLocal) { - second = date.substring(date.length() - 1); - } else { - if (date.contains("+")) { - second = date.substring(date.indexOf("+")); - } else { - second = date.substring(date.indexOf("-")); - } - } - - date = first + second; - } - if (isLocal) { - try { - result = format.parse(date); - } catch (ParseException e) { - e.printStackTrace(); - } - } else { - format.applyPattern(RFC3339LOCAL); - // remove last colon - StringBuffer buf = new StringBuffer(date.length() - 1); - int colonIdx = date.lastIndexOf(':'); - for (int x = 0; x < date.length(); x++) { - if (x != colonIdx) - buf.append(date.charAt(x)); - } - String bufStr = buf.toString(); - try { - result = format.parse(bufStr); - } catch (ParseException e) { - e.printStackTrace(); - Log.e(TAG, "Unable to parse date"); - } finally { - format.applyPattern(RFC3339UTC); - } - - } - - return result; - - } - - /** - * Takes a string of the form [HH:]MM:SS[.mmm] and converts it to - * milliseconds. - * - * @throws java.lang.NumberFormatException if the number segments contain invalid numbers. - */ - public static long parseTimeString(final String time) { - String[] parts = time.split(":"); - long result = 0; - int idx = 0; - if (parts.length == 3) { - // string has hours - result += Integer.valueOf(parts[idx]) * 3600000L; - idx++; - } - result += Integer.valueOf(parts[idx]) * 60000L; - idx++; - 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); - } -} diff --git a/src/de/danoeh/antennapod/syndication/util/SyndTypeUtils.java b/src/de/danoeh/antennapod/syndication/util/SyndTypeUtils.java deleted file mode 100644 index d0fa3a5fc..000000000 --- a/src/de/danoeh/antennapod/syndication/util/SyndTypeUtils.java +++ /dev/null @@ -1,42 +0,0 @@ -package de.danoeh.antennapod.syndication.util; - -import android.webkit.MimeTypeMap; -import org.apache.commons.io.FilenameUtils; - -/** Utility class for handling MIME-Types of enclosures */ -public class SyndTypeUtils { - - private final static String VALID_MIMETYPE = "audio/.*" + "|" + "video/.*" - + "|" + "application/ogg"; - - private SyndTypeUtils() { - - } - - public static boolean enclosureTypeValid(String type) { - if (type == null) { - return false; - } else { - return type.matches(VALID_MIMETYPE); - } - } - - /** - * Should be used if mime-type of enclosure tag is not supported. This - * method will check if the mime-type of the file extension is supported. If - * the type is not supported, this method will return null. - */ - public static String getValidMimeTypeFromUrl(String url) { - if (url != null) { - String extension = FilenameUtils.getExtension(url); - if (extension != null) { - String type = MimeTypeMap.getSingleton() - .getMimeTypeFromExtension(extension); - if (type != null && enclosureTypeValid(type)) { - return type; - } - } - } - return null; - } -} |