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:com.amalto.core.util.Util.java

private static String[] getTextNodes(Node contextNode, String xPath, final Node namespaceNode)
        throws TransformerException {
    String[] results;//  w w  w  .j  ava2 s.c  om
    // test for hard-coded values
    if (xPath.startsWith("\"") && xPath.endsWith("\"")) { //$NON-NLS-1$ //$NON-NLS-2$
        return new String[] { xPath.substring(1, xPath.length() - 1) };
    }
    // test for incomplete path (elements missing /text())
    if (!xPath.matches(".*@[^/\\]]+")) { // attribute
        if (!xPath.endsWith(")")) { // function
            xPath += "/text()";
        }
    }
    try {
        XPath path = XPathFactory.newInstance().newXPath();
        path.setNamespaceContext(new NamespaceContext() {

            @Override
            public String getNamespaceURI(String s) {
                return namespaceNode.getNamespaceURI();
            }

            @Override
            public String getPrefix(String s) {
                return namespaceNode.getPrefix();
            }

            @Override
            public Iterator getPrefixes(String s) {
                return Collections.singleton(namespaceNode.getPrefix()).iterator();
            }
        });
        NodeList xo = (NodeList) path.evaluate(xPath, contextNode, XPathConstants.NODESET);
        results = new String[xo.getLength()];
        for (int i = 0; i < xo.getLength(); i++) {
            results[i] = xo.item(i).getTextContent();
        }
    } catch (Exception e) {
        String err = "Unable to get the text node(s) of " + xPath + ": " + e.getClass().getName() + ": "
                + e.getLocalizedMessage();
        throw new TransformerException(err);
    }
    return results;

}

From source file:de.betterform.xml.xforms.XFormsElement.java

/**
 * returns the list of all attributes that are not in 'known' namespaces and do not have the null (default?) namespace
 *
 * /*  w w w.j  a va2 s .co  m*/
 * @return the key-value-pair of the attributes
 */
public Map<String, String> getCustomMIPAttributes() {

    HashMap<String, String> customMIPAttributes = new HashMap<String, String>();
    NamedNodeMap nnm = element.getAttributes();
    for (int i = 0; i < nnm.getLength(); i++) {
        Node attribute = nnm.item(i);
        if (attribute.getNamespaceURI() != null
                && !NamespaceConstants.BETTERFORM_NS.equals(attribute.getNamespaceURI())
                && !NamespaceConstants.XFORMS_NS.equals(attribute.getNamespaceURI())
                && !NamespaceConstants.XHTML_NS.equals(attribute.getNamespaceURI())
                && !NamespaceConstants.XMLNS_NS.equals(attribute.getNamespaceURI())
                && !NamespaceConstants.XMLSCHEMA_INSTANCE_NS.equals(attribute.getNamespaceURI())
                && !NamespaceConstants.XMLEVENTS_NS.equals(attribute.getNamespaceURI())) {
            customMIPAttributes.put(attribute.getPrefix() + WordUtils.capitalize(attribute.getLocalName()),
                    attribute.getTextContent());
        }
    }
    return customMIPAttributes;

}

From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.action.ClinicalDateObscurer.java

/**
 * Return the namespace from the given <code>Node</code> (prefixed by ":" if there is a namespace)
 *
 * @param node the <code>Node</code>
 * @return the namespace from the given <code>Node</code> (prefixed by ":" if there is a namespace)
 *///w ww .j a v  a  2 s . c om
private String getNamespaceFromNodeName(final Node node) {

    String result = node.getPrefix();

    //If a namespace was found, prefix it by ":"
    if (!StringUtils.isBlank(result)) {
        result += ":";
    }

    return result;
}

From source file:DOMWriter.java

private void printInternal(Node node, boolean indentEndMarker) {
    // is there anything to do?
    if (node == null) {
        return;/*from   ww w.  ja v  a2 s  .  com*/
    }

    // JBAS-2117 - Don't skip the DOCUMENT_NODE
    // if (node instanceof Document) node = ((Document)node).getDocumentElement();

    if (wroteXMLDeclaration == false && writeXMLDeclaration == true && canonical == false) {
        out.print("<?xml version='1.0'");
        if (charsetName != null)
            out.print(" encoding='" + charsetName + "'");

        out.print("?>");
        if (prettyprint)
            out.println();

        wroteXMLDeclaration = true;
    }

    int type = node.getNodeType();
    boolean hasChildNodes = node.getChildNodes().getLength() > 0;

    String nodeName = node.getNodeName();
    switch (type) {
    // print document
    case Node.DOCUMENT_NODE: {
        NodeList children = node.getChildNodes();
        for (int iChild = 0; iChild < children.getLength(); iChild++) {
            printInternal(children.item(iChild), false);
        }
        out.flush();
        break;
    }

    // print element with attributes
    case Node.ELEMENT_NODE: {
        Element element = (Element) node;
        if (prettyprint) {
            for (int i = 0; i < prettyIndent; i++) {
                out.print(' ');
            }
            prettyIndent++;
        }

        out.print('<');
        out.print(nodeName);

        Map nsMap = new HashMap();
        String elPrefix = node.getPrefix();
        String elNamespaceURI = node.getNamespaceURI();
        if (elPrefix != null) {
            String nsURI = getNamespaceURI(elPrefix, element, rootNode);
            nsMap.put(elPrefix, nsURI);
        }

        Attr attrs[] = sortAttributes(node.getAttributes());
        for (int i = 0; i < attrs.length; i++) {
            Attr attr = attrs[i];
            String atPrefix = attr.getPrefix();
            String atName = attr.getNodeName();
            String atValue = normalize(attr.getNodeValue(), canonical);

            if (atName.equals("xmlns"))
                currentDefaultNamespace = atValue;

            if (atPrefix != null && !atPrefix.equals("xmlns") && !atPrefix.equals("xml")) {
                String nsURI = getNamespaceURI(atPrefix, element, rootNode);
                nsMap.put(atPrefix, nsURI);
                // xsi:type='ns1:SubType', xsi:type='xsd:string'
                if (atName.equals(atPrefix + ":type") && atValue.indexOf(":") > 0) {
                    // xsi defined on the envelope
                    if (nsURI == null)
                        nsURI = getNamespaceURI(atPrefix, element, null);

                    if ("http://www.w3.org/2001/XMLSchema-instance".equals(nsURI)) {
                        String typePrefix = atValue.substring(0, atValue.indexOf(":"));
                        String typeURI = getNamespaceURI(typePrefix, element, rootNode);
                        nsMap.put(typePrefix, typeURI);
                    }
                }
            }

            out.print(" " + atName + "='" + atValue + "'");
        }

        // Add namespace declaration for prefixes
        // that are defined further up the tree
        if (completeNamespaces) {
            Iterator itPrefix = nsMap.keySet().iterator();
            while (itPrefix.hasNext()) {
                String prefix = (String) itPrefix.next();
                String nsURI = (String) nsMap.get(prefix);
                if (nsURI == null) {
                    nsURI = getNamespaceURI(prefix, element, null);
                    out.print(" xmlns:" + prefix + "='" + nsURI + "'");
                }
            }
        }

        // The SAX ContentHandler will by default not add the namespace declaration
        // <Hello xmlns='http://somens'>World</Hello>
        if (elPrefix == null && elNamespaceURI != null) {
            String defaultNamespace = element.getAttribute("xmlns");
            if (defaultNamespace.length() == 0 && !elNamespaceURI.equals(currentDefaultNamespace)) {
                out.print(" xmlns='" + elNamespaceURI + "'");
                currentDefaultNamespace = elNamespaceURI;
            }
        }

        if (hasChildNodes) {
            out.print('>');
        }

        // Find out if the end marker is indented
        indentEndMarker = isEndMarkerIndented(node);

        if (indentEndMarker) {
            out.print('\n');
        }

        NodeList childNodes = node.getChildNodes();
        int len = childNodes.getLength();
        for (int i = 0; i < len; i++) {
            Node childNode = childNodes.item(i);
            printInternal(childNode, false);
        }
        break;
    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        if (canonical) {
            NodeList children = node.getChildNodes();
            if (children != null) {
                int len = children.getLength();
                for (int i = 0; i < len; i++) {
                    printInternal(children.item(i), false);
                }
            }
        } else {
            out.print('&');
            out.print(nodeName);
            out.print(';');
        }
        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        if (canonical) {
            out.print(normalize(node.getNodeValue(), canonical));
        } else {
            out.print("<![CDATA[");
            out.print(node.getNodeValue());
            out.print("]]&gt;");
        }
        break;
    }

    // print text
    case Node.TEXT_NODE: {
        String text = normalize(node.getNodeValue(), canonical);
        if (text.trim().length() > 0) {
            out.print(text);
        } else if (prettyprint == false && ignoreWhitespace == false) {
            out.print(text);
        }
        break;
    }

    // print processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(nodeName);
        String data = node.getNodeValue();
        if (data != null && data.length() > 0) {
            out.print(' ');
            out.print(data);
        }
        out.print("?>");
        break;
    }

    // print comment
    case Node.COMMENT_NODE: {
        for (int i = 0; i < prettyIndent; i++) {
            out.print(' ');
        }

        out.print("<!--");
        String data = node.getNodeValue();
        if (data != null) {
            out.print(data);
        }
        out.print("-->");

        if (prettyprint) {
            out.print('\n');
        }

        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        if (prettyprint)
            prettyIndent--;

        if (hasChildNodes == false) {
            out.print("/>");
        } else {
            if (indentEndMarker) {
                for (int i = 0; i < prettyIndent; i++) {
                    out.print(' ');
                }
            }

            out.print("</");
            out.print(nodeName);
            out.print('>');
        }

        if (prettyIndent > 0) {
            out.print('\n');
        }
    }
    out.flush();
}

From source file:DOMWriter.java

private void printInternal(Node node, boolean indentEndMarker) {
    // is there anything to do?
    if (node == null) {
        return;/*from w ww.  j a  v a 2 s  .  c o  m*/
    }

    // JBAS-2117 - Don't skip the DOCUMENT_NODE
    // if (node instanceof Document) node =
    // ((Document)node).getDocumentElement();

    if (wroteXMLDeclaration == false && writeXMLDeclaration == true && canonical == false) {
        out.print("<?xml version='1.0'");
        if (charsetName != null)
            out.print(" encoding='" + charsetName + "'");

        out.print("?>");
        if (prettyprint)
            out.println();

        wroteXMLDeclaration = true;
    }

    int type = node.getNodeType();
    boolean hasChildNodes = node.getChildNodes().getLength() > 0;

    String nodeName = node.getNodeName();
    switch (type) {
    // print document
    case Node.DOCUMENT_NODE: {
        NodeList children = node.getChildNodes();
        for (int iChild = 0; iChild < children.getLength(); iChild++) {
            printInternal(children.item(iChild), false);
        }
        out.flush();
        break;
    }

    // print element with attributes
    case Node.ELEMENT_NODE: {
        Element element = (Element) node;
        if (prettyprint) {
            for (int i = 0; i < prettyIndent; i++) {
                out.print(' ');
            }
            prettyIndent++;
        }

        out.print('<');
        out.print(nodeName);

        Map nsMap = new HashMap();
        String elPrefix = node.getPrefix();
        String elNamespaceURI = node.getNamespaceURI();
        if (elPrefix != null) {
            String nsURI = getNamespaceURI(elPrefix, element, rootNode);
            nsMap.put(elPrefix, nsURI);
        }

        Attr attrs[] = sortAttributes(node.getAttributes());
        for (int i = 0; i < attrs.length; i++) {
            Attr attr = attrs[i];
            String atPrefix = attr.getPrefix();
            String atName = attr.getNodeName();
            String atValue = normalize(attr.getNodeValue(), canonical);

            if (atName.equals("xmlns"))
                currentDefaultNamespace = atValue;

            if (atPrefix != null && !atPrefix.equals("xmlns") && !atPrefix.equals("xml")) {
                String nsURI = getNamespaceURI(atPrefix, element, rootNode);
                nsMap.put(atPrefix, nsURI);
                // xsi:type='ns1:SubType', xsi:type='xsd:string'
                if (atName.equals(atPrefix + ":type") && atValue.indexOf(":") > 0) {
                    // xsi defined on the envelope
                    if (nsURI == null)
                        nsURI = getNamespaceURI(atPrefix, element, null);

                    if ("http://www.w3.org/2001/XMLSchema-instance".equals(nsURI)) {
                        String typePrefix = atValue.substring(0, atValue.indexOf(":"));
                        String typeURI = getNamespaceURI(typePrefix, element, rootNode);
                        nsMap.put(typePrefix, typeURI);
                    }
                }
            }

            out.print(" " + atName + "='" + atValue + "'");
        }

        // Add namespace declaration for prefixes
        // that are defined further up the tree
        if (completeNamespaces) {
            Iterator itPrefix = nsMap.keySet().iterator();
            while (itPrefix.hasNext()) {
                String prefix = (String) itPrefix.next();
                String nsURI = (String) nsMap.get(prefix);
                if (nsURI == null) {
                    nsURI = getNamespaceURI(prefix, element, null);
                    out.print(" xmlns:" + prefix + "='" + nsURI + "'");
                }
            }
        }

        // The SAX ContentHandler will by default not add the namespace
        // declaration
        // <Hello xmlns='http://somens'>World</Hello>
        if (elPrefix == null && elNamespaceURI != null) {
            String defaultNamespace = element.getAttribute("xmlns");
            if (defaultNamespace.length() == 0 && !elNamespaceURI.equals(currentDefaultNamespace)) {
                out.print(" xmlns='" + elNamespaceURI + "'");
                currentDefaultNamespace = elNamespaceURI;
            }
        }

        if (hasChildNodes) {
            out.print('>');
        }

        // Find out if the end marker is indented
        indentEndMarker = isEndMarkerIndented(node);

        if (indentEndMarker) {
            out.print('\n');
        }

        NodeList childNodes = node.getChildNodes();
        int len = childNodes.getLength();
        for (int i = 0; i < len; i++) {
            Node childNode = childNodes.item(i);
            printInternal(childNode, false);
        }
        break;
    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        if (canonical) {
            NodeList children = node.getChildNodes();
            if (children != null) {
                int len = children.getLength();
                for (int i = 0; i < len; i++) {
                    printInternal(children.item(i), false);
                }
            }
        } else {
            out.print('&');
            out.print(nodeName);
            out.print(';');
        }
        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        if (canonical) {
            out.print(normalize(node.getNodeValue(), canonical));
        } else {
            out.print("<![CDATA[");
            out.print(node.getNodeValue());
            out.print("]]&gt;");
        }
        break;
    }

    // print text
    case Node.TEXT_NODE: {
        String text = normalize(node.getNodeValue(), canonical);
        if (text.trim().length() > 0) {
            out.print(text);
        } else if (prettyprint == false && ignoreWhitespace == false) {
            out.print(text);
        }
        break;
    }

    // print processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(nodeName);
        String data = node.getNodeValue();
        if (data != null && data.length() > 0) {
            out.print(' ');
            out.print(data);
        }
        out.print("?>");
        break;
    }

    // print comment
    case Node.COMMENT_NODE: {
        for (int i = 0; i < prettyIndent; i++) {
            out.print(' ');
        }

        out.print("<!--");
        String data = node.getNodeValue();
        if (data != null) {
            out.print(data);
        }
        out.print("-->");

        if (prettyprint) {
            out.print('\n');
        }

        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        if (prettyprint)
            prettyIndent--;

        if (hasChildNodes == false) {
            out.print("/>");
        } else {
            if (indentEndMarker) {
                for (int i = 0; i < prettyIndent; i++) {
                    out.print(' ');
                }
            }

            out.print("</");
            out.print(nodeName);
            out.print('>');
        }

        if (prettyIndent > 0) {
            out.print('\n');
        }
    }
    out.flush();
}

From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java

private synchronized boolean isEqualNode(final Node original, final Node patch) {
    if (patch == original) {
        return true;
    }/*from w w w.  ja  va 2s.co m*/
    if (patch.getNodeType() != original.getNodeType()) {
        return false;
    }

    if (original.getNodeName() == null) {
        if (patch.getNodeName() != null) {
            return false;
        }
    } else if (!original.getNodeName().equals(patch.getNodeName())) {
        return false;
    }

    if (original.getLocalName() == null) {
        if (patch.getLocalName() != null) {
            return false;
        }
    } else if (!original.getLocalName().equals(patch.getLocalName())) {
        return false;
    }

    if (original.getNamespaceURI() == null) {
        if (patch.getNamespaceURI() != null) {
            return false;
        }
    } else if (!original.getNamespaceURI().equals(patch.getNamespaceURI())) {
        return false;
    }

    if (original.getPrefix() == null) {
        if (patch.getPrefix() != null) {
            return false;
        }
    } else if (!original.getPrefix().equals(patch.getPrefix())) {
        return false;
    }

    if (original.getNodeValue() == null) {
        if (patch.getNodeValue() != null) {
            return false;
        }
    } else if (!original.getNodeValue().equals(patch.getNodeValue())) {
        return false;
    }

    if (original.getTextContent() == null) {
        if (patch.getTextContent() != null) {
            return false;
        }
    } else if (!original.getTextContent().equals(patch.getTextContent())) {
        return false;
    }

    return true;
}

From source file:de.escidoc.core.test.EscidocTestBase.java

/**
 * Gets the prefix of the provided node.<br>
 * This returns Node.getPrefix() if this is not null. Otherwise, ittries to extract the prefix from
 * Node.getNodeName(). If this fails, null is returned.
 * //from w  ww.ja  v a2  s  .c o  m
 * @param node
 *            The node to get the prefix from.
 * @return Returns the determined prefix or null.
 * @throws Exception
 *             Thrown if anything fails.
 */
public static String getPrefix(final Node node) throws Exception {

    String prefix = node.getPrefix();
    if (prefix == null) {
        String nodeName = node.getNodeName();
        int index = nodeName.indexOf(":");
        if (index != -1) {
            prefix = nodeName.substring(0, index);
        }
    }
    return prefix;
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * recursive copy/*from   w  ww  .  jav a  2s . c  o  m*/
 * @param dest element to copy into
 * @param source child element
 */
private void copyNode(MessageElement dest, org.w3c.dom.Node source) {
    dest.setPrefix(source.getPrefix());
    if (source.getLocalName() != null) {
        dest.setQName(new QName(source.getNamespaceURI(), source.getLocalName()));
    } else {
        dest.setQName(new QName(source.getNamespaceURI(), source.getNodeName()));
    }

    NamedNodeMap attrs = source.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Node att = attrs.item(i);
        if (att.getNamespaceURI() != null && att.getPrefix() != null
                && att.getNamespaceURI().equals(Constants.NS_URI_XMLNS) && "xmlns".equals(att.getPrefix())) {
            Mapping map = new Mapping(att.getNodeValue(), att.getLocalName());
            dest.addMapping(map);
        }
        if (att.getLocalName() != null) {
            dest.addAttribute(att.getPrefix(), (att.getNamespaceURI() != null ? att.getNamespaceURI() : ""),
                    att.getLocalName(), att.getNodeValue());
        } else if (att.getNodeName() != null) {
            dest.addAttribute(att.getPrefix(), (att.getNamespaceURI() != null ? att.getNamespaceURI() : ""),
                    att.getNodeName(), att.getNodeValue());
        }
    }

    NodeList children = source.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == TEXT_NODE || child.getNodeType() == CDATA_SECTION_NODE
                || child.getNodeType() == COMMENT_NODE) {
            org.apache.axis.message.Text childElement = new org.apache.axis.message.Text((CharacterData) child);
            dest.appendChild(childElement);
        } else {
            MessageElement childElement = new MessageElement();
            dest.appendChild(childElement);
            copyNode(childElement, child);
        }
    }
}

From source file:org.apache.camel.component.xmlsecurity.api.XAdESSignatureProperties.java

protected void replacePrefix(Element el, Input input) {
    replacePrefixForNode(el, input);/*  ww w. j  a va2 s  .c  o m*/
    NamedNodeMap nnm = el.getAttributes();
    List<Attr> xmlnsToBeRemoved = new ArrayList<Attr>(2);
    int length = nnm.getLength();
    for (int i = 0; i < length; i++) {
        Node attr = nnm.item(i);
        replacePrefixForNode(attr, input);
        if (attr.getNodeType() == Node.ATTRIBUTE_NODE) {
            if ("xmlns".equals(attr.getLocalName()) || "xmlns".equals(attr.getPrefix())) {
                if (XMLSignature.XMLNS.equals(attr.getTextContent())
                        || findNamespace(input.getMessage()).equals(attr.getTextContent())) {
                    xmlnsToBeRemoved.add((Attr) attr);
                }
            }
        }
    }
    // remove xml namespace declaration for XML signature and XAdES namespace
    for (Attr toBeRemoved : xmlnsToBeRemoved) {
        el.removeAttributeNode(toBeRemoved);
    }

}