Example usage for android.content.res XmlResourceParser getPositionDescription

List of usage examples for android.content.res XmlResourceParser getPositionDescription

Introduction

In this page you can find the example usage for android.content.res XmlResourceParser getPositionDescription.

Prototype

public String getPositionDescription();

Source Link

Document

Returns a description of the current position of the attribute set.

Usage

From source file:edu.umich.oasis.policy.Filter.java

public static Filter parseFilter(XmlResourceParser parser, Resources resources)
        throws XmlPullParserException, IOException {
    Filter filter;/*from   w  w  w.j  a  va  2s .  c  o  m*/
    parser.require(START_TAG, "", null);
    String tagName = parser.getName();
    int depth = parser.getDepth();

    try {
        String sinkName = parser.getAttributeValue(Utils.OASIS_NAMESPACE, "sink");

        if (sinkName != null) {
            // This rule is scoped to a sink. Try to look it up.
            Sink sink = Sink.forName(sinkName);
            if (sink == null) {
                String msg = String.format("Unknown sink '%s' at %s", sinkName,
                        parser.getPositionDescription());
                throw new PolicyParseException(msg);
            }

            filter = sink.newFilter(parser, resources);
        } else {
            filter = Filter.ALWAYS;
        }
        return filter;
    } finally {
        Utils.skip(parser, depth);
        parser.require(END_TAG, "", tagName);
    }
}

From source file:edu.umich.flowfence.policy.Filter.java

public static Filter parseFilter(XmlResourceParser parser, Resources resources)
        throws XmlPullParserException, IOException {
    Filter filter;//from  w  w  w.java  2  s. c  o  m
    parser.require(START_TAG, "", null);
    String tagName = parser.getName();
    int depth = parser.getDepth();

    try {
        String sinkName = parser.getAttributeValue(Utils.FLOWFENCE_NAMESPACE, "sink");
        if (sinkName != null) {
            // This rule is scoped to a sink. Try to look it up.
            Sink sink = Sink.forName(sinkName);
            if (sink == null) {
                String msg = String.format("Unknown sink '%s' at %s", sinkName,
                        parser.getPositionDescription());
                throw new PolicyParseException(msg);
            }

            filter = sink.newFilter(parser, resources);
        } else {
            filter = Filter.ALWAYS;
        }
        return filter;
    } finally {
        Utils.skip(parser, depth);
        parser.require(END_TAG, "", tagName);
    }
}

From source file:android.support.transition.TransitionInflater.java

/**
 * Loads a {@link Transition} object from a resource
 *
 * @param resource The resource id of the transition to load
 * @return The loaded Transition object/* w w  w  . j  a  v  a 2 s.  c  o m*/
 * @throws android.content.res.Resources.NotFoundException when the
 *                                                         transition cannot be loaded
 */
public Transition inflateTransition(int resource) {
    XmlResourceParser parser = mContext.getResources().getXml(resource);
    try {
        return createTransitionFromXml(parser, Xml.asAttributeSet(parser), null);
    } catch (XmlPullParserException e) {
        throw new InflateException(e.getMessage(), e);
    } catch (IOException e) {
        throw new InflateException(parser.getPositionDescription() + ": " + e.getMessage(), e);
    } finally {
        parser.close();
    }
}

From source file:com.givewaygames.transition.TransitionInflater.java

/**
 * Loads a {@link Transition} object from a resource
 *
 * @param resource The resource id of the transition to load
 * @return The loaded Transition object//  w w  w  . j  ava  2  s  . c om
 * @throws android.content.res.Resources.NotFoundException when the
 * transition cannot be loaded
 */
public Transition inflateTransition(int resource) {
    XmlResourceParser parser = mContext.getResources().getXml(resource);
    try {
        return createTransitionFromXml(parser, Xml.asAttributeSet(parser), null);
    } 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;
    } finally {
        parser.close();
    }
}

From source file:android.support.transition.TransitionInflater.java

/**
 * Loads a {@link TransitionManager} object from a resource
 *
 * @param resource The resource id of the transition manager to load
 * @return The loaded TransitionManager object
 * @throws android.content.res.Resources.NotFoundException when the
 *                                                         transition manager cannot be loaded
 *///from ww  w. ja va2  s  .c  o m
public TransitionManager inflateTransitionManager(int resource, ViewGroup sceneRoot) {
    XmlResourceParser parser = mContext.getResources().getXml(resource);
    try {
        return createTransitionManagerFromXml(parser, Xml.asAttributeSet(parser), sceneRoot);
    } 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;
    } finally {
        parser.close();
    }
}

From source file:com.achep.base.ui.activities.SettingsActivity.java

/**
 * Parse the given XML file as a categories description, adding each
 * parsed categories and tiles into the target list.
 *
 * @param resourceId The XML resource to load and parse.
 * @param target     The list in which the parsed categories and tiles should be placed.
 *///  w  w w.  j a v  a  2  s  .c om
protected final void loadDashboardFromResource(@XmlRes int resourceId,
        @NonNull List<DashboardCategory> target) {
    XmlResourceParser parser = null;
    try {
        parser = getResources().getXml(resourceId);
        AttributeSet attrs = Xml.asAttributeSet(parser);

        int type;

        for (type = parser.next(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) {
            if (type == XmlPullParser.START_TAG)
                break;
        }

        String nodeName = parser.getName();
        if (!RESOURCE_TAG_DASHBOARD.equals(nodeName))
            throw new RuntimeException(String.format("XML document must start with <%s> tag; found %s at %s",
                    RESOURCE_TAG_DASHBOARD, nodeName, parser.getPositionDescription()));

        for (type = parser.next(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) {
            if (type == XmlPullParser.END_TAG && parser.getDepth() <= 1 /* root tag */)
                break;
            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                continue;
            }

            switch (parser.getName()) {
            case RESOURCE_TAG_DASHBOARD_CATEGORY:
                DashboardCategory category = new DashboardCategory(this, attrs);

                final int categoryDepth = parser.getDepth();
                for (type = parser.next(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) {
                    if (type == XmlPullParser.END_TAG && parser.getDepth() <= categoryDepth)
                        break;
                    if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                        continue;
                    }

                    switch (parser.getName()) {
                    case RESOURCE_TAG_DASHBOARD_TILE:
                        DashboardTile tile = new DashboardTile(this, attrs);
                        Bundle bundle = null;

                        final int tileDepth = parser.getDepth();
                        for (type = parser.next(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) {
                            if (type == XmlPullParser.END_TAG && parser.getDepth() <= tileDepth)
                                break;
                            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                                continue;
                            }

                            switch (parser.getName()) {
                            case RESOURCE_TAG_DASHBOARD_TILE_EXTRA:
                                if (bundle == null) {
                                    bundle = new Bundle();
                                }

                                getResources().parseBundleExtra(RESOURCE_TAG_DASHBOARD_TILE_EXTRA, attrs,
                                        bundle);
                                XmlUtils.skipCurrentTag(parser);
                                break;
                            case RESOURCE_TAG_DASHBOARD_TILE_INTENT:
                                tile.intent = Intent.parseIntent(getResources(), parser, attrs);
                                break;
                            default:
                                XmlUtils.skipCurrentTag(parser);
                            }
                        }

                        tile.fragmentArguments = bundle;
                        category.add(tile);
                        break;
                    default:
                        XmlUtils.skipCurrentTag(parser);
                    }
                }

                target.add(category);
                break;
            default:
                XmlUtils.skipCurrentTag(parser);
            }
        }
    } catch (XmlPullParserException | IOException e) {
        throw new RuntimeException("Error parsing categories", e);
    } finally {
        if (parser != null)
            parser.close();
    }
}

From source file:com.androzic.Preferences.java

/**
 * Parse the given XML file as a header description, adding each
 * parsed Header into the target list./*  w ww  .ja v  a2  s  .  com*/
 *
 * @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) {
    Androzic application = Androzic.getApplication();
    XmlResourceParser parser = null;
    try {
        Resources resources = getResources();
        parser = resources.getXml(resid);
        AttributeSet attrs = Xml.asAttributeSet(parser);

        int type;
        //noinspection StatementWithEmptyBody
        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 = resources.obtainAttributes(attrs, R.styleable.PreferenceHeader);
                header.id = sa.getResourceId(R.styleable.PreferenceHeader_id, (int) HEADER_ID_UNDEFINED);
                TypedValue tv = sa.peekValue(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(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(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(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(R.styleable.PreferenceHeader_icon, 0);
                header.fragment = sa.getString(R.styleable.PreferenceHeader_fragment);
                header.help = sa.getResourceId(R.styleable.PreferenceHeader_help, 0);
                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")) {
                        resources.parseBundleExtra(innerNodeName, attrs, curBundle);
                        XmlUtils.skipCurrentTag(parser);

                    } else if (innerNodeName.equals("intent")) {
                        header.intent = Intent.parseIntent(resources, parser, attrs);

                    } else {
                        XmlUtils.skipCurrentTag(parser);
                    }
                }

                if (curBundle.size() > 0) {
                    header.fragmentArguments = curBundle;
                    curBundle = null;
                }

                if (header.id == R.id.pref_plugins && application.getPluginsPreferences().size() == 0)
                    continue;

                target.add(header);
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
        }

    } catch (XmlPullParserException | IOException e) {
        throw new RuntimeException("Error parsing headers", e);
    } finally {
        if (parser != null)
            parser.close();
    }

}

From source file:android.content.pm.PackageParser.java

private void parseSplitApk(Package pkg, int splitIndex, AssetManager assets, int flags)
        throws PackageParserException {
    final String apkPath = pkg.splitCodePaths[splitIndex];
    final File apkFile = new File(apkPath);

    mParseError = PackageManager.INSTALL_SUCCEEDED;
    mArchiveSourcePath = apkPath;/*from w  ww. ja v  a2 s.  c om*/

    if (DEBUG_JAR)
        Slog.d(TAG, "Scanning split APK: " + apkPath);

    final int cookie = loadApkIntoAssetManager(assets, apkPath, flags);

    Resources res = null;
    XmlResourceParser parser = null;
    try {
        res = new Resources(assets, mMetrics, null);
        assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                Build.VERSION.RESOURCES_SDK_INT);
        parser = assets.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);

        final String[] outError = new String[1];
        pkg = parseSplitApk(pkg, res, parser, flags, splitIndex, outError);
        if (pkg == null) {
            throw new PackageParserException(mParseError,
                    apkPath + " (at " + parser.getPositionDescription() + "): " + outError[0]);
        }

    } catch (PackageParserException e) {
        throw e;
    } catch (Exception e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,
                "Failed to read manifest from " + apkPath, e);
    } finally {
        IoUtils.closeQuietly(parser);
    }
}

From source file:android.content.pm.PackageParser.java

private Package parseBaseApk(File apkFile, AssetManager assets, int flags) throws PackageParserException {
    final String apkPath = apkFile.getAbsolutePath();

    String volumeUuid = null;//from   w w  w  .  j  ava  2 s  .  c o  m
    if (apkPath.startsWith(MNT_EXPAND)) {
        final int end = apkPath.indexOf('/', MNT_EXPAND.length());
        volumeUuid = apkPath.substring(MNT_EXPAND.length(), end);
    }

    mParseError = PackageManager.INSTALL_SUCCEEDED;
    mArchiveSourcePath = apkFile.getAbsolutePath();

    if (DEBUG_JAR)
        Slog.d(TAG, "Scanning base APK: " + apkPath);

    final int cookie = loadApkIntoAssetManager(assets, apkPath, flags);

    Resources res = null;
    XmlResourceParser parser = null;
    try {
        res = new Resources(assets, mMetrics, null);
        assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                Build.VERSION.RESOURCES_SDK_INT);
        parser = assets.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);

        final String[] outError = new String[1];
        final Package pkg = parseBaseApk(res, parser, flags, outError);
        if (pkg == null) {
            throw new PackageParserException(mParseError,
                    apkPath + " (at " + parser.getPositionDescription() + "): " + outError[0]);
        }

        pkg.volumeUuid = volumeUuid;
        pkg.applicationInfo.volumeUuid = volumeUuid;
        pkg.baseCodePath = apkPath;
        pkg.mSignatures = null;

        return pkg;

    } catch (PackageParserException e) {
        throw e;
    } catch (Exception e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,
                "Failed to read manifest from " + apkPath, e);
    } finally {
        IoUtils.closeQuietly(parser);
    }
}

From source file:android.content.pm.PackageParser.java

private boolean parseUsesPermission(Package pkg, Resources res, XmlResourceParser parser, AttributeSet attrs)
        throws XmlPullParserException, IOException {
    TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifestUsesPermission);

    // Note: don't allow this value to be a reference to a resource
    // that may change.
    String name = sa.getNonResourceString(com.android.internal.R.styleable.AndroidManifestUsesPermission_name);

    int maxSdkVersion = 0;
    TypedValue val = sa.peekValue(com.android.internal.R.styleable.AndroidManifestUsesPermission_maxSdkVersion);
    if (val != null) {
        if (val.type >= TypedValue.TYPE_FIRST_INT && val.type <= TypedValue.TYPE_LAST_INT) {
            maxSdkVersion = val.data;
        }/*from  ww w.j  a  v a  2s .  c o  m*/
    }

    sa.recycle();

    if ((maxSdkVersion == 0) || (maxSdkVersion >= Build.VERSION.RESOURCES_SDK_INT)) {
        if (name != null) {
            int index = pkg.requestedPermissions.indexOf(name);
            if (index == -1) {
                pkg.requestedPermissions.add(name.intern());
            } else {
                Slog.w(TAG, "Ignoring duplicate uses-permissions/uses-permissions-sdk-m: " + name
                        + " in package: " + pkg.packageName + " at: " + parser.getPositionDescription());
            }
        }
    }

    XmlUtils.skipCurrentTag(parser);
    return true;
}