use of org.xmlpull.v1.XmlPullParserFactory in project AntennaPod by AntennaPod.
the class TypeGetter method getType.
public Type getType(Feed feed) throws UnsupportedFeedtypeException {
XmlPullParserFactory factory;
if (feed.getFile_url() != null) {
Reader reader = null;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
reader = createReader(feed);
xpp.setInput(reader);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
String tag = xpp.getName();
switch(tag) {
case ATOM_ROOT:
feed.setType(Feed.TYPE_ATOM1);
Log.d(TAG, "Recognized type Atom");
String strLang = xpp.getAttributeValue("http://www.w3.org/XML/1998/namespace", "lang");
if (strLang != null) {
feed.setLanguage(strLang);
}
return Type.ATOM;
case RSS_ROOT:
String strVersion = xpp.getAttributeValue(null, "version");
if (strVersion != null) {
if (strVersion.equals("2.0")) {
feed.setType(Feed.TYPE_RSS2);
Log.d(TAG, "Recognized type RSS 2.0");
return Type.RSS20;
} else if (strVersion.equals("0.91") || strVersion.equals("0.92")) {
Log.d(TAG, "Recognized type RSS 0.91/0.92");
return Type.RSS091;
}
}
throw new UnsupportedFeedtypeException(Type.INVALID);
default:
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();
}
throw new UnsupportedFeedtypeException(Type.INVALID, rootElement);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Log.d(TAG, "Type is invalid");
throw new UnsupportedFeedtypeException(Type.INVALID);
}
use of org.xmlpull.v1.XmlPullParserFactory in project AntennaPod by AntennaPod.
the class OpmlReader method readDocument.
/**
* 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<>();
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 (BuildConfig.DEBUG)
Log.d(TAG, "Reached beginning of document");
break;
case XmlPullParser.START_TAG:
if (xpp.getName().equals(OpmlSymbols.OPML)) {
isInOpml = true;
if (BuildConfig.DEBUG)
Log.d(TAG, "Reached beginning of OPML tree.");
} else if (isInOpml && xpp.getName().equals(OpmlSymbols.OUTLINE)) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Found new Opml element");
OpmlElement element = new OpmlElement();
final String title = xpp.getAttributeValue(null, OpmlSymbols.TITLE);
if (title != null) {
Log.i(TAG, "Using title: " + title);
element.setText(title);
} else {
Log.i(TAG, "Title not found, using text");
element.setText(xpp.getAttributeValue(null, OpmlSymbols.TEXT));
}
element.setXmlUrl(xpp.getAttributeValue(null, OpmlSymbols.XMLURL));
element.setHtmlUrl(xpp.getAttributeValue(null, OpmlSymbols.HTMLURL));
element.setType(xpp.getAttributeValue(null, OpmlSymbols.TYPE));
if (element.getXmlUrl() != null) {
if (element.getText() == null) {
Log.i(TAG, "Opml element has no text attribute.");
element.setText(element.getXmlUrl());
}
elementList.add(element);
} else {
if (BuildConfig.DEBUG)
Log.d(TAG, "Skipping element because of missing xml url");
}
}
break;
}
eventType = xpp.next();
}
if (BuildConfig.DEBUG)
Log.d(TAG, "Parsing finished.");
return elementList;
}
use of org.xmlpull.v1.XmlPullParserFactory in project BarcodeEye by BarcodeEye.
the class AmazonInfoRetriever method buildParser.
private static XmlPullParser buildParser(CharSequence contents) throws XmlPullParserException {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(contents.toString()));
return xpp;
}
use of org.xmlpull.v1.XmlPullParserFactory in project AndroidChromium by JackyAndroid.
the class OMADownloadHandler method parseDownloadDescriptor.
/**
* Parses the input stream and returns the OMA information.
*
* @param is The input stream to the parser.
* @return OMA information about the download content, or null if an error is found.
*/
@VisibleForTesting
static OMAInfo parseDownloadDescriptor(InputStream is) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(is, null);
int eventType = parser.getEventType();
String currentAttribute = null;
OMAInfo info = new OMAInfo();
StringBuilder sb = null;
List<String> attributeList = new ArrayList<String>(Arrays.asList(OMA_TYPE, OMA_SIZE, OMA_OBJECT_URI, OMA_INSTALL_NOTIFY_URI, OMA_NEXT_URL, OMA_DD_VERSION, OMA_NAME, OMA_DESCRIPTION, OMA_VENDOR, OMA_INFO_URL, OMA_ICON_URI, OMA_INSTALL_PARAM));
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
if (!info.isEmpty())
return null;
} else if (eventType == XmlPullParser.START_TAG) {
String tagName = parser.getName();
if (attributeList.contains(tagName)) {
if (currentAttribute != null) {
Log.w(TAG, "Nested attributes was found in the download descriptor");
return null;
}
sb = new StringBuilder();
currentAttribute = tagName;
}
} else if (eventType == XmlPullParser.END_TAG) {
if (currentAttribute != null) {
if (!currentAttribute.equals(parser.getName())) {
Log.w(TAG, "Nested attributes was found in the download descriptor");
return null;
}
info.addAttributeValue(currentAttribute, sb.toString().trim());
currentAttribute = null;
sb = null;
}
} else if (eventType == XmlPullParser.TEXT) {
if (currentAttribute != null) {
sb.append(parser.getText());
}
}
eventType = parser.next();
}
return info;
} catch (XmlPullParserException e) {
Log.w(TAG, "Failed to parse download descriptor.", e);
return null;
} catch (IOException e) {
Log.w(TAG, "Failed to read download descriptor.", e);
return null;
}
}
use of org.xmlpull.v1.XmlPullParserFactory in project processdash by dtuma.
the class XmlSnippetContentSerializer method format.
public void format(PageContentTO page, OutputStream out) throws IOException {
XmlSerializer ser = null;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
ser = factory.newSerializer();
} catch (XmlPullParserException xppe) {
throw new RuntimeException("Couldn't obtain xml serializer", xppe);
}
ser.setOutput(out, ENCODING);
ser.startDocument(ENCODING, null);
ser.ignorableWhitespace(NEWLINE);
ser.startTag(null, DOC_ROOT_ELEM);
ser.ignorableWhitespace(NEWLINE + NEWLINE);
writePageMetadata(page, ser);
Iterator headerSnippets = page.getHeaderSnippets();
writeWrappedSnippets(ser, PAGE_HEADING_TAG, headerSnippets);
Iterator contentSnippets = page.getContentSnippets();
writeSnippets(ser, contentSnippets);
Iterator footerSnippets = page.getFooterSnippets();
writeWrappedSnippets(ser, PAGE_FOOTER_TAG, footerSnippets);
ser.endTag(null, DOC_ROOT_ELEM);
ser.ignorableWhitespace(NEWLINE);
ser.endDocument();
out.close();
}
Aggregations