summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/syndication/handler/TypeGetter.java
blob: 0ac1b7ae2c305c199fe621ba2e8c92caa4764169 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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.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";

	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);
						}
					} else {
						eventType = xpp.next();
					}
				}

			} catch (XmlPullParserException e) {
				e.printStackTrace();
			} 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;
	}
}