use of javax.xml.xpath.XPathFactoryConfigurationException in project camel by apache.
the class XPathBuilder method createXPathExpression.
/**
* Creates a new xpath expression as there we no available in the pool.
* <p/>
* This implementation must be synchronized to ensure thread safety, as this XPathBuilder instance may not have been
* started prior to being used.
*/
protected synchronized XPathExpression createXPathExpression() throws XPathExpressionException, XPathFactoryConfigurationException {
// ensure we are started
try {
start();
} catch (Exception e) {
throw new RuntimeExpressionException("Error starting XPathBuilder", e);
}
// XPathFactory is not thread safe
XPath xPath = getXPathFactory().newXPath();
if (!logNamespaces && LOG.isTraceEnabled()) {
LOG.trace("Creating new XPath expression in pool. Namespaces on XPath expression: {}", getNamespaceContext().toString());
} else if (logNamespaces && LOG.isInfoEnabled()) {
LOG.info("Creating new XPath expression in pool. Namespaces on XPath expression: {}", getNamespaceContext().toString());
}
xPath.setNamespaceContext(getNamespaceContext());
xPath.setXPathVariableResolver(getVariableResolver());
XPathFunctionResolver parentResolver = getFunctionResolver();
if (parentResolver == null) {
parentResolver = xPath.getXPathFunctionResolver();
}
xPath.setXPathFunctionResolver(createDefaultFunctionResolver(parentResolver));
return xPath.compile(text);
}
use of javax.xml.xpath.XPathFactoryConfigurationException in project jdk8u_jdk by JetBrains.
the class XPathExFuncTest method testExtFuncNotAllowed.
/**
* Security is enabled, extension function not allowed
*/
public void testExtFuncNotAllowed() {
Policy p = new SimplePolicy(new AllPermission());
Policy.setPolicy(p);
System.setSecurityManager(new SecurityManager());
try {
evaluate(false);
} catch (XPathFactoryConfigurationException e) {
fail(e.getMessage());
} catch (XPathExpressionException ex) {
//expected since extension function is disallowed
System.out.println("testExtFuncNotAllowed: OK");
} finally {
System.setSecurityManager(null);
}
}
use of javax.xml.xpath.XPathFactoryConfigurationException in project jdk8u_jdk by JetBrains.
the class JDKXPathAPI method selectNodeList.
/**
* Use an XPath string to select a nodelist.
* XPath namespace prefixes are resolved from the namespaceNode.
*
* @param contextNode The node to start searching from.
* @param xpathnode
* @param str
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
* @return A NodeIterator, should never be null.
*
* @throws TransformerException
*/
public NodeList selectNodeList(Node contextNode, Node xpathnode, String str, Node namespaceNode) throws TransformerException {
if (!str.equals(xpathStr) || xpathExpression == null) {
if (xpf == null) {
xpf = XPathFactory.newInstance();
try {
xpf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
} catch (XPathFactoryConfigurationException ex) {
throw new TransformerException("empty", ex);
}
}
XPath xpath = xpf.newXPath();
xpath.setNamespaceContext(new DOMNamespaceContext(namespaceNode));
xpathStr = str;
try {
xpathExpression = xpath.compile(xpathStr);
} catch (XPathExpressionException ex) {
throw new TransformerException("empty", ex);
}
}
try {
return (NodeList) xpathExpression.evaluate(contextNode, XPathConstants.NODESET);
} catch (XPathExpressionException ex) {
throw new TransformerException("empty", ex);
}
}
use of javax.xml.xpath.XPathFactoryConfigurationException in project jdk8u_jdk by JetBrains.
the class JDKXPathAPI method evaluate.
/**
* Evaluate an XPath string and return true if the output is to be included or not.
* @param contextNode The node to start searching from.
* @param xpathnode The XPath node
* @param str The XPath expression
* @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
*/
public boolean evaluate(Node contextNode, Node xpathnode, String str, Node namespaceNode) throws TransformerException {
if (!str.equals(xpathStr) || xpathExpression == null) {
if (xpf == null) {
xpf = XPathFactory.newInstance();
try {
xpf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
} catch (XPathFactoryConfigurationException ex) {
throw new TransformerException("empty", ex);
}
}
XPath xpath = xpf.newXPath();
xpath.setNamespaceContext(new DOMNamespaceContext(namespaceNode));
xpathStr = str;
try {
xpathExpression = xpath.compile(xpathStr);
} catch (XPathExpressionException ex) {
throw new TransformerException("empty", ex);
}
}
try {
Boolean result = (Boolean) xpathExpression.evaluate(contextNode, XPathConstants.BOOLEAN);
return result.booleanValue();
} catch (XPathExpressionException ex) {
throw new TransformerException("empty", ex);
}
}
use of javax.xml.xpath.XPathFactoryConfigurationException in project TranskribusCore by Transkribus.
the class TrpXPathProcessorTest method testNonWorkingXPath.
@Test
public void testNonWorkingXPath() {
final String lineId = "esGibtSicherKeineLineDieDieseIdHat";
TrpXPathProcessor proc;
try {
proc = new TrpXPathProcessor();
} catch (XPathFactoryConfigurationException | ParserConfigurationException e) {
Assert.fail("Could not initiate XPathProcessor: " + e.getMessage());
return;
}
URL url = this.getClass().getClassLoader().getResource(docPath);
final String xPath = "//TextLine[@id='" + lineId + "']";
XPathExpression exp;
try {
exp = proc.compile(xPath);
} catch (XPathExpressionException e) {
Assert.fail("Could not compile xPath: " + e.getMessage());
return;
}
Node node;
try {
node = proc.getNode(url, exp);
} catch (XPathExpressionException | SAXException | IOException e) {
Assert.fail("Could not evaluate xPath: " + e.getMessage());
return;
}
Assert.assertNull(node);
}
Aggregations