Search in sources :

Example 1 with XPathFactoryConfigurationException

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);
    }
}
Also used : Policy(java.security.Policy) XPathExpressionException(javax.xml.xpath.XPathExpressionException) AllPermission(java.security.AllPermission) XPathFactoryConfigurationException(javax.xml.xpath.XPathFactoryConfigurationException)

Example 2 with XPathFactoryConfigurationException

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);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) NamespaceContext(javax.xml.namespace.NamespaceContext) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Iterator(java.util.Iterator) XPathFactoryConfigurationException(javax.xml.xpath.XPathFactoryConfigurationException)

Example 3 with XPathFactoryConfigurationException

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);
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) XPathFactoryConfigurationException(javax.xml.xpath.XPathFactoryConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) URL(java.net.URL) SAXException(org.xml.sax.SAXException) Test(org.junit.Test)

Example 4 with XPathFactoryConfigurationException

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;
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) XPathFactoryConfigurationException(javax.xml.xpath.XPathFactoryConfigurationException) Selector(org.jsoup.select.Selector)

Example 5 with XPathFactoryConfigurationException

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;
        }
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) XPathFactoryConfigurationException(javax.xml.xpath.XPathFactoryConfigurationException)

Aggregations

XPathExpressionException (javax.xml.xpath.XPathExpressionException)12 XPathFactoryConfigurationException (javax.xml.xpath.XPathFactoryConfigurationException)12 XPath (javax.xml.xpath.XPath)7 TransformerException (javax.xml.transform.TransformerException)4 NodeList (org.w3c.dom.NodeList)4 XPathExpression (javax.xml.xpath.XPathExpression)3 XPathFactory (javax.xml.xpath.XPathFactory)3 IOException (java.io.IOException)2 URL (java.net.URL)2 AllPermission (java.security.AllPermission)2 Policy (java.security.Policy)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Test (org.junit.Test)2 Node (org.w3c.dom.Node)2 SAXException (org.xml.sax.SAXException)2 Iterator (java.util.Iterator)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 XPathFunctionException (javax.xml.xpath.XPathFunctionException)1 XPathFunctionResolver (javax.xml.xpath.XPathFunctionResolver)1 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)1