Search in sources :

Example 76 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project platform_frameworks_base by android.

the class XmlUtils method readThisStringArrayXml.

/**
     * Read a String[] object from an XmlPullParser.  The XML data could
     * previously have been generated by writeStringArrayXml().  The XmlPullParser
     * must be positioned <em>after</em> the tag that begins the list.
     *
     * @param parser The XmlPullParser from which to read the list data.
     * @param endTag Name of the tag that will end the list, usually "string-array".
     * @param name An array of one string, used to return the name attribute
     *             of the list's tag.
     *
     * @return Returns a newly generated String[].
     *
     * @see #readListXml
     */
public static final String[] readThisStringArrayXml(XmlPullParser parser, String endTag, String[] name) throws XmlPullParserException, java.io.IOException {
    int num;
    try {
        num = Integer.parseInt(parser.getAttributeValue(null, "num"));
    } catch (NullPointerException e) {
        throw new XmlPullParserException("Need num attribute in string-array");
    } catch (NumberFormatException e) {
        throw new XmlPullParserException("Not a number in num attribute in string-array");
    }
    parser.next();
    String[] array = new String[num];
    int i = 0;
    int eventType = parser.getEventType();
    do {
        if (eventType == parser.START_TAG) {
            if (parser.getName().equals("item")) {
                try {
                    array[i] = parser.getAttributeValue(null, "value");
                } catch (NullPointerException e) {
                    throw new XmlPullParserException("Need value attribute in item");
                } catch (NumberFormatException e) {
                    throw new XmlPullParserException("Not a number in value attribute in item");
                }
            } else {
                throw new XmlPullParserException("Expected item tag at: " + parser.getName());
            }
        } else if (eventType == parser.END_TAG) {
            if (parser.getName().equals(endTag)) {
                return array;
            } else if (parser.getName().equals("item")) {
                i++;
            } else {
                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)

Example 77 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project platform_frameworks_base by android.

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, StandardCharsets.UTF_8.name());
    return (ArrayList) readValueXml(parser, new String[1]);
}
Also used : XmlPullParser(org.xmlpull.v1.XmlPullParser) ArrayList(java.util.ArrayList)

Example 78 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project platform_frameworks_base by android.

the class XmlUtils method readThisByteArrayXml.

/**
     * Read a byte[] object from an XmlPullParser.  The XML data could
     * previously have been generated by writeByteArrayXml().  The XmlPullParser
     * must be positioned <em>after</em> the tag that begins the list.
     *
     * @param parser The XmlPullParser from which to read the list data.
     * @param endTag Name of the tag that will end the list, usually "list".
     * @param name An array of one string, used to return the name attribute
     *             of the list's tag.
     *
     * @return Returns a newly generated byte[].
     *
     * @see #writeByteArrayXml
     */
public static final byte[] readThisByteArrayXml(XmlPullParser parser, String endTag, String[] name) throws XmlPullParserException, java.io.IOException {
    int num;
    try {
        num = Integer.parseInt(parser.getAttributeValue(null, "num"));
    } catch (NullPointerException e) {
        throw new XmlPullParserException("Need num attribute in byte-array");
    } catch (NumberFormatException e) {
        throw new XmlPullParserException("Not a number in num attribute in byte-array");
    }
    byte[] array = new byte[num];
    int eventType = parser.getEventType();
    do {
        if (eventType == parser.TEXT) {
            if (num > 0) {
                String values = parser.getText();
                if (values == null || values.length() != num * 2) {
                    throw new XmlPullParserException("Invalid value found in byte-array: " + values);
                }
                // This is ugly, but keeping it to mirror the logic in #writeByteArrayXml.
                for (int i = 0; i < num; i++) {
                    char nibbleHighChar = values.charAt(2 * i);
                    char nibbleLowChar = values.charAt(2 * i + 1);
                    int nibbleHigh = nibbleHighChar > 'a' ? (nibbleHighChar - 'a' + 10) : (nibbleHighChar - '0');
                    int nibbleLow = nibbleLowChar > 'a' ? (nibbleLowChar - 'a' + 10) : (nibbleLowChar - '0');
                    array[i] = (byte) ((nibbleHigh & 0x0F) << 4 | (nibbleLow & 0x0F));
                }
            }
        } else if (eventType == parser.END_TAG) {
            if (parser.getName().equals(endTag)) {
                return array;
            } else {
                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)

Example 79 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project platform_frameworks_base by android.

the class XmlUtils method readThisValueXml.

private static final Object readThisValueXml(XmlPullParser parser, String[] name, ReadMapCallback callback, boolean arrayMap) 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("byte-array")) {
        res = readThisByteArrayXml(parser, "byte-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("int-array")) {
        res = readThisIntArrayXml(parser, "int-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("long-array")) {
        res = readThisLongArrayXml(parser, "long-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("double-array")) {
        res = readThisDoubleArrayXml(parser, "double-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("string-array")) {
        res = readThisStringArrayXml(parser, "string-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("boolean-array")) {
        res = readThisBooleanArrayXml(parser, "boolean-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("map")) {
        parser.next();
        res = arrayMap ? readThisArrayMapXml(parser, "map", name, callback) : readThisMapXml(parser, "map", name, callback);
        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, callback, arrayMap);
        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, callback, arrayMap);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (callback != null) {
        res = callback.readThisUnknownObjectXml(parser, tagName);
        name[0] = valueName;
        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 80 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project platform_frameworks_base by android.

the class XmlUtils method readThisDoubleArrayXml.

/**
     * Read a double[] object from an XmlPullParser.  The XML data could
     * previously have been generated by writeDoubleArrayXml().  The XmlPullParser
     * must be positioned <em>after</em> the tag that begins the list.
     *
     * @param parser The XmlPullParser from which to read the list data.
     * @param endTag Name of the tag that will end the list, usually "double-array".
     * @param name An array of one string, used to return the name attribute
     *             of the list's tag.
     *
     * @return Returns a newly generated double[].
     *
     * @see #readListXml
     */
public static final double[] readThisDoubleArrayXml(XmlPullParser parser, String endTag, String[] name) throws XmlPullParserException, java.io.IOException {
    int num;
    try {
        num = Integer.parseInt(parser.getAttributeValue(null, "num"));
    } catch (NullPointerException e) {
        throw new XmlPullParserException("Need num attribute in double-array");
    } catch (NumberFormatException e) {
        throw new XmlPullParserException("Not a number in num attribute in double-array");
    }
    parser.next();
    double[] array = new double[num];
    int i = 0;
    int eventType = parser.getEventType();
    do {
        if (eventType == parser.START_TAG) {
            if (parser.getName().equals("item")) {
                try {
                    array[i] = Double.parseDouble(parser.getAttributeValue(null, "value"));
                } catch (NullPointerException e) {
                    throw new XmlPullParserException("Need value attribute in item");
                } catch (NumberFormatException e) {
                    throw new XmlPullParserException("Not a number in value attribute in item");
                }
            } else {
                throw new XmlPullParserException("Expected item tag at: " + parser.getName());
            }
        } else if (eventType == parser.END_TAG) {
            if (parser.getName().equals(endTag)) {
                return array;
            } else if (parser.getName().equals("item")) {
                i++;
            } else {
                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)

Aggregations

XmlPullParser (org.xmlpull.v1.XmlPullParser)673 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)623 IOException (java.io.IOException)377 FileInputStream (java.io.FileInputStream)185 FileNotFoundException (java.io.FileNotFoundException)185 File (java.io.File)107 ArrayList (java.util.ArrayList)77 StringReader (java.io.StringReader)65 AttributeSet (android.util.AttributeSet)61 Test (org.junit.Test)57 TypedArray (android.content.res.TypedArray)56 InputStream (java.io.InputStream)48 AtomicFile (android.util.AtomicFile)47 HashMap (java.util.HashMap)45 BridgeXmlBlockParser (com.android.layoutlib.bridge.android.BridgeXmlBlockParser)39 FileReader (java.io.FileReader)36 BufferedInputStream (java.io.BufferedInputStream)30 XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)30 RemoteException (android.os.RemoteException)28 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)28