Search in sources :

Example 26 with XmlPullParserFactory

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

the class KmlContainerParserTest 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 27 with XmlPullParserFactory

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

the class KmlFeatureParserTest 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 28 with XmlPullParserFactory

use of org.xmlpull.v1.XmlPullParserFactory in project violations-plugin by jenkinsci.

the class ParseXML method parse.

/**
     * Parse an xml file using a parser object.
     * @param xmlFile the file to parse.
     * @param xmlParser the parser object.
     * @throws IOException if there is a problem.
     */
public static void parse(File xmlFile, AbstractParser xmlParser) throws IOException {
    InputStream in = null;
    boolean seenException = false;
    try {
        in = new FileInputStream(xmlFile);
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(new XmlReader(in));
        xmlParser.setParser(parser);
        xmlParser.execute();
    } catch (IOException ex) {
        seenException = true;
        throw ex;
    } catch (Exception ex) {
        seenException = true;
        throw new IOException2(ex);
    } finally {
        CloseUtil.close(in, seenException);
    }
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) IOException2(hudson.util.IOException2)

Example 29 with XmlPullParserFactory

use of org.xmlpull.v1.XmlPullParserFactory in project KeepScore by nolanlawson.

the class GamesBackupSerializer method readGamesBackupSummary.

/**
     * Don't read the entire file; just read the game count and other basic, summarized information.
     * 
     * @param backupFilename
     * @return
     */
@SuppressWarnings("incomplete-switch")
public static GamesBackupSummary readGamesBackupSummary(Uri uri, Format format, ContentResolver contentResolver) {
    GamesBackupSummary result = new GamesBackupSummary();
    int infoReceived = 0;
    try {
        XmlPullParser parser = null;
        BufferedReader reader = null;
        int parserEvent = -1;
        try {
            XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
            parser = parserFactory.newPullParser();
            InputStream inputStream = contentResolver.openInputStream(uri);
            if (format == Format.GZIP) {
                // new, gzipped format
                inputStream = new GZIPInputStream(inputStream);
            }
            reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 0x1000);
            parser.setInput(reader);
            parserEvent = parser.getEventType();
            Tag tag = null;
            String text = null;
            while (parserEvent != XmlPullParser.END_DOCUMENT) {
                parserEvent = parser.next();
                switch(parserEvent) {
                    case XmlPullParser.START_TAG:
                        tag = Tag.valueOf(parser.getName());
                        break;
                    case XmlPullParser.TEXT:
                        text = parser.getText();
                        break;
                    case XmlPullParser.END_TAG:
                        switch(tag) {
                            case gameCount:
                                result.setGameCount(Integer.parseInt(text));
                                infoReceived++;
                                break;
                            case version:
                                result.setVersion(Integer.parseInt(text));
                                infoReceived++;
                                if (result.getVersion() < VERSION_TWO) {
                                    // no automatic vs. manual distinction in version one
                                    result.setAutomatic(false);
                                    infoReceived++;
                                }
                                if (result.getVersion() < VERSION_THREE) {
                                    // filename not stored in XML file itself until version three
                                    result.setFilename(uri.getLastPathSegment());
                                    infoReceived++;
                                }
                                break;
                            case automatic:
                                result.setAutomatic(Boolean.parseBoolean(text));
                                infoReceived++;
                                break;
                            case dateBackupSaved:
                                result.setDateSaved(Long.parseLong(text));
                                infoReceived++;
                                break;
                            case backupFilename:
                                result.setFilename(text);
                                infoReceived++;
                                break;
                        }
                        break;
                }
                if (infoReceived == 5) {
                    // this is all the info required to create a summary
                    return result;
                }
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    } catch (NumberFormatException e) {
        log.e(e, "unexpected exception for " + uri);
        throw new RuntimeException(e);
    } catch (IOException e) {
        log.e(e, "unexpected exception for " + uri);
        throw new RuntimeException(e);
    } catch (XmlPullParserException e) {
        log.e(e, "unexpected exception for " + uri);
        throw new RuntimeException(e);
    }
    throw new RuntimeException("failed to find summary for " + uri);
}
Also used : InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) BufferedReader(java.io.BufferedReader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 30 with XmlPullParserFactory

use of org.xmlpull.v1.XmlPullParserFactory in project LitePal by LitePalFramework.

the class ModelListActivity method populateMappingClasses.

private void populateMappingClasses() {
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xmlPullParser = factory.newPullParser();
        xmlPullParser.setInput(getInputStream(), "UTF-8");
        int eventType = xmlPullParser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            String nodeName = xmlPullParser.getName();
            switch(eventType) {
                case XmlPullParser.START_TAG:
                    {
                        if ("mapping".equals(nodeName)) {
                            String className = xmlPullParser.getAttributeValue("", "class");
                            mList.add(className);
                        }
                        break;
                    }
                default:
                    break;
            }
            eventType = xmlPullParser.next();
        }
    } catch (XmlPullParserException e) {
        throw new ParseConfigurationFileException(ParseConfigurationFileException.FILE_FORMAT_IS_NOT_CORRECT);
    } catch (IOException e) {
        throw new ParseConfigurationFileException(ParseConfigurationFileException.IO_EXCEPTION);
    }
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) ParseConfigurationFileException(org.litepal.exceptions.ParseConfigurationFileException) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

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