Android XmlPullParser Check requireAttr(final XmlPullParser pp, final String attrName, final String requiredValue)

Here you can find the source of requireAttr(final XmlPullParser pp, final String attrName, final String requiredValue)

Description

require Attr

License

Open Source License

Declaration

public static void requireAttr(final XmlPullParser pp,
            final String attrName, final String requiredValue) 

Method Source Code

//package com.java2s;
/*/*from   w ww.  ja v  a 2  s . c o  m*/
 * 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 org.xmlpull.v1.XmlPullParser;

public class Main {
    public static void requireAttr(final XmlPullParser pp,
            final String attrName, final String requiredValue) {
        if (!requiredValue.equals(attr(pp, attrName)))
            throw new IllegalStateException("cannot find " + attrName
                    + "=\"" + requiredValue + "\" />");
    }

    public static String attr(final XmlPullParser pp, final String attrName) {
        final String attr = optAttr(pp, attrName, null);

        if (attr != null)
            return attr;
        else
            throw new IllegalStateException("expecting attribute: "
                    + attrName);
    }

    public static String optAttr(final XmlPullParser pp,
            final String attrName, final String defaultValue) {
        final String attr = pp.getAttributeValue(null, attrName);

        if (attr != null) {
            final String trimmedAttr = attr.trim();

            if (trimmedAttr.length() > 0)
                return trimmedAttr;
        }

        return defaultValue;
    }
}

Related

  1. IsName(XmlPullParser reader, String namespaceName, String localName)
  2. IsStartElement(XmlPullParser reader)
  3. IsStartElement(XmlPullParser reader, String name)
  4. IsStartElement(XmlPullParser reader, String namespaceName, String localName)
  5. require(final XmlPullParser pp, final String tagName)