Java XML Attribute Get getBooleanAttribute(Element elem, String attName, boolean mandatory, boolean defaultValue)

Here you can find the source of getBooleanAttribute(Element elem, String attName, boolean mandatory, boolean defaultValue)

Description

getBooleanAttribute purpose.

License

Open Source License

Parameter

Parameter Description
elem The root element to look for children in.
attName The name of the attribute to look for.
mandatory true when an exception should be thrown if the attribute element does not exist.
defaultValue what to return for a non-mandatory that is not found.

Exception

Parameter Description
Exception When a child attribute is required andnot found.

Return

The value if the attribute was found, the false otherwise.

Declaration

public static boolean getBooleanAttribute(Element elem, String attName,
        boolean mandatory, boolean defaultValue) throws Exception 

Method Source Code

//package com.java2s;
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
 * (c) 2001 - 2013 OpenPlans/* w w w.  ja  v  a 2  s  .  c o  m*/
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */

import org.w3c.dom.Attr;

import org.w3c.dom.Element;

public class Main {
    /**
     * getBooleanAttribute purpose.
     *
     * <p>
     * Used to help with XML manipulations. Returns the first child integer
     * attribute of the specified name.  An exception occurs when the node is
     * required and not found.
     * </p>
     *
     * @param elem The root element to look for children in.
     * @param attName The name of the attribute to look for.
     * @param mandatory true when an exception should be thrown if the
     *        attribute element does not exist.
     * @param defaultValue what to return for a non-mandatory that is not
     *        found.
     *
     * @return The value if the attribute was found, the false otherwise.
     *
     * @throws Exception When a child attribute is required and
     *         not found.
     */
    public static boolean getBooleanAttribute(Element elem, String attName,
            boolean mandatory, boolean defaultValue) throws Exception {
        String value = getAttribute(elem, attName, mandatory);

        if ((value == null) || (value == "")) {
            return defaultValue;
        }

        return Boolean.valueOf(value).booleanValue();
    }

    /**
     * getIntAttribute purpose.
     *
     * <p>
     * Used to help with XML manipulations. Returns the first child integer
     * attribute of the specified name.  An exception occurs when the node is
     * required and not found.
     * </p>
     *
     * @param elem The root element to look for children in.
     * @param attName The name of the attribute to look for.
     * @param mandatory true when an exception should be thrown if the
     *        attribute element does not exist.
     *
     * @return The value if the attribute was found, the null otherwise.
     *
     * @throws Exception When a child attribute is required and
     *         not found.
     * @throws NullPointerException DOCUMENT ME!
     */
    public static String getAttribute(Element elem, String attName,
            boolean mandatory) throws Exception {
        if (elem == null) {
            if (mandatory) {
                throw new NullPointerException();
            }

            return "";
        }

        Attr att = elem.getAttributeNode(attName);

        String value = null;

        if (att != null) {
            value = att.getValue();
        }

        if (mandatory) {
            if (att == null) {
                throw new Exception("element " + elem.getNodeName()
                        + " does not contains an attribute named "
                        + attName);
            } else if ("".equals(value)) {
                throw new Exception("attribute " + attName + "in element "
                        + elem.getNodeName() + " is empty");
            }
        }

        return value;
    }
}

Related

  1. getBooleanAttr(Element element, String name)
  2. getBooleanAttrib(Element element, String attribName, String namespaceURI)
  3. getBooleanAttribute(Element e, String name)
  4. getBooleanAttribute(Element el, String name)
  5. getBooleanAttribute(Element elem, String attName, boolean mandatory)
  6. getBooleanAttribute(Element element, String attributeName)
  7. getBooleanAttribute(Element element, String key, boolean defValue)
  8. getBooleanAttribute(Element element, String name)
  9. getBooleanAttribute(Element element, String name, boolean defaultValue)