Example usage for org.w3c.dom Document getElementsByTagNameNS

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

Introduction

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

Prototype

public NodeList getElementsByTagNameNS(String namespaceURI, String localName);

Source Link

Document

Returns a NodeList of all the Elements with a given local name and namespace URI in document order.

Usage

From source file:be.e_contract.mycarenet.xkms2.KeyBindingAuthenticationSignatureSOAPHandler.java

@Override
public boolean handleMessage(SOAPMessageContext context) {
    Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (false == outboundProperty) {
        return true;
    }//  w  ww.  j  a  va 2s.c o m
    LOG.debug("adding key binding authentication signature");
    SOAPMessage soapMessage = context.getMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String requestElementName;
    if (null != this.prototypeKeyBindingId) {
        requestElementName = "RegisterRequest";
        this.referenceUri = "#" + this.prototypeKeyBindingId;
    } else if (null != this.revokeKeyBindingId) {
        requestElementName = "RevokeRequest";
        this.referenceUri = "#" + this.revokeKeyBindingId;
    } else {
        LOG.error("missing key binding id");
        return false;
    }
    NodeList requestNodeList = soapPart.getElementsByTagNameNS(XKMS2ServiceFactory.XKMS2_NAMESPACE,
            requestElementName);
    Element requestElement = (Element) requestNodeList.item(0);
    if (null == requestElement) {
        LOG.error("request element not present");
        return false;
    }
    Document xkmsDocument;
    try {
        xkmsDocument = copyDocument(requestElement);
    } catch (ParserConfigurationException e) {
        LOG.error("error copying XKMS request: " + e.getMessage(), e);
        return false;
    }

    NodeList keyBindingAuthenticationNodeList = xkmsDocument
            .getElementsByTagNameNS(XKMS2ServiceFactory.XKMS2_NAMESPACE, "KeyBindingAuthentication");
    Element keyBindingAuthenticationElement = (Element) keyBindingAuthenticationNodeList.item(0);
    try {
        prepareDocument(xkmsDocument);
        addSignature(keyBindingAuthenticationElement);
    } catch (Exception e) {
        LOG.error("error adding authn signature: " + e.getMessage(), e);
        return false;
    }

    Node signatureNode = soapPart.importNode(keyBindingAuthenticationElement.getFirstChild(), true);

    keyBindingAuthenticationNodeList = soapPart.getElementsByTagNameNS(XKMS2ServiceFactory.XKMS2_NAMESPACE,
            "KeyBindingAuthentication");
    keyBindingAuthenticationElement = (Element) keyBindingAuthenticationNodeList.item(0);
    keyBindingAuthenticationElement.appendChild(signatureNode);
    return true;
}

From source file:org.eclipse.lyo.testsuite.server.oslcv1tests.QueryTests.java

@Test
public void validEqualsTypeQueryContainsExpectedDefect()
        throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
    String query = getQueryBase();
    query = query + "oslc_cm.query=" + queryProperty
            + URLEncoder.encode("=\"" + queryPropertyValue + "\"", "UTF-8") + "&oslc_cm.properties="
            + queryProperty;//w  ww  . j  av a2  s . c o  m
    //Get the response
    HttpResponse resp = OSLCUtils.getResponseFromUrl(baseUrl, currentUrl + query, basicCreds,
            "application/xml");
    String respBody = EntityUtils.toString(resp.getEntity());
    Document doc = OSLCUtils.createXMLDocFromResponseBody(respBody);
    //Check for the expected result
    NodeList lst = doc.getElementsByTagNameNS("*", queryProperty);
    lst = (lst.getLength() == 0) ? doc.getElementsByTagName(queryProperty) : lst;
    assertTrue(lst.getLength() > 0);
    boolean containsExpectedDefectResults = true;
    for (int i = 0; i < lst.getLength(); i++) {
        if (!lst.item(i).getTextContent().equals(queryPropertyValue)) {
            containsExpectedDefectResults = false;
        }
    }
    assertTrue(containsExpectedDefectResults);
}

From source file:org.eclipse.lyo.testsuite.server.oslcv1tests.QueryTests.java

@Test
public void validLessThanQueryContainsExpectedDefects() throws IOException, SAXException,
        ParserConfigurationException, XPathExpressionException, ParseException {
    String query = getQueryBase();
    query = query + "oslc_cm.query=" + queryComparisonProperty
            + URLEncoder.encode("<=\"" + queryComparisonValue + "\"", "UTF-8") + "&"
            + URLEncoder.encode("oslc_cm.properties", "UTF-8") + "=" + queryComparisonProperty;

    //Get response
    HttpResponse resp = OSLCUtils.getResponseFromUrl(baseUrl, currentUrl + query, basicCreds,
            "application/xml");
    String respBody = EntityUtils.toString(resp.getEntity());
    Document doc = OSLCUtils.createXMLDocFromResponseBody(respBody);

    //Verify that all returned items were modified before yesterday
    NodeList lst = doc.getElementsByTagNameNS("*", queryComparisonProperty);
    lst = (lst.getLength() == 0) ? doc.getElementsByTagName(queryComparisonProperty) : lst;
    assertTrue(lst.getLength() > 0);//from  w  w w  . ja  v  a 2 s.co m
    // TODO: How to validate the list is what is expected?
}

From source file:org.eclipse.lyo.testsuite.server.oslcv1tests.QueryTests.java

@Test
public void validGreaterThanQueriesContainExpectedDefects() throws IOException, SAXException,
        ParserConfigurationException, XPathExpressionException, ParseException {
    String query = getQueryBase();
    query = query + "oslc_cm.query=" + queryComparisonProperty
            + URLEncoder.encode(">=\"" + queryComparisonValue + "\"", "UTF-8") + "&"
            + URLEncoder.encode("oslc_cm.properties", "UTF-8") + "=" + queryComparisonProperty;

    //Get response
    HttpResponse resp = OSLCUtils.getResponseFromUrl(baseUrl, currentUrl + query, basicCreds,
            "application/xml");
    String respBody = EntityUtils.toString(resp.getEntity());
    Document doc = OSLCUtils.createXMLDocFromResponseBody(respBody);

    //Verify that all returned items were modified before yesterday
    NodeList lst = doc.getElementsByTagNameNS("*", queryComparisonProperty);
    lst = (lst.getLength() == 0) ? doc.getElementsByTagName(queryComparisonProperty) : lst;
    assertTrue(lst.getLength() > 0);/*from w w  w .j  av a 2s . co  m*/
    for (int i = 0; i < lst.getLength(); i++) {
        assertTrue(lst.item(i).getTextContent().compareTo(queryComparisonValue) >= 0);
    }
}

From source file:org.eclipse.lyo.testsuite.server.oslcv1tests.QueryTests.java

@Test
public void validNotEqualQueryContainsExpectedDefect()
        throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
    String query = getQueryBase();
    query = query + "oslc_cm.query=" + queryProperty
            + URLEncoder.encode("!=\"" + queryPropertyValue + "\"", "UTF-8") + "&oslc_cm.properties="
            + queryProperty;//  w  ww  .  ja va  2  s . co  m

    //Get response
    HttpResponse resp = OSLCUtils.getResponseFromUrl(baseUrl, currentUrl + query, basicCreds,
            "application/xml");
    String respBody = EntityUtils.toString(resp.getEntity());
    Document doc = OSLCUtils.createXMLDocFromResponseBody(respBody);

    //verify that the results did not contain entries whose property = propertyValue
    NodeList lst = doc.getElementsByTagNameNS("*", queryProperty);
    for (int i = 0; i < lst.getLength(); i++) {
        assertFalse(lst.item(i).getTextContent().equals(queryPropertyValue));
    }
}

From source file:net.sf.jabref.logic.msbib.MSBibDatabase.java

public List<BibEntry> importEntries(BufferedReader reader) {
    entries = new HashSet<>();
    Document inputDocument;
    try {/*  w  w w.  j a v a2s  .  c  o  m*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        inputDocument = documentBuilder.parse(new InputSource(reader));
    } catch (ParserConfigurationException | SAXException | IOException e) {
        LOGGER.warn("Could not parse document", e);
        return Collections.emptyList();
    }
    NodeList rootList = inputDocument.getElementsByTagNameNS("*", "Sources");
    if (rootList.getLength() == 0) {
        rootList = inputDocument.getElementsByTagNameNS("*", "Sources");
    }
    List<BibEntry> bibitems = new ArrayList<>();
    if (rootList.getLength() == 0) {
        return bibitems;
    }

    NodeList sourceList = ((Element) rootList.item(0)).getElementsByTagNameNS("*", "Source");
    for (int i = 0; i < sourceList.getLength(); i++) {
        MSBibEntry entry = new MSBibEntry((Element) sourceList.item(i));
        entries.add(entry);
        bibitems.add(BibTeXConverter.convert(entry));
    }

    return bibitems;
}

From source file:org.eclipse.lyo.testsuite.server.oslcv1tests.QueryTests.java

@Test
public void validCompoundQueryContainsExpectedDefect()
        throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
    String query = getQueryBase();
    query = query + "oslc_cm.query="
            + URLEncoder.encode(queryProperty + "=\"" + queryPropertyValue + "\" and " + queryComparisonProperty
                    + ">=\"" + queryComparisonValue + "\"", "UTF-8")
            + "&" + URLEncoder.encode("oslc_cm.properties", "UTF-8") + "=" + queryProperty + ","
            + queryComparisonProperty;/*from w  w  w .java 2 s  .  c o m*/

    //Get response
    HttpResponse resp = OSLCUtils.getResponseFromUrl(baseUrl, currentUrl + query, basicCreds,
            "application/xml");
    String respBody = EntityUtils.toString(resp.getEntity());
    Document doc = OSLCUtils.createXMLDocFromResponseBody(respBody);

    //Make sure each entry has a matching property element with a value that matches the query
    NodeList lst = doc.getElementsByTagNameNS("*", queryProperty);
    lst = (lst.getLength() == 0) ? doc.getElementsByTagName(queryProperty) : lst;
    assertTrue(lst.getLength() > 0);
    for (int i = 0; i < lst.getLength(); i++) {
        assertTrue(lst.item(i).getTextContent().equals(queryPropertyValue));
    }
    lst = doc.getElementsByTagNameNS("*", queryComparisonProperty);
    lst = (lst.getLength() == 0) ? doc.getElementsByTagName(queryComparisonProperty) : lst;
    assertTrue(lst.getLength() > 0);
    for (int i = 0; i < lst.getLength(); i++) {
        assertTrue(lst.item(i).getTextContent().compareTo(queryComparisonValue) >= 0);
    }
}

From source file:org.eclipse.lyo.testsuite.server.oslcv1tests.QueryTests.java

@Test
public void validTypeQueryReturnsCorrectType()
        throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
    String query = getQueryBase();
    query = query + "oslc_cm.query=" + queryProperty
            + URLEncoder.encode("=\"" + queryPropertyValue + "\"", "UTF-8") + "&"
            + URLEncoder.encode("oslc_cm.properties", "UTF-8") + "=" + queryProperty;

    //Get response
    HttpResponse resp = OSLCUtils.getResponseFromUrl(baseUrl, currentUrl + query, basicCreds,
            OSLCConstants.CT_XML);//from  w w  w  . j  a  v  a2  s  .  c  o m
    String respBody = EntityUtils.toString(resp.getEntity());
    Document doc = OSLCUtils.createXMLDocFromResponseBody(respBody);

    //Make sure each entry has a matching property element with a value that matches the query
    NodeList propertyEntries = doc.getElementsByTagNameNS("*", queryProperty);
    propertyEntries = (propertyEntries.getLength() == 0) ? doc.getElementsByTagName(queryProperty)
            : propertyEntries;
    assertTrue(propertyEntries.getLength() > 0);
    for (int i = 0; i < propertyEntries.getLength(); i++) {
        assertTrue(propertyEntries.item(i).getTextContent().equals(queryPropertyValue));
    }
}

From source file:com.marklogic.client.impl.CombinedQueryBuilderImpl.java

private CombinedQueryDefinitionImpl parseCombinedQuery(RawCombinedQueryDefinition qdef) {
    DOMHandle handle = new DOMHandle();
    HandleAccessor.receiveContent(handle, HandleAccessor.contentAsString(qdef.getHandle()));
    Document combinedQueryXml = handle.get();
    DOMImplementationLS domImplementation = (DOMImplementationLS) combinedQueryXml.getImplementation();
    LSSerializer lsSerializer = domImplementation.createLSSerializer();
    lsSerializer.getDomConfig().setParameter("xml-declaration", false);

    NodeList nl = combinedQueryXml.getElementsByTagNameNS("http://marklogic.com/appservices/search", "options");
    Node n = nl.item(0);//from  w ww  . j  a va 2 s .com
    String options = null;
    StringHandle optionsHandle = null;
    if (n != null) {
        options = lsSerializer.writeToString(n);
        optionsHandle = new StringHandle(options).withFormat(Format.XML);
    }

    //TODO this could be more than one string...
    nl = combinedQueryXml.getElementsByTagNameNS("http://marklogic.com/appservices/search", "qtext");
    n = nl.item(0);
    String qtext = null;
    if (n != null) {
        qtext = lsSerializer.writeToString(n);
    }

    nl = combinedQueryXml.getElementsByTagNameNS("http://marklogic.com/appservices/search", "sparql");
    n = nl.item(0);
    String sparql = null;
    if (n != null) {
        sparql = lsSerializer.writeToString(n);
    }

    nl = combinedQueryXml.getElementsByTagNameNS("http://marklogic.com/appservices/search", "query");
    n = nl.item(0);
    String query = null;
    if (n != null) {
        query = lsSerializer.writeToString(nl.item(0));
    }
    StringHandle structuredQueryHandle = new StringHandle().with(query).withFormat(Format.XML);
    RawStructuredQueryDefinition structuredQueryDefinition = new RawQueryDefinitionImpl.Structured(
            structuredQueryHandle);
    return new CombinedQueryDefinitionImpl(structuredQueryDefinition, optionsHandle, qtext, sparql);
}

From source file:com.vmware.identity.sts.ws.SignatureValidator.java

/**
 * Retrieves the SoapBody element from the document.
 * @param document not null/*from   w w w .ja v  a 2 s  . co m*/
 * @return Node of the Soap Body element.
 * @throws XMLSignatureException when unable to locate the Soap Body element.
 */
private Node getSoapBody(Document document) throws XMLSignatureException {
    assert document != null;

    NodeList nodes = null;
    Node soapBody = null;

    nodes = document.getElementsByTagNameNS(javax.xml.soap.SOAPConstants.URI_NS_SOAP_ENVELOPE,
            BODY_ELEMENT_NAME);

    if ((nodes == null) || (nodes.getLength() == 0)) {
        throw new XMLSignatureException("Unexpected soap format - unable to find soap body.");
    } else if (nodes.getLength() > 1) {
        throw new XMLSignatureException("Unexpected soap format - found more than 1 soap body elements.");
    } else {
        soapBody = nodes.item(0);
    }

    if (soapBody == null) {
        throw new XMLSignatureException("Unexpected soap format - unable to resolve soap body.");
    }

    return soapBody;
}