Example usage for javax.xml.xpath XPathExpression evaluate

List of usage examples for javax.xml.xpath XPathExpression evaluate

Introduction

In this page you can find the example usage for javax.xml.xpath XPathExpression evaluate.

Prototype

public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException;

Source Link

Document

Evaluate the compiled XPath expression in the context of the specified InputSource and return the result as the specified type.

Usage

From source file:org.eclipse.sw360.licenseinfo.parsers.AbstractCLIParser.java

protected NodeList getNodeListByXpath(Document doc, String xpathString) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression xpathExpression = xpath.compile(xpathString);
    return (NodeList) xpathExpression.evaluate(doc, XPathConstants.NODESET);
}

From source file:org.eclipse.thym.android.core.adt.AndroidProjectGenerator.java

private void updateAppName(String appName) throws CoreException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from w w w .  jav  a 2 s  .c  o  m
    DocumentBuilder db;

    try {
        db = dbf.newDocumentBuilder();
        IPath stringsPath = new Path(getDestination().toString()).append(DIR_RES).append(DIR_VALUES)
                .append(FILE_XML_STRINGS);
        File strings = stringsPath.toFile();
        Document configDocument = db.parse(strings);
        XPath xpath = XPathFactory.newInstance().newXPath();

        try {
            XPathExpression expr = xpath.compile("//string[@name=\"app_name\"]");
            Node node = (Node) expr.evaluate(configDocument, XPathConstants.NODE);
            node.setTextContent(appName);
            configDocument.setXmlStandalone(true);
            Source source = new DOMSource(configDocument);
            StreamResult result = new StreamResult(strings);
            // Write the DOM document to the file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer xformer = transformerFactory.newTransformer();
            xformer.transform(source, result);

        } catch (XPathExpressionException e) {//We continue because this affects the displayed app name
                                              // which is not a show stopper during development
            AndroidCore.log(IStatus.ERROR, "Error when updating the application name", e);
        } catch (TransformerConfigurationException e) {
            AndroidCore.log(IStatus.ERROR, "Error when updating the application name", e);
        } catch (TransformerException e) {
            AndroidCore.log(IStatus.ERROR, "Error when updating the application name", e);
        }

    } catch (ParserConfigurationException e) {
        throw new CoreException(new Status(IStatus.ERROR, HybridCore.PLUGIN_ID,
                "Parser error when parsing /res/values/strings.xml", e));
    } catch (SAXException e) {
        throw new CoreException(
                new Status(IStatus.ERROR, HybridCore.PLUGIN_ID, "Parsing error on /res/values/strings.xml", e));
    } catch (IOException e) {
        throw new CoreException(new Status(IStatus.ERROR, HybridCore.PLUGIN_ID,
                "IO error when parsing /res/values/strings.xml", e));
    }
}

From source file:org.eclipse.thym.blackberry.core.bdt.BlackBerryProjectGenerator.java

private void updateAppName(String appName) throws CoreException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*  ww w.j  ava  2s .  c om*/
    DocumentBuilder db;

    try {
        db = dbf.newDocumentBuilder();
        IPath stringsPath = new Path(getDestination().toString()).append(DIR_RES).append(DIR_VALUES)
                .append(FILE_XML_STRINGS);
        File strings = stringsPath.toFile();
        Document configDocument = db.parse(strings);
        XPath xpath = XPathFactory.newInstance().newXPath();

        try {
            XPathExpression expr = xpath.compile("//string[@name=\"app_name\"]");
            Node node = (Node) expr.evaluate(configDocument, XPathConstants.NODE);
            node.setTextContent(appName);

            configDocument.setXmlStandalone(true);

            Source source = new DOMSource(configDocument);

            StreamResult result = new StreamResult(strings);

            // Write the DOM document to the file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer xformer = transformerFactory.newTransformer();

            xformer.transform(source, result);

        } catch (XPathExpressionException e) {//We continue because this affects the displayed app name
                                              // which is not a show stopper during development
            BlackBerryCore.log(IStatus.ERROR, "Error when updating the application name", e);
        } catch (TransformerConfigurationException e) {
            BlackBerryCore.log(IStatus.ERROR, "Error when updating the application name", e);
        } catch (TransformerException e) {
            BlackBerryCore.log(IStatus.ERROR, "Error when updating the application name", e);
        }

    } catch (ParserConfigurationException e) {
        throw new CoreException(new Status(IStatus.ERROR, HybridCore.PLUGIN_ID,
                "Parser error when parsing /res/values/strings.xml", e));
    } catch (SAXException e) {
        throw new CoreException(
                new Status(IStatus.ERROR, HybridCore.PLUGIN_ID, "Parsing error on /res/values/strings.xml", e));
    } catch (IOException e) {
        throw new CoreException(new Status(IStatus.ERROR, HybridCore.PLUGIN_ID,
                "IO error when parsing /res/values/strings.xml", e));
    }
}

From source file:org.eurekastreams.server.service.actions.strategies.galleryitem.PluginDefinitionPopulator.java

/**
 * Checks for the required plugin feature before updating the plugin def.
 * //from  w ww.  j  ava2  s. c o m
 * @param inPluginDefinitionUrl
 *            The url to the plugin definition.
 * @return true if the feature is found and false if not found.
 * @throws XPathExpressionException
 *             not expected.
 * @throws ParserConfigurationException
 *             not expected.
 * @throws SAXException
 *             not expected.
 * @throws IOException
 *             expected if the url is invalid.
 */
private boolean checkForRequiredFeature(final String inPluginDefinitionUrl)
        throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(inPluginDefinitionUrl);
    XPath xpath = XPathFactory.newInstance().newXPath();

    // this is used to get the amount of required feature we need to check.
    XPathExpression getFeatureAmountExpr = xpath.compile("//Module/ModulePrefs/Require");

    NodeList nodeResults = (NodeList) getFeatureAmountExpr.evaluate(doc, XPathConstants.NODESET);

    for (int i = 1; i <= nodeResults.getLength(); i++) {
        XPathExpression getFeatureExpr = xpath.compile("//Module/ModulePrefs/Require[" + i + "]/@feature");
        String thing = (String) getFeatureExpr.evaluate(doc, XPathConstants.STRING);
        if (thing.equals("eurekastreams-streamplugin")) {
            return true;
        }
    }
    return false;
}

From source file:org.forgerock.maven.plugins.LinkTester.java

private void extractXmlIds(XPathExpression expr, Document doc, String path) throws XPathExpressionException {
    NodeList ids = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    if (ids != null) {
        for (int i = 0; i < ids.getLength(); i++) {
            Node node = ids.item(i);
            File file = new File(path);
            xmlIds.put(file.getParentFile().getName(), node.getNodeValue());
        }/*from www .j  ava 2 s. co  m*/
    }
}

From source file:org.glite.lb.NotifParser.java

/**
 * a method for handling xpath queries//from   www .  ja  v a 2s.co m
 *
 * @param xpathString xpath expression
 * @return the result nodelist
 */
private NodeList evaluateXPath(String xpathString) {
    try {
        XPathFactory xfactory = XPathFactory.newInstance();
        XPath xpath = xfactory.newXPath();
        XPathExpression expr = xpath.compile(xpathString);
        return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:org.gluu.authentication.remote.saml2.selector.ApplicationSelectorConfiguration.java

private Map<String, String> loadIdpMapping(Document xmlDoc) throws XPathExpressionException {
    Map<String, String> result = new HashMap<String, String>();
    XPath xPath = XPathFactory.newInstance().newXPath();

    XPathExpression query = xPath.compile("/asimba-selector/application");
    NodeList nodes = (NodeList) query.evaluate(xmlDoc, XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);

        Node entityIdNode = node.getAttributes().getNamedItem("entityId");
        if (entityIdNode == null) {
            continue;
        }//  ww w. ja v  a 2s . c  om

        Node organizationIdNode = node.getAttributes().getNamedItem("organizationId");
        if (organizationIdNode == null) {
            continue;
        }

        result.put(entityIdNode.getNodeValue(), organizationIdNode.getNodeValue());
    }

    return result;
}

From source file:org.gluu.saml.Response.java

public Map<String, List<String>> getAttributes() throws XPathExpressionException {
    Map<String, List<String>> result = new HashMap<String, List<String>>();
    XPath xPath = XPathFactory.newInstance().newXPath();

    xPath.setNamespaceContext(NAMESPACES);
    XPathExpression query = xPath
            .compile("/samlp:Response/saml:Assertion/saml:AttributeStatement/saml:Attribute");
    NodeList nodes = (NodeList) query.evaluate(xmlDoc, XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);

        Node nameNode = node.getAttributes().getNamedItem("Name");
        if (nameNode == null) {
            continue;
        }/*from   w ww. j  a v a2 s .  c  om*/

        String attributeName = nameNode.getNodeValue();
        List<String> attributeValues = new ArrayList<String>();

        NodeList nameChildNodes = node.getChildNodes();
        for (int j = 0; j < nameChildNodes.getLength(); j++) {
            Node nameChildNode = nameChildNodes.item(j);

            if (nameChildNode.getNamespaceURI().equalsIgnoreCase("urn:oasis:names:tc:SAML:2.0:assertion")
                    && nameChildNode.getLocalName().equals("AttributeValue")) {
                NodeList valueChildNodes = nameChildNode.getChildNodes();
                for (int k = 0; k < valueChildNodes.getLength(); k++) {
                    Node valueChildNode = valueChildNodes.item(k);
                    attributeValues.add(valueChildNode.getNodeValue());
                }
            }
        }

        result.put(attributeName, attributeValues);
    }

    return result;
}

From source file:org.hawkular.wildfly.module.installer.ExtensionDeployerTest.java

private void assertXpath(String expression, Document doc, int expectedCount) throws Exception {
    XPathExpression expr = xpath.compile(expression);
    NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    Assert.assertEquals(expectedCount, nl.getLength());
}

From source file:org.hyperic.hq.plugin.jboss7.JBossDetectorBase.java

final ConfigResponse getServerProductConfig(Map<String, String> args) {
    ConfigResponse cfg = new ConfigResponse();
    String port = null;/* ww w.  j av a  2 s .  c  o  m*/
    String address = null;

    File cfgFile = getConfigFile(args);

    try {
        log.debug("[getProductConfig] cfgFile=" + cfgFile.getCanonicalPath());
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = (Document) dBuilder.parse(cfgFile);

        XPathFactory factory = XPathFactory.newInstance();
        XPathExpression expr = factory.newXPath()
                .compile(getConfigRoot() + "/management/management-interfaces/http-interface");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodeList = (NodeList) result;

        String mgntIf = null;
        for (int i = 0; i < nodeList.getLength(); i++) {
            if (nodeList.item(i).getAttributes().getNamedItem("port") != null) {
                port = nodeList.item(i).getAttributes().getNamedItem("port").getNodeValue();
                mgntIf = nodeList.item(i).getAttributes().getNamedItem("interface").getNodeValue();
            }
        }

        if (mgntIf != null) {
            expr = factory.newXPath()
                    .compile(getConfigRoot() + "/interfaces/interface[@name='" + mgntIf + "']/inet-address");
            result = expr.evaluate(doc, XPathConstants.NODESET);
            nodeList = (NodeList) result;

            for (int i = 0; i < nodeList.getLength(); i++) {
                address = nodeList.item(i).getAttributes().getNamedItem("value").getNodeValue();
            }
        }

        setUpExtraProductConfig(cfg, doc);

    } catch (Exception ex) {
        log.debug("Error discovering the jmx.url : " + ex, ex);
    }

    log.debug("[getProductConfig] address='" + address + "' port='" + port + "'");
    if ((address != null) && (port != null)) {
        cfg.setValue(PORT, port);
        cfg.setValue(ADDR, parseAddress(address, args));
    }
    return cfg;
}