⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlutils.java

📁 Android平台上的media player, iPhone风格
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.android.internal.util;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import org.xmlpull.v1.XmlSerializer;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import android.util.Xml;/** {@hide} */public class XmlUtils{    public static void skipCurrentTag(XmlPullParser parser)            throws XmlPullParserException, IOException {        int outerDepth = parser.getDepth();        int type;        while ((type=parser.next()) != XmlPullParser.END_DOCUMENT               && (type != XmlPullParser.END_TAG                       || parser.getDepth() > outerDepth)) {        }    }    public static final int    convertValueToList(CharSequence value, String[] options, int defaultValue)    {        if (null != value) {            for (int i = 0; i < options.length; i++) {                if (value.equals(options[i]))                    return i;            }        }        return defaultValue;    }    public static final boolean    convertValueToBoolean(CharSequence value, boolean defaultValue)    {        boolean result = false;        if (null == value)            return defaultValue;        if (value.equals("1")        ||  value.equals("true")        ||  value.equals("TRUE"))            result = true;        return result;    }    public static final int    convertValueToInt(CharSequence charSeq, int defaultValue)    {        if (null == charSeq)            return defaultValue;        String nm = charSeq.toString();        // XXX This code is copied from Integer.decode() so we don't        // have to instantiate an Integer!                int value;        int sign = 1;        int index = 0;        int len = nm.length();        int base = 10;        if ('-' == nm.charAt(0)) {            sign = -1;            index++;        }        if ('0' == nm.charAt(index)) {            //  Quick check for a zero by itself            if (index == (len - 1))                return 0;            char    c = nm.charAt(index + 1);            if ('x' == c || 'X' == c) {                index += 2;                base = 16;            } else {                index++;                base = 8;            }        }        else if ('#' == nm.charAt(index))        {            index++;            base = 16;        }        return Integer.parseInt(nm.substring(index), base) * sign;    }    public static final int    convertValueToUnsignedInt(String value, int defaultValue)    {        if (null == value)            return defaultValue;        return parseUnsignedIntAttribute(value);    }    public static final int    parseUnsignedIntAttribute(CharSequence charSeq)    {                String  value = charSeq.toString();        long    bits;        int     index = 0;        int     len = value.length();        int     base = 10;                if ('0' == value.charAt(index)) {            //  Quick check for zero by itself            if (index == (len - 1))                return 0;                        char    c = value.charAt(index + 1);                        if ('x' == c || 'X' == c) {     //  check for hex                index += 2;                base = 16;            } else {                        //  check for octal                index++;                base = 8;            }        } else if ('#' == value.charAt(index)) {            index++;            base = 16;        }                return (int) Long.parseLong(value.substring(index), base);    }    /**     * 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();    }    /**     * Flatten a List into an output stream as XML.  The list can later be     * read back with readListXml().     *      * @param val The list to be flattened.     * @param out Where to write the XML data.     *      * @see #writeListXml(List, String, XmlSerializer)     * @see #writeMapXml     * @see #writeValueXml     * @see #readListXml     */    public static final void writeListXml(List val, OutputStream out)    throws XmlPullParserException, java.io.IOException    {        XmlSerializer serializer = Xml.newSerializer();        serializer.setOutput(out, "utf-8");        serializer.startDocument(null, true);        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);        writeListXml(val, null, serializer);        serializer.endDocument();    }    /**     * Flatten a Map into an XmlSerializer.  The map can later be read back     * with readThisMapXml().     *      * @param val The map to be flattened.     * @param name Name attribute to include with this list's tag, or null for     *             none.     * @param out XmlSerializer to write the map into.     *      * @see #writeMapXml(Map, OutputStream)     * @see #writeListXml     * @see #writeValueXml     * @see #readMapXml     */    public static final void writeMapXml(Map val, String name, XmlSerializer out)    throws XmlPullParserException, java.io.IOException    {        if (val == null) {            out.startTag(null, "null");            out.endTag(null, "null");            return;        }        Set s = val.entrySet();        Iterator i = s.iterator();        out.startTag(null, "map");        if (name != null) {            out.attribute(null, "name", name);        }        while (i.hasNext()) {            Map.Entry e = (Map.Entry)i.next();            writeValueXml(e.getValue(), (String)e.getKey(), out);        }        out.endTag(null, "map");    }    /**     * Flatten a List into an XmlSerializer.  The list can later be read back     * with readThisListXml().     *      * @param val The list to be flattened.     * @param name Name attribute to include with this list's tag, or null for     *             none.     * @param out XmlSerializer to write the list into.     *      * @see #writeListXml(List, OutputStream)     * @see #writeMapXml     * @see #writeValueXml     * @see #readListXml     */    public static final void writeListXml(List val, String name, XmlSerializer out)    throws XmlPullParserException, java.io.IOException    {        if (val == null) {            out.startTag(null, "null");            out.endTag(null, "null");            return;        }        out.startTag(null, "list");        if (name != null) {            out.attribute(null, "name", name);        }        int N = val.size();        int i=0;        while (i < N) {            writeValueXml(val.get(i), null, out);            i++;        }        out.endTag(null, "list");    }    /**     * Flatten a byte[] into an XmlSerializer.  The list can later be read back     * with readThisByteArrayXml().     *      * @param val The byte array to be flattened.     * @param name Name attribute to include with this array's tag, or null for     *             none.     * @param out XmlSerializer to write the array into.     *      * @see #writeMapXml     * @see #writeValueXml     */    public static final void writeByteArrayXml(byte[] val, String name,            XmlSerializer out)            throws XmlPullParserException, java.io.IOException {                if (val == null) {            out.startTag(null, "null");            out.endTag(null, "null");            return;        }        out.startTag(null, "byte-array");        if (name != null) {            out.attribute(null, "name", name);        }        final int N = val.length;        out.attribute(null, "num", Integer.toString(N));                StringBuilder sb = new StringBuilder(val.length*2);        for (int i=0; i<N; i++) {            int b = val[i];            int h = b>>4;            sb.append(h >= 10 ? ('a'+h-10) : ('0'+h));            h = b&0xff;            sb.append(h >= 10 ? ('a'+h-10) : ('0'+h));        }        out.text(sb.toString());        out.endTag(null, "byte-array");    }    /**     * Flatten an int[] into an XmlSerializer.  The list can later be read back     * with readThisIntArrayXml().     *      * @param val The int array to be flattened.     * @param name Name attribute to include with this array's tag, or null for     *             none.     * @param out XmlSerializer to write the array into.     *      * @see #writeMapXml     * @see #writeValueXml     * @see #readThisIntArrayXml     */    public static final void writeIntArrayXml(int[] val, String name,            XmlSerializer out)            throws XmlPullParserException, java.io.IOException {                if (val == null) {            out.startTag(null, "null");            out.endTag(null, "null");            return;        }        out.startTag(null, "int-array");        if (name != null) {            out.attribute(null, "name", name);        }                final int N = val.length;        out.attribute(null, "num", Integer.toString(N));        for (int i=0; i<N; i++) {            out.startTag(null, "item");            out.attribute(null, "value", Integer.toString(val[i]));            out.endTag(null, "item");        }        out.endTag(null, "int-array");    }    /**     * Flatten an object's value into an XmlSerializer.  The value can later     * be read back with readThisValueXml().     *      * Currently supported value types are: null, String, Integer, Long,     * Float, Double Boolean, Map, List.     *      * @param v The object to be flattened.     * @param name Name attribute to include with this value's tag, or null     *             for none.     * @param out XmlSerializer to write the object into.     *      * @see #writeMapXml     * @see #writeListXml     * @see #readValueXml     */    public static final void writeValueXml(Object v, String name, XmlSerializer out)    throws XmlPullParserException, java.io.IOException    {        String typeStr;        if (v == null) {            out.startTag(null, "null");            if (name != null) {                out.attribute(null, "name", name);            }            out.endTag(null, "null");            return;        } else if (v instanceof String) {            out.startTag(null, "string");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -