Search in sources :

Example 51 with XPath

use of javax.xml.xpath.XPath in project openhab1-addons by openhab.

the class Tr064Comm method readAllServices.

/***
     * Connects to fbox service xml to get a list of all services
     * which are offered by TR064. Saves it into local list
     */
private void readAllServices() {
    Document xml = getFboxXmlResponse(_url + "/" + TR064DOWNLOADFILE);
    if (xml == null) {
        logger.error("Could not read xml response services");
        return;
    }
    // get all service nodes
    NodeList nlServices = xml.getElementsByTagName("service");
    Node currentNode = null;
    XPath xPath = XPathFactory.newInstance().newXPath();
    for (int i = 0; i < nlServices.getLength(); i++) {
        // iterate over all services fbox offered us
        currentNode = nlServices.item(i);
        Tr064Service trS = new Tr064Service();
        try {
            trS.setControlUrl((String) xPath.evaluate("controlURL", currentNode, XPathConstants.STRING));
            trS.setEventSubUrl((String) xPath.evaluate("eventSubURL", currentNode, XPathConstants.STRING));
            trS.setScpdurl((String) xPath.evaluate("SCPDURL", currentNode, XPathConstants.STRING));
            trS.setServiceId((String) xPath.evaluate("serviceId", currentNode, XPathConstants.STRING));
            trS.setServiceType((String) xPath.evaluate("serviceType", currentNode, XPathConstants.STRING));
        } catch (XPathExpressionException e) {
            logger.debug("Could not parse service {}", currentNode.getTextContent());
            e.printStackTrace();
        }
        _alServices.add(trS);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Example 52 with XPath

use of javax.xml.xpath.XPath in project languagetool by languagetool-org.

the class PatternRuleXmlCreator method toXML.

/**
   * Return the given pattern rule as an indented XML string.
   * @since 2.3
   */
public final String toXML(PatternRuleId ruleId, Language language) {
    List<String> filenames = language.getRuleFileNames();
    XPath xpath = XPathFactory.newInstance().newXPath();
    for (String filename : filenames) {
        try (InputStream is = this.getClass().getResourceAsStream(filename)) {
            Document doc = getDocument(is);
            Node ruleNode = (Node) xpath.evaluate("/rules/category/rule[@id='" + ruleId.getId() + "']", doc, XPathConstants.NODE);
            if (ruleNode != null) {
                return nodeToString(ruleNode);
            }
            Node ruleNodeInGroup = (Node) xpath.evaluate("/rules/category/rulegroup/rule[@id='" + ruleId.getId() + "']", doc, XPathConstants.NODE);
            if (ruleNodeInGroup != null) {
                return nodeToString(ruleNodeInGroup);
            }
            if (ruleId.getSubId() != null) {
                NodeList ruleGroupNodes = (NodeList) xpath.evaluate("/rules/category/rulegroup[@id='" + ruleId.getId() + "']/rule", doc, XPathConstants.NODESET);
                if (ruleGroupNodes != null) {
                    for (int i = 0; i < ruleGroupNodes.getLength(); i++) {
                        if (Integer.toString(i + 1).equals(ruleId.getSubId())) {
                            return nodeToString(ruleGroupNodes.item(i));
                        }
                    }
                }
            } else {
                Node ruleGroupNode = (Node) xpath.evaluate("/rules/category/rulegroup[@id='" + ruleId.getId() + "']", doc, XPathConstants.NODE);
                if (ruleGroupNode != null) {
                    return nodeToString(ruleGroupNode);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Could not turn rule '" + ruleId + "' for language " + language + " into a string", e);
        }
    }
    throw new RuntimeException("Could not find rule '" + ruleId + "' for language " + language + " in files: " + filenames);
}
Also used : XPath(javax.xml.xpath.XPath) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException)

Example 53 with XPath

use of javax.xml.xpath.XPath in project buck by facebook.

the class AndroidXPathFactory method newXPath.

/**
     * Creates a new XPath object, specifying which prefix in the query is used for the
     * android namespace.
     * @param androidPrefix The namespace prefix.
     */
public static XPath newXPath(String androidPrefix) {
    XPath xpath = sFactory.newXPath();
    xpath.setNamespaceContext(new AndroidNamespaceContext(androidPrefix));
    return xpath;
}
Also used : XPath(javax.xml.xpath.XPath)

Example 54 with XPath

use of javax.xml.xpath.XPath in project buck by facebook.

the class AndroidXPathFactory method newXPath.

/**
     * Creates a new XPath object using the default prefix for the android namespace.
     * @see #DEFAULT_NS_PREFIX
     */
public static XPath newXPath() {
    XPath xpath = sFactory.newXPath();
    xpath.setNamespaceContext(AndroidNamespaceContext.getDefault());
    return xpath;
}
Also used : XPath(javax.xml.xpath.XPath)

Example 55 with XPath

use of javax.xml.xpath.XPath in project flyway by flyway.

the class GradleLargeTest method getPomVersion.

/**
     * Retrieves the version embedded in the project pom. Useful for running these tests in IntelliJ.
     *
     * @return The POM version.
     */
private String getPomVersion() {
    try {
        File pom = new File("pom.xml");
        if (!pom.exists()) {
            return "unknown";
        }
        XPath xPath = XPathFactory.newInstance().newXPath();
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(false);
        Document document = documentBuilderFactory.newDocumentBuilder().parse(pom);
        return xPath.evaluate("/project/version", document);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to read POM version", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Document(org.w3c.dom.Document) File(java.io.File)

Aggregations

XPath (javax.xml.xpath.XPath)526 Document (org.w3c.dom.Document)254 NodeList (org.w3c.dom.NodeList)230 XPathFactory (javax.xml.xpath.XPathFactory)215 Node (org.w3c.dom.Node)171 XPathExpressionException (javax.xml.xpath.XPathExpressionException)159 XPathExpression (javax.xml.xpath.XPathExpression)142 DocumentBuilder (javax.xml.parsers.DocumentBuilder)125 Element (org.w3c.dom.Element)118 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)97 IOException (java.io.IOException)89 Test (org.junit.Test)80 InputSource (org.xml.sax.InputSource)59 File (java.io.File)57 SAXException (org.xml.sax.SAXException)57 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)51 ByteArrayInputStream (java.io.ByteArrayInputStream)44 ArrayList (java.util.ArrayList)44 InputStream (java.io.InputStream)39 DSNamespaceContext (org.apache.xml.security.test.dom.DSNamespaceContext)37