Search in sources :

Example 1 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project camel by apache.

the class XPathBuilder method evaluateAs.

/**
     * Evaluates the expression as the given result type
     */
protected Object evaluateAs(Exchange exchange, QName resultQName) {
    // pool a pre compiled expression from pool
    XPathExpression xpathExpression = pool.poll();
    if (xpathExpression == null) {
        LOG.trace("Creating new XPathExpression as none was available from pool");
        // no avail in pool then create one
        try {
            xpathExpression = createXPathExpression();
        } catch (XPathExpressionException e) {
            throw new InvalidXPathExpression(getText(), e);
        } catch (Exception e) {
            throw new RuntimeExpressionException("Cannot create xpath expression", e);
        }
    } else {
        LOG.trace("Acquired XPathExpression from pool");
    }
    try {
        if (logNamespaces && LOG.isInfoEnabled()) {
            logNamespaces(exchange);
        }
        return doInEvaluateAs(xpathExpression, exchange, resultQName);
    } finally {
        // release it back to the pool
        pool.add(xpathExpression);
        LOG.trace("Released XPathExpression back to pool");
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XPathExpressionException(javax.xml.xpath.XPathExpressionException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) XPathFactoryConfigurationException(javax.xml.xpath.XPathFactoryConfigurationException) XPathFunctionException(javax.xml.xpath.XPathFunctionException) RuntimeExpressionException(org.apache.camel.RuntimeExpressionException) RuntimeExpressionException(org.apache.camel.RuntimeExpressionException)

Example 2 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project moco by dreamhead.

the class XPathRequestExtractor method doExtract.

@Override
protected Optional<String[]> doExtract(final HttpRequest request) {
    try {
        Optional<InputSource> source = helper.extractAsInputSource(request, extractor);
        if (!source.isPresent()) {
            return absent();
        }
        NodeList list = (NodeList) xPathExpression.evaluate(source.get(), XPathConstants.NODESET);
        if (list.getLength() == 0) {
            return absent();
        }
        return doExtract(list);
    } catch (XPathExpressionException e) {
        return absent();
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList)

Example 3 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project qi4j-sdk by Qi4j.

the class RssReaderTest method testReadRssFeed.

@Test
public void testReadRssFeed() {
    Client client = new Client(Protocol.HTTPS);
    Reference ref = new Reference("https://github.com/Qi4j/qi4j-sdk/commits/develop.atom");
    ContextResourceClientFactory contextResourceClientFactory = module.newObject(ContextResourceClientFactory.class, client);
    contextResourceClientFactory.registerResponseReader(new ResponseReader() {

        @Override
        public Object readResponse(Response response, Class<?> resultType) throws ResourceException {
            if (resultType.equals(Document.class)) {
                try {
                    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                    documentBuilderFactory.setNamespaceAware(false);
                    return documentBuilderFactory.newDocumentBuilder().parse(response.getEntity().getStream());
                } catch (Exception e) {
                    throw new ResourceException(e);
                }
            }
            return null;
        }
    });
    contextResourceClientFactory.setErrorHandler(new ErrorHandler().onError(ErrorHandler.RECOVERABLE_ERROR, new ResponseHandler() {

        @Override
        public HandlerCommand handleResponse(Response response, ContextResourceClient client) {
            System.out.println(">> REFRESH on recoverable error: " + response.getStatus());
            return refresh();
        }
    }));
    crc = contextResourceClientFactory.newClient(ref);
    crc.onResource(new ResultHandler<Document>() {

        Iterator<Node> itemNodes;

        @Override
        public HandlerCommand handleResult(Document result, ContextResourceClient client) {
            try {
                final XPath xPath = XPathFactory.newInstance().newXPath();
                System.out.println("== " + xPath.evaluate("feed/title", result) + " ==");
                final NodeList nodes = (NodeList) xPath.evaluate("feed/entry", result, XPathConstants.NODESET);
                List<Node> items = new ArrayList<>();
                for (int i = 0; i < nodes.getLength(); i++) {
                    items.add(nodes.item(i));
                }
                itemNodes = items.iterator();
                return processEntry(xPath);
            } catch (XPathExpressionException e) {
                throw new ResourceException(e);
            }
        }

        private HandlerCommand processEntry(final XPath xPath) throws XPathExpressionException {
            if (!itemNodes.hasNext()) {
                return null;
            }
            Node item = itemNodes.next();
            String title = xPath.evaluate("title", item);
            String detailUrl = xPath.evaluate("link/@href", item);
            System.out.println("-- " + title + " --");
            System.out.println("-- " + detailUrl + " --");
            return processEntry(xPath);
        }
    });
    crc.start();
}
Also used : ContextResourceClientFactory(org.qi4j.library.rest.client.api.ContextResourceClientFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ResponseHandler(org.qi4j.library.rest.client.spi.ResponseHandler) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) HandlerCommand(org.qi4j.library.rest.client.api.HandlerCommand) ResourceException(org.restlet.resource.ResourceException) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) Client(org.restlet.Client) ContextResourceClient(org.qi4j.library.rest.client.api.ContextResourceClient) XPath(javax.xml.xpath.XPath) ErrorHandler(org.qi4j.library.rest.client.api.ErrorHandler) Reference(org.restlet.data.Reference) NodeList(org.w3c.dom.NodeList) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ResourceException(org.restlet.resource.ResourceException) AssemblyException(org.qi4j.bootstrap.AssemblyException) Response(org.restlet.Response) ResponseReader(org.qi4j.library.rest.client.spi.ResponseReader) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 4 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project databus by linkedin.

the class GGXMLTrailTransactionFinder method xpathQuery.

private void xpathQuery() throws DatabusTrailFileParseException {
    try {
        //Set SCN
        InputSource source = new InputSource(new StringReader(_currTxnStr.toString()));
        // count time consumed by XML parsing
        _queryRateMonitor.resume();
        Object result = _expr.evaluate(source, XPathConstants.NODESET);
        _queryRateMonitor.ticks(_currTxnStr.length());
        _queryRateMonitor.suspend();
        NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            long newScn = Long.parseLong((nodes.item(i).getNodeValue().trim()));
            _minScn = Math.min(_minScn, newScn);
            _maxScn = Math.max(_maxScn, newScn);
        }
    } catch (XPathExpressionException xpxe) {
        throw new DatabusTrailFileParseException("Got XPath exception for trail-file entry: " + _currTxnStr, xpxe);
    } catch (NumberFormatException nfe) {
        throw new DatabusTrailFileParseException("Got parseLong() exception for trail-file entry: " + _currTxnStr, nfe);
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader)

Example 5 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException 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)

Aggregations

XPathExpressionException (javax.xml.xpath.XPathExpressionException)139 NodeList (org.w3c.dom.NodeList)65 XPath (javax.xml.xpath.XPath)64 Document (org.w3c.dom.Document)46 Node (org.w3c.dom.Node)46 IOException (java.io.IOException)42 XPathExpression (javax.xml.xpath.XPathExpression)38 SAXException (org.xml.sax.SAXException)27 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)25 XPathFactory (javax.xml.xpath.XPathFactory)23 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)18 Test (org.junit.Test)17 InputSource (org.xml.sax.InputSource)17 Element (org.w3c.dom.Element)16 Response (com.jayway.restassured.response.Response)12 ValidatableResponse (com.jayway.restassured.response.ValidatableResponse)12 DocumentBuilder (javax.xml.parsers.DocumentBuilder)12 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)12 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)11