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:biz.webgate.tools.urlfetcher.URLFetcher.java

public static void checkDescription(PageAnalyse analyse, NodeList ndlMet) {
    if ("".equals(analyse.getDescription())) {
        for (int nCounter = 0; nCounter < ndlMet.getLength(); nCounter++) {
            Element elCurrent = (Element) ndlMet.item(nCounter);
            if ("description".equalsIgnoreCase(elCurrent.getAttribute("name"))
                    && elCurrent.hasAttribute("content")) {
                analyse.setDescription(elCurrent.getAttribute("content"));
                break;
            }//from ww  w  .  j a  va 2s .  c om
        }
    }
}

From source file:com.mtgi.analytics.aop.config.TemplateBeanDefinitionParser.java

/**
 * Convenience method to update a template bean definition from overriding XML data.  
 * If <code>overrides</code> contains attribute <code>attribute</code>, transfer that
 * attribute onto <code>template</code>, overwriting the default value.
 *///from www  . j  a  v a  2 s.c  om
public static String overrideAttribute(String attribute, BeanDefinition template, Element overrides) {
    String value = (String) template.getAttribute(attribute);
    if (overrides.hasAttribute(attribute)) {
        value = overrides.getAttribute(attribute);
        template.setAttribute(attribute, value);
    }
    return value;
}

From source file:com.mtgi.analytics.aop.config.TemplateBeanDefinitionParser.java

/**
 * Convenience method to update a template bean definition from overriding XML data.  
 * If <code>overrides</code> contains attribute <code>attribute</code> or a child element
 * with name <code>attribute</code>, transfer that
 * attribute as a bean property onto <code>template</code>, overwriting the default value.
 * @param reference if true, the value of the attribute is to be interpreted as a runtime bean name reference; otherwise it is interpreted as a literal value
 *//*  ww w  .j  ava  2 s.  c  o m*/
public static boolean overrideProperty(String attribute, BeanDefinition template, Element overrides,
        boolean reference) {
    Object value = null;
    if (overrides.hasAttribute(attribute)) {
        value = overrides.getAttribute(attribute);
    } else {
        NodeList children = overrides.getElementsByTagNameNS("*", attribute);
        if (children.getLength() == 1) {
            Element child = (Element) children.item(0);
            value = child.getTextContent();
        }
    }

    if (value != null) {
        if (reference)
            value = new RuntimeBeanReference(value.toString());

        String propName = Conventions.attributeNameToPropertyName(attribute);
        MutablePropertyValues props = template.getPropertyValues();
        props.removePropertyValue(propName);
        props.addPropertyValue(propName, value);
        return true;
    }

    return false;
}

From source file:XMLUtils.java

public static void replaceAttribute(Element element, String attr, String value) {
    if (element.hasAttribute(attr)) {
        element.removeAttribute(attr);/*from  w w w  . j  a v  a 2s . c  o m*/
    }
    element.setAttribute(attr, value);
}

From source file:Main.java

/**
 * Get attribute value, returning <code>null</code> if unset.
 * Some DOM implementations return an empty string for an unset
 * attribute./*from ww w .j av a 2 s  .  c om*/
 * @param element The DOM element.
 * @param attributeName The attribute to get.
 * @param namespaceURI Namespace URI of the required attribute, or null
 * to perform a non-namespaced get.
 * @return The attribute value, or &lt;code&gt;null&lt;/code&gt; if unset.
 */
public static String getAttributeValue(Element element, String attributeName, String namespaceURI) {
    String attributeValue;
    if (namespaceURI == null) {
        attributeValue = element.getAttribute(attributeName);
    } else {
        attributeValue = element.getAttributeNS(namespaceURI, attributeName);
    }
    if (attributeValue.length() == 0 && !element.hasAttribute(attributeName)) {
        return null;
    }
    return attributeValue;
}

From source file:importer.handler.post.stages.Discriminator.java

/**
 * Add a new version to the current element
 * @param elem the element to add the new version to
 * @param wits the new version/*from w  w  w  .ja  v  a2s  .  co m*/
 */
static void addVersion(Element elem, String wits) {
    if (elem.hasAttribute(Splitter.VERSIONS))
        elem.setAttribute(Splitter.VERSIONS, mergeVersions(elem.getAttribute(Splitter.VERSIONS), wits));
    else
        elem.setAttribute(Splitter.VERSIONS, wits);
}

From source file:com.codebutler.farebot.mifare.Card.java

public static Card fromXml(String xml) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(xml)));

    Element rootElement = doc.getDocumentElement();

    CardType type = CardType.class.getEnumConstants()[Integer.parseInt(rootElement.getAttribute("type"))];
    byte[] id = Utils.hexStringToByteArray(rootElement.getAttribute("id"));
    Date scannedAt = rootElement.hasAttribute("scanned_at")
            ? new Date(Long.valueOf(rootElement.getAttribute("scanned_at")))
            : new Date(0);
    switch (type) {
    case MifareDesfire:
        return DesfireCard.fromXml(id, scannedAt, rootElement);
    case CEPAS://from  w  ww . j a  v a2  s  .c  o m
        return CEPASCard.fromXML(id, scannedAt, rootElement);
    case FeliCa:
        return FelicaCard.fromXml(id, scannedAt, rootElement);
    default:
        throw new UnsupportedOperationException("Unsupported card type: " + type);
    }
}

From source file:com.codebutler.farebot.card.Card.java

public static Card fromXml(String xml) throws Exception {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(xml)));

    Element rootElement = doc.getDocumentElement();

    CardType type = CardType.class.getEnumConstants()[Integer.parseInt(rootElement.getAttribute("type"))];
    byte[] id = Utils.hexStringToByteArray(rootElement.getAttribute("id"));
    Date scannedAt = rootElement.hasAttribute("scanned_at")
            ? new Date(Long.valueOf(rootElement.getAttribute("scanned_at")))
            : new Date(0);
    switch (type) {
    case MifareDesfire:
        return DesfireCard.fromXml(id, scannedAt, rootElement);
    case CEPAS://from www .  ja  v  a2 s. c om
        return CEPASCard.fromXML(id, scannedAt, rootElement);
    case FeliCa:
        return FelicaCard.fromXml(id, scannedAt, rootElement);
    case MifareClassic:
        return ClassicCard.fromXml(id, scannedAt, rootElement);
    default:
        throw new UnsupportedOperationException("Unsupported card type: " + type);
    }
}

From source file:Main.java

/**
 * Are elements equal./*from w w  w .j  a v a 2  s.  c  o  m*/
 * 
 * @param element1
 *            the element1
 * @param element2
 *            the element2
 * @return true, if successful
 */
public static boolean areElementsEqual(Element element1, Element element2) {
    if (!element1.getTagName().equals(element2.getTagName())) {
        return false;
    }
    NamedNodeMap nodeAttrMap = element1.getAttributes();
    NamedNodeMap pathAttrMap = element2.getAttributes();
    if ((nodeAttrMap == null && pathAttrMap == null)
            || (pathAttrMap.getLength() == 0 && nodeAttrMap.getLength() == 0)) {
        return true;
    } else {
        if (element1.hasAttribute("name") && element2.hasAttribute("name")) {
            if (element1.getAttribute("name").equals(element2.getAttribute("name"))) {
                return true;
            }
        } else if (nodeAttrMap != null && pathAttrMap != null
                && (nodeAttrMap.getLength() == pathAttrMap.getLength())) {
            for (int k = 0; k < nodeAttrMap.getLength(); k++) {
                Node nodeAttr = nodeAttrMap.item(k);
                String nodeAttrName = nodeAttr.getNodeName();
                String nodeAttrValue = nodeAttr.getNodeValue();
                if (element2.hasAttribute(nodeAttrName)
                        && nodeAttrValue.equals(element2.getAttribute(nodeAttrName))) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:com.cburch.draw.shapes.SvgReader.java

private static AbstractCanvasObject createRectangle(Element elt) {
    int x = Integer.parseInt(elt.getAttribute("x"));
    int y = Integer.parseInt(elt.getAttribute("y"));
    int w = Integer.parseInt(elt.getAttribute("width"));
    int h = Integer.parseInt(elt.getAttribute("height"));
    if (elt.hasAttribute("rx")) {
        AbstractCanvasObject ret = new RoundRectangle(x, y, w, h);
        int rx = Integer.parseInt(elt.getAttribute("rx"));
        ret.setValue(DrawAttr.CORNER_RADIUS, Integer.valueOf(rx));
        return ret;
    } else {/* w  w  w . ja va  2s.c  om*/
        return new Rectangle(x, y, w, h);
    }
}