Example usage for android.content.res XmlResourceParser getAttributeValue

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

Introduction

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

Prototype

public String getAttributeValue(String namespace, String name);

Source Link

Document

Returns the value of the specified attribute as a string representation.

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  va2 s.c  om*/
    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 ww w  . j  ava 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:com.dm.material.dashboard.candybar.helpers.IconsHelper.java

@NonNull
public static List<Icon> getIconsList(@NonNull Context context) throws Exception {
    XmlResourceParser parser = context.getResources().getXml(R.xml.drawable);
    int eventType = parser.getEventType();
    String section = "";
    List<Icon> icons = new ArrayList<>();
    List<Icon> sections = new ArrayList<>();

    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {
            if (parser.getName().equals("category")) {
                String title = parser.getAttributeValue(null, "title");
                if (!section.equals(title)) {
                    if (section.length() > 0)
                        sections.add(new Icon(section, icons));
                }//from w  w  w  .j a  v  a 2s  .  c  o m
                section = title;
                icons = new ArrayList<>();
            } else if (parser.getName().equals("item")) {
                String name = parser.getAttributeValue(null, "drawable");
                int id = DrawableHelper.getResourceId(context, name);
                if (id > 0) {
                    icons.add(new Icon(name, id));
                }
            }
        }

        eventType = parser.next();
    }
    sections.add(new Icon(section, icons));
    parser.close();
    return sections;
}

From source file:utils.bobo.com.boboutils.App.appwidget.CustomViewFileProvider.java

/**
 * Parse and return {@link PathStrategy} for given authority as defined in
 * {@link #META_DATA_FILE_PROVIDER_PATHS} {@code &lt;meta-data>}.
 *
 * @see #getPathStrategy(Context, String)
 *///ww w  . ja  v  a2s .com
private static PathStrategy parsePathStrategy(Context context, String authority)
        throws IOException, XmlPullParserException {
    final SimplePathStrategy strat = new SimplePathStrategy(authority);

    final ProviderInfo info = context.getPackageManager().resolveContentProvider(authority,
            PackageManager.GET_META_DATA);
    final XmlResourceParser in = info.loadXmlMetaData(context.getPackageManager(),
            META_DATA_FILE_PROVIDER_PATHS);
    if (in == null) {
        throw new IllegalArgumentException("Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
    }

    int type;
    while ((type = in.next()) != END_DOCUMENT) {
        if (type == START_TAG) {
            final String tag = in.getName();

            final String name = in.getAttributeValue(null, ATTR_NAME);
            String path = in.getAttributeValue(null, ATTR_PATH);

            File target = null;
            if (TAG_ROOT_PATH.equals(tag)) {
                target = buildPath(DEVICE_ROOT, path);
            } else if (TAG_FILES_PATH.equals(tag)) {
                target = buildPath(context.getFilesDir(), path);
            } else if (TAG_CACHE_PATH.equals(tag)) {
                target = buildPath(context.getCacheDir(), path);
            } else if (TAG_EXTERNAL.equals(tag)) {
                target = buildPath(Environment.getExternalStorageDirectory(), path);
            }

            if (target != null) {
                strat.addRoot(name, target);
            }
        }
    }

    return strat;
}

From source file:com.mattprecious.telescope.FileProvider.java

/**
 * Parse and return {@link PathStrategy} for given authority as defined in
 * {@link #META_DATA_FILE_PROVIDER_PATHS} {@code &lt;meta-data>}.
 *
 * @see #getPathStrategy(Context, String)
 *//*from www .  ja  v  a 2  s .  c o m*/
private static PathStrategy parsePathStrategy(Context context, String authority)
        throws IOException, XmlPullParserException {
    final SimplePathStrategy strat = new SimplePathStrategy(authority);

    final ProviderInfo info = context.getPackageManager().resolveContentProvider(authority,
            PackageManager.GET_META_DATA);
    final XmlResourceParser in = info.loadXmlMetaData(context.getPackageManager(),
            META_DATA_FILE_PROVIDER_PATHS);
    if (in == null) {
        throw new IllegalArgumentException("Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
    }

    int type;
    while ((type = in.next()) != END_DOCUMENT) {
        if (type == START_TAG) {
            final String tag = in.getName();

            final String name = in.getAttributeValue(null, ATTR_NAME);
            String path = in.getAttributeValue(null, ATTR_PATH);

            File target = null;
            if (TAG_ROOT_PATH.equals(tag)) {
                target = buildPath(DEVICE_ROOT, path);
            } else if (TAG_FILES_PATH.equals(tag)) {
                target = buildPath(context.getFilesDir(), path);
            } else if (TAG_CACHE_PATH.equals(tag)) {
                target = buildPath(context.getCacheDir(), path);
            } else if (TAG_EXTERNAL.equals(tag)) {
                target = buildPath(Environment.getExternalStorageDirectory(), path);
            } else if (TAG_EXTERNAL_APP.equals(tag)) {
                try {
                    // This sometimes causes an exception on API level 19
                    // Just avoid this specific file provider, so we can try to keep going
                    target = buildPath(context.getExternalFilesDir(null), path);
                } catch (NullPointerException npe) {
                    npe.printStackTrace();
                }
            }

            if (target != null) {
                strat.addRoot(name, target);
            }
        }
    }

    return strat;
}

From source file:com.hippo.content.FileProvider.java

/**
 * Parse and return {@link PathStrategy} for given authority as defined in
 * {@link #META_DATA_FILE_PROVIDER_PATHS} {@code <meta-data>}.
 *
 * @see #getPathStrategy(Context, String)
 *//*w w w .  j a  v  a2s.c o m*/
private static PathStrategy parsePathStrategy(Context context, String authority)
        throws IOException, XmlPullParserException {
    final SimplePathStrategy strat = new SimplePathStrategy(authority);

    final ProviderInfo info = context.getPackageManager().resolveContentProvider(authority,
            PackageManager.GET_META_DATA);
    final XmlResourceParser in = info.loadXmlMetaData(context.getPackageManager(),
            META_DATA_FILE_PROVIDER_PATHS);
    if (in == null) {
        throw new IllegalArgumentException("Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
    }

    int type;
    while ((type = in.next()) != END_DOCUMENT) {
        if (type == START_TAG) {
            final String tag = in.getName();

            final String name = in.getAttributeValue(null, ATTR_NAME);
            String path = in.getAttributeValue(null, ATTR_PATH);

            File target = null;
            if (TAG_ROOT_PATH.equals(tag)) {
                target = DEVICE_ROOT;
            } else if (TAG_FILES_PATH.equals(tag)) {
                target = context.getFilesDir();
            } else if (TAG_CACHE_PATH.equals(tag)) {
                target = context.getCacheDir();
            } else if (TAG_EXTERNAL.equals(tag)) {
                target = Environment.getExternalStorageDirectory();
            } else if (TAG_EXTERNAL_FILES.equals(tag)) {
                File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(context, null);
                if (externalFilesDirs.length > 0) {
                    target = externalFilesDirs[0];
                }
            } else if (TAG_EXTERNAL_CACHE.equals(tag)) {
                File[] externalCacheDirs = ContextCompat.getExternalCacheDirs(context);
                if (externalCacheDirs.length > 0) {
                    target = externalCacheDirs[0];
                }
            }

            if (target != null) {
                strat.addRoot(name, buildPath(target, path));
            }
        }
    }

    return strat;
}

From source file:Main.java

public static String getMetaData(Context app, String name) {
    Bundle bundle = app.getApplicationInfo().metaData;
    if (bundle != null) {
        return bundle.getString(name);
    } else {//from w  w  w. j a  va 2s  .  c  o m
        XmlResourceParser parser = null;
        AssetManager assmgr = null;

        try {
            assmgr = (AssetManager) AssetManager.class.newInstance();
            Method e = AssetManager.class.getDeclaredMethod("addAssetPath", new Class[] { String.class });
            e.setAccessible(true);
            int cookie = ((Integer) e.invoke(assmgr, new Object[] { app.getApplicationInfo().sourceDir }))
                    .intValue();
            if (cookie != 0) {
                String ANDROID_RESOURCES = "http://schemas.android.com/apk/res/android";
                parser = assmgr.openXmlResourceParser(cookie, "AndroidManifest.xml");
                boolean findAppMetadata = false;
                int event = parser.getEventType();
                while (event != 1) {
                    switch (event) {
                    case 2:
                        String nodeName = parser.getName();
                        String metadataName;
                        if ("meta-data".equals(nodeName)) {
                            findAppMetadata = true;
                            metadataName = parser.getAttributeValue(ANDROID_RESOURCES, "name");
                            if (metadataName.equals(name)) {
                                String var12 = parser.getAttributeValue(ANDROID_RESOURCES, "value");
                                return var12;
                            }
                        } else if (findAppMetadata) {
                            metadataName = null;
                            return metadataName;
                        }
                    default:
                        event = parser.next();
                    }
                }
            }
        } catch (Throwable var16) {
            var16.printStackTrace();
        } finally {
            if (parser != null) {
                parser.close();
            }

            if (assmgr != null) {
                assmgr.close();
            }

        }

        return null;
    }
}

From source file:com.entertailion.android.slideshow.utils.Utils.java

/**
 * Load the list of featured photo web sites from embedded XML file.
 * //w w  w  . j  a va2  s. c om
 * @see res/xml/sites.xml
 * 
 * @param context
 * @return
 */
public static ArrayList<SiteInfo> getSites(Context context) {
    ArrayList<SiteInfo> sites = new ArrayList<SiteInfo>();
    XmlResourceParser parser = null;
    try {
        Resources r = context.getResources();
        int resourceId = r.getIdentifier("sites", "xml", "com.entertailion.android.slideshow");
        if (resourceId != 0) {
            parser = context.getResources().getXml(resourceId); // R.xml.sites
            parser.next();
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        String tagName = parser.getName();
                        if (tagName.equalsIgnoreCase("site")) {
                            String name = parser.getAttributeValue(null, "name");
                            String url = parser.getAttributeValue(null, "url");
                            sites.add(new SiteInfo(name, url));
                        }
                        break;
                    }
                }
                eventType = parser.next();
            }
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "getSites", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
    return sites;
}

From source file:com.pomelodesign.cordova.metaio.MetaioPlugin.java

public String GetConfigFilePath(Activity action) {
    if (action == null) {
        return null;
    }//from   w  w  w .j av  a2  s. c  om

    int id = action.getResources().getIdentifier("config", "xml", action.getClass().getPackage().getName());
    if (id == 0) {
        id = action.getResources().getIdentifier("cordova", "xml", action.getPackageName());
        return null;
    }
    if (id == 0) {
        return null;
    }

    XmlResourceParser xml = action.getResources().getXml(id);
    int eventType = -1;
    while (eventType != XmlResourceParser.END_DOCUMENT) {
        if (eventType == XmlResourceParser.START_TAG) {
            String strNode = xml.getName();

            if (strNode.equals("preference")) {
                String name = xml.getAttributeValue(null, "name").toLowerCase(Locale.getDefault());
                if (name.equalsIgnoreCase("arelConfigPath")) {
                    String arelConfigPath = xml.getAttributeValue(null, "value");
                    return arelConfigPath;
                }
            }
        }
        try {
            eventType = xml.next();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    return null;
}

From source file:com.frostwire.android.gui.fragments.SlideMenuFragment.java

private List<MenuItem> parseXml(Context context, int menu) {

    List<MenuItem> list = new ArrayList<MenuItem>();

    try {//  w  w  w . j ava  2  s. c  om
        XmlResourceParser xpp = context.getResources().getXml(menu);

        xpp.next();
        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {

            if (eventType == XmlPullParser.START_TAG) {

                String elemName = xpp.getName();

                if (elemName.equals("item")) {

                    String textId = xpp.getAttributeValue("http://schemas.android.com/apk/res/android",
                            "title");
                    String iconId = xpp.getAttributeValue("http://schemas.android.com/apk/res/android", "icon");
                    String resId = xpp.getAttributeValue("http://schemas.android.com/apk/res/android", "id");

                    MenuItem item = new MenuItem();
                    item.id = Integer.valueOf(resId.replace("@", ""));
                    item.icon = context.getResources().getDrawable(Integer.valueOf(iconId.replace("@", "")));
                    item.label = resourceIdToString(context, textId);

                    list.add(item);
                }
            }

            eventType = xpp.next();
        }
    } catch (Throwable e) {
        LOG.error("Error loading menu items from resource", e);
    }

    return list;
}