Android XmlPullParser Skip requireSkip(final XmlPullParser pp, final String tagName)

Here you can find the source of requireSkip(final XmlPullParser pp, final String tagName)

Description

require Skip

License

Open Source License

Declaration

public static void requireSkip(final XmlPullParser pp,
            final String tagName) throws XmlPullParserException,
            IOException 

Method Source Code

//package com.java2s;
/*/*from  w w w  .  ja  v  a  2  s . c  om*/
 * For license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/).
 * According to www.xmlpull.org, this code is in the public domain.
 */

import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class Main {
    public static void requireSkip(final XmlPullParser pp,
            final String tagName) throws XmlPullParserException,
            IOException {
        require(pp, tagName);

        if (!pp.isEmptyElementTag()) {
            enter(pp, tagName);
            skipExit(pp, tagName);
        } else {
            next(pp);
        }
    }

    public static void require(final XmlPullParser pp, final String tagName)
            throws XmlPullParserException, IOException {
        skipWhitespace(pp);

        pp.require(XmlPullParser.START_TAG, null, tagName);
    }

    public static void enter(final XmlPullParser pp)
            throws XmlPullParserException, IOException {
        skipWhitespace(pp);

        if (pp.getEventType() != XmlPullParser.START_TAG)
            throw new IllegalStateException("expecting start tag to enter");
        if (pp.isEmptyElementTag())
            throw new IllegalStateException("cannot enter empty tag");

        pp.next();
    }

    public static void enter(final XmlPullParser pp, final String tagName)
            throws XmlPullParserException, IOException {
        skipWhitespace(pp);

        pp.require(XmlPullParser.START_TAG, null, tagName);
        enter(pp);
    }

    public static void skipExit(final XmlPullParser pp)
            throws XmlPullParserException, IOException {
        skipToEnd(pp);

        if (pp.getEventType() != XmlPullParser.END_TAG)
            throw new IllegalStateException("expecting end tag to exit");

        pp.next();
    }

    public static void skipExit(final XmlPullParser pp, final String tagName)
            throws XmlPullParserException, IOException {
        skipToEnd(pp);

        pp.require(XmlPullParser.END_TAG, null, tagName);
        pp.next();
    }

    public static void next(final XmlPullParser pp)
            throws XmlPullParserException, IOException {
        skipSubTree(pp);
        pp.next();
    }

    private static void skipWhitespace(final XmlPullParser pp)
            throws XmlPullParserException, IOException {
        if (pp.getEventType() == XmlPullParser.START_DOCUMENT)
            pp.next();
        if (pp.getEventType() == XmlPullParser.TEXT && pp.isWhitespace())
            pp.next();
    }

    private static void skipToEnd(final XmlPullParser pp)
            throws XmlPullParserException, IOException {
        while (pp.getEventType() != XmlPullParser.END_TAG) {
            if (pp.getEventType() == XmlPullParser.START_TAG)
                next(pp);
            else if (pp.getEventType() == XmlPullParser.TEXT)
                pp.next();
            else
                throw new IllegalStateException();
        }
    }

    /**
     * Skip sub tree that is currently porser positioned on. <br>
     * NOTE: parser must be on START_TAG and when funtion returns parser will be positioned on corresponding END_TAG
     */
    public static void skipSubTree(final XmlPullParser pp)
            throws XmlPullParserException, IOException {
        pp.require(XmlPullParser.START_TAG, null, null);

        int level = 1;
        while (level > 0) {
            final int eventType = pp.next();
            if (eventType == XmlPullParser.END_TAG)
                --level;
            else if (eventType == XmlPullParser.START_TAG)
                ++level;
        }
    }
}

Related

  1. skipSubTree(XmlPullParser pp)
  2. skipSubTree(XmlPullParser pp)
  3. skipSubTree(final XmlPullParser pp)
  4. skipTag(XmlPullParser parser)
  5. skipWhitespace(final XmlPullParser pp)