Search in sources :

Example 6 with XmlPullParser

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

the class SearchableInfo method getActivityMetaData.

/**
     * Get the metadata for a given activity
     * 
     * @param context runtime context
     * @param xml XML parser for reading attributes
     * @param cName The component name of the searchable activity
     * 
     * @result A completely constructed SearchableInfo, or null if insufficient XML data for it
     */
private static SearchableInfo getActivityMetaData(Context context, XmlPullParser xml, final ComponentName cName) {
    SearchableInfo result = null;
    Context activityContext = createActivityContext(context, cName);
    if (activityContext == null)
        return null;
    // forward through the file until it's reading the tag of interest.
    try {
        int tagType = xml.next();
        while (tagType != XmlPullParser.END_DOCUMENT) {
            if (tagType == XmlPullParser.START_TAG) {
                if (xml.getName().equals(MD_XML_ELEMENT_SEARCHABLE)) {
                    AttributeSet attr = Xml.asAttributeSet(xml);
                    if (attr != null) {
                        try {
                            result = new SearchableInfo(activityContext, attr, cName);
                        } catch (IllegalArgumentException ex) {
                            Log.w(LOG_TAG, "Invalid searchable metadata for " + cName.flattenToShortString() + ": " + ex.getMessage());
                            return null;
                        }
                    }
                } else if (xml.getName().equals(MD_XML_ELEMENT_SEARCHABLE_ACTION_KEY)) {
                    if (result == null) {
                        // Can't process an embedded element if we haven't seen the enclosing
                        return null;
                    }
                    AttributeSet attr = Xml.asAttributeSet(xml);
                    if (attr != null) {
                        try {
                            result.addActionKey(new ActionKeyInfo(activityContext, attr));
                        } catch (IllegalArgumentException ex) {
                            Log.w(LOG_TAG, "Invalid action key for " + cName.flattenToShortString() + ": " + ex.getMessage());
                            return null;
                        }
                    }
                }
            }
            tagType = xml.next();
        }
    } catch (XmlPullParserException e) {
        Log.w(LOG_TAG, "Reading searchable metadata for " + cName.flattenToShortString(), e);
        return null;
    } catch (IOException e) {
        Log.w(LOG_TAG, "Reading searchable metadata for " + cName.flattenToShortString(), e);
        return null;
    }
    return result;
}
Also used : Context(android.content.Context) AttributeSet(android.util.AttributeSet) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 7 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser 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 8 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser 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 9 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser 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)

Example 10 with XmlPullParser

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

the class PreferenceInflater method onCreateCustomFromTag.

@Override
protected boolean onCreateCustomFromTag(XmlPullParser parser, Preference parentPreference, AttributeSet attrs) throws XmlPullParserException {
    final String tag = parser.getName();
    if (tag.equals(INTENT_TAG_NAME)) {
        Intent intent = null;
        try {
            intent = Intent.parseIntent(getContext().getResources(), parser, attrs);
        } catch (IOException e) {
            XmlPullParserException ex = new XmlPullParserException("Error parsing preference");
            ex.initCause(e);
            throw ex;
        }
        if (intent != null) {
            parentPreference.setIntent(intent);
        }
        return true;
    } else if (tag.equals(EXTRA_TAG_NAME)) {
        getContext().getResources().parseBundleExtra(EXTRA_TAG_NAME, attrs, parentPreference.getExtras());
        try {
            XmlUtils.skipCurrentTag(parser);
        } catch (IOException e) {
            XmlPullParserException ex = new XmlPullParserException("Error parsing preference");
            ex.initCause(e);
            throw ex;
        }
        return true;
    }
    return false;
}
Also used : Intent(android.content.Intent) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Aggregations

XmlPullParser (org.xmlpull.v1.XmlPullParser)665 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)614 IOException (java.io.IOException)376 FileNotFoundException (java.io.FileNotFoundException)185 FileInputStream (java.io.FileInputStream)184 File (java.io.File)107 ArrayList (java.util.ArrayList)75 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)42 BridgeXmlBlockParser (com.android.layoutlib.bridge.android.BridgeXmlBlockParser)39 FileReader (java.io.FileReader)36 BufferedInputStream (java.io.BufferedInputStream)30 XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)29 RemoteException (android.os.RemoteException)28 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)28