Example usage for javax.xml.xpath XPathConstants NODESET

List of usage examples for javax.xml.xpath XPathConstants NODESET

Introduction

In this page you can find the example usage for javax.xml.xpath XPathConstants NODESET.

Prototype

QName NODESET

To view the source code for javax.xml.xpath XPathConstants NODESET.

Click Source Link

Document

The XPath 1.0 NodeSet data type.

Maps to Java org.w3c.dom.NodeList .

Usage

From source file:org.openmrs.module.rheashradapter.web.controller.RHEApatientController.java

private Map<String, String> identifyPostUpdateIdentifiers(String message) {
    Map<String, String> postUpdateIdentifiers = new HashMap<String, String>();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//  w  ww . j av a 2 s.c o m
    org.w3c.dom.Document doc = null;
    XPathExpression expr = null;
    XPathExpression exprIdType = null;

    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(message)));

        XPathFactory xFactory = XPathFactory.newInstance();

        XPath xpath = xFactory.newXPath();
        expr = xpath.compile("//postUpdateIdentifiers/postUpdateIdentifier/identifier/text()");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        XPath xpathIdType = xFactory.newXPath();
        exprIdType = xpathIdType.compile(
                "//postUpdateIdentifiers/postUpdateIdentifier/identifierDomain/universalIdentifierTypeCode/text()");
        Object resultIdType = exprIdType.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        NodeList nodesIdType = (NodeList) resultIdType;

        for (int i = 0; i < nodes.getLength(); i++) {
            postUpdateIdentifiers.put(nodesIdType.item(i).getTextContent(), nodes.item(i).getTextContent());
        }

    } catch (XPathExpressionException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return postUpdateIdentifiers;
}

From source file:org.eclipse.lyo.testsuite.oslcv2.cm.ChangeRequestXmlTests.java

@Test
public void changeRequestHasAtMostOneApprovedElement() throws XPathExpressionException {
    NodeList approvedEles = (NodeList) OSLCUtils.getXPath()
            .evaluate("//oslc_cm_v2:ChangeRequest/" + "oslc_cm_v2:approved", doc, XPathConstants.NODESET);
    assertTrue(getFailureMessage(), approvedEles.getLength() <= 1);
}

From source file:com.photon.phresco.impl.ConfigManagerImpl.java

private NodeList getNodeList(String xpath) throws ConfigurationException {
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath newXPath = xPathFactory.newXPath();
    XPathExpression xPathExpression;
    NodeList xpathNodes = null;//from  w w  w. j av a  2s  . c  o  m
    try {
        xPathExpression = newXPath.compile(xpath);
        xpathNodes = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        throw new ConfigurationException(e);
    }
    return xpathNodes;
}

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

public static ArrayList<String> getCapabilityURLsUsingXML(String xpathStmt, ArrayList<String> serviceUrls,
        boolean useDefaultUsage)
        throws IOException, ParserConfigurationException, SAXException, XPathExpressionException {
    // Collection to contain the creationFactory urls from all SPs
    ArrayList<String> data = new ArrayList<String>();
    String firstUrl = null;//from  w w  w.  j  a v a2s. c o  m

    for (String base : serviceUrls) {
        HttpResponse resp = OSLCUtils.getResponseFromUrl(base, base, basicCreds, OSLCConstants.CT_XML, headers);

        Document baseDoc = OSLCUtils.createXMLDocFromResponseBody(EntityUtils.toString(resp.getEntity()));

        NodeList sDescs = (NodeList) OSLCUtils.getXPath().evaluate(xpathStmt, baseDoc, XPathConstants.NODESET);
        String xpathSubStmt = "../../oslc_v2:usage/@rdf:resource";
        for (int i = 0; i < sDescs.getLength(); i++) {
            if (firstUrl == null)
                firstUrl = sDescs.item(i).getNodeValue();
            if (useDefaultUsage) {
                NodeList usages = (NodeList) OSLCUtils.getXPath().evaluate(xpathSubStmt, sDescs.item(i),
                        XPathConstants.NODESET);
                for (int u = 0; u < usages.getLength(); u++) {
                    String usageValue = usages.item(u).getNodeValue();
                    if (OSLCConstants.USAGE_DEFAULT_URI.equals(usageValue)) {
                        data.add(sDescs.item(i).getNodeValue());
                        return data;
                    }
                }
            } else {
                data.add(sDescs.item(i).getNodeValue());
                if (onlyOnce)
                    return data;
            }
        }
    }
    // If we didn't find the default, then just send back the first one we
    // found.
    if (useDefaultUsage && firstUrl != null)
        data.add(firstUrl);
    return data;
}

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

@Test
public void publisherElementsAreValid() throws XPathExpressionException {
    // Get all Publisher xml blocks
    NodeList publishers = (NodeList) OSLCUtils.getXPath().evaluate("//dc:publisher/*", doc,
            XPathConstants.NODESET);

    // Verify that each block contains a title and identifier, and at most
    // one icon and label
    for (int i = 0; i < publishers.getLength(); i++) {
        NodeList publisherElements = publishers.item(i).getChildNodes();
        int titleCount = 0;
        int identifierCount = 0;
        int iconCount = 0;
        int labelCount = 0;
        for (int j = 0; j < publisherElements.getLength(); j++) {
            Node ele = publisherElements.item(j);
            if (ele.getLocalName() == null) {
                continue;
            }/*w w  w  .  j  av a 2  s.  com*/
            if (ele.getNamespaceURI().equals(OSLCConstants.DC) && ele.getLocalName().equals("title")) {
                titleCount++;
            }
            if (ele.getNamespaceURI().equals(OSLCConstants.DC) && ele.getLocalName().equals("identifier")) {
                identifierCount++;
            }
            if (ele.getNamespaceURI().equals(OSLCConstants.OSLC_V2) && ele.getLocalName().equals("label")) {
                labelCount++;
            }
            if (ele.getNamespaceURI().equals(OSLCConstants.OSLC_V2) && ele.getLocalName().equals("icon")) {
                iconCount++;
            }
        }
        assertTrue(titleCount == 1);
        assertTrue(identifierCount == 1);
        assertTrue(iconCount <= 1);
        assertTrue(labelCount <= 1);
    }
}

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

@Test
public void changeManagementServiceDescriptionHasValidSelectionDialog() throws XPathException {
    //If ServiceDescription is oslc_cm, make sure it has a valid selection dialog child element
    Node cmRequest = (Node) OSLCUtils.getXPath().evaluate("//oslc_cm:changeRequests", doc, XPathConstants.NODE);
    if (cmRequest != null) {
        NodeList sD = (NodeList) OSLCUtils.getXPath()
                .evaluate("//oslc_cm:changeRequests/oslc_cm:selectionDialog", doc, XPathConstants.NODESET);
        for (int i = 0; i < sD.getLength(); i++) {
            Node sQUrl = (Node) OSLCUtils.getXPath().evaluate(
                    "//oslc_cm:changeRequests/oslc_cm:selectionDialog[" + (i + 1) + "]/oslc_cm:url", doc,
                    XPathConstants.NODE);
            assertNotNull(sQUrl);/*from w w  w  .  j  a v  a2  s  .  c o  m*/
            Node sDtitle = (Node) OSLCUtils.getXPath().evaluate(
                    "//oslc_cm:changeRequests/oslc_cm:selectionDialog[" + (i + 1) + "]/dc:title", doc,
                    XPathConstants.NODE);
            assertNotNull(sDtitle);
            assertFalse(sDtitle.getTextContent().isEmpty());
        }
    }
}

From source file:cc.siara.csv_ml_demo.MultiLevelCSVSwingDemo.java

/**
 * Evaluates given XPath from Input box against Document generated by
 * parsing csv_ml in input box and sets value or node list to output box.
 *///from  w  w  w.ja va 2s  .c om
private void processXPath() {
    XPath xpath = XPathFactory.newInstance().newXPath();
    Document doc = parseInputToDOM();
    if (doc == null)
        return;
    StringBuffer out_str = new StringBuffer();
    try {
        XPathExpression expr = xpath.compile(tfXPath.getText());
        try {
            Document outDoc = Util.parseXMLToDOM("<output></output>");
            Element rootElement = outDoc.getDocumentElement();
            NodeList ret = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
            for (int i = 0; i < ret.getLength(); i++) {
                Object o = ret.item(i);
                if (o instanceof String) {
                    out_str.append(o);
                } else if (o instanceof Node) {
                    Node n = (Node) o;
                    short nt = n.getNodeType();
                    switch (nt) {
                    case Node.TEXT_NODE:
                    case Node.ATTRIBUTE_NODE:
                    case Node.CDATA_SECTION_NODE: // Only one value gets
                                                  // evaluated?
                        if (out_str.length() > 0)
                            out_str.append(',');
                        if (nt == Node.ATTRIBUTE_NODE)
                            out_str.append(n.getNodeValue());
                        else
                            out_str.append(n.getTextContent());
                        break;
                    case Node.ELEMENT_NODE:
                        rootElement.appendChild(outDoc.importNode(n, true));
                        break;
                    }
                }
            }
            if (out_str.length() > 0) {
                rootElement.setTextContent(out_str.toString());
                out_str.setLength(0);
            }
            out_str.append(Util.docToString(outDoc, true));
        } catch (Exception e) {
            // Thrown most likely because the given XPath evaluates to a
            // string
            out_str.append(expr.evaluate(doc));
        }
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    taOutput.setText(out_str.toString());
    tfOutputSize.setText(String.valueOf(out_str.length()));
}

From source file:org.eclipse.lyo.testsuite.oslcv2.cm.ChangeRequestXmlTests.java

@Test
public void changeRequestHasAtMostOneReviewedElement() throws XPathExpressionException {
    NodeList reviewedEles = (NodeList) OSLCUtils.getXPath()
            .evaluate("//oslc_cm_v2:ChangeRequest/" + "oslc_cm_v2:reviewed", doc, XPathConstants.NODESET);
    assertTrue(getFailureMessage(), reviewedEles.getLength() <= 1);
}

From source file:io.fabric8.tooling.archetype.generator.ArchetypeHelper.java

/**
 * Extracts properties declared in "META-INF/maven/archetype-metadata.xml" file
 *
 * @param zip//from   w ww .  j  a  v a 2 s  . c o m
 * @param replaceProperties
 * @throws IOException
 */
protected void parseReplaceProperties(ZipInputStream zip, Map<String, String> replaceProperties)
        throws IOException, ParserConfigurationException, SAXException, XPathExpressionException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    copy(zip, bos);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();

    InputSource inputSource = new InputSource(new ByteArrayInputStream(bos.toByteArray()));
    Document document = db.parse(inputSource);

    XPath xpath = XPathFactory.newInstance().newXPath();
    SimpleNamespaceContext nsContext = new SimpleNamespaceContext();
    nsContext.registerMapping("ad", archetypeDescriptorUri);
    xpath.setNamespaceContext(nsContext);

    NodeList properties = (NodeList) xpath.evaluate(requiredPropertyXPath, document, XPathConstants.NODESET);

    for (int p = 0; p < properties.getLength(); p++) {
        Element requiredProperty = (Element) properties.item(p);

        String key = requiredProperty.getAttribute("key");
        NodeList children = requiredProperty.getElementsByTagNameNS(archetypeDescriptorUri, "defaultValue");
        String value = "";
        if (children.getLength() == 1 && children.item(0).hasChildNodes()) {
            value = children.item(0).getTextContent();
        } else {
            if ("name".equals(key) && value.isEmpty()) {
                value = "HelloWorld";
            }
        }
        replaceProperties.put(key, value);
    }
}