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");
}
}
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();
}
}
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();
}
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);
}
}
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);
}
}
Aggregations