Search in sources :

Example 1 with XQueryExpression

use of net.sf.saxon.query.XQueryExpression in project pentaho-platform by pentaho.

the class XQConnection method executeQuery.

/*
   * (non-Javadoc)
   * 
   * @see org.pentaho.connection.IPentahoConnection#executeQuery(java.lang.String)
   */
public IPentahoResultSet executeQuery(final String query, final String[] columnTypes) throws XPathException {
    XQueryExpression exp = sqc.compileQuery(query);
    DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
    try {
        resultSet = new XQResultSet(this, exp, dynamicContext, columnTypes);
    } catch (XPathException e) {
        if (e.getException() instanceof FileNotFoundException) {
            // $NON-NLS-1$
            logger.error(Messages.getInstance().getString("XQConnection.ERROR_0001_UNABLE_TO_READ", query));
        } else {
            // $NON-NLS-1$
            logger.error(Messages.getInstance().getString("XQConnection.ERROR_0002_XQUERY_EXCEPTION", query), e);
        }
    } catch (Throwable t) {
        // $NON-NLS-1$
        logger.error(Messages.getInstance().getErrorString("XQConnection.ERROR_0002_XQUERY_EXCEPTION", query), t);
    }
    lastQuery = query;
    return resultSet;
}
Also used : DynamicQueryContext(net.sf.saxon.query.DynamicQueryContext) XPathException(net.sf.saxon.trans.XPathException) FileNotFoundException(java.io.FileNotFoundException) XQueryExpression(net.sf.saxon.query.XQueryExpression)

Example 2 with XQueryExpression

use of net.sf.saxon.query.XQueryExpression in project ph-schematron by phax.

the class XQueryAsXPathFunctionConverter method loadXQuery.

/**
 * Load XQuery functions from an input stream. As this function is supposed to
 * work with Saxon HE, this method allows only for loading full XQuery modules
 * and not for XQuery libraries.
 *
 * @param aXQueryIS
 *        The Input Stream to read from. May not be <code>null</code>. Will be
 *        closed automatically in this method.
 * @return A non-<code>null</code> {@link MapBasedXPathFunctionResolver}
 *         containing all loaded functions.
 * @throws XPathException
 *         if the syntax of the expression is wrong, or if it references
 *         namespaces, variables, or functions that have not been declared, or
 *         any other static error is reported.
 * @throws IOException
 *         if a failure occurs reading the supplied input.
 */
@Nonnull
public MapBasedXPathFunctionResolver loadXQuery(@Nonnull @WillClose final InputStream aXQueryIS) throws XPathException, IOException {
    ValueEnforcer.notNull(aXQueryIS, "XQueryIS");
    try {
        final MapBasedXPathFunctionResolver aFunctionResolver = new MapBasedXPathFunctionResolver();
        // create a Configuration object
        final Configuration aConfiguration = new Configuration();
        final DynamicQueryContext aDynamicQueryContext = new DynamicQueryContext(aConfiguration);
        final StaticQueryContext aStaticQueryCtx = aConfiguration.newStaticQueryContext();
        // The base URI required for resolving within the XQuery
        aStaticQueryCtx.setBaseURI(m_sBaseURL);
        // null == auto detect
        final String sEncoding = null;
        final XQueryExpression exp = aStaticQueryCtx.compileQuery(aXQueryIS, sEncoding);
        final Controller aXQController = exp.newController(aDynamicQueryContext);
        // find all loaded methods and convert them to XPath functions
        final FunctionLibraryList aFuncLibList = exp.getExecutable().getFunctionLibrary();
        for (final FunctionLibrary aFuncLib : aFuncLibList.getLibraryList()) {
            // Ignore all Vendor, System etc. internal libraries
            if (aFuncLib instanceof FunctionLibraryList) {
                // This block works with Saxon HE 9.5.1-x :)
                // This is the custom function library list
                final FunctionLibraryList aRealFuncLib = (FunctionLibraryList) aFuncLib;
                for (final FunctionLibrary aNestedFuncLib : aRealFuncLib.getLibraryList()) {
                    // Currently the user functions are in ExecutableFunctionLibrary
                    if (aNestedFuncLib instanceof ExecutableFunctionLibrary)
                        for (final UserFunction aUserFunc : new IterableIterator<>(((ExecutableFunctionLibrary) aNestedFuncLib).iterateFunctions())) {
                            // Saxon 9.7 changes "getNumberOfArguments" to "getArity"
                            aFunctionResolver.addUniqueFunction(aUserFunc.getFunctionName().getNamespaceBinding().getURI(), aUserFunc.getFunctionName().getLocalPart(), aUserFunc.getArity(), new XPathFunctionFromUserFunction(aConfiguration, aXQController, aUserFunc));
                        }
                }
            } else if (aFuncLib instanceof XQueryFunctionLibrary) {
                // This block works with Saxon HE 9.6.0-x :)
                final XQueryFunctionLibrary aRealFuncLib = (XQueryFunctionLibrary) aFuncLib;
                for (final XQueryFunction aXQueryFunction : new IterableIterator<>(aRealFuncLib.getFunctionDefinitions())) {
                    // Ensure the function is compiled
                    aXQueryFunction.compile();
                    aFunctionResolver.addUniqueFunction(aXQueryFunction.getFunctionName().getNamespaceBinding().getURI(), aXQueryFunction.getFunctionName().getLocalPart(), aXQueryFunction.getNumberOfArguments(), new XPathFunctionFromUserFunction(aConfiguration, aXQController, aXQueryFunction.getUserFunction()));
                }
            }
        }
        return aFunctionResolver;
    } finally {
        StreamHelper.close(aXQueryIS);
    }
}
Also used : FunctionLibraryList(net.sf.saxon.functions.FunctionLibraryList) Configuration(net.sf.saxon.Configuration) XQueryFunctionLibrary(net.sf.saxon.query.XQueryFunctionLibrary) MapBasedXPathFunctionResolver(com.helger.xml.xpath.MapBasedXPathFunctionResolver) XQueryExpression(net.sf.saxon.query.XQueryExpression) ExecutableFunctionLibrary(net.sf.saxon.functions.ExecutableFunctionLibrary) FunctionLibrary(net.sf.saxon.functions.FunctionLibrary) XQueryFunctionLibrary(net.sf.saxon.query.XQueryFunctionLibrary) Controller(net.sf.saxon.Controller) StaticQueryContext(net.sf.saxon.query.StaticQueryContext) ExecutableFunctionLibrary(net.sf.saxon.functions.ExecutableFunctionLibrary) DynamicQueryContext(net.sf.saxon.query.DynamicQueryContext) UserFunction(net.sf.saxon.expr.instruct.UserFunction) XQueryFunction(net.sf.saxon.query.XQueryFunction) Nonnull(javax.annotation.Nonnull)

Example 3 with XQueryExpression

use of net.sf.saxon.query.XQueryExpression in project camel by apache.

the class XQueryBuilder method evaluateAsDOM.

public Node evaluateAsDOM(Exchange exchange) throws Exception {
    LOG.debug("evaluateAsDOM: {} for exchange: {}", expression, exchange);
    initialize(exchange);
    DOMResult result = new DOMResult();
    DynamicQueryContext context = createDynamicContext(exchange);
    XQueryExpression expression = getExpression();
    expression.pull(context, result, properties);
    return result.getNode();
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) DynamicQueryContext(net.sf.saxon.query.DynamicQueryContext) XQueryExpression(net.sf.saxon.query.XQueryExpression)

Aggregations

DynamicQueryContext (net.sf.saxon.query.DynamicQueryContext)3 XQueryExpression (net.sf.saxon.query.XQueryExpression)3 MapBasedXPathFunctionResolver (com.helger.xml.xpath.MapBasedXPathFunctionResolver)1 FileNotFoundException (java.io.FileNotFoundException)1 Nonnull (javax.annotation.Nonnull)1 DOMResult (javax.xml.transform.dom.DOMResult)1 Configuration (net.sf.saxon.Configuration)1 Controller (net.sf.saxon.Controller)1 UserFunction (net.sf.saxon.expr.instruct.UserFunction)1 ExecutableFunctionLibrary (net.sf.saxon.functions.ExecutableFunctionLibrary)1 FunctionLibrary (net.sf.saxon.functions.FunctionLibrary)1 FunctionLibraryList (net.sf.saxon.functions.FunctionLibraryList)1 StaticQueryContext (net.sf.saxon.query.StaticQueryContext)1 XQueryFunction (net.sf.saxon.query.XQueryFunction)1 XQueryFunctionLibrary (net.sf.saxon.query.XQueryFunctionLibrary)1 XPathException (net.sf.saxon.trans.XPathException)1