use of javax.xml.xpath.XPathFactoryConfigurationException in project jdk8u_jdk by JetBrains.
the class XPathExFuncTest method testEnableExtFunc.
/**
* Security is enabled, use new feature: enableExtensionFunctions
*/
public void testEnableExtFunc() {
Policy p = new SimplePolicy(new AllPermission());
Policy.setPolicy(p);
System.setSecurityManager(new SecurityManager());
try {
evaluate(true);
System.out.println("testEnableExt: OK");
} catch (XPathFactoryConfigurationException e) {
fail(e.getMessage());
} catch (XPathExpressionException e) {
fail(e.getMessage());
} finally {
System.setSecurityManager(null);
}
}
use of javax.xml.xpath.XPathFactoryConfigurationException in project santuario-java by apache.
the class XIncludeHandler method evaluateXPointer.
private NodeList evaluateXPointer(String xpointer, Node node) throws SAXException {
final String xPointerSchemeString = "xpointer(";
final String xmlnsSchemeString = "xmlns(";
int xPointerSchemeIndex = xpointer.indexOf(xPointerSchemeString);
if (xPointerSchemeIndex < 0) {
throw new SAXException("Only xpointer scheme is supported ATM");
}
xPointerSchemeIndex += xPointerSchemeString.length();
int xPointerSchemeEndIndex = this.findBalancedEndIndex(xpointer, xPointerSchemeIndex, '(', ')');
XPathFactory xPathFactory = XPathFactory.newInstance();
try {
xPathFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
} catch (XPathFactoryConfigurationException ex) {
throw new SAXException(ex);
}
XPath xPath = xPathFactory.newXPath();
int xmlnsSchemeIndex = xpointer.indexOf(xmlnsSchemeString);
if (xmlnsSchemeIndex >= 0) {
xmlnsSchemeIndex += xmlnsSchemeString.length();
int xmlnsSchemeEndIndex = this.findBalancedEndIndex(xpointer, xmlnsSchemeIndex, '(', ')');
String namespaceScheme = xpointer.substring(xmlnsSchemeIndex, xmlnsSchemeEndIndex);
final String[] namespaceSplit = namespaceScheme.split("=");
xPath.setNamespaceContext(new NamespaceContext() {
@Override
public String getNamespaceURI(String prefix) {
if (prefix.equals(namespaceSplit[0])) {
return namespaceSplit[1];
}
return null;
}
@Override
public String getPrefix(String namespaceURI) {
if (namespaceURI.equals(namespaceSplit[1])) {
return namespaceSplit[0];
}
return null;
}
@Override
public Iterator<String> getPrefixes(String namespaceURI) {
return null;
}
});
}
try {
return (NodeList) xPath.evaluate(xpointer.substring(xPointerSchemeIndex, xPointerSchemeEndIndex), node, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
throw new SAXException(e);
}
}
use of javax.xml.xpath.XPathFactoryConfigurationException in project TranskribusCore by Transkribus.
the class TrpXPathProcessorTest method testXPath.
@Test
public void testXPath() {
final String lineId = "r28";
final String docPath = "TrpTestDoc_20131209/StAZ-Sign.2-1_001.xml";
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.assertEquals("Element name does not match!", "TextLine", node.getNodeName());
final String resultId = node.getAttributes().getNamedItem("id").getTextContent();
Assert.assertEquals("Element ID does not match!", lineId, resultId);
}
use of javax.xml.xpath.XPathFactoryConfigurationException in project jsoup by jhy.
the class W3CDom method selectXpath.
/**
* Evaluate an XPath query against the supplied context node, and return the results.
* @param xpath an XPath query
* @param contextNode the context node to evaluate against
* @return the matches nodes
*/
public NodeList selectXpath(String xpath, Node contextNode) {
Validate.notEmpty(xpath);
Validate.notNull(contextNode);
NodeList nodeList;
try {
// if there is a configured XPath factory, use that instead of the Java base impl:
String property = System.getProperty(XPathFactoryProperty);
final XPathFactory xPathFactory = property != null ? XPathFactory.newInstance("jsoup") : XPathFactory.newInstance();
XPathExpression expression = xPathFactory.newXPath().compile(xpath);
// love the strong typing here /s
nodeList = (NodeList) expression.evaluate(contextNode, XPathConstants.NODESET);
Validate.notNull(nodeList);
} catch (XPathExpressionException | XPathFactoryConfigurationException e) {
throw new Selector.SelectorParseException("Could not evaluate XPath query [%s]: %s", xpath, e.getMessage());
}
return nodeList;
}
use of javax.xml.xpath.XPathFactoryConfigurationException in project cxf by apache.
the class XMLSource method evaluate.
private Object evaluate(String expression, Map<String, String> namespaces, QName type) {
XPathFactory factory = XPathFactory.newInstance();
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
} catch (XPathFactoryConfigurationException e) {
throw new RuntimeException(e);
}
XPath xpath = factory.newXPath();
xpath.setNamespaceContext(new NamespaceContextImpl(namespaces));
boolean releaseDoc = false;
try {
if (stream != null) {
// xalan xpath evaluate parses to a DOM via a DocumentBuilderFactory, but doesn't
// set the SecureProcessing on that. Since a DOM is always created, might as well
// do it via stax and avoid the service factory performance hits that the
// DocumentBuilderFactory will entail as well as get the extra security
// that woodstox provides
setBuffering();
releaseDoc = true;
}
return xpath.compile(expression).evaluate(doc, type);
} catch (XPathExpressionException ex) {
throw new IllegalArgumentException("Illegal XPath expression '" + expression + "'", ex);
} finally {
if (releaseDoc) {
// don't need to maintain the doc
doc = null;
}
}
}
Aggregations