Search in sources :

Example 16 with XmlPullParserFactory

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);
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlStreamReader(org.apache.commons.io.input.XmlStreamReader) Reader(java.io.Reader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) File(java.io.File)

Example 17 with XmlPullParserFactory

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;
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) XmlPullParser(org.xmlpull.v1.XmlPullParser)

Example 18 with XmlPullParserFactory

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;
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) XmlPullParser(org.xmlpull.v1.XmlPullParser) StringReader(java.io.StringReader)

Example 19 with XmlPullParserFactory

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;
    }
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) XmlPullParser(org.xmlpull.v1.XmlPullParser) ArrayList(java.util.ArrayList) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) VisibleForTesting(org.chromium.base.VisibleForTesting)

Example 20 with XmlPullParserFactory

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();
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) Iterator(java.util.Iterator) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Aggregations

XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)40 XmlPullParser (org.xmlpull.v1.XmlPullParser)28 IOException (java.io.IOException)15 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)15 InputStream (java.io.InputStream)11 StringReader (java.io.StringReader)7 InputStreamReader (java.io.InputStreamReader)6 XmlSerializer (org.xmlpull.v1.XmlSerializer)5 XmlResourceParser (android.content.res.XmlResourceParser)4 IOException2 (hudson.util.IOException2)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 HashMap (java.util.HashMap)3 Reader (java.io.Reader)2 Iterator (java.util.Iterator)2 ParseConfigurationFileException (org.litepal.exceptions.ParseConfigurationFileException)2 PackageManager (android.content.pm.PackageManager)1 Resources (android.content.res.Resources)1 NonNull (android.support.annotation.NonNull)1 VisibleForTesting (android.support.annotation.VisibleForTesting)1