Example usage for org.w3c.dom Node getNamespaceURI

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

Introduction

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

Prototype

public String getNamespaceURI();

Source Link

Document

The namespace URI of this node, or null if it is unspecified (see ).

Usage

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;/*from ww  w .j  ava2s.  c  o m*/
    // 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

/**
 * Tells whether the namespace URI of the specified node matches the given namespace URI.
 * @param node the node to examine//from  w  ww  .  j  a v  a  2 s . c o m
 * @param namespaceURI the namespace URI to match
 * @return <code>true</code> if the namespace URI matches, <code>false</code> otherwise
 */
public static boolean nodeNamespaceURIEquals(Node node, String namespaceURI) {
    String nodeNamespaceURI = node.getNamespaceURI();
    return nodeNamespaceURI == null || nodeNamespaceURI.length() == 0
            ? namespaceURI == null || namespaceURI.length() == 0
            : nodeNamespaceURI.equals(namespaceURI);
}

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;//w  ww . j  a v a  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;//from w w  w  .j a  va2s. c om
    // 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.jbpm.bpel.xml.util.XmlUtil.java

private static void findLocalNamespaceDeclarations(Element elem, Map namespaces) {
    // traverse attributes to discover namespace declarations
    NamedNodeMap attributes = elem.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 prefix = attribute.getLocalName();

        // exclude default and overriden namespace declarations
        if ("xmlns".equals(prefix) || namespaces.containsKey(prefix))
            continue;

        String namespaceURI = attribute.getNodeValue();
        namespaces.put(prefix, namespaceURI);
    }// w ww  . jav  a 2s.co m
}

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

/**
 * Retrieves the prefix associated with a namespace URI in the given context node.
 * @param namespaceURI the namespace whose prefix is required
 * @param contextNode the node where to search for namespace declarations
 * @return the prefix associated with the namespace URI; the empty string indicates the default
 * namespace, while <code>null</code> indicates no association
 *///from   ww  w  . j av  a 2 s. co  m
public static String getPrefix(String namespaceURI, Node contextNode) {
    switch (contextNode.getNodeType()) {
    case Node.ATTRIBUTE_NODE:
        contextNode = ((Attr) contextNode).getOwnerElement();
        break;
    case Node.ELEMENT_NODE:
        break;
    default:
        contextNode = contextNode.getParentNode();
    }

    while (contextNode != null && contextNode.getNodeType() == Node.ELEMENT_NODE) {
        NamedNodeMap attributes = contextNode.getAttributes();
        for (int i = 0, n = attributes.getLength(); i < n; i++) {
            Node attr = attributes.item(i);

            // is attribute a namespace declaration and matches the given URI?
            if (BpelConstants.NS_XMLNS.equals(attr.getNamespaceURI())
                    && namespaceURI.equals(attr.getNodeValue())) {
                String prefix = attr.getLocalName();
                return "xmlns".equals(prefix) ? "" : prefix;
            }
        }
        contextNode = contextNode.getParentNode();
    }
    return null;
}

From source file:org.kalypsodeegree.xml.XMLTools.java

/**
 * inserts a node into a dom element (of a different dom document)
 *//*  ww w . ja  v a  2s  .c  om*/
public static Node insertNodeInto(final Node source, final Node dest) {

    Document dDoc = null;
    final Document sDoc = source.getOwnerDocument();

    if (dest instanceof Document) {
        dDoc = (Document) dest;
    } else {
        dDoc = dest.getOwnerDocument();
    }

    if (dDoc.equals(sDoc)) {
        dest.appendChild(source);
    } else {
        final Element element = dDoc.createElementNS(source.getNamespaceURI(), source.getNodeName());
        dest.appendChild(element);

        copyNode(source, element);
    }

    return dest;
}

From source file:org.kalypsodeegree_impl.graphics.sld.SLDFactory.java

private static SortedMap<Double, ColorMapEntry> createColorMap(final Element colorMapElement) {
    final SortedMap<Double, ColorMapEntry> colorMap = new TreeMap<>();
    if (colorMapElement == null)
        return null;

    final NodeList entryNodes = colorMapElement.getChildNodes();
    for (int i = 0; i < entryNodes.getLength(); i++) {
        final Node entryNode = entryNodes.item(i);
        final String nodeName = entryNode.getNodeName();
        final String namespaceUri = entryNode.getNamespaceURI();

        if (entryNode instanceof Element && NS.SLD.equals(namespaceUri) && "ColorMapEntry".equals(nodeName)) //$NON-NLS-1$
        {/*from   w  w  w.  j  a  v a2s . com*/
            final Element entryElement = (Element) entryNode;
            final String colorAttribute = entryElement.getAttribute("color"); //$NON-NLS-1$
            final String opacityAttribite = entryElement.getAttribute("opacity"); //$NON-NLS-1$
            final String quantityAttribute = entryElement.getAttribute("quantity"); //$NON-NLS-1$
            final String labelAttribute = entryElement.getAttribute("label"); //$NON-NLS-1$

            final Color color = Color.decode(colorAttribute);
            final Double opacity = NumberUtils.toDouble(opacityAttribite, 1.0);
            final Double quantity = NumberUtils.toDouble(quantityAttribute, Double.NaN);
            final String label = StringUtils.trimToEmpty(labelAttribute);

            if (Double.isNaN(quantity))
                System.err.println(
                        String.format("SLD-Parser: skipping quantity '%s', not a number.", quantityAttribute));
            else {
                final ColorMapEntry colorMapEntryObject = new ColorMapEntry_Impl(color, opacity, quantity,
                        label);
                colorMap.put(quantity, colorMapEntryObject);
            }
        }
    }

    return colorMap;
}

From source file:org.kepler.kar.karxml.KarXml.java

/**
 * Validate the document against the schema. Currently we only validate against
 * kar xml 2.0.0 and 2.1.0. If it is not a kar xml 2.0.0 or 2.1.0 xml, this method will return true.
 * @param document  the document need to be validate
 * @return true if it is a valid document
 *//*from  ww w. ja  v a2  s. c  o  m*/
public static boolean validateDocument(Document document) {
    if (document == null) {
        return false;
    }
    try {
        Node docElement = document.getDocumentElement();
        String nameSpace = docElement.getNamespaceURI();
        log.debug("The name space is ===== " + nameSpace);

        if (nameSpace == null || !nameSpace.equals(KARFile.KAR_VERSION_200_NAMESPACE_DEFAULT)
                || !nameSpace.equals(KARFile.KAR_VERSION_210_NAMESPACE_DEFAULT)) {
            return true;
        }
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        String resourceDir = KARFile.getResourceDir(nameSpace);
        String resourceFileName = KARFile.getResourceFileName(nameSpace);
        // ClassLoader.getResource javadoc says: 
        // The name of a resource is a '/'-separated path name that identifies the resource.
        // so I am using a hardcode / in this path:
        Source schemaFile = new StreamSource(
                KarXml.class.getClassLoader().getResourceAsStream(resourceDir + "/" + resourceFileName));
        Schema schema = factory.newSchema(schemaFile);
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(document));
    } catch (SAXException ex) {
        ex.printStackTrace();
        return false;
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    }
    log.debug("return true");
    return true;
}

From source file:org.kisoonlineapp.XthmlCheckTest.java

/**
 * Huebsches String-Format fuer einen Node
 *
 * @param n//  w  w w . j  a  va  2 s . c  o  m
 * @return human readable repr. of a Node
 */
private String formatNode(Node n) {
    final String SEP = "\n";
    StringBuilder sb = new StringBuilder();
    sb.append(n.getNodeName()).append(SEP).append(n.getNodeValue()).append(SEP).append(n.getNodeType())
            .append(SEP).append(n.getBaseURI()).append(SEP).append(n.getLocalName()).append(SEP)
            .append(n.getNamespaceURI()).append(SEP).append(n.getPrefix()).append(SEP);
    String formatNode = sb.toString();
    return formatNode;
}