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:ca.mcgill.music.ddmal.mei.MeiXmlReader.java

private MeiElement makeMeiElement(Node element) {
    // TODO: CDATA
    // Comments get a name #comment
    String nshref = element.getNamespaceURI();
    String nsprefix = element.getPrefix();
    MeiNamespace elns = new MeiNamespace(nshref, nsprefix);
    MeiElement e = new MeiElement(elns, element.getNodeName());
    if (element.getNodeType() == Node.COMMENT_NODE) {
        e.setValue(element.getNodeValue());
    }//from  ww  w. java2  s.  com

    NamedNodeMap attributes = element.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Node item = attributes.item(i);
            if (XML_ID_ATTRIBUTE.equals(item.getNodeName())) {
                e.setId(item.getNodeValue());
            } else {
                String attrns = item.getNamespaceURI();
                String attrpre = item.getPrefix();
                MeiNamespace atns = new MeiNamespace(attrns, attrpre);
                MeiAttribute a = new MeiAttribute(atns, item.getNodeName(), item.getNodeValue());
                e.addAttribute(a);
            }
        }
    }

    NodeList childNodes = element.getChildNodes();
    MeiElement lastElement = null;
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.item(i);
        if (item.getNodeType() == Node.TEXT_NODE) {
            if (lastElement == null) {
                e.setValue(item.getNodeValue());
            } else {
                lastElement.setTail(item.getNodeValue());
            }
        } else {
            MeiElement child = makeMeiElement(item);
            e.addChild(child);
            lastElement = child;
        }
    }
    return e;
}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * returns the first child of the contextnode which has the specified tagname and namespace uri regardless of the
 * depth in the tree./*from   w  w  w .j  a va  2s.  c o  m*/
 *
 * @param contextNode where to start the search
 * @param nsuri       the namespace uri
 * @param tag         the local name part of the wanted child
 * @return the first child found under the contextnode
 * todo: should return Element instead of Node
 */
public static Node getFirstChildByTagNameNS(Node contextNode, String nsuri, String tag) {
    Node n = null;

    if (contextNode.getNodeType() == Node.DOCUMENT_NODE) {
        n = ((Document) contextNode).getDocumentElement();

        if (!(n.getNamespaceURI().equals(nsuri) && n.getNodeName().equals(tag))) {
            n = null;
        }
    } else {
        NodeList nodes = ((Element) contextNode).getElementsByTagNameNS(nsuri, tag);

        if (nodes != null) {
            n = nodes.item(0);
        }
    }

    return n;
}

From source file:ddf.mime.mapper.MimeTypeMapperImpl.java

private String getRootElementNamespace(InputStream is) {
    LOGGER.trace("ENTERING: getRootElementNamespace()");

    if (is == null) {
        return null;
    }//from ww  w  .j  a  va2  s . com

    String namespace = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);

    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(is);
        Node node = document.getDocumentElement();
        namespace = node.getNamespaceURI();
    } catch (ParserConfigurationException | SAXException | IOException e) {
        LOGGER.debug("Unable to get root element namespace");
    }

    LOGGER.trace("ENXITING: getRootElementNamespace() - namespace = {}", namespace);

    return namespace;
}

From source file:com.amalto.core.history.accessor.AttributeAccessor.java

public void delete() {
    if (exist()) {
        Node parentNode = parent.getNode();
        Node attribute = getAttribute();
        if (attribute != null) {
            parentNode.getAttributes().removeNamedItemNS(attribute.getNamespaceURI(), attribute.getLocalName());
        } else {//from   w ww. j  a  v a2  s .co m
            logger.warn("Attempt to delete the attribute '" + attributeName + "'that does not exist."); //$NON-NLS-1$ //$NON-NLS-2$
        }
    }
}

From source file:com.vmware.identity.wstrust.client.impl.AcquireTokenByGssResponseHandler.java

/**
 * Helper: Whether response contains an RSTRC
 *
 * @param node/*  w ww .jav a  2  s .c  o m*/
 *            the root of the DOM tree to search for TSTRC.
 * @return true if rstrc is founf false otherwise.
 */
private boolean hasRSTRC(Node node) throws ParserException {

    boolean found = false;
    if ((RSTRC_ELEMENT_NAME.equalsIgnoreCase(node.getLocalName()))
            && RSTRC_NAMESPACE.equalsIgnoreCase(node.getNamespaceURI())) {
        log.debug("Node is the RSTRC.");
        found = true;
    } else {
        NodeList rstrcNodes = ((Element) node).getElementsByTagNameNS(RSTRC_NAMESPACE, RSTRC_ELEMENT_NAME);

        log.debug(
                String.format("Found %d RSTRC elements.", ((rstrcNodes == null) ? 0 : rstrcNodes.getLength())));
        found = (rstrcNodes.getLength() > 0);
    }

    return found;
}

From source file:com.amalto.core.history.accessor.AttributeAccessor.java

private QName getQName(Document domDocument) {
    QName qName;/*from  w  w w.j  a  v  a  2 s  . c om*/
    String prefix = StringUtils.substringBefore(attributeName, ":"); //$NON-NLS-1$
    String name = StringUtils.substringAfter(attributeName, ":"); //$NON-NLS-1$
    if (name.isEmpty()) {
        // No prefix (so prefix is attribute name due to substring calls).
        String attributeNamespaceURI = domDocument.getDocumentURI();
        if (attributeNamespaceURI == null || attributeNamespaceURI.isEmpty()) {
            Node attributeNode = getAttributeNode(domDocument);
            if (attributeNode != null) {
                attributeNamespaceURI = attributeNode.getNamespaceURI();
            }
        }
        qName = new QName(attributeNamespaceURI, prefix);
    } else {
        String attributeNamespaceURI = domDocument.lookupNamespaceURI(prefix);
        if (attributeNamespaceURI == null || attributeNamespaceURI.isEmpty()) {
            Node attributeNode = getAttributeNode(domDocument);
            if (attributeNode != null) {
                attributeNamespaceURI = attributeNode.lookupNamespaceURI(prefix);
            }
        }
        qName = new QName(attributeNamespaceURI, name, prefix);
    }
    return qName;
}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * copies all attributes from one Element to another
 *
 * @param from   - the Element which the source attributes
 * @param to     - the target Element for the Attributes
 * @param filter - a NodeFilter to apply during copy
 *//*from ww w. j a v  a2  s. co m*/
public static void copyAttributes(Element from, Element to, NodeFilter filter) {
    if ((from != null) && (to != null)) {
        NamedNodeMap map = from.getAttributes();

        /* if filter is null use our own default filter, which accepts
           everything (this saves us from always check if filter is
           null */
        if (filter == null) {
            filter = new NodeFilter() {
                public short acceptNode(Node n) {
                    return NodeFilter.FILTER_ACCEPT;
                }
            };
        }

        if (map != null) {
            int len = map.getLength();

            for (int i = 0; i < len; i++) {
                Node attr = map.item(i);

                if (attr.getNodeType() == Node.ATTRIBUTE_NODE) {
                    if (filter.acceptNode(attr) == NodeFilter.FILTER_ACCEPT) {
                        to.setAttributeNS(attr.getNamespaceURI(), attr.getNodeName(), attr.getNodeValue());
                    }
                }
            }
        }
    }
}

From source file:com.nortal.jroad.endpoint.AbstractXTeeBaseEndpoint.java

@SuppressWarnings("unchecked")
protected SOAPElement createXteeMessageStructure(SOAPMessage requestMessage, SOAPMessage responseMessage)
        throws Exception {
    SOAPUtil.addBaseMimeHeaders(responseMessage);
    SOAPUtil.addBaseNamespaces(responseMessage);
    if (!metaService) {
        // Assign xroad namespaces according to request
        List<String> xteeNamespaces = new ArrayList<String>();
        xteeNamespaces.add(version.getNamespaceUri());
        if (XRoadProtocolVersion.V4_0 == version) {
            xteeNamespaces.add(XTeeWsdlDefinition.XROAD_IDEN_NAMESPACE);
        }/*from www . ja va  2  s.  com*/

        Iterator<String> prefixes = requestMessage.getSOAPPart().getEnvelope().getNamespacePrefixes();
        while (prefixes.hasNext()) {
            String nsPrefix = (String) prefixes.next();
            String nsURI = requestMessage.getSOAPPart().getEnvelope().getNamespaceURI(nsPrefix).toLowerCase();
            if (xteeNamespaces.contains(nsURI)) {
                SOAPUtil.addNamespace(responseMessage, nsPrefix, nsURI);
            }
        }

        // Copy headers from request
        NodeList reqHeaders = requestMessage.getSOAPHeader().getChildNodes();
        for (int i = 0; i < reqHeaders.getLength(); i++) {
            Node reqHeader = reqHeaders.item(i);
            if (reqHeader.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            Node rspHeader = responseMessage.getSOAPPart().importNode(reqHeader, true);
            responseMessage.getSOAPHeader().appendChild(rspHeader);
        }
    }
    responseMessage.getSOAPPart().getEnvelope().setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");

    Node teenusElement = SOAPUtil.getFirstNonTextChild(requestMessage.getSOAPBody());
    if (teenusElement.getPrefix() == null || teenusElement.getNamespaceURI() == null) {
        throw new IllegalStateException("Service request is missing namespace.");
    }
    SOAPUtil.addNamespace(responseMessage, teenusElement.getPrefix(), teenusElement.getNamespaceURI());

    String teenusElementName = teenusElement.getLocalName();
    if (teenusElementName.endsWith(SuffixBasedMessagesProvider.DEFAULT_REQUEST_SUFFIX)) {
        teenusElementName = teenusElementName.substring(0,
                teenusElementName.lastIndexOf(SuffixBasedMessagesProvider.DEFAULT_REQUEST_SUFFIX));
    }
    teenusElementName += SuffixBasedMessagesProvider.DEFAULT_RESPONSE_SUFFIX;
    return responseMessage.getSOAPBody().addChildElement(teenusElementName, teenusElement.getPrefix(),
            teenusElement.getNamespaceURI());
}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * returns a canonical XPath locationpath for a given Node. Each step in the path will contain the positional
 * predicate of the Element. Example '/root[1]/a[1]/b[2]/c[5]/@d'. This would point to<br/>
 * to Attribute named 'd'<br/>//w  w  w  .  j a v a  2s  .co  m
 * on 5th Element 'c"<br/>
 * on 2nd Element 'b'<br/>
 * on first Element a<br/>
 * which is a child of the Document Element.
 *
 * @param node the Node where to start
 * @return canonical XPath locationPath for given Node or the empty string if node is null
 */
public static String getCanonicalPath(Node node) {

    if (node == null) {
        return "";
    }

    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        return "/";
    }

    //add ourselves
    String canonPath;
    String ns = node.getNamespaceURI();
    String nodeName1 = node.getNodeName();
    String nodeName2 = node.getLocalName();
    if (ns != null && ns.equals("http://www.w3.org/1999/xhtml")
            && node.getNodeName().equals(node.getLocalName())) {
        canonPath = "html:" + node.getNodeName();
    } else {
        canonPath = node.getNodeName();
    }
    if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        canonPath = "@" + canonPath;
    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
        int position = DOMUtil.getCurrentNodesetPosition(node);
        //append position if we are an Element
        canonPath += "[" + position + "]";
    }

    //check for parent - if there's none we're root
    Node parent = null;
    if (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.TEXT_NODE) {
        parent = node.getParentNode();
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        parent = ((Attr) node).getOwnerElement();
    }
    if (parent == null) {
        parent = node.getOwnerDocument().getDocumentElement();
    }
    if (parent.getNodeType() == Node.DOCUMENT_NODE || parent.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) {
        canonPath = "/" + canonPath;
    } else {
        canonPath = DOMUtil.getCanonicalPath(parent) + "/" + canonPath;
    }

    return canonPath;
}

From source file:org.eclipse.lyo.testsuite.oslcv2.QueryTests.java

public void checkInequalityProperty(NodeList resultList, String queryProperty, String qVal, Document doc) {
    String queryPropertyNS = "*";
    String queryPropertyName = queryProperty;
    if (queryProperty.contains(":")) {
        queryPropertyNS = queryProperty.substring(0, queryProperty.indexOf(':'));
        queryPropertyName = queryProperty.substring(queryProperty.indexOf(':') + 1);
    }/*from  w  w  w. j  a  v a 2 s. c  o m*/
    for (int i = 0; i < resultList.getLength(); i++) {
        NodeList elements = resultList.item(i).getChildNodes();
        for (int j = 0; j < elements.getLength(); j++) {
            Node element = elements.item(j);
            if (element.getLocalName() != null && element.getLocalName().equals(queryPropertyName)
                    && (element.getNamespaceURI().equals(doc.lookupNamespaceURI(queryPropertyNS))
                            || queryPropertyNS.equals("*"))) {
                assertTrue(!element.getTextContent().equals(qVal));
            }
        }
    }
}