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.regenstrief.util.XMLUtil.java

/**
 * Imports a Node into a Document without creating new prefixes like Document.importNode
 * /*w  w  w  . j  a  v a 2s.c  o m*/
 * @param doc the Document
 * @param toImport the Node to import
 * @return the imported Node
 **/
public final static Node importNode(final Document doc, final Node toImport) {
    Node imported;
    NodeList list;
    NamedNodeMap map;
    int i, size;

    if (toImport instanceof Element) {
        //imported = doc.createElement(((Element) toImport).getTagName()); // Ever want this?
        // This should copy toImport's prefix; I don't think that's what we want, but we don't have a namespace context for doc
        imported = doc.createElementNS(toImport.getNamespaceURI(), toImport.getNodeName());
        for (map = toImport.getAttributes(), size = size(map), i = 0; i < size; i++) {
            final Node n = map.item(i);
            if (n != null) {
                ((Element) imported).setAttributeNode((Attr) importNode(doc, n));
            }
        }
    } else if (toImport instanceof Attr) {
        final String uri = toImport.getNamespaceURI();
        if (Util.isEmpty(uri)) {
            imported = doc.createAttribute(
                    "xmlns".equals(getPrefix(toImport)) ? toImport.getNodeName() : getLocalName(toImport));
        } else {
            imported = doc.createAttributeNS(uri, toImport.getNodeName());
        }
        //imported.setNodeValue(toImport.getNodeValue()); // The value will be copied when we import children below
    } else {
        imported = doc.importNode(toImport, false);
    }
    for (list = toImport.getChildNodes(), size = size(list), i = 0; i < size; i++) {
        final Node n = list.item(i);
        if (n != null) {
            imported.appendChild(importNode(doc, n));
        }
    }

    return imported;
}

From source file:org.regenstrief.util.XMLUtil.java

/**
 * Retrieves the namespace for the given Node and prefix
 * /*from w ww.  ja v a2  s. co  m*/
 * @param n the the Node
 * @param xmlnsPlusPrefix the xmlns[:<prefix>] String
 * @return the namespace String
 **/
private final static String getNamespaceURIIntern(final Node n, final String xmlnsPlusPrefix) {
    if (n == null) {
        return null;
    }

    final String prefix = getPrefix(n);
    if (prefix == null ? (xmlnsPlusPrefix.length() == 5)
            : (xmlnsPlusPrefix.endsWith(prefix) && (xmlnsPlusPrefix.charAt(5) == ':'))) {
        final String namespaceURI = n.getNamespaceURI();
        if (namespaceURI != null) {
            return namespaceURI;
        }
        //if (n instanceof Attr)
        //{   return NAMESPACE_URI_XMLNS;
        //}
    }

    if (!(n instanceof Element)
            || ((n instanceof Element) && Util.isEmpty(((Element) n).getAttribute(xmlnsPlusPrefix)))) {
        return getNamespaceURIIntern(n.getParentNode(), xmlnsPlusPrefix);
    }

    return ((Element) n).getAttribute(xmlnsPlusPrefix);
}

From source file:org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java

/**
 * Get the namespace URI for the supplied node.
 * <p>The default implementation uses {@link Node#getNamespaceURI}.
 * Subclasses may override the default implementation to provide a
 * different namespace identification mechanism.
 * @param node the node//from w  ww  .j  a va  2 s.  c  om
 */
@Nullable
public String getNamespaceURI(Node node) {
    return node.getNamespaceURI();
}

From source file:org.talend.commons.runtime.xml.XPathPrefixHandler.java

/**
 * DOC chuang Comment method "getNodeInfo".
 * /*w w  w .jav a  2s.c  o m*/
 * @param node
 * @param list
 */
private NodeInfo getNodeInfo(Node node, List<NodeInfo> list) {
    NodeInfo info = null;

    for (int i = 0; i < list.size(); i++) {

        if (list.get(i).namespace.equals(node.getNamespaceURI())) {
            info = list.get(i);
            break;
        } else if (StringUtils.isEmpty(list.get(i).namespace) && StringUtils.isEmpty(node.getNamespaceURI())) {
            info = list.get(i);
            break;
        }
    }
    return info;
}

From source file:org.unitedinternet.cosmo.util.DomWriter.java

public static String write(Node n) throws XMLStreamException, IOException {
    XMLStreamWriter writer = null;
    try {//from w w  w .java 2 s .c o  m
        if ((n.getNamespaceURI() != null && n.getAttributes().getLength() > 0)
                && (n.getNamespaceURI().equals(n.getAttributes().item(0).getNodeValue()))) {
            n.getAttributes().removeNamedItem(n.getAttributes().item(0).getNodeName());
        }

        StringWriter out = new StringWriter();
        writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
        writeNode(n, writer);
        writer.close();
        return out.toString();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (XMLStreamException e2) {
                LOG.warn("Unable to close XML writer", e2);
            }
        }
    }
}

From source file:org.wso2.balana.ctx.RequestCtxFactory.java

/**
 *  Returns instance of <code>AbstractRequestCtx</code> based one the XACML version.
 *
 * @param root  the node to parse for the <code>AbstractRequestCtx</code>
 * @return <code>AbstractRequestCtx</code> object
 * @throws org.wso2.balana.ParsingException  if the DOM node is invalid
 *///from   w  w  w .  ja va2 s . co  m
public AbstractRequestCtx getRequestCtx(Node root) throws ParsingException {

    String requestCtxNs = root.getNamespaceURI();

    if (requestCtxNs != null) {
        if (XACMLConstants.REQUEST_CONTEXT_3_0_IDENTIFIER.equals(requestCtxNs.trim())) {
            return RequestCtx.getInstance(root);
        } else if (XACMLConstants.REQUEST_CONTEXT_1_0_IDENTIFIER.equals(requestCtxNs.trim())
                || XACMLConstants.REQUEST_CONTEXT_2_0_IDENTIFIER.equals(requestCtxNs.trim())) {
            return org.wso2.balana.ctx.xacml2.RequestCtx.getInstance(root);
        } else {
            throw new ParsingException("Invalid namespace in XACML request");
        }
    } else {
        log.warn("No Namespace defined in XACML request and Assume as XACML 3.0");
        return RequestCtx.getInstance(root);
    }
}

From source file:org.wso2.balana.ctx.RequestCtxFactory.java

/**
 *  Returns instance of <code>AbstractRequestCtx</code> based one the XACML version.
 *
 * @param request  the String to parse for the <code>AbstractRequestCtx</code>
 * @return <code>AbstractRequestCtx</code> object
 * @throws ParsingException  if the request is invalid
 *///from w  ww.j  a v a 2s  . c  om
public AbstractRequestCtx getRequestCtx(String request) throws ParsingException {

    Node root = getXacmlRequest(request);
    String requestCtxNs = root.getNamespaceURI();

    if (requestCtxNs != null) {
        if (XACMLConstants.REQUEST_CONTEXT_3_0_IDENTIFIER.equals(requestCtxNs.trim())) {
            return RequestCtx.getInstance(root);
        } else if (XACMLConstants.REQUEST_CONTEXT_1_0_IDENTIFIER.equals(requestCtxNs.trim())
                || XACMLConstants.REQUEST_CONTEXT_2_0_IDENTIFIER.equals(requestCtxNs.trim())) {
            return org.wso2.balana.ctx.xacml2.RequestCtx.getInstance(root);
        } else {
            throw new ParsingException("Invalid namespace in XACML request");
        }
    } else {
        log.warn("No Namespace defined in XACML request and Assume as XACML 3.0");
        return RequestCtx.getInstance(root);
    }
}

From source file:org.wso2.balana.ctx.RequestCtxFactory.java

/**
 *  Returns instance of <code>AbstractRequestCtx</code> based one the XACML version.
 *
 * Creates a new <code>RequestCtx</code> by parsing XML from an input stream. Note that this a
 * convenience method, and it will not do schema validation by default. You should be parsing
 * the data yourself, and then providing the root node to the other <code>getInstance</code>
 * method. If you use this convenience method, you probably want to turn on validation by
 * setting the context schema file (see the programmer guide for more information on this).
 *
 * @param input input a stream providing the XML data
 * @return <code>AbstractRequestCtx</code> object
 * @throws ParsingException  if the DOM node is invalid
 *///from   w w  w .  ja v a  2  s  .  c o m
public AbstractRequestCtx getRequestCtx(InputStream input) throws ParsingException {

    Node root = InputParser.parseInput(input, "Request");
    String requestCtxNs = root.getNamespaceURI();

    if (requestCtxNs != null) {
        if (XACMLConstants.REQUEST_CONTEXT_3_0_IDENTIFIER.equals(requestCtxNs.trim())) {
            return RequestCtx.getInstance(root);
        } else if (XACMLConstants.REQUEST_CONTEXT_1_0_IDENTIFIER.equals(requestCtxNs.trim())
                || XACMLConstants.REQUEST_CONTEXT_2_0_IDENTIFIER.equals(requestCtxNs.trim())) {
            return org.wso2.balana.ctx.xacml2.RequestCtx.getInstance(root);
        } else {
            throw new ParsingException("Invalid namespace in XACML request");
        }
    } else {
        log.warn("No Namespace defined in XACML request and Assume as XACML 3.0");
        return RequestCtx.getInstance(root);
    }
}

From source file:org.wso2.carbon.apimgt.impl.soaptorest.WSDL11SOAPOperationExtractor.java

private void setNamespaceDetails(ModelImpl model, Node currentNode) {

    Xml xml = new Xml();
    xml.setNamespace(currentNode.getNamespaceURI());
    xml.setPrefix(currentNode.getPrefix());
    model.setXml(xml);/*from   ww w. ja v  a 2  s  .com*/
}

From source file:org.wso2.carbon.apimgt.impl.soaptorest.WSDL11SOAPOperationExtractor.java

private void setNamespaceDetails(Property property, Node currentNode) {

    Xml xml = new Xml();
    xml.setNamespace(currentNode.getNamespaceURI());
    xml.setPrefix(currentNode.getPrefix());
    property.setXml(xml);//from   ww w  .  j a v a 2 s.c o  m
}