Example usage for org.w3c.dom Element hasAttribute

List of usage examples for org.w3c.dom Element hasAttribute

Introduction

In this page you can find the example usage for org.w3c.dom Element hasAttribute.

Prototype

public boolean hasAttribute(String name);

Source Link

Document

Returns true when an attribute with a given name is specified on this element or has a default value, false otherwise.

Usage

From source file:Main.java

public static URI getURIAttribute(Element el, String attr, URI defaultValue) {
    if (el.hasAttribute(attr)) {
        try {// w  ww .  j av a  2  s .  c  om
            return new URI(el.getAttribute(attr));
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return defaultValue;
}

From source file:Main.java

public static int attributeIntValue(Element e, String attr, Integer defaultValue) {
    if (!e.hasAttribute(attr)) {
        return defaultValue;
    }//from ww  w  .  j  a va2s. c om
    if (e.getAttribute(attr).equals("yes") || e.getAttribute(attr).equals("true")) {
        return 1;
    }
    return Integer.parseInt(e.getAttribute(attr));
}

From source file:Main.java

public static Integer attributeIntValue(Element e, String attr, Integer defaultValue) {
    if (!e.hasAttribute(attr)) {
        return defaultValue;
    }/*from   w  w  w  .ja  va  2  s  .co  m*/
    if (e.getAttribute(attr).equals("yes") || e.getAttribute(attr).equals("true")) {
        return 1;
    }
    return Integer.parseInt(e.getAttribute(attr));
}

From source file:Main.java

public static String readAttributeValue(Element e, String attributeName) {
    if (!e.hasAttribute(attributeName)) {
        throw new IllegalStateException("Attribute '" + attributeName + "' is absent");
    }//from   w w w  .j a va  2  s.  c  o m

    return e.getAttribute(attributeName);
}

From source file:Main.java

public static boolean getElementBooleanValue(Element element, String attribute, boolean defaultValue) {
    if (!element.hasAttribute(attribute))
        return defaultValue;
    return Boolean.valueOf(getElementStringValue(element, attribute)).booleanValue();
}

From source file:Main.java

/**
 * Adds a reference for the given object to the references map if it has a refId
 *
 * @author Tristan Bepler//ww  w.  j a  va 2 s .com
 */
private static void addReference(Element cur, Map<Integer, Object> references, Object o) {
    if (cur.hasAttribute(REF_ID)) {
        int id = Integer.parseInt(cur.getAttribute(REF_ID));
        references.put(id, o);
    }
}

From source file:Main.java

private static Properties importProperties(NodeList nodeList) {
    Properties properties = new Properties();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element entry = (Element) nodeList.item(i);
        if (entry.hasAttribute("name")) {
            String val = (entry == null) ? "" : entry.getFirstChild().getNodeValue();
            String key = entry.getAttribute("name");
            properties.setProperty(key, val);
        }/*  ww  w  . j a  v a  2s  .  c  om*/
    }
    return properties;
}

From source file:Main.java

public static Element getChildWithAttribute(Element element, String attributeName) {

    if (element == null) {
        return null;
    }//w  ww.  j  a v  a2s . c  om

    NodeList children = element.getChildNodes();

    for (int loop = 0, length = children.getLength(); loop < length; loop++) {
        Node node = children.item(loop);

        if (!(node instanceof Element)) {
            continue;
        }

        Element child = (Element) node;

        if (child.hasAttribute(attributeName)) {
            return child;
        }
    }

    return null;
}

From source file:Main.java

public static int getOptionalInt(Element element, String attribute, int defaultValue) {
    if (element.hasAttribute(attribute)) {
        String s = element.getAttribute(attribute);

        if (s.startsWith("+")) {
            s = s.substring(1);/*w w w  . j a v  a 2  s .com*/
        }

        return Integer.parseInt(s);
    }
    return defaultValue;
}

From source file:Main.java

public static String readAttributeValue(Element e, String attributeName, String defaultValue) {
    if (e.hasAttribute(attributeName)) {
        return e.getAttribute(attributeName);
    } else {/*w  w w.ja  v  a  2  s  .co  m*/
        return defaultValue;
    }
}