Java Code Examples for org.xmlpull.v1.XmlPullParser#getDepth()

The following examples show how to use org.xmlpull.v1.XmlPullParser#getDepth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WatchlistSettings.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void reloadSettings() {
    if (!mXmlFile.exists()) {
        // No settings config
        return;
    }
    try (FileInputStream stream = mXmlFile.openRead()){
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(stream, StandardCharsets.UTF_8.name());
        XmlUtils.beginDocument(parser, "network-watchlist-settings");
        final int outerDepth = parser.getDepth();
        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
            if (parser.getName().equals("secret-key")) {
                mPrivacySecretKey = parseSecretKey(parser);
            }
        }
        Slog.i(TAG, "Reload watchlist settings done");
    } catch (IllegalStateException | NullPointerException | NumberFormatException |
            XmlPullParserException | IOException | IndexOutOfBoundsException e) {
        Slog.e(TAG, "Failed parsing xml", e);
    }
}
 
Example 2
Source File: AppOpsService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
void readPackage(XmlPullParser parser) throws NumberFormatException,
        XmlPullParserException, IOException {
    String pkgName = parser.getAttributeValue(null, "n");
    int outerDepth = parser.getDepth();
    int type;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
            continue;
        }

        String tagName = parser.getName();
        if (tagName.equals("uid")) {
            readUid(parser, pkgName);
        } else {
            Slog.w(TAG, "Unknown element under <pkg>: "
                    + parser.getName());
            XmlUtils.skipCurrentTag(parser);
        }
    }
}
 
Example 3
Source File: ComponentDescription.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 *
 */
public static ComponentDescription parseComponent(Bundle b, XmlPullParser p)
  throws IOException, XmlPullParserException
{
  while (findNextStartTag(p, true)) {
    Activator.logDebug("Check for component description: " +
                       p.getPositionDescription());
    if (p.getEventType() == XmlPullParser.START_TAG &&
        "component".equals(p.getName()) &&
        (p.getDepth() == 1 ||
         SCR_NAMESPACE_V1_3_0_URI.equals(p.getNamespace()) ||
         SCR_NAMESPACE_V1_2_0_URI.equals(p.getNamespace()) ||
         SCR_NAMESPACE_V1_1_0_URI.equals(p.getNamespace()) ||
         SCR_NAMESPACE_V1_0_0_URI.equals(p.getNamespace()))) {
      return new ComponentDescription(b, p);
    }
    p.next();
  }
  return null;
}
 
Example 4
Source File: XmlUtils.java    From WeexOne with MIT License 5 votes vote down vote up
public static boolean nextElementWithin(XmlPullParser parser, int outerDepth)
    throws IOException, XmlPullParserException {
    for (;;) {
        int type = parser.next();
        if (type == XmlPullParser.END_DOCUMENT
            || (type == XmlPullParser.END_TAG && parser.getDepth() == outerDepth)) {
            return false;
        }
        if (type == XmlPullParser.START_TAG
            && parser.getDepth() == outerDepth + 1) {
            return true;
        }
    }
}
 
Example 5
Source File: XmlUtils.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
public static void skipCurrentTag(XmlPullParser parser)
        throws XmlPullParserException, IOException {
    int outerDepth = parser.getDepth();
    int type;
    //noinspection StatementWithEmptyBody
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG
            || parser.getDepth() > outerDepth)) {
    }
}
 
Example 6
Source File: PersistentDataStore.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void loadBlockedRatingsFromXml(XmlPullParser parser)
        throws IOException, XmlPullParserException {
    final int outerDepth = parser.getDepth();
    while (XmlUtils.nextElementWithin(parser, outerDepth)) {
        if (parser.getName().equals(TAG_RATING)) {
            String ratingString = parser.getAttributeValue(null, ATTR_STRING);
            if (TextUtils.isEmpty(ratingString)) {
                throw new XmlPullParserException(
                        "Missing " + ATTR_STRING + " attribute on " + TAG_RATING);
            }
            mBlockedRatings.add(TvContentRating.unflattenFromString(ratingString));
        }
    }
}
 
Example 7
Source File: CosuConfig.java    From android-testdpc with Apache License 2.0 5 votes vote down vote up
/**
 * Continue to the end of the current xml tag
 */
private 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)) {
    }
}
 
Example 8
Source File: XMLResolver.java    From Utils with Apache License 2.0 5 votes vote down vote up
private XMLNode initNode(XmlPullParser parser) {
    XMLNode node = new XMLNode();
    node.mId = node.hashCode();
    node.mName = parser.getName();
    node.mLevel = parser.getDepth();
    for (int i = 0; i < parser.getAttributeCount(); i++) {
        node.addAttribute(parser.getAttributeName(i), parser.getAttributeValue(i));
    }
    return node;
}
 
Example 9
Source File: XmlUtils.java    From HtmlCompat with Apache License 2.0 5 votes vote down vote up
public static boolean nextElementWithin(XmlPullParser parser, int outerDepth)
        throws IOException, XmlPullParserException {
    for (;;) {
        int type = parser.next();
        if (type == XmlPullParser.END_DOCUMENT
                || (type == XmlPullParser.END_TAG && parser.getDepth() == outerDepth)) {
            return false;
        }
        if (type == XmlPullParser.START_TAG
                && parser.getDepth() == outerDepth + 1) {
            return true;
        }
    }
}
 
Example 10
Source File: XmlUtils.java    From ticdesign with Apache License 2.0 5 votes vote down vote up
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)) {
    }
}
 
Example 11
Source File: XmlUtils.java    From android-job with Apache License 2.0 5 votes vote down vote up
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)) {
    }
}
 
Example 12
Source File: XmlUtils.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
public static boolean nextElementWithin(XmlPullParser parser, int outerDepth)
        throws IOException, XmlPullParserException {
    for (; ; ) {
        int type = parser.next();
        if (type == XmlPullParser.END_DOCUMENT
                || (type == XmlPullParser.END_TAG && parser.getDepth() == outerDepth)) {
            return false;
        }
        if (type == XmlPullParser.START_TAG
                && parser.getDepth() == outerDepth + 1) {
            return true;
        }
    }
}
 
Example 13
Source File: IntentFilterVerificationInfo.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/** @hide */
public void readFromXml(XmlPullParser parser) throws XmlPullParserException,
        IOException {
    mPackageName = getStringFromXml(parser, ATTR_PACKAGE_NAME, null);
    if (mPackageName == null) {
        Log.e(TAG, "Package name cannot be null!");
    }
    int status = getIntFromXml(parser, ATTR_STATUS, -1);
    if (status == -1) {
        Log.e(TAG, "Unknown status value: " + status);
    }
    mMainStatus = status;

    int outerDepth = parser.getDepth();
    int type;
    while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG
            || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG
                || type == XmlPullParser.TEXT) {
            continue;
        }

        String tagName = parser.getName();
        if (tagName.equals(TAG_DOMAIN)) {
            String name = getStringFromXml(parser, ATTR_DOMAIN_NAME, null);
            if (!TextUtils.isEmpty(name)) {
                mDomains.add(name);
            }
        } else {
            Log.w(TAG, "Unknown tag parsing IntentFilter: " + tagName);
        }
        XmlUtils.skipCurrentTag(parser);
    }
}
 
Example 14
Source File: KeySetManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
void readKeysLPw(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)) {
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
            continue;
        }
        final String tagName = parser.getName();
        if (tagName.equals("public-key")) {
            readPublicKeyLPw(parser);
        }
    }
}
 
Example 15
Source File: XmlUtils.java    From HtmlCompat with Apache License 2.0 5 votes vote down vote up
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)) {
    }
}
 
Example 16
Source File: TransitionInflater.java    From Transitions-Everywhere with Apache License 2.0 4 votes vote down vote up
@Nullable
private Transition createTransitionFromXml(@NonNull XmlPullParser parser,
                                           @NonNull AttributeSet attrs,
                                           @Nullable Transition parent)
        throws XmlPullParserException, IOException {

    Transition transition = null;

    // Make sure we are on a start tag.
    int type;
    int depth = parser.getDepth();

    TransitionSet transitionSet = (parent instanceof TransitionSet)
            ? (TransitionSet) parent : null;

    while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
            && type != XmlPullParser.END_DOCUMENT) {

        if (type != XmlPullParser.START_TAG) {
            continue;
        }

        String  name = parser.getName();
        if ("fade".equals(name)) {
            transition = new Fade(mContext, attrs);
        } else if ("changeBounds".equals(name)) {
            transition = new ChangeBounds(mContext, attrs);
        } else if ("slide".equals(name)) {
            transition = new Slide(mContext, attrs);
        } else if ("explode".equals(name)) {
            transition = new Explode(mContext, attrs);
        } else if ("changeImageTransform".equals(name)) {
            transition = new ChangeImageTransform(mContext, attrs);
        } else if ("changeTransform".equals(name)) {
            transition = new ChangeTransform(mContext, attrs);
        } else if ("changeClipBounds".equals(name)) {
            transition = new ChangeClipBounds(mContext, attrs);
        } else if ("autoTransition".equals(name)) {
            transition = new AutoTransition(mContext, attrs);
        } else if ("recolor".equals(name)) {
            transition = new Recolor(mContext, attrs);
        } else if ("changeScroll".equals(name)) {
            transition = new ChangeScroll(mContext, attrs);
        } else if ("transitionSet".equals(name)) {
            transition = new TransitionSet(mContext, attrs);
        } else if ("scale".equals(name)) {
            transition = new Scale(mContext, attrs);
        } else if ("translation".equals(name)) {
            transition = new TranslationTransition(mContext, attrs);
        } else if ("transition".equals(name)) {
            transition = (Transition) createCustom(attrs, Transition.class, "transition");
        } else if ("targets".equals(name) && parent != null) {
            getTargetIds(parser, attrs, parent);
        } else if ("arcMotion".equals(name) && parent != null) {
            parent.setPathMotion(new ArcMotion(mContext, attrs));
        } else if ("pathMotion".equals(name) && parent != null) {
            parent.setPathMotion((PathMotion)createCustom(attrs, PathMotion.class, "pathMotion"));
        } else if ("patternPathMotion".equals(name) && parent != null) {
            parent.setPathMotion(new PatternPathMotion(mContext, attrs));
        } else {
            throw new RuntimeException("Unknown scene name: " + parser.getName());
        }
        if (transition != null) {
            if (!parser.isEmptyElementTag()) {
                createTransitionFromXml(parser, attrs, transition);
            }
            if (transitionSet != null) {
                transitionSet.addTransition(transition);
                transition = null;
            } else if (parent != null) {
                throw new InflateException("Could not add transition to another transition.");
            }
        }
    }

    return transition;
}
 
Example 17
Source File: LayerDrawableInflateImpl.java    From timecat with Apache License 2.0 4 votes vote down vote up
@Override
public Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    final int innerDepth = parser.getDepth() + 1;
    int type;
    int depth;
    int layerAttrUseCount = 0;
    int drawableUseCount = 0;
    int space = STEP << 1;
    //L,T,R,B,S,E,id
    int[][] childLayersAttrs = new int[space][ATTRS.length];
    Drawable[] drawables = new Drawable[space];

    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;
        }

        if (layerAttrUseCount >= childLayersAttrs.length) {
            int[][] dstInt = new int[drawables.length + STEP][ATTRS.length];
            System.arraycopy(childLayersAttrs, 0, dstInt, 0, childLayersAttrs.length);
            childLayersAttrs = dstInt;
        }
        updateLayerAttrs(context, attrs, childLayersAttrs[layerAttrUseCount]);
        layerAttrUseCount++;

        Drawable drawable = DrawableUtils.getAttrDrawable(context, attrs, android.R.attr.drawable);

        // If the layer doesn't have a drawable or unresolved theme
        // attribute for a drawable, attempt to parse one from the child
        // element.
        if (drawable == null) {
            while ((type = parser.next()) == XmlPullParser.TEXT) {
            }
            if (type != XmlPullParser.START_TAG) {
                throw new XmlPullParserException(parser.getPositionDescription() + ": <item> tag requires a 'drawable' attribute or " + "child tag defining a drawable");
            }
            drawable = DrawableUtils.createFromXmlInner(context, parser, attrs);
        } else {
            final ColorStateList cls = DrawableUtils.getTintColorList(context, attrs, R.attr.drawableTint);
            if (cls != null) {
                drawable = ThemeUtils.tintDrawable(drawable, cls, DrawableUtils.getTintMode(context, attrs, R.attr.drawableTintMode));
            }
        }

        if (drawable != null) {
            if (drawableUseCount >= drawables.length) {
                Drawable[] dst = new Drawable[drawables.length + STEP];
                System.arraycopy(drawables, 0, dst, 0, drawables.length);
                drawables = dst;
            }
            drawables[drawableUseCount] = drawable;
            drawableUseCount++;
        }
    }

    if (drawables[0] == null || drawableUseCount != layerAttrUseCount) {
        return null;
    } else {
        LayerDrawable layerDrawable = new LayerDrawable(drawables);
        for (int i = 0; i < drawables.length; i++) {
            int[] childLayersAttr = childLayersAttrs[i];
            if (childLayersAttr[0] != 0 || childLayersAttr[1] != 0 || childLayersAttr[2] != 0 || childLayersAttr[3] != 0) {
                layerDrawable.setLayerInset(i, childLayersAttr[0], childLayersAttr[1], childLayersAttr[2], childLayersAttr[3]);
            }
            if (childLayersAttr[4] != 0) {
                layerDrawable.setId(i, childLayersAttr[4]);
            }
        }
        return layerDrawable;
    }
}
 
Example 18
Source File: GenericInflater.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Recursive method used to descend down the xml hierarchy and instantiate
 * items, instantiate their children, and then call onFinishInflate().
 */
private void rInflate(XmlPullParser parser, T parent, final AttributeSet attrs)
        throws XmlPullParserException, IOException {
    final int depth = parser.getDepth();

    int type;
    while (((type = parser.next()) != parser.END_TAG || 
            parser.getDepth() > depth) && type != parser.END_DOCUMENT) {

        if (type != parser.START_TAG) {
            continue;
        }

        if (onCreateCustomFromTag(parser, parent, attrs)) {
            continue;
        }

        if (DEBUG) {
            System.out.println("Now inflating tag: " + parser.getName());
        }
        String name = parser.getName();

        T item = createItemFromTag(parser, name, attrs);

        if (DEBUG) {
            System.out
                    .println("Creating params from parent: " + parent);
        }

        ((P) parent).addItemFromInflater(item);

        if (DEBUG) {
            System.out.println("-----> start inflating children");
        }
        rInflate(parser, item, attrs);
        if (DEBUG) {
            System.out.println("-----> done inflating children");
        }
    }

}
 
Example 19
Source File: ShortcutLauncher.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Load.
 */
public static ShortcutLauncher loadFromXml(XmlPullParser parser, ShortcutUser shortcutUser,
        int ownerUserId, boolean fromBackup) throws IOException, XmlPullParserException {
    final String launcherPackageName = ShortcutService.parseStringAttribute(parser,
            ATTR_PACKAGE_NAME);

    // If restoring, just use the real user ID.
    final int launcherUserId =
            fromBackup ? ownerUserId
            : ShortcutService.parseIntAttribute(parser, ATTR_LAUNCHER_USER_ID, ownerUserId);

    final ShortcutLauncher ret = new ShortcutLauncher(shortcutUser, ownerUserId,
            launcherPackageName, launcherUserId);

    ArraySet<String> ids = null;
    final int outerDepth = parser.getDepth();
    int type;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        final int depth = parser.getDepth();
        final String tag = parser.getName();
        if (depth == outerDepth + 1) {
            switch (tag) {
                case ShortcutPackageInfo.TAG_ROOT:
                    ret.getPackageInfo().loadFromXml(parser, fromBackup);
                    continue;
                case TAG_PACKAGE: {
                    final String packageName = ShortcutService.parseStringAttribute(parser,
                            ATTR_PACKAGE_NAME);
                    final int packageUserId = fromBackup ? ownerUserId
                            : ShortcutService.parseIntAttribute(parser,
                            ATTR_PACKAGE_USER_ID, ownerUserId);
                    ids = new ArraySet<>();
                    ret.mPinnedShortcuts.put(
                            PackageWithUser.of(packageUserId, packageName), ids);
                    continue;
                }
            }
        }
        if (depth == outerDepth + 2) {
            switch (tag) {
                case TAG_PIN: {
                    if (ids == null) {
                        Slog.w(TAG, TAG_PIN + " in invalid place");
                    } else {
                        ids.add(ShortcutService.parseStringAttribute(parser, ATTR_VALUE));
                    }
                    continue;
                }
            }
        }
        ShortcutService.warnForInvalidTag(depth, tag);
    }
    return ret;
}
 
Example 20
Source File: FrameDrawable.java    From ProjectX with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void inflateFrames(Resources resources, XmlPullParser parser, AttributeSet attrs,
                           Resources.Theme theme) throws XmlPullParserException, IOException {
    final int innerDepth = parser.getDepth() + 1;
    int type;
    int depth;
    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;
        final TypedArray custom = DrawableHelper.obtainAttributes(resources, theme, attrs,
                R.styleable.FrameDrawableItem);
        final int width = custom.getLayoutDimension(
                R.styleable.FrameDrawableItem_android_layout_width, "layout_width");
        final int height = custom.getLayoutDimension(
                R.styleable.FrameDrawableItem_android_layout_height, "layout_height");
        int marginStart = 0;
        int marginTop = 0;
        int marginEnd = 0;
        int marginBottom = 0;
        if (custom.hasValue(R.styleable.FrameDrawableItem_android_layout_margin)) {
            final int margin = custom.getDimensionPixelSize(
                    R.styleable.FrameDrawableItem_android_layout_margin, 0);
            marginStart = marginTop = marginEnd = marginBottom = margin;
        }
        if (custom.hasValue(R.styleable.FrameDrawableItem_android_layout_marginStart))
            marginStart = custom.getDimensionPixelSize(
                    R.styleable.FrameDrawableItem_android_layout_marginStart, marginStart);
        if (custom.hasValue(R.styleable.FrameDrawableItem_android_layout_marginTop))
            marginTop = custom.getDimensionPixelSize(
                    R.styleable.FrameDrawableItem_android_layout_marginTop, marginTop);
        if (custom.hasValue(R.styleable.FrameDrawableItem_android_layout_marginEnd))
            marginEnd = custom.getDimensionPixelSize(
                    R.styleable.FrameDrawableItem_android_layout_marginEnd, marginEnd);
        if (custom.hasValue(R.styleable.FrameDrawableItem_android_layout_marginBottom))
            marginBottom = custom.getDimensionPixelSize(
                    R.styleable.FrameDrawableItem_android_layout_marginBottom, marginBottom);
        final int gravity = custom.getInteger(R.styleable.FrameDrawableItem_android_gravity,
                Gravity.NO_GRAVITY);
        final int id = custom.getResourceId(R.styleable.FrameDrawableItem_android_id,
                View.NO_ID);
        final Drawable dr = custom.getDrawable(R.styleable.FrameDrawableItem_android_drawable);
        if (dr != null) {
            dr.setCallback(this);
            mItems.add(new ChildDrawable(dr, width, height, gravity, id,
                    marginStart, marginTop, marginEnd, marginBottom));
        }
        custom.recycle();
        if (dr == null) {
            //noinspection StatementWithEmptyBody
            while ((type = parser.next()) == XmlPullParser.TEXT) {
            }
            if (type != XmlPullParser.START_TAG)
                throw new XmlPullParserException(parser.getPositionDescription()
                        + ": <item> tag requires a 'drawable' attribute or "
                        + "child tag defining a drawable");
            final Drawable item = Drawable.createFromXmlInner(resources, parser, attrs, theme);
            item.setCallback(this);
            mItems.add(new ChildDrawable(item, width, height, gravity, id,
                    marginStart, marginTop, marginEnd, marginBottom));
        }
    }
}