Search in sources :

Example 6 with XPathFactoryConfigurationException

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

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

Example 8 with XPathFactoryConfigurationException

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);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) XPathFactoryConfigurationException(javax.xml.xpath.XPathFactoryConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 9 with XPathFactoryConfigurationException

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

Example 10 with XPathFactoryConfigurationException

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);
}
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)

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