Example usage for org.w3c.dom Element hasAttributes

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

Introduction

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

Prototype

public boolean hasAttributes();

Source Link

Document

Returns whether this node (if it is an element) has any attributes.

Usage

From source file:org.apache.xml.security.utils.IdResolver.java

public static int isElement(Element el, String id, Element[] els) {
    if (!el.hasAttributes()) {
        return 0;
    }/*from  w  w w .  j  a va  2 s  .  c  om*/
    NamedNodeMap ns = el.getAttributes();
    int elementIndex = names.indexOf(el.getNamespaceURI());
    elementIndex = (elementIndex < 0) ? namesLength : elementIndex;
    for (int length = ns.getLength(), i = 0; i < length; i++) {
        Attr n = (Attr) ns.item(i);
        String s = n.getNamespaceURI();

        int index = s == null ? elementIndex : names.indexOf(n.getNamespaceURI());
        index = (index < 0) ? namesLength : index;
        String name = n.getLocalName();
        if (name == null) {
            name = n.getName();
        }
        if (name.length() > 2) {
            continue;
        }
        String value = n.getNodeValue();
        if (name.charAt(0) == 'I') {
            char ch = name.charAt(1);
            if (ch == 'd' && value.equals(id)) {
                els[index] = el;
                if (index == 0) {
                    return 1;
                }
            } else if (ch == 'D' && value.endsWith(id)) {
                if (index != 3) {
                    index = namesLength;
                }
                els[index] = el;
            }
        } else if ("id".equals(name) && value.equals(id)) {
            if (index != 2) {
                index = namesLength;
            }
            els[index] = el;
        }
    }
    //For an element namespace search for importants
    if ((elementIndex == 3) && (el.getAttribute("OriginalRequestID").equals(id)
            || el.getAttribute("RequestID").equals(id) || el.getAttribute("ResponseID").equals(id))) {
        els[3] = el;
    } else if ((elementIndex == 4) && (el.getAttribute("AssertionID").equals(id))) {
        els[4] = el;
    } else if ((elementIndex == 5)
            && (el.getAttribute("RequestID").equals(id) || el.getAttribute("ResponseID").equals(id))) {
        els[5] = el;
    }
    return 0;
}

From source file:org.apache.xml.security.utils.XMLUtils.java

/**
 * This is the work horse for {@link #circumventBug2650}.
 *
 * @param node//from   w  w w  .j a v  a 2  s .c om
 * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
 * Namespace axis resolution is not XPath compliant </A>
 */
private static void circumventBug2650internal(Node node) {
    Node parent = null;
    Node sibling = null;
    final String namespaceNs = Constants.NamespaceSpecNS;
    do {
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            Element element = (Element) node;
            if (!element.hasChildNodes()) {
                break;
            }
            if (element.hasAttributes()) {
                NamedNodeMap attributes = element.getAttributes();
                int attributesLength = attributes.getLength();

                for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {

                    if (child.getNodeType() != Node.ELEMENT_NODE) {
                        continue;
                    }
                    Element childElement = (Element) child;

                    for (int i = 0; i < attributesLength; i++) {
                        Attr currentAttr = (Attr) attributes.item(i);
                        if (!namespaceNs.equals(currentAttr.getNamespaceURI()))
                            continue;
                        if (childElement.hasAttributeNS(namespaceNs, currentAttr.getLocalName())) {
                            continue;
                        }
                        childElement.setAttributeNS(namespaceNs, currentAttr.getName(),
                                currentAttr.getNodeValue());
                    }
                }
            }
        case Node.ENTITY_REFERENCE_NODE:
            parent = node;
            sibling = node.getFirstChild();
            break;
        case Node.DOCUMENT_NODE:
            parent = node;
            sibling = node.getFirstChild();
            break;
        }
        while ((sibling == null) && (parent != null)) {
            sibling = parent.getNextSibling();
            parent = parent.getParentNode();
        }
        if (sibling == null) {
            return;
        }

        node = sibling;
        sibling = node.getNextSibling();
    } while (true);
}

From source file:org.jboss.jaxr.juddi.transport.WS4EESaajTransport.java

private SOAPElement getSOAPElement(SOAPBody soapBody, Element elem) {
    String xmlns = IRegistry.UDDI_V2_NAMESPACE;
    SOAPElement soapElement = null;
    SOAPFactory factory = null;// ww w  .  j  av  a  2 s  . co m
    try {
        factory = SOAPFactory.newInstance();
        //Go through the element
        String name = elem.getNodeName();
        String nsuri = elem.getNamespaceURI();
        if (nsuri == null)
            nsuri = xmlns;
        soapElement = factory.createElement(name, "ns1", nsuri);
        //Get Attributes
        if (elem.hasAttributes()) {
            NamedNodeMap nnm = elem.getAttributes();
            int len = nnm != null ? nnm.getLength() : 0;
            for (int i = 0; i < len; i++) {
                Node n = nnm.item(i);
                String nodename = n.getNodeName();
                String nodevalue = n.getNodeValue();
                soapElement.addAttribute(factory.createName(nodename), nodevalue);
            }
        } else {
            soapElement.addAttribute(factory.createName("xmlns:ns1"), nsuri);
        }

        NodeList nlist = elem.getChildNodes();
        int len = nlist != null ? nlist.getLength() : 0;

        for (int i = 0; i < len; i++) {
            Node node = nlist.item(i);
            short nodeType = node != null ? node.getNodeType() : -100;
            if (Node.ELEMENT_NODE == nodeType) {
                soapElement.addChildElement(getSOAPElement(soapBody, (Element) node));
            } else if (nodeType == Node.TEXT_NODE) {
                soapElement.addTextNode(node.getNodeValue());
            }

        }
    } catch (SOAPException e) {
        e.printStackTrace();
    }
    return soapElement;
}

From source file:org.jbpm.bpel.integration.soap.SoapUtil.java

public static void copyNamespaces(SOAPElement target, Element source) throws SOAPException {
    // easy way out: no attributes
    if (!source.hasAttributes())
        return;// ww  w. j  ava2s  .c  om
    // traverse attributes to discover namespace declarations
    NamedNodeMap attributes = source.getAttributes();
    for (int i = 0, n = attributes.getLength(); i < n; i++) {
        Node attribute = attributes.item(i);
        // is attribute a namespace declaration?
        if (!BpelConstants.NS_XMLNS.equals(attribute.getNamespaceURI()))
            continue;
        // namespace declaration format xmlns:prefix="namespaceURI" |
        // xmlns="defaultNamespaceURI"
        String namespaceURI = attribute.getNodeValue();
        String prefix = attribute.getLocalName();
        // non-default namespace declaration?
        if (!"xmlns".equals(prefix)) {
            // BPEL-195: prevent addition matching visible declaration at target
            if (namespaceURI.equals(target.getNamespaceURI(prefix)))
                continue;
            target.addNamespaceDeclaration(prefix, namespaceURI);
            if (traceEnabled)
                log.trace("added namespace declaration: " + prefix + "->" + namespaceURI);
        }
        // non-empty default namespace declaration
        else if (namespaceURI.length() > 0) {
            prefix = XmlUtil.generatePrefix(DEFAULT_NAMESPACE_PREFIX, source);
            target.addNamespaceDeclaration(prefix, namespaceURI);
            if (traceEnabled)
                log.trace("reassigned default namespace declaration: " + prefix + "->" + namespaceURI);
        }
    }
}

From source file:org.jbpm.bpel.integration.soap.SoapUtil.java

public static void copyAttributes(SOAPElement target, Element source) {
    // easy way out: no attributes
    if (!source.hasAttributes())
        return;/*w w  w  .j  a v  a2 s  .  c om*/
    // traverse attributes
    NamedNodeMap attributes = source.getAttributes();
    for (int i = 0, n = attributes.getLength(); i < n; i++) {
        Node attribute = attributes.item(i);
        String namespaceURI = attribute.getNamespaceURI();
        // isn't the attribute a namespace declaration?
        if (BpelConstants.NS_XMLNS.equals(namespaceURI))
            continue;

        String name = attribute.getNodeName();
        String value = attribute.getNodeValue();
        if (namespaceURI == null) {
            /*
             * use the DOM level 1 method as some SAAJ implementations complain when presented a null
             * namespace URI
             */
            target.setAttribute(name, value);
        } else
            target.setAttributeNS(namespaceURI, name, value);

        if (traceEnabled)
            log.trace("set attribute: " + name);
    }
}

From source file:org.jbpm.bpel.xml.util.XmlUtil.java

public static void copyNamespaces(final Element target, Element source) {
    // easy way out: no attributes
    if (!source.hasAttributes())
        return;//from   w  w w  . ja va 2 s .co  m
    // traverse attributes to discover namespace declarations
    NamedNodeMap attributes = source.getAttributes();
    for (int i = 0, n = attributes.getLength(); i < n; i++) {
        Node attribute = attributes.item(i);
        // is attribute a namespace declaration?
        if (!BpelConstants.NS_XMLNS.equals(attribute.getNamespaceURI()))
            continue;
        // namespace declaration format
        // xmlns:prefix="namespaceURI" | xmlns="defaultNamespaceURI"
        String namespaceURI = attribute.getNodeValue();
        String prefix = attribute.getLocalName();
        // default namespace declaration?
        if ("xmlns".equals(prefix)) {
            // BPEL-195: prevent addition matching visible declaration at target
            if ("".equals(getPrefix(namespaceURI, target)))
                continue;
            addNamespaceDeclaration(target, namespaceURI);
            if (traceEnabled)
                log.trace("added default namespace declaration: " + namespaceURI);
        } else {
            // BPEL-195: prevent addition matching visible declaration at target
            if (prefix.equals(getPrefix(namespaceURI, target)))
                continue;
            addNamespaceDeclaration(target, namespaceURI, prefix);
            if (traceEnabled)
                log.trace("added namespace declaration: " + prefix + "->" + namespaceURI);
        }
    }
}

From source file:org.jbpm.bpel.xml.util.XmlUtil.java

public static void copyAttributes(Element target, Element source) {
    // easy way out: no attributes
    if (!source.hasAttributes())
        return;/*  w ww.j a v  a 2  s. c o m*/
    // traverse attributes
    NamedNodeMap attributes = source.getAttributes();
    for (int i = 0, n = attributes.getLength(); i < n; i++) {
        Node sourceAttr = attributes.item(i);
        String namespaceURI = sourceAttr.getNamespaceURI();
        String name = sourceAttr.getNodeName();
        // namespace declaration?
        if (BpelConstants.NS_XMLNS.equals(namespaceURI))
            continue;
        // unqualified?
        if (namespaceURI == null || namespaceURI.length() == 0) {
            target.setAttribute(name, sourceAttr.getNodeValue());
            if (traceEnabled)
                log.trace("set attribute: " + name);
        }
        // qualified
        else {
            Attr targetAttr = target.getOwnerDocument().createAttributeNS(namespaceURI, name);
            targetAttr.setValue(sourceAttr.getNodeValue());
            target.setAttributeNodeNS(targetAttr);
            ensureNamespaceDeclared(targetAttr, namespaceURI, sourceAttr.getPrefix());
            if (traceEnabled)
                log.trace("set attribute: {" + namespaceURI + '}' + name);
        }
    }
}

From source file:org.odk.aggregate.parser.SubmissionParser.java

/**
 * Recursive function that prints the nodes from an XML tree
 * /*from w w  w .j ava2  s.c  o m*/
 * @param node xml node to be recursively printed
 */
private void printNode(Element node) {
    System.out.println(ParserConsts.NODE_FORMATTED + node.getTagName());
    if (node.hasAttributes()) {
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attr = attributes.item(i);
            System.out.println(ParserConsts.ATTRIBUTE_FORMATTED + attr.getNodeName() + BasicConsts.EQUALS
                    + attr.getNodeValue());
        }
    }
    if (node.hasChildNodes()) {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                printNode((Element) child);
            } else if (child.getNodeType() == Node.TEXT_NODE) {
                String value = child.getNodeValue().trim();
                if (value.length() > 0) {
                    System.out.println(ParserConsts.VALUE_FORMATTED + value);
                }
            }
        }
    }

}

From source file:org.opendatakit.aggregate.parser.SubmissionParser.java

/**
 * Recursive function that prints the nodes from an XML tree
 * /*from  w w w .  j ava  2 s.c  o  m*/
 * @param node
 *          xml node to be recursively printed
 */
@SuppressWarnings("unused")
private void printNode(Element node) {
    System.out.println(ParserConsts.NODE_FORMATTED + node.getTagName());
    if (node.hasAttributes()) {
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attr = attributes.item(i);
            System.out.println(ParserConsts.ATTRIBUTE_FORMATTED + attr.getNodeName() + BasicConsts.EQUALS
                    + attr.getNodeValue());
        }
    }
    if (node.hasChildNodes()) {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                printNode((Element) child);
            } else if (child.getNodeType() == Node.TEXT_NODE) {
                String value = child.getNodeValue().trim();
                if (value.length() > 0) {
                    System.out.println(ParserConsts.VALUE_FORMATTED + value);
                }
            }
        }
    }

}

From source file:tkwatch.Utilities.java

/**
 * Recursively travels through a DOM tree. Adapted from Vohra and Vohra,
 * <i>Pro XML Development with Java Technology</i>, p. 47. This was used
 * during development and is not called by <strong>TKWatch+</strong>.
 * Retained for completeness./*from  w  w w. ja  v a2 s  . co  m*/
 * 
 * @param previousNode
 * @param visitNode
 */
public static void visitNode(Element previousNode, Element visitNode) {
    if (previousNode != null) {
        System.out.println("Element " + previousNode.getTagName() + " has element:");
    }
    System.out.println("Element Name: " + visitNode.getTagName());
    if (visitNode.hasAttributes()) {
        System.out.println("Element " + visitNode.getTagName() + " has attributes: ");
        NamedNodeMap attributes = visitNode.getAttributes();
        for (int j = 0; j < attributes.getLength(); j++) {
            Attr attribute = (Attr) (attributes.item(j));
            System.out.println("Attribute:" + attribute.getName() + " with value " + attribute.getValue());
        }
    }

    // Obtain a NodeList of nodes in an Element node.
    NodeList nodeList = visitNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            visitNode(visitNode, element);
        } else if (node.getNodeType() == Node.TEXT_NODE) {
            String str = node.getNodeValue().trim();
            if (str.length() > 0) {
                System.out.println("Element Text: " + str);
            }
        }
    }
}