Implemented opml reader

This commit is contained in:
daniel oeh 2012-07-23 13:18:42 +02:00
parent 26e8b191cb
commit 65505fad56
2 changed files with 124 additions and 0 deletions

View File

@ -0,0 +1,46 @@
package de.danoeh.antennapod.opml;
/** Represents a single feed in an OPML file. */
public class OpmlElement {
private String text;
private String xmlUrl;
private String htmlUrl;
private String type;
public OpmlElement() {
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getXmlUrl() {
return xmlUrl;
}
public void setXmlUrl(String xmlUrl) {
this.xmlUrl = xmlUrl;
}
public String getHtmlUrl() {
return htmlUrl;
}
public void setHtmlUrl(String htmlUrl) {
this.htmlUrl = htmlUrl;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}

View File

@ -0,0 +1,78 @@
package de.danoeh.antennapod.opml;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import de.danoeh.antennapod.AppConfig;
import android.content.Context;
import android.util.Log;
/** Reads OPML documents. */
public class OpmlReader {
private static final String TAG = "OpmlReader";
// TAGS
private static final String OPML = "opml";
private static final String BODY = "body";
private static final String OUTLINE = "outline";
private static final String TEXT = "text";
private static final String XMLURL = "xmlUrl";
private static final String HTMLURL = "htmlUrl";
private static final String TYPE = "type";
// ATTRIBUTES
private boolean isInOpml = false;
private boolean isInBody = false;
private ArrayList<OpmlElement> elementList;
/** Reads an Opml document and returns a list of all OPML elements it can find
* @throws IOException
* @throws XmlPullParserException*/
public ArrayList<OpmlElement> readDocument(Reader reader) throws XmlPullParserException, IOException {
elementList = new ArrayList<OpmlElement>();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(reader);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
if (AppConfig.DEBUG) Log.d(TAG, "Reached beginning of document");
break;
case XmlPullParser.START_TAG:
if (xpp.getName().equals(OPML)) {
isInOpml = true;
if (AppConfig.DEBUG) Log.d(TAG, "Reached beginning of OPML tree.");
} else if (isInOpml && xpp.getName().equals(BODY)) {
isInBody = true;
if (AppConfig.DEBUG) Log.d(TAG, "Reached beginning of body tree.");
} else if (isInBody && xpp.getName().equals(OUTLINE)) {
if (AppConfig.DEBUG) Log.d(TAG, "Found new Opml element");
OpmlElement element = new OpmlElement();
element.setText(xpp.getAttributeValue(null, TEXT));
element.setXmlUrl(xpp.getAttributeValue(null, XMLURL));
element.setHtmlUrl(xpp.getAttributeValue(null, HTMLURL));
element.setType(xpp.getAttributeValue(null, TYPE));
elementList.add(element);
}
break;
}
eventType = xpp.next();
}
if (AppConfig.DEBUG) Log.d(TAG, "Parsing finished.");
return elementList;
}
}