Search in sources :

Example 1 with XmlPullParserFactory

use of org.xmlpull.v1.XmlPullParserFactory in project android_frameworks_base by ResurrectionRemix.

the class DaylightHeaderProvider method loadHeaders.

private void loadHeaders() throws XmlPullParserException, IOException {
    mHeadersList = new ArrayList<DaylightHeaderInfo>();
    InputStream in = null;
    XmlPullParser parser = null;
    try {
        if (mHeaderName == null) {
            if (DEBUG)
                Log.i(TAG, "Load header pack config daylight_header.xml");
            in = mRes.getAssets().open("daylight_header.xml");
        } else {
            int idx = mHeaderName.lastIndexOf(".");
            String headerConfigFile = mHeaderName.substring(idx + 1) + ".xml";
            if (DEBUG)
                Log.i(TAG, "Load header pack config " + headerConfigFile);
            in = mRes.getAssets().open(headerConfigFile);
        }
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        parser = factory.newPullParser();
        parser.setInput(in, "UTF-8");
        loadResourcesFromXmlParser(parser);
    } finally {
        // Cleanup resources
        if (parser instanceof XmlResourceParser) {
            ((XmlResourceParser) parser).close();
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) InputStream(java.io.InputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) IOException(java.io.IOException)

Example 2 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 3 with XmlPullParserFactory

use of org.xmlpull.v1.XmlPullParserFactory in project android_frameworks_base by DirtyUnicorns.

the class DaylightHeaderProvider method loadHeaders.

private void loadHeaders() throws XmlPullParserException, IOException {
    mHeadersList = new ArrayList<DaylightHeaderInfo>();
    InputStream in = null;
    XmlPullParser parser = null;
    try {
        if (mHeaderName == null) {
            in = mRes.getAssets().open("daylight_header.xml");
        } else {
            int idx = mHeaderName.lastIndexOf(".");
            String headerConfigFile = mHeaderName.substring(idx + 1) + ".xml";
            in = mRes.getAssets().open(headerConfigFile);
        }
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        parser = factory.newPullParser();
        parser.setInput(in, "UTF-8");
        loadResourcesFromXmlParser(parser);
    } finally {
        // Cleanup resources
        if (parser instanceof XmlResourceParser) {
            ((XmlResourceParser) parser).close();
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) InputStream(java.io.InputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) IOException(java.io.IOException)

Example 4 with XmlPullParserFactory

use of org.xmlpull.v1.XmlPullParserFactory in project android-maps-utils by googlemaps.

the class KmlParserTest method createParser.

public XmlPullParser createParser(int res) throws Exception {
    InputStream stream = getInstrumentation().getContext().getResources().openRawResource(res);
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser parser = factory.newPullParser();
    parser.setInput(stream, null);
    parser.next();
    return parser;
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) InputStream(java.io.InputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser)

Example 5 with XmlPullParserFactory

use of org.xmlpull.v1.XmlPullParserFactory in project Mindroid.java by Himmele.

the class Configuration method read.

public static Configuration read(File configurationFile) throws Exception {
    FileInputStream inputStream = null;
    try {
        inputStream = new FileInputStream(configurationFile);
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();
        parser.setInput((InputStream) inputStream, "UTF-8");
        parser.require(XmlPullParser.START_DOCUMENT, null, null);
        parser.nextTag();
        parser.require(XmlPullParser.START_TAG, null, ROOT_TAG);
        Configuration configuration = new Configuration();
        for (int eventType = parser.nextTag(); !parser.getName().equals(ROOT_TAG) && eventType != XmlPullParser.END_TAG; eventType = parser.nextTag()) {
            if (parser.getName().equals(PLUGINS_TAG)) {
                parsePlugins(parser, configuration);
            } else {
                String tag = parser.getName();
                skipSubTree(parser);
                parser.require(XmlPullParser.END_TAG, null, tag);
            }
        }
        parser.require(XmlPullParser.END_TAG, null, ROOT_TAG);
        parser.next();
        parser.require(XmlPullParser.END_DOCUMENT, null, null);
        return configuration;
    } catch (Exception e) {
        throw e;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) XmlPullParser(org.xmlpull.v1.XmlPullParser) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Aggregations

XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)71 XmlPullParser (org.xmlpull.v1.XmlPullParser)56 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)30 IOException (java.io.IOException)24 InputStream (java.io.InputStream)16 InputStreamReader (java.io.InputStreamReader)10 StringReader (java.io.StringReader)10 FileInputStream (java.io.FileInputStream)8 XmlSerializer (org.xmlpull.v1.XmlSerializer)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 File (java.io.File)5 XmlResourceParser (android.content.res.XmlResourceParser)4 IOException2 (hudson.util.IOException2)4 BufferedReader (java.io.BufferedReader)4 Reader (java.io.Reader)4 Map (java.util.Map)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 StringWriter (java.io.StringWriter)2