From f78755bd1392c28bbad45bbd2a5ad884d97c7bb5 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Fri, 8 Jun 2012 20:26:48 +0200 Subject: [PATCH] Created the TypeGetter --- .../podfetcher/syndication/FeedHandler.java | 2 + src/de/podfetcher/syndication/TypeGetter.java | 73 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/de/podfetcher/syndication/TypeGetter.java diff --git a/src/de/podfetcher/syndication/FeedHandler.java b/src/de/podfetcher/syndication/FeedHandler.java index 2b6cc9c08..dc9f660c0 100644 --- a/src/de/podfetcher/syndication/FeedHandler.java +++ b/src/de/podfetcher/syndication/FeedHandler.java @@ -27,6 +27,8 @@ public class FeedHandler { public final static String ENC_TYPE = "type"; public Feed parseFeed(Feed feed) { + TypeGetter tg = new TypeGetter(); + tg.getType(feed); RSSHandler handler = new RSSHandler(feed); try { SAXParserFactory factory = SAXParserFactory.newInstance(); diff --git a/src/de/podfetcher/syndication/TypeGetter.java b/src/de/podfetcher/syndication/TypeGetter.java new file mode 100644 index 000000000..b3fd2bb54 --- /dev/null +++ b/src/de/podfetcher/syndication/TypeGetter.java @@ -0,0 +1,73 @@ +package de.podfetcher.syndication; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; + +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; +import org.xmlpull.v1.XmlPullParserFactory; + +import android.util.Log; + +import de.podfetcher.feed.Feed; + +/** Gets the type of a specific feed. */ +public class TypeGetter { + private static final String TAG = "TypeGetter"; + + enum Type { + RSS20, ATOM, INVALID + } + + private static final String ATOM_ROOT = "feed"; + private static final String RSS_ROOT = "rss"; + + public Type getType(Feed feed) { + XmlPullParserFactory factory; + 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)) { + Log.d(TAG, "Recongnized type Atom"); + return Type.ATOM; + } else if (tag.equals(RSS_ROOT) + && (xpp.getAttributeValue(null, "version") + .equals("2.0"))) { + Log.d(TAG, "Recognized type RSS 2.0"); + return Type.RSS20; + } else { + return Type.INVALID; + } + } else { + eventType = xpp.next(); + } + } + + } catch (XmlPullParserException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return Type.INVALID; + } + + private Reader createReader(Feed feed) { + FileReader reader; + try { + reader = new FileReader(new File(feed.getFile_url())); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return null; + } + return reader; + } +}