Search in sources :

Example 1 with XPathFunction

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

the class XPathBuilder method createSimpleFunction.

private XPathFunction createSimpleFunction() {
    return new XPathFunction() {

        @SuppressWarnings("rawtypes")
        public Object evaluate(List list) throws XPathFunctionException {
            if (!list.isEmpty()) {
                Object value = list.get(0);
                if (value != null) {
                    String text = exchange.get().getContext().getTypeConverter().convertTo(String.class, value);
                    Language simple = exchange.get().getContext().resolveLanguage("simple");
                    Expression exp = simple.createExpression(text);
                    Object answer = exp.evaluate(exchange.get(), Object.class);
                    return answer;
                }
            }
            return null;
        }
    };
}
Also used : Language(org.apache.camel.spi.Language) XPathExpression(javax.xml.xpath.XPathExpression) Expression(org.apache.camel.Expression) XPathFunction(javax.xml.xpath.XPathFunction) NodeList(org.w3c.dom.NodeList) List(java.util.List)

Example 2 with XPathFunction

use of javax.xml.xpath.XPathFunction in project robovm by robovm.

the class JAXPExtensionsProvider method extFunction.

/**
     * Execute the extension function.
     */
public Object extFunction(String ns, String funcName, Vector argVec, Object methodKey) throws javax.xml.transform.TransformerException {
    try {
        if (funcName == null) {
            String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "Function Name" });
            throw new NullPointerException(fmsg);
        }
        //Find the XPathFunction corresponding to namespace and funcName
        javax.xml.namespace.QName myQName = new QName(ns, funcName);
        // throw XPathFunctionException
        if (extensionInvocationDisabled) {
            String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED, new Object[] { myQName.toString() });
            throw new XPathFunctionException(fmsg);
        }
        // Assuming user is passing all the needed parameters ( including
        // default values )
        int arity = argVec.size();
        javax.xml.xpath.XPathFunction xpathFunction = resolver.resolveFunction(myQName, arity);
        // not using methodKey
        ArrayList argList = new ArrayList(arity);
        for (int i = 0; i < arity; i++) {
            Object argument = argVec.elementAt(i);
            // Explicitly getting NodeList by using nodelist()
            if (argument instanceof XNodeSet) {
                argList.add(i, ((XNodeSet) argument).nodelist());
            } else if (argument instanceof XObject) {
                Object passedArgument = ((XObject) argument).object();
                argList.add(i, passedArgument);
            } else {
                argList.add(i, argument);
            }
        }
        return (xpathFunction.evaluate(argList));
    } catch (XPathFunctionException xfe) {
        // further execution by throwing WrappedRuntimeException 
        throw new org.apache.xml.utils.WrappedRuntimeException(xfe);
    } catch (Exception e) {
        throw new javax.xml.transform.TransformerException(e);
    }
}
Also used : TransformerException(javax.xml.transform.TransformerException) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) TransformerException(javax.xml.transform.TransformerException) XPathFunctionException(javax.xml.xpath.XPathFunctionException) XNodeSet(org.apache.xpath.objects.XNodeSet) XPathFunctionException(javax.xml.xpath.XPathFunctionException) XObject(org.apache.xpath.objects.XObject) QName(javax.xml.namespace.QName) XPathFunction(javax.xml.xpath.XPathFunction) XObject(org.apache.xpath.objects.XObject)

Example 3 with XPathFunction

use of javax.xml.xpath.XPathFunction in project robovm by robovm.

the class JAXPExtensionsProvider method functionAvailable.

/**
     * Is the extension function available?
     */
public boolean functionAvailable(String ns, String funcName) throws javax.xml.transform.TransformerException {
    try {
        if (funcName == null) {
            String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "Function Name" });
            throw new NullPointerException(fmsg);
        }
        //Find the XPathFunction corresponding to namespace and funcName
        javax.xml.namespace.QName myQName = new QName(ns, funcName);
        javax.xml.xpath.XPathFunction xpathFunction = resolver.resolveFunction(myQName, 0);
        if (xpathFunction == null) {
            return false;
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}
Also used : QName(javax.xml.namespace.QName) QName(javax.xml.namespace.QName) XPathFunction(javax.xml.xpath.XPathFunction) TransformerException(javax.xml.transform.TransformerException) XPathFunctionException(javax.xml.xpath.XPathFunctionException)

Example 4 with XPathFunction

use of javax.xml.xpath.XPathFunction in project robovm by robovm.

the class JAXPExtensionsProvider method extFunction.

/**
     * Execute the extension function.
     */
public Object extFunction(FuncExtFunction extFunction, Vector argVec) throws javax.xml.transform.TransformerException {
    try {
        String namespace = extFunction.getNamespace();
        String functionName = extFunction.getFunctionName();
        int arity = extFunction.getArgCount();
        javax.xml.namespace.QName myQName = new javax.xml.namespace.QName(namespace, functionName);
        // throw XPathFunctionException
        if (extensionInvocationDisabled) {
            String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED, new Object[] { myQName.toString() });
            throw new XPathFunctionException(fmsg);
        }
        XPathFunction xpathFunction = resolver.resolveFunction(myQName, arity);
        ArrayList argList = new ArrayList(arity);
        for (int i = 0; i < arity; i++) {
            Object argument = argVec.elementAt(i);
            // Explicitly getting NodeList by using nodelist()
            if (argument instanceof XNodeSet) {
                argList.add(i, ((XNodeSet) argument).nodelist());
            } else if (argument instanceof XObject) {
                Object passedArgument = ((XObject) argument).object();
                argList.add(i, passedArgument);
            } else {
                argList.add(i, argument);
            }
        }
        return (xpathFunction.evaluate(argList));
    } catch (XPathFunctionException xfe) {
        // further execution by throwing WrappedRuntimeException
        throw new org.apache.xml.utils.WrappedRuntimeException(xfe);
    } catch (Exception e) {
        throw new javax.xml.transform.TransformerException(e);
    }
}
Also used : TransformerException(javax.xml.transform.TransformerException) QName(javax.xml.namespace.QName) XPathFunction(javax.xml.xpath.XPathFunction) ArrayList(java.util.ArrayList) TransformerException(javax.xml.transform.TransformerException) XPathFunctionException(javax.xml.xpath.XPathFunctionException) XNodeSet(org.apache.xpath.objects.XNodeSet) XPathFunctionException(javax.xml.xpath.XPathFunctionException) XObject(org.apache.xpath.objects.XObject) QName(javax.xml.namespace.QName) XObject(org.apache.xpath.objects.XObject)

Example 5 with XPathFunction

use of javax.xml.xpath.XPathFunction in project jbpm by kiegroup.

the class XPATHReturnValueEvaluator method evaluate.

public Object evaluate(final ProcessContext context) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpathEvaluator = factory.newXPath();
    xpathEvaluator.setXPathFunctionResolver(new XPathFunctionResolver() {

        public XPathFunction resolveFunction(QName functionName, int arity) {
            String localName = functionName.getLocalPart();
            if ("getVariable".equals(localName)) {
                return new GetVariableData();
            } else {
                throw new IllegalArgumentException("Unknown BPMN function: " + functionName);
            }
        }

        class GetVariableData implements XPathFunction {

            public Object evaluate(List args) throws XPathFunctionException {
                String varname = (String) args.get(0);
                return context.getVariable(varname);
            }
        }
    });
    xpathEvaluator.setXPathVariableResolver(new XPathVariableResolver() {

        public Object resolveVariable(QName variableName) {
            return context.getVariable(variableName.getLocalPart());
        }
    });
    xpathEvaluator.setNamespaceContext(new NamespaceContext() {

        private static final String DROOLS_NAMESPACE_URI = "http://www.jboss.org/drools";

        private String[] prefixes = { "drools", "bpmn2" };

        @Override
        public Iterator getPrefixes(String namespaceURI) {
            return Arrays.asList(prefixes).iterator();
        }

        @Override
        public String getPrefix(String namespaceURI) {
            if (DROOLS_NAMESPACE_URI.equalsIgnoreCase(namespaceURI)) {
                return "bpmn2";
            }
            return null;
        }

        @Override
        public String getNamespaceURI(String prefix) {
            if ("bpmn2".equalsIgnoreCase(prefix)) {
                return DROOLS_NAMESPACE_URI;
            }
            return null;
        }
    });
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    return xpathEvaluator.evaluate(this.expression, builder.newDocument(), XPathConstants.BOOLEAN);
}
Also used : XPath(javax.xml.xpath.XPath) QName(javax.xml.namespace.QName) XPathFunction(javax.xml.xpath.XPathFunction) XPathVariableResolver(javax.xml.xpath.XPathVariableResolver) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NamespaceContext(javax.xml.namespace.NamespaceContext) XPathFunctionResolver(javax.xml.xpath.XPathFunctionResolver) Iterator(java.util.Iterator) List(java.util.List)

Aggregations

XPathFunction (javax.xml.xpath.XPathFunction)8 QName (javax.xml.namespace.QName)7 TransformerException (javax.xml.transform.TransformerException)6 XPathFunctionException (javax.xml.xpath.XPathFunctionException)6 ArrayList (java.util.ArrayList)4 XNodeSet (org.apache.xpath.objects.XNodeSet)4 XObject (org.apache.xpath.objects.XObject)4 List (java.util.List)2 Iterator (java.util.Iterator)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 XPath (javax.xml.xpath.XPath)1 XPathExpression (javax.xml.xpath.XPathExpression)1 XPathFactory (javax.xml.xpath.XPathFactory)1 XPathFunctionResolver (javax.xml.xpath.XPathFunctionResolver)1 XPathVariableResolver (javax.xml.xpath.XPathVariableResolver)1 Expression (org.apache.camel.Expression)1 Language (org.apache.camel.spi.Language)1 NodeList (org.w3c.dom.NodeList)1