Search in sources :

Example 11 with XmlPullParserException

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

the class Resources method parseBundleExtra.

/**
     * Parse a name/value pair out of an XML tag holding that data.  The
     * AttributeSet must be holding the data defined by
     * {@link android.R.styleable#Extra}.  The following value types are supported:
     * <ul>
     * <li> {@link TypedValue#TYPE_STRING}:
     * {@link Bundle#putCharSequence Bundle.putCharSequence()}
     * <li> {@link TypedValue#TYPE_INT_BOOLEAN}:
     * {@link Bundle#putCharSequence Bundle.putBoolean()}
     * <li> {@link TypedValue#TYPE_FIRST_INT}-{@link TypedValue#TYPE_LAST_INT}:
     * {@link Bundle#putCharSequence Bundle.putBoolean()}
     * <li> {@link TypedValue#TYPE_FLOAT}:
     * {@link Bundle#putCharSequence Bundle.putFloat()}
     * </ul>
     * 
     * @param tagName The name of the tag these attributes come from; this is
     * only used for reporting error messages.
     * @param attrs The attributes from which to retrieve the name/value pair.
     * @param outBundle The Bundle in which to place the parsed value.
     * @throws XmlPullParserException If the attributes are not valid.
     */
public void parseBundleExtra(String tagName, AttributeSet attrs, Bundle outBundle) throws XmlPullParserException {
    TypedArray sa = obtainAttributes(attrs, com.android.internal.R.styleable.Extra);
    String name = sa.getString(com.android.internal.R.styleable.Extra_name);
    if (name == null) {
        sa.recycle();
        throw new XmlPullParserException("<" + tagName + "> requires an android:name attribute at " + attrs.getPositionDescription());
    }
    TypedValue v = sa.peekValue(com.android.internal.R.styleable.Extra_value);
    if (v != null) {
        if (v.type == TypedValue.TYPE_STRING) {
            CharSequence cs = v.coerceToString();
            outBundle.putCharSequence(name, cs);
        } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
            outBundle.putBoolean(name, v.data != 0);
        } else if (v.type >= TypedValue.TYPE_FIRST_INT && v.type <= TypedValue.TYPE_LAST_INT) {
            outBundle.putInt(name, v.data);
        } else if (v.type == TypedValue.TYPE_FLOAT) {
            outBundle.putFloat(name, v.getFloat());
        } else {
            sa.recycle();
            throw new XmlPullParserException("<" + tagName + "> only supports string, integer, float, color, and boolean at " + attrs.getPositionDescription());
        }
    } else {
        sa.recycle();
        throw new XmlPullParserException("<" + tagName + "> requires an android:value or android:resource attribute at " + attrs.getPositionDescription());
    }
    sa.recycle();
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException) TypedValue(android.util.TypedValue)

Example 12 with XmlPullParserException

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

the class ColorStateList method createFromXml.

/**
     * Create a ColorStateList from an XML document, given a set of {@link Resources}.
     */
public static ColorStateList createFromXml(Resources r, XmlPullParser parser) throws XmlPullParserException, IOException {
    AttributeSet attrs = Xml.asAttributeSet(parser);
    int type;
    while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
    }
    if (type != XmlPullParser.START_TAG) {
        throw new XmlPullParserException("No start tag found");
    }
    return createFromXmlInner(r, parser, attrs);
}
Also used : AttributeSet(android.util.AttributeSet) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 13 with XmlPullParserException

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

the class ColorStateList method inflate.

/**
     * Fill in this object based on the contents of an XML "selector" element.
     */
private void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException {
    int type;
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    int listAllocated = 20;
    int listSize = 0;
    int[] colorList = new int[listAllocated];
    int[][] stateSpecList = new int[listAllocated][];
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        if (depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }
        int colorRes = 0;
        int color = 0xffff0000;
        boolean haveColor = false;
        int i;
        int j = 0;
        final int numAttrs = attrs.getAttributeCount();
        int[] stateSpec = new int[numAttrs];
        for (i = 0; i < numAttrs; i++) {
            final int stateResId = attrs.getAttributeNameResource(i);
            if (stateResId == 0)
                break;
            if (stateResId == com.android.internal.R.attr.color) {
                colorRes = attrs.getAttributeResourceValue(i, 0);
                if (colorRes == 0) {
                    color = attrs.getAttributeIntValue(i, color);
                    haveColor = true;
                }
            } else {
                stateSpec[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId;
            }
        }
        stateSpec = StateSet.trimStateSet(stateSpec, j);
        if (colorRes != 0) {
            color = r.getColor(colorRes);
        } else if (!haveColor) {
            throw new XmlPullParserException(parser.getPositionDescription() + ": <item> tag requires a 'android:color' attribute.");
        }
        if (listSize == 0 || stateSpec.length == 0) {
            mDefaultColor = color;
        }
        if (listSize + 1 >= listAllocated) {
            listAllocated = ArrayUtils.idealIntArraySize(listSize + 1);
            int[] ncolor = new int[listAllocated];
            System.arraycopy(colorList, 0, ncolor, 0, listSize);
            int[][] nstate = new int[listAllocated][];
            System.arraycopy(stateSpecList, 0, nstate, 0, listSize);
            colorList = ncolor;
            stateSpecList = nstate;
        }
        colorList[listSize] = color;
        stateSpecList[listSize] = stateSpec;
        listSize++;
    }
    mColors = new int[listSize];
    mStateSpecs = new int[listSize][];
    System.arraycopy(colorList, 0, mColors, 0, listSize);
    System.arraycopy(stateSpecList, 0, mStateSpecs, 0, listSize);
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 14 with XmlPullParserException

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

the class PreferenceActivity method loadHeadersFromResource.

/**
     * Parse the given XML file as a header description, adding each
     * parsed Header into the target list.
     *
     * @param resid The XML resource to load and parse.
     * @param target The list in which the parsed headers should be placed.
     */
public void loadHeadersFromResource(int resid, List<Header> target) {
    XmlResourceParser parser = null;
    try {
        parser = getResources().getXml(resid);
        AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
        // Parse next until start tag is found
        }
        String nodeName = parser.getName();
        if (!"preference-headers".equals(nodeName)) {
            throw new RuntimeException("XML document must start with <preference-headers> tag; found" + nodeName + " at " + parser.getPositionDescription());
        }
        Bundle curBundle = null;
        final int outerDepth = parser.getDepth();
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                continue;
            }
            nodeName = parser.getName();
            if ("header".equals(nodeName)) {
                Header header = new Header();
                TypedArray sa = getResources().obtainAttributes(attrs, com.android.internal.R.styleable.PreferenceHeader);
                header.id = sa.getResourceId(com.android.internal.R.styleable.PreferenceHeader_id, (int) HEADER_ID_UNDEFINED);
                TypedValue tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_title);
                if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                    if (tv.resourceId != 0) {
                        header.titleRes = tv.resourceId;
                    } else {
                        header.title = tv.string;
                    }
                }
                tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_summary);
                if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                    if (tv.resourceId != 0) {
                        header.summaryRes = tv.resourceId;
                    } else {
                        header.summary = tv.string;
                    }
                }
                tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_breadCrumbTitle);
                if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                    if (tv.resourceId != 0) {
                        header.breadCrumbTitleRes = tv.resourceId;
                    } else {
                        header.breadCrumbTitle = tv.string;
                    }
                }
                tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_breadCrumbShortTitle);
                if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                    if (tv.resourceId != 0) {
                        header.breadCrumbShortTitleRes = tv.resourceId;
                    } else {
                        header.breadCrumbShortTitle = tv.string;
                    }
                }
                header.iconRes = sa.getResourceId(com.android.internal.R.styleable.PreferenceHeader_icon, 0);
                header.fragment = sa.getString(com.android.internal.R.styleable.PreferenceHeader_fragment);
                sa.recycle();
                if (curBundle == null) {
                    curBundle = new Bundle();
                }
                final int innerDepth = parser.getDepth();
                while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
                    if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                        continue;
                    }
                    String innerNodeName = parser.getName();
                    if (innerNodeName.equals("extra")) {
                        getResources().parseBundleExtra("extra", attrs, curBundle);
                        XmlUtils.skipCurrentTag(parser);
                    } else if (innerNodeName.equals("intent")) {
                        header.intent = Intent.parseIntent(getResources(), parser, attrs);
                    } else {
                        XmlUtils.skipCurrentTag(parser);
                    }
                }
                if (curBundle.size() > 0) {
                    header.fragmentArguments = curBundle;
                    curBundle = null;
                }
                target.add(header);
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
        }
    } catch (XmlPullParserException e) {
        throw new RuntimeException("Error parsing headers", e);
    } catch (IOException e) {
        throw new RuntimeException("Error parsing headers", e);
    } finally {
        if (parser != null)
            parser.close();
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) Bundle(android.os.Bundle) TypedArray(android.content.res.TypedArray) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) TypedValue(android.util.TypedValue)

Example 15 with XmlPullParserException

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

the class GenericInflater method inflate.

/**
     * Inflate a new hierarchy from the specified XML node. Throws
     * InflaterException if there is an error.
     * <p>
     * <em><strong>Important</strong></em>&nbsp;&nbsp;&nbsp;For performance
     * reasons, inflation relies heavily on pre-processing of XML files
     * that is done at build time. Therefore, it is not currently possible to
     * use inflater with an XmlPullParser over a plain XML file at runtime.
     * 
     * @param parser XML dom node containing the description of the
     *        hierarchy.
     * @param root Optional to be the parent of the generated hierarchy (if
     *        <em>attachToRoot</em> is true), or else simply an object that
     *        provides a set of values for root of the returned
     *        hierarchy (if <em>attachToRoot</em> is false.)
     * @param attachToRoot Whether the inflated hierarchy should be attached to
     *        the root parameter?
     * @return The root of the inflated hierarchy. If root was supplied and
     *         attachToRoot is true, this is root; otherwise it is the root of
     *         the inflated XML file.
     */
public T inflate(XmlPullParser parser, P root, boolean attachToRoot) {
    synchronized (mConstructorArgs) {
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        mConstructorArgs[0] = mContext;
        T result = (T) root;
        try {
            // Look for the root node.
            int type;
            while ((type = parser.next()) != parser.START_TAG && type != parser.END_DOCUMENT) {
                ;
            }
            if (type != parser.START_TAG) {
                throw new InflateException(parser.getPositionDescription() + ": No start tag found!");
            }
            if (DEBUG) {
                System.out.println("**************************");
                System.out.println("Creating root: " + parser.getName());
                System.out.println("**************************");
            }
            // Temp is the root that was found in the xml
            T xmlRoot = createItemFromTag(parser, parser.getName(), attrs);
            result = (T) onMergeRoots(root, attachToRoot, (P) xmlRoot);
            if (DEBUG) {
                System.out.println("-----> start inflating children");
            }
            // Inflate all children under temp
            rInflate(parser, result, attrs);
            if (DEBUG) {
                System.out.println("-----> done inflating children");
            }
        } catch (InflateException e) {
            throw e;
        } catch (XmlPullParserException e) {
            InflateException ex = new InflateException(e.getMessage());
            ex.initCause(e);
            throw ex;
        } catch (IOException e) {
            InflateException ex = new InflateException(parser.getPositionDescription() + ": " + e.getMessage());
            ex.initCause(e);
            throw ex;
        }
        return result;
    }
}
Also used : AttributeSet(android.util.AttributeSet) InflateException(android.view.InflateException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

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