Search in sources :

Example 21 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project android_frameworks_base by ParanoidAndroid.

the class PowerProfile method readPowerValuesFromXml.

private void readPowerValuesFromXml(Context context) {
    int id = com.android.internal.R.xml.power_profile;
    XmlResourceParser parser = context.getResources().getXml(id);
    boolean parsingArray = false;
    ArrayList<Double> array = new ArrayList<Double>();
    String arrayName = null;
    try {
        XmlUtils.beginDocument(parser, TAG_DEVICE);
        while (true) {
            XmlUtils.nextElement(parser);
            String element = parser.getName();
            if (element == null)
                break;
            if (parsingArray && !element.equals(TAG_ARRAYITEM)) {
                // Finish array
                sPowerMap.put(arrayName, array.toArray(new Double[array.size()]));
                parsingArray = false;
            }
            if (element.equals(TAG_ARRAY)) {
                parsingArray = true;
                array.clear();
                arrayName = parser.getAttributeValue(null, ATTR_NAME);
            } else if (element.equals(TAG_ITEM) || element.equals(TAG_ARRAYITEM)) {
                String name = null;
                if (!parsingArray)
                    name = parser.getAttributeValue(null, ATTR_NAME);
                if (parser.next() == XmlPullParser.TEXT) {
                    String power = parser.getText();
                    double value = 0;
                    try {
                        value = Double.valueOf(power);
                    } catch (NumberFormatException nfe) {
                    }
                    if (element.equals(TAG_ITEM)) {
                        sPowerMap.put(name, value);
                    } else if (parsingArray) {
                        array.add(value);
                    }
                }
            }
        }
        if (parsingArray) {
            sPowerMap.put(arrayName, array.toArray(new Double[array.size()]));
        }
    } catch (XmlPullParserException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        parser.close();
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) ArrayList(java.util.ArrayList) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 22 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project android_frameworks_base by ParanoidAndroid.

the class XmlUtils method readThisValueXml.

private static final Object readThisValueXml(XmlPullParser parser, String[] name) throws XmlPullParserException, java.io.IOException {
    final String valueName = parser.getAttributeValue(null, "name");
    final String tagName = parser.getName();
    //System.out.println("Reading this value tag: " + tagName + ", name=" + valueName);
    Object res;
    if (tagName.equals("null")) {
        res = null;
    } else if (tagName.equals("string")) {
        String value = "";
        int eventType;
        while ((eventType = parser.next()) != parser.END_DOCUMENT) {
            if (eventType == parser.END_TAG) {
                if (parser.getName().equals("string")) {
                    name[0] = valueName;
                    //System.out.println("Returning value for " + valueName + ": " + value);
                    return value;
                }
                throw new XmlPullParserException("Unexpected end tag in <string>: " + parser.getName());
            } else if (eventType == parser.TEXT) {
                value += parser.getText();
            } else if (eventType == parser.START_TAG) {
                throw new XmlPullParserException("Unexpected start tag in <string>: " + parser.getName());
            }
        }
        throw new XmlPullParserException("Unexpected end of document in <string>");
    } else if ((res = readThisPrimitiveValueXml(parser, tagName)) != null) {
    // all work already done by readThisPrimitiveValueXml
    } else if (tagName.equals("int-array")) {
        parser.next();
        res = readThisIntArrayXml(parser, "int-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("map")) {
        parser.next();
        res = readThisMapXml(parser, "map", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("list")) {
        parser.next();
        res = readThisListXml(parser, "list", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("set")) {
        parser.next();
        res = readThisSetXml(parser, "set", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else {
        throw new XmlPullParserException("Unknown tag: " + tagName);
    }
    // Skip through to end tag.
    int eventType;
    while ((eventType = parser.next()) != parser.END_DOCUMENT) {
        if (eventType == parser.END_TAG) {
            if (parser.getName().equals(tagName)) {
                name[0] = valueName;
                //System.out.println("Returning value for " + valueName + ": " + res);
                return res;
            }
            throw new XmlPullParserException("Unexpected end tag in <" + tagName + ">: " + parser.getName());
        } else if (eventType == parser.TEXT) {
            throw new XmlPullParserException("Unexpected text in <" + tagName + ">: " + parser.getName());
        } else if (eventType == parser.START_TAG) {
            throw new XmlPullParserException("Unexpected start tag in <" + tagName + ">: " + parser.getName());
        }
    }
    throw new XmlPullParserException("Unexpected end of document in <" + tagName + ">");
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 23 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project android_frameworks_base by ParanoidAndroid.

the class XmlUtils method writeMapXml.

/**
     * Flatten a Map into an output stream as XML.  The map can later be
     * read back with readMapXml().
     *
     * @param val The map to be flattened.
     * @param out Where to write the XML data.
     *
     * @see #writeMapXml(Map, String, XmlSerializer)
     * @see #writeListXml
     * @see #writeValueXml
     * @see #readMapXml
     */
public static final void writeMapXml(Map val, OutputStream out) throws XmlPullParserException, java.io.IOException {
    XmlSerializer serializer = new FastXmlSerializer();
    serializer.setOutput(out, "utf-8");
    serializer.startDocument(null, true);
    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
    writeMapXml(val, null, serializer);
    serializer.endDocument();
}
Also used : XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 24 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project android_frameworks_base by ParanoidAndroid.

the class XmlUtils method readThisSetXml.

/**
     * Read a HashSet object from an XmlPullParser. The XML data could previously
     * have been generated by writeSetXml(). The XmlPullParser must be positioned
     * <em>after</em> the tag that begins the set.
     * 
     * @param parser The XmlPullParser from which to read the set data.
     * @param endTag Name of the tag that will end the set, usually "set".
     * @param name An array of one string, used to return the name attribute
     *             of the set's tag.
     *
     * @return HashSet The newly generated set.
     * 
     * @throws XmlPullParserException
     * @throws java.io.IOException
     * 
     * @see #readSetXml
     */
public static final HashSet readThisSetXml(XmlPullParser parser, String endTag, String[] name) throws XmlPullParserException, java.io.IOException {
    HashSet set = new HashSet();
    int eventType = parser.getEventType();
    do {
        if (eventType == parser.START_TAG) {
            Object val = readThisValueXml(parser, name);
            set.add(val);
        //System.out.println("Adding to set: " + val);
        } else if (eventType == parser.END_TAG) {
            if (parser.getName().equals(endTag)) {
                return set;
            }
            throw new XmlPullParserException("Expected " + endTag + " end tag at: " + parser.getName());
        }
        eventType = parser.next();
    } while (eventType != parser.END_DOCUMENT);
    throw new XmlPullParserException("Document ended before " + endTag + " end tag");
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException) HashSet(java.util.HashSet)

Example 25 with XmlPullParserException

use of org.xmlpull.v1.XmlPullParserException in project android_frameworks_base by ParanoidAndroid.

the class XmlUtils method readListXml.

/**
     * Read an ArrayList from an InputStream containing XML.  The stream can
     * previously have been written by writeListXml().
     *
     * @param in The InputStream from which to read.
     *
     * @return ArrayList The resulting list.
     *
     * @see #readMapXml
     * @see #readValueXml
     * @see #readThisListXml
     * @see #writeListXml
     */
public static final ArrayList readListXml(InputStream in) throws XmlPullParserException, java.io.IOException {
    XmlPullParser parser = Xml.newPullParser();
    parser.setInput(in, null);
    return (ArrayList) readValueXml(parser, new String[1]);
}
Also used : XmlPullParser(org.xmlpull.v1.XmlPullParser) ArrayList(java.util.ArrayList)

Aggregations

XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1062 IOException (java.io.IOException)630 XmlPullParser (org.xmlpull.v1.XmlPullParser)437 FileNotFoundException (java.io.FileNotFoundException)187 XmlResourceParser (android.content.res.XmlResourceParser)185 FileInputStream (java.io.FileInputStream)182 AttributeSet (android.util.AttributeSet)159 TypedArray (android.content.res.TypedArray)156 Resources (android.content.res.Resources)101 File (java.io.File)101 ArrayList (java.util.ArrayList)97 PackageManager (android.content.pm.PackageManager)62 ComponentName (android.content.ComponentName)57 InputStream (java.io.InputStream)56 HashMap (java.util.HashMap)56 Intent (android.content.Intent)54 XmlSerializer (org.xmlpull.v1.XmlSerializer)52 AtomicFile (android.util.AtomicFile)50 BufferedInputStream (java.io.BufferedInputStream)44 TypedValue (android.util.TypedValue)43