Search in sources :

Example 1 with XPathFunctionResolver

use of javax.xml.xpath.XPathFunctionResolver in project camel by apache.

the class XPathTest method testUsingJavaExtensions.

public void testUsingJavaExtensions() throws Exception {
    Object instance = null;
    // we may not have Xalan on the classpath
    try {
        instance = Class.forName("org.apache.xalan.extensions.XPathFunctionResolverImpl").newInstance();
    } catch (Throwable e) {
        log.debug("Could not find Xalan on the classpath so ignoring this test case: " + e);
    }
    if (instance instanceof XPathFunctionResolver) {
        XPathFunctionResolver functionResolver = (XPathFunctionResolver) instance;
        XPathBuilder builder = xpath("java:" + getClass().getName() + ".func(string(/header/value))").namespace("java", "http://xml.apache.org/xalan/java").functionResolver(functionResolver).stringResult();
        String xml = "<header><value>12</value></header>";
        // it can throw the exception if we put the xalan into the test class path
        assertExpression(builder, xml, "modified12");
    }
}
Also used : XPathFunctionResolver(javax.xml.xpath.XPathFunctionResolver)

Example 2 with XPathFunctionResolver

use of javax.xml.xpath.XPathFunctionResolver 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)

Aggregations

XPathFunctionResolver (javax.xml.xpath.XPathFunctionResolver)2 XPath (javax.xml.xpath.XPath)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 XPathFactoryConfigurationException (javax.xml.xpath.XPathFactoryConfigurationException)1 XPathFunctionException (javax.xml.xpath.XPathFunctionException)1 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)1 RuntimeExpressionException (org.apache.camel.RuntimeExpressionException)1