/** * retrieves an attribute value by name. if the attribute is not present, a problem will be added * to the parse. * * @return the attribute value or <code>defaultValue</code> if there is no such attribute */ public static String attribute( Element element, String attributeName, Parse parse, String defaultValue) { Attr attribute = element.getAttributeNode(attributeName); if (attribute != null) { String value = attribute.getValue(); if (value.length() == 0) { parse.addProblem( "attribute <" + element.getLocalName() + " " + attributeName + "=\"\" is empty", element); } return value; } if (parse != null) { parse.addProblem( "attribute <" + element.getLocalName() + " " + attributeName + "=\"...\" is required", element); } return defaultValue; }
/** retrieves an attribute value by name. */ public static Integer attributeInteger(Element element, String attributeName, Parse parse) { Attr attribute = element.getAttributeNode(attributeName); if (attribute != null) { String attributeValue = attribute.getValue(); try { return Integer.parseInt(attributeValue); } catch (NumberFormatException e) { parse.addProblem( errorMessageAttribute(element, attributeName, attributeValue, e.getMessage()), element); } } return null; }
public static Element element(Element element, String tagName, Parse parse) { if (element != null && element.hasChildNodes()) { for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE && tagName.equals(child.getLocalName())) { return (Element) child; } } } if (parse != null) { parse.addProblem("missing element " + tagName, element); } return null; }
/** parse an attribute as an boolean. */ public static Boolean attributeBoolean(Element element, String attributeName, Parse parse) { Attr attribute = element.getAttributeNode(attributeName); if (attribute != null) { String attributeValue = attribute.getValue(); Boolean value = parseBooleanValue(attributeValue); if (value != null) return value; String message = errorMessageAttribute( element, attributeName, attributeValue, "value not in {true, enabled, on, false, disabled, off}"); parse.addProblem(message, element); } return null; }