Example usage for org.w3c.dom Node getPrefix

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

Introduction

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

Prototype

public String getPrefix();

Source Link

Document

The namespace prefix of this node, or null if it is unspecified.

Usage

From source file:Main.java

public static String getPrefix(Node contextNode, Object[] arguments, int position) {
    String prefix = null;//ww w. ja v a2  s. co m
    if (arguments.length > position) {
        prefix = arguments[position] + ":";
    } else if (contextNode != null && contextNode.getPrefix() != null) {
        prefix = contextNode.getPrefix() + ":";
    } else {
        prefix = "";
    }
    return prefix;
}

From source file:Main.java

public static String getPrefix(org.w3c.dom.Element el, String ns) {
    NamedNodeMap atts = el.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node node = atts.item(i);
        String name = node.getNodeName();
        if (ns.equals(node.getNodeValue())
                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) {
            return node.getPrefix();
        }//from   w  w  w.  j  a  va  2  s .c o  m
    }
    return null;
}

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 ava  2  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:Utils.java

public static String getPrefix(Element el, String ns) {
    NamedNodeMap atts = el.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node node = atts.item(i);
        String name = node.getNodeName();
        if (ns.equals(node.getNodeValue())
                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) {
            return node.getPrefix();
        }//from  w  w  w .  j a  v a  2 s  .  c o m
    }
    return null;
}

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 www.j av  a2 s .com*/

    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 w  w 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()));
    }

    return b.toString();
}

From source file:Utils.java

/**
 * Get all prefixes defined on this element for the specified namespace.
 * // ww  w .  j  a  v a 2 s.c  om
 * @param element
 * @param namespaceUri
 * @param prefixes
 */
public static void getPrefixes(Element element, String namespaceUri, List<String> prefixes) {
    NamedNodeMap atts = element.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node node = atts.item(i);
        String name = node.getNodeName();
        if (namespaceUri.equals(node.getNodeValue())
                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) {
            prefixes.add(node.getPrefix());
        }
    }
}

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 w  w.j ava 2 s.c om*/

    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

/**
 * 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:/*from   w ww.  ja  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:Main.java

private static boolean isEqualNode(Node arg0, Node arg) {
    if (arg == arg0) {
        return true;
    }//from  w  w  w .j  av  a 2  s . co m
    if (arg.getNodeType() != arg0.getNodeType()) {
        return false;
    }

    if (arg0.getNodeName() == null) {
        if (arg.getNodeName() != null) {
            return false;
        }
    } else if (!arg0.getNodeName().equals(arg.getNodeName())) {
        return false;
    }

    if (arg0.getLocalName() == null) {
        if (arg.getLocalName() != null) {
            return false;
        }
    } else if (!arg0.getLocalName().equals(arg.getLocalName())) {
        return false;
    }

    if (arg0.getNamespaceURI() == null) {
        if (arg.getNamespaceURI() != null) {
            return false;
        }
    } else if (!arg0.getNamespaceURI().equals(arg.getNamespaceURI())) {
        return false;
    }

    if (arg0.getPrefix() == null) {
        if (arg.getPrefix() != null) {
            return false;
        }
    } else if (!arg0.getPrefix().equals(arg.getPrefix())) {
        return false;
    }

    if (arg0.getNodeValue() == null) {
        if (arg.getNodeValue() != null) {
            return false;
        }
    } else if (!arg0.getNodeValue().equals(arg.getNodeValue())) {
        return false;
    }
    return true;
}