Example usage for org.w3c.dom Node ATTRIBUTE_NODE

List of usage examples for org.w3c.dom Node ATTRIBUTE_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node ATTRIBUTE_NODE.

Prototype

short ATTRIBUTE_NODE

To view the source code for org.w3c.dom Node ATTRIBUTE_NODE.

Click Source Link

Document

The node is an Attr.

Usage

From source file:Main.java

/**
 * Builds an XPointer that refers to the given node. If a shorthand pointer
 * (using a schema-determined identifier) cannot be constructed, then a
 * scheme-based pointer is derived that indicates the absolute location path
 * of a node in a DOM document. The location is specified as a scheme-based
 * XPointer having two parts:// w ww . j a  v a2 s  . co m
 * <ul>
 * <li>an xmlns() part that declares a namespace binding context;</li>
 * <li>an xpointer() part that includes an XPath expression using the
 * abbreviated '//' syntax for selecting a descendant node.</li>
 * </ul>
 * 
 * @param node
 *            A node in a DOM document.
 * @return A String containing either a shorthand or a scheme-based pointer
 *         that refers to the node.
 * 
 * @see <a href="http://www.w3.org/TR/xptr-framework/"target="_blank">
 *      XPointer Framework</a>
 * @see <a href="http://www.w3.org/TR/xptr-xmlns/" target="_blank">XPointer
 *      xmlns() Scheme</a>
 * @see <a href="http://www.w3.org/TR/xptr-xpointer/"
 *      target="_blank">XPointer xpointer() Scheme</a>
 */
public static String buildXPointer(Node node) {
    if (null == node) {
        return "";
    }
    StringBuilder xpointer = new StringBuilder();
    if (null != node.getAttributes() && null != node.getAttributes().getNamedItem("id")) {
        String id = node.getAttributes().getNamedItem("id").getNodeValue();
        xpointer.append(node.getLocalName()).append("[@id='").append(id).append("']");
        return xpointer.toString();
    }
    String nsURI = node.getNamespaceURI();
    String nsPrefix = node.getPrefix();
    if (null == nsPrefix)
        nsPrefix = "tns";
    // WARNING: Escaping rules are currently ignored.
    xpointer.append("xmlns(").append(nsPrefix).append("=").append(nsURI).append(")");
    xpointer.append("xpointer((");
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        // Find the element in the list of all similarly named descendants
        // of the document root.
        NodeList elementsByName = node.getOwnerDocument().getElementsByTagNameNS(nsURI, node.getLocalName());
        for (int i = 0; i < elementsByName.getLength(); i++) {
            if (elementsByName.item(i).isSameNode(node)) {
                xpointer.append("//");
                xpointer.append(nsPrefix).append(':').append(node.getLocalName()).append(")[").append(i + 1)
                        .append("])");
                break;
            }
        }
        break;
    case Node.DOCUMENT_NODE:
        xpointer.append("/");
        break;
    case Node.ATTRIBUTE_NODE:
        Attr attrNode = (Attr) node;
        xpointer = new StringBuilder(buildXPointer(attrNode.getOwnerElement()));
        xpointer.insert(xpointer.lastIndexOf(")"), "/@" + attrNode.getName());
        break;
    default:
        xpointer.setLength(0);
        break;
    }
    return xpointer.toString();
}

From source file:io.cloudslang.content.xml.utils.XmlUtils.java

public static String nodeToString(Node node) throws TransformerException {

    if (node == null) {
        return EMPTY;
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        return node.toString();
    } else {//w ww . j a v  a2 s .c o  m
        return transformElementNode(node);
    }
}

From source file:Main.java

/**
 * Returns String representation of specified xml node.
 *
 * @param node   the specified xml node/*  ww w  .  ja v a 2 s  .  c o m*/
 * @return string representation of the node
 */
public static String getNodeData(Node node) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_FRAGMENT_NODE:
    case Node.DOCUMENT_NODE:
    case Node.ELEMENT_NODE: {
        /*for (Node child = node.getFirstChild(); null != child;
          child = child.getNextSibling())*/
        Node child = node.getFirstChild();
        if (child != null)
            return getNodeData(child);
    }
        break;
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        return node.getNodeValue();
    case Node.ATTRIBUTE_NODE:
        return node.getNodeValue();
    case Node.PROCESSING_INSTRUCTION_NODE:
        break;
    default:
        break;
    }
    return "";
}

From source file:Main.java

protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI)
        throws Exception {
    StringBuilder b = new StringBuilder();

    if (node == null) {
        return "";
    }//from ww  w.j  a  va2 s.c  o  m

    if (node instanceof Element) {
        Element element = (Element) node;
        b.append("<");
        b.append(element.getNodeName());

        Map<String, String> thisLevelPrefixes = new HashMap();
        if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) {
            thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI());
        }

        if (element.hasAttributes()) {
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node attr = map.item(i);
                if (attr.getNodeName().startsWith("xmlns"))
                    continue;
                if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) {
                    thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI());
                }
                b.append(" ");
                b.append(attr.getNodeName());
                b.append("=\"");
                b.append(attr.getNodeValue());
                b.append("\"");
            }
        }

        if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI)
                && !namespaceURI.equals(element.getParentNode().getNamespaceURI())) {
            b.append(" xmlns=\"").append(namespaceURI).append("\"");
        }

        for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) {
            b.append(" xmlns:").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
            parentPrefixes.add(entry.getKey());
        }

        NodeList children = element.getChildNodes();
        boolean hasOnlyAttributes = true;
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ATTRIBUTE_NODE) {
                hasOnlyAttributes = false;
                break;
            }
        }
        if (!hasOnlyAttributes) {
            b.append(">");
            for (int i = 0; i < children.getLength(); i++) {
                b.append(nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI()));
            }
            b.append("</");
            b.append(element.getNodeName());
            b.append(">");
        } else {
            b.append("/>");
        }

        for (String thisLevelPrefix : thisLevelPrefixes.keySet()) {
            parentPrefixes.remove(thisLevelPrefix);
        }

    } else if (node.getNodeValue() != null) {
        b.append(encodeText(node.getNodeValue(), node instanceof Attr));
    }

    return b.toString();
}

From source file:Main.java

protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI)
        throws Exception {
    StringBuilder b = new StringBuilder();

    if (node == null) {
        return "";
    }//from   ww w .  ja  va  2s. co m

    if (node instanceof Element) {
        Element element = (Element) node;
        b.append("<");
        b.append(element.getNodeName());

        Map<String, String> thisLevelPrefixes = new HashMap();
        if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) {
            thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI());
        }

        if (element.hasAttributes()) {
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node attr = map.item(i);
                if (attr.getNodeName().startsWith("xmlns"))
                    continue;
                if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) {
                    thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI());
                }
                b.append(" ");
                b.append(attr.getNodeName());
                b.append("=\"");
                b.append(attr.getNodeValue());
                b.append("\"");
            }
        }

        if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI)
                && !namespaceURI.equals(element.getParentNode().getNamespaceURI())) {
            b.append(" xmlns=\"").append(namespaceURI).append("\"");
        }

        for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) {
            b.append(" xmlns:").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
            parentPrefixes.add(entry.getKey());
        }

        NodeList children = element.getChildNodes();
        boolean hasOnlyAttributes = true;
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ATTRIBUTE_NODE) {
                hasOnlyAttributes = false;
                break;
            }
        }
        if (!hasOnlyAttributes) {
            b.append(">");
            for (int i = 0; i < children.getLength(); i++) {
                b.append(nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI()));
            }
            b.append("</");
            b.append(element.getNodeName());
            b.append(">");
        } else {
            b.append("/>");
        }

        for (String thisLevelPrefix : thisLevelPrefixes.keySet()) {
            parentPrefixes.remove(thisLevelPrefix);
        }

    } else if (node.getNodeValue() != null) {
        b.append(encodeText(node.getNodeValue()));
    }

    return b.toString();
}

From source file:Main.java

protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI)
        throws Exception {
    StringBuilder b = new StringBuilder();

    if (node == null) {
        return "";
    }//  w ww . j ava2 s .  c o m

    if (node instanceof Element) {
        Element element = (Element) node;
        b.append("<");
        b.append(element.getNodeName());

        Map<String, String> thisLevelPrefixes = new HashMap();
        if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) {
            thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI());
        }

        if (element.hasAttributes()) {
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node attr = map.item(i);
                if (attr.getNodeName().startsWith("xmlns"))
                    continue;
                if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) {
                    thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI());
                }
                b.append(" ");
                b.append(attr.getNodeName());
                b.append("=\"");
                b.append(attr.getNodeValue());
                b.append("\"");
            }
        }

        if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI)
                && !namespaceURI.equals(element.getParentNode().getNamespaceURI())) {
            b.append(" xmlns=\"").append(namespaceURI).append("\"");
        }

        for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) {
            b.append(" xmlns:").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
            parentPrefixes.add(entry.getKey());
        }

        NodeList children = element.getChildNodes();
        boolean hasOnlyAttributes = true;
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ATTRIBUTE_NODE) {
                hasOnlyAttributes = false;
                break;
            }
        }
        if (!hasOnlyAttributes) {
            b.append(">");
            for (int i = 0; i < children.getLength(); i++) {
                b.append(nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI()));
            }
            b.append("</");
            b.append(element.getNodeName());
            b.append(">");
        } else {
            b.append("/>");
        }

        for (String thisLevelPrefix : thisLevelPrefixes.keySet()) {
            parentPrefixes.remove(thisLevelPrefix);
        }

    } else if (node.getNodeValue() != null) {

        b.append(encodeText(node.getNodeValue()));
    }

    return b.toString();
}

From source file:Main.java

public static void assertEquivalent(Node node, Node node2) {
    if (node == null) {
        throw new IllegalArgumentException("the first node to be compared is null");
    }/*from   w w w.  j  a  v a 2 s.  com*/

    if (node2 == null) {
        throw new IllegalArgumentException("the second node to be compared is null");
    }

    if (!node.getNodeName().equals(node2.getNodeName())) {
        throw new IllegalArgumentException("nodes have different node names");
    }

    int attrCount = 0;
    NamedNodeMap attrs = node.getAttributes();
    if (attrs != null) {
        attrCount = attrs.getLength();
    }

    int attrCount2 = 0;
    NamedNodeMap attrs2 = node2.getAttributes();
    if (attrs2 != null) {
        attrCount2 = attrs2.getLength();
    }

    if (attrCount != attrCount2) {
        throw new IllegalArgumentException("nodes hava a different number of attributes");
    }

    outer: for (int i = 0; i < attrCount; i++) {
        Node n = attrs.item(i);
        String name = n.getNodeName();
        String value = n.getNodeValue();

        for (int j = 0; j < attrCount; j++) {
            Node n2 = attrs2.item(j);
            String name2 = n2.getNodeName();
            String value2 = n2.getNodeValue();

            if (name.equals(name2) && value.equals(value2)) {
                continue outer;
            }
        }
        throw new IllegalArgumentException("attribute " + name + "=" + value + " doesn't match");
    }

    boolean hasChildren = node.hasChildNodes();

    if (hasChildren != node2.hasChildNodes()) {
        throw new IllegalArgumentException("one node has children and the other doesn't");
    }

    if (hasChildren) {
        NodeList nl = node.getChildNodes();
        NodeList nl2 = node2.getChildNodes();

        short[] toFilter = new short[] { Node.TEXT_NODE, Node.ATTRIBUTE_NODE, Node.COMMENT_NODE };
        List nodes = filter(nl, toFilter);
        List nodes2 = filter(nl2, toFilter);

        int length = nodes.size();

        if (length != nodes2.size()) {
            throw new IllegalArgumentException("nodes hava a different number of children");
        }

        for (int i = 0; i < length; i++) {
            Node n = (Node) nodes.get(i);
            Node n2 = (Node) nodes2.get(i);
            assertEquivalent(n, n2);
        }
    }
}

From source file:com.cloudseal.spring.client.namespace.Utility.java

public static void removeNode(final Element rootElement, final String xPathLocation)
        throws XPathExpressionException {
    final XPath xPath = XPathFactory.newInstance().newXPath();
    xPath.setNamespaceContext(new CloudSealNamespaceContext());
    final Node node = (Node) xPath.evaluate(xPathLocation, rootElement, XPathConstants.NODE);
    final short nodeType = node.getNodeType();

    switch (nodeType) {
    case Node.ELEMENT_NODE:
        final Node parent = node.getParentNode();
        parent.removeChild(node);/*from  w  ww  .  j ava 2 s  .  c  o m*/
        break;

    case Node.ATTRIBUTE_NODE:
        final Attr attribute = (Attr) node;
        final Element element = attribute.getOwnerElement();
        element.removeAttributeNode(attribute);
        break;

    default:
        throw new IllegalArgumentException("Not supported node type: " + nodeType);
    }
}

From source file:Main.java

protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI)
        throws Exception {
    StringBuilder b = new StringBuilder();

    if (node == null) {
        return "";
    }/*from   w  w w  .ja v a2 s .  c  o  m*/

    if (node instanceof Element) {
        Element element = (Element) node;
        b.append("<");
        b.append(element.getNodeName());

        Map<String, String> thisLevelPrefixes = new HashMap();
        if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) {
            thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI());
        }

        if (element.hasAttributes()) {
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node attr = map.item(i);
                if (attr.getNodeName().startsWith("xmlns"))
                    continue;
                if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) {
                    thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI());
                }
                b.append(" ");
                b.append(attr.getNodeName());
                b.append("=\"");
                b.append(attr.getNodeValue());
                b.append("\"");
            }
        }

        if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI)
                && !namespaceURI.equals(element.getParentNode().getNamespaceURI())) {
            b.append(" xmlns=\"").append(namespaceURI).append("\"");
        }

        for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) {
            b.append(" xmlns:").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
            parentPrefixes.add(entry.getKey());
        }

        NodeList children = element.getChildNodes();
        boolean hasOnlyAttributes = true;
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ATTRIBUTE_NODE) {
                hasOnlyAttributes = false;
                break;
            }
        }
        if (!hasOnlyAttributes) {
            b.append(">");
            for (int i = 0; i < children.getLength(); i++) {
                b.append(nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI()));
            }
            b.append("</");
            b.append(element.getNodeName());
            b.append(">");
        } else {
            b.append("/>");
        }

        for (String thisLevelPrefix : thisLevelPrefixes.keySet()) {
            parentPrefixes.remove(thisLevelPrefix);
        }

    } else if (node.getNodeValue() != null) {
        b.append(encodeText(node, node.getNodeValue()));
    }

    return b.toString();
}

From source file:Main.java

public final static Class<? extends Node> toClass(final short nodeType) {
    switch (nodeType) {
    case Node.ATTRIBUTE_NODE:
        return Attr.class;
    case Node.CDATA_SECTION_NODE:
        return CDATASection.class;
    case Node.COMMENT_NODE:
        return Comment.class;
    case Node.DOCUMENT_FRAGMENT_NODE:
        return DocumentFragment.class;
    case Node.DOCUMENT_NODE:
        return Document.class;
    case Node.DOCUMENT_TYPE_NODE:
        return DocumentType.class;
    case Node.ELEMENT_NODE:
        return Element.class;
    case Node.ENTITY_NODE:
        return Entity.class;
    case Node.ENTITY_REFERENCE_NODE:
        return EntityReference.class;
    case Node.NOTATION_NODE:
        return Notation.class;
    case Node.PROCESSING_INSTRUCTION_NODE:
        return ProcessingInstruction.class;
    case Node.TEXT_NODE:
        return Text.class;
    }/*  w  ww  .j a  va 2  s. c  om*/
    throw new RuntimeException("Unrecognized node type " + nodeType);
}