Java XML Attribute Read readBooleanAttributeElement(final XMLStreamReader reader, final String attributeName)

Here you can find the source of readBooleanAttributeElement(final XMLStreamReader reader, final String attributeName)

Description

Read an element which contains only a single boolean attribute.

License

Apache License

Parameter

Parameter Description
reader the reader
attributeName the attribute name, usually "value"

Exception

Parameter Description

Return

the boolean value

Declaration

public static boolean readBooleanAttributeElement(final XMLStreamReader reader, final String attributeName)
        throws XMLStreamException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import javax.xml.XMLConstants;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class Main {
    /**/*from  w  ww .j a  v a2s  . c  o  m*/
     * Read an element which contains only a single boolean attribute.
     * @param reader the reader
     * @param attributeName the attribute name, usually "value"
     * @return the boolean value
     * @throws javax.xml.stream.XMLStreamException if an error occurs or if the
     *         element does not contain the specified attribute, contains other
     *         attributes, or contains child elements.
     */
    public static boolean readBooleanAttributeElement(final XMLStreamReader reader, final String attributeName)
            throws XMLStreamException {
        requireSingleAttribute(reader, attributeName);
        final boolean value = Boolean.parseBoolean(reader.getAttributeValue(0));
        requireNoContent(reader);
        return value;
    }

    /**
     * Require that the current element have only a single attribute with the
     * given name.
     * @param reader the reader
     * @param attributeName the attribute name
     * @throws javax.xml.stream.XMLStreamException if an error occurs
     */
    public static String requireSingleAttribute(final XMLStreamReader reader, final String attributeName)
            throws XMLStreamException {
        final int count = reader.getAttributeCount();
        if (count == 0) {
            throw missingRequired(reader, Collections.singleton(attributeName));
        }
        requireNoNamespaceAttribute(reader, 0);
        if (!attributeName.equals(reader.getAttributeLocalName(0))) {
            throw unexpectedAttribute(reader, 0);
        }
        if (count > 1) {
            throw unexpectedAttribute(reader, 1);
        }
        return reader.getAttributeValue(0);
    }

    /**
     * Consumes the remainder of the current element, throwing an
     * {@link javax.xml.stream.XMLStreamException} if it contains any child
     * elements.
     * @param reader the reader
     * @throws javax.xml.stream.XMLStreamException if an error occurs
     */
    public static void requireNoContent(final XMLStreamReader reader) throws XMLStreamException {
        if (reader.hasNext() && reader.nextTag() != END_ELEMENT) {
            throw unexpectedElement(reader);
        }
    }

    /**
     * Get an exception reporting a missing, required XML attribute.
     * @param reader the stream reader
     * @param required a set of enums whose toString method returns the
     *        attribute name
     * @return the exception
     */
    public static XMLStreamException missingRequired(final XMLStreamReader reader, final Set<?> required) {
        final StringBuilder b = new StringBuilder();
        Iterator<?> iterator = required.iterator();
        while (iterator.hasNext()) {
            final Object o = iterator.next();
            b.append(o.toString());
            if (iterator.hasNext()) {
                b.append(", ");
            }
        }
        return new XMLStreamException("Missing required attribute(s): " + b, reader.getLocation());
    }

    public static void requireNoNamespaceAttribute(final XMLStreamReader reader, final int index)
            throws XMLStreamException {
        if (!isNoNamespaceAttribute(reader, index)) {
            throw unexpectedAttribute(reader, index);
        }
    }

    /**
     * Get an exception reporting an unexpected XML attribute.
     * @param reader the stream reader
     * @param index the attribute index
     * @return the exception
     */
    public static XMLStreamException unexpectedAttribute(final XMLStreamReader reader, final int index) {
        return new XMLStreamException("Unexpected attribute '" + reader.getAttributeName(index) + "' encountered",
                reader.getLocation());
    }

    /**
     * Get an exception reporting an unexpected XML element.
     * @param reader the stream reader
     * @return the exception
     */
    public static XMLStreamException unexpectedElement(final XMLStreamReader reader) {
        return new XMLStreamException("Unexpected element '" + reader.getName() + "' encountered",
                reader.getLocation());
    }

    public static boolean isNoNamespaceAttribute(final XMLStreamReader reader, final int index) {
        String namespace = reader.getAttributeNamespace(index);
        // FIXME when STXM-8 is done, remove the null check
        return namespace == null || XMLConstants.NULL_NS_URI.equals(namespace);
    }
}

Related

  1. readAttribute(Node element, String attributeName)
  2. readAttribute(Node node, String attribute, String defaultValue)
  3. readAttributeWithPrefix(Node node, String attributePrefix, String defaultValue)
  4. readBoolAttr(Element element, String attributeName)
  5. readBooleanAttribute(Element elem, String name, boolean defaultValue)
  6. readFloat(Node node, String attributeName, float def)
  7. readFloatAttribute(XMLStreamReader reader, String attributeName)
  8. readInt(Node node, String attributeName, int def)
  9. readIntAttr(Element element, String attributeName, int defaultValue)