Example usage for org.w3c.dom Document lookupNamespaceURI

List of usage examples for org.w3c.dom Document lookupNamespaceURI

Introduction

In this page you can find the example usage for org.w3c.dom Document lookupNamespaceURI.

Prototype

public String lookupNamespaceURI(String prefix);

Source Link

Document

Look up the namespace URI associated to the given prefix, starting from this node.

Usage

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

private Attr createAttribute(Node parentNode, Document domDocument) {
    // Ensure xsi prefix is declared
    if (attributeName.indexOf(':') > 0) {
        String attributePrefix = StringUtils.substringBefore(attributeName, ":"); //$NON-NLS-1$
        String namespaceURI = domDocument.lookupNamespaceURI(attributePrefix);
        if (namespaceURI == null) {
            if ("xsi".equals(attributePrefix)) { //$NON-NLS-1$
                domDocument.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                        "xmlns:xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); //$NON-NLS-1$
            } else if ("tmdm".equals(attributePrefix)) { //$NON-NLS-1$
                domDocument.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                        "xmlns:tmdm", SkipAttributeDocumentBuilder.TALEND_NAMESPACE); //$NON-NLS-1$
            } else {
                throw new IllegalArgumentException("Unrecognized attribute prefix: '" + attributePrefix + "'."); //$NON-NLS-1$ //$NON-NLS-2$
            }/*from  www  .ja v a2s.c  o m*/
        }
    }
    QName qName = getQName(domDocument);
    Attr newAttribute = domDocument.createAttributeNS(qName.getNamespaceURI(), qName.getLocalPart());
    String prefix = qName.getPrefix();
    newAttribute.setPrefix(prefix);
    parentNode.getAttributes().setNamedItemNS(newAttribute);
    return newAttribute;
}

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  www  . ja va2s  .  c om*/
    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));
            }
        }
    }
}

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

public void checkLessThanProperty(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  www  .ja v a2s  . 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().compareTo(qVal) < 0);
            }
        }
    }
}

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

public void checkGreaterThanProperty(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 ww 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().compareTo(qVal) >= 0);
            }
        }
    }
}

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

public void checkEqualityProperty(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  .  ja  v  a 2  s. com*/
    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("*"))) {
                // TODO: Determine if OSLC queries are case-sensitive.
                assertTrue(qVal.equalsIgnoreCase(element.getTextContent()));
            }
        }
    }
}

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

private QName getQName(Document domDocument) {
    QName qName;/*from  w  ww .  j av a2  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:org.alfresco.web.forms.xforms.Schema2XForms.java

private Pair<String, String> parseName(String name, Document resolver) {
    int pos = name.indexOf(':');
    String localName = name;/*ww  w. j av a  2 s. c  om*/
    String namespace = null;

    if (pos != -1) {
        localName = name.substring(pos + 1);
        namespace = resolver.lookupNamespaceURI(name.substring(0, pos));
    }

    return new Pair<String, String>(localName, namespace);
}

From source file:org.kuali.rice.ken.xpath.XPathTest.java

@Test
public void testXPathWithValidatedDOMFixedNamespace() throws Exception {
    LOG.debug("TEST");
    Document doc = getDocument(true, true);
    LOG.info("Default namespace: " + doc.lookupNamespaceURI(null));
    XPath xpath = getXPath(null);
    String channelName = (String) xpath.evaluate("/nreq:notification/nreq:channel", doc.getDocumentElement(),
            XPathConstants.STRING);
    assertEquals("Test Channel #1", channelName);
}

From source file:org.kuali.rice.ken.xpath.XPathTest.java

@Test
public void testXPathWithValidatedDOMDocNamespace() throws Exception {
    LOG.debug("TEST");
    Document doc = getDocument(true, true);
    LOG.info("Default namespace: " + doc.lookupNamespaceURI(null));
    LOG.info("default prefix: " + doc.lookupPrefix(doc.lookupNamespaceURI(null)));
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(Util.getNotificationNamespaceContext(doc));
    String channelName = (String) xpath.evaluate("/nreq:notification/nreq:channel", doc.getDocumentElement(),
            XPathConstants.STRING);
    assertEquals("Test Channel #1", channelName);
}