Search in sources :

Example 6 with Configuration

use of net.sf.saxon.Configuration in project sirix by sirixdb.

the class XQueryEvaluator method call.

@Override
public XdmValue call() throws Exception {
    XdmValue value = null;
    try {
        final Processor proc = new Processor(false);
        final Configuration config = proc.getUnderlyingConfiguration();
        final NodeInfo doc = new DocumentWrapper(mSession, config);
        final XQueryCompiler comp = proc.newXQueryCompiler();
        final XQueryExecutable exp = comp.compile(mExpression);
        final net.sf.saxon.s9api.XQueryEvaluator exe = exp.load();
        exe.setSource(doc);
        value = exe.evaluate();
    } catch (final SaxonApiException e) {
        LOGGER.error("Saxon Exception: " + e.getMessage(), e);
        throw e;
    }
    return value;
}
Also used : XdmValue(net.sf.saxon.s9api.XdmValue) XQueryExecutable(net.sf.saxon.s9api.XQueryExecutable) DocumentWrapper(org.sirix.saxon.wrapper.DocumentWrapper) Processor(net.sf.saxon.s9api.Processor) Configuration(net.sf.saxon.Configuration) NodeInfo(net.sf.saxon.om.NodeInfo) XQueryCompiler(net.sf.saxon.s9api.XQueryCompiler) SaxonApiException(net.sf.saxon.s9api.SaxonApiException)

Example 7 with Configuration

use of net.sf.saxon.Configuration in project sirix by sirixdb.

the class XQueryEvaluatorOutputStream method call.

@Override
public Void call() throws Exception {
    try {
        final Processor proc = new Processor(false);
        final Configuration config = proc.getUnderlyingConfiguration();
        final NodeInfo doc = new DocumentWrapper(mSession, config);
        final XQueryCompiler comp = proc.newXQueryCompiler();
        final XQueryExecutable exp = comp.compile(mExpression);
        if (mSerializer == null) {
            final Serializer out = new Serializer();
            out.setOutputProperty(Serializer.Property.METHOD, "xml");
            out.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes");
            out.setOutputStream(mOut);
            mSerializer = out;
        }
        final net.sf.saxon.s9api.XQueryEvaluator exe = exp.load();
        exe.setSource(doc);
        exe.run(mSerializer);
        return null;
    } catch (final SaxonApiException e) {
        LOGGER.error("Saxon Exception: " + e.getMessage(), e);
        throw e;
    }
}
Also used : XQueryExecutable(net.sf.saxon.s9api.XQueryExecutable) DocumentWrapper(org.sirix.saxon.wrapper.DocumentWrapper) Processor(net.sf.saxon.s9api.Processor) Configuration(net.sf.saxon.Configuration) NodeInfo(net.sf.saxon.om.NodeInfo) XQueryCompiler(net.sf.saxon.s9api.XQueryCompiler) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) Serializer(net.sf.saxon.s9api.Serializer)

Example 8 with Configuration

use of net.sf.saxon.Configuration in project teiid by teiid.

the class XQueryEvaluator method evaluate.

public static Object evaluate(XMLType value, XMLCast expression, CommandContext context) throws ExpressionEvaluationException {
    Configuration config = new Configuration();
    Type t = value.getType();
    try {
        Item i = null;
        switch(t) {
            case CONTENT:
            // content could map to an array value, but we aren't handling that case here yet - only in xmltable
            case COMMENT:
            case PI:
                throw new FunctionExecutionException();
            case TEXT:
                i = new StringValue(value.getString());
                break;
            case UNKNOWN:
            case DOCUMENT:
            case ELEMENT:
                StreamSource ss = value.getSource(StreamSource.class);
                try {
                    i = config.buildDocument(ss);
                } finally {
                    if (ss.getInputStream() != null) {
                        ss.getInputStream().close();
                    }
                    if (ss.getReader() != null) {
                        ss.getReader().close();
                    }
                }
                break;
            default:
                // $NON-NLS-1$
                throw new AssertionError("Unknown xml value type " + t);
        }
        return XMLTableNode.getValue(expression.getType(), i, config, context);
    } catch (IOException e) {
        throw new FunctionExecutionException(e);
    } catch (ValidationException e) {
        throw new FunctionExecutionException(e);
    } catch (TransformationException e) {
        throw new FunctionExecutionException(e);
    } catch (XPathException e) {
        throw new FunctionExecutionException(e);
    } catch (SQLException e) {
        throw new FunctionExecutionException(e);
    }
}
Also used : Item(net.sf.saxon.om.Item) BinaryType(org.teiid.core.types.BinaryType) XMLType(org.teiid.core.types.XMLType) DocType(nu.xom.DocType) Type(org.teiid.core.types.XMLType.Type) TransformationException(org.teiid.core.types.TransformationException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) ValidationException(net.sf.saxon.type.ValidationException) Configuration(net.sf.saxon.Configuration) XPathException(net.sf.saxon.trans.XPathException) SQLException(java.sql.SQLException) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) StringValue(net.sf.saxon.value.StringValue)

Example 9 with Configuration

use of net.sf.saxon.Configuration 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 10 with Configuration

use of net.sf.saxon.Configuration in project camel by apache.

the class XQueryEndpointConfigurationTest method testConfiguration.

@Test
public void testConfiguration() throws Exception {
    Configuration configuration = context.getRegistry().lookupByNameAndType("saxon-configuration", Configuration.class);
    Map<String, Object> properties = context.getRegistry().lookupByNameAndType("saxon-properties", Map.class);
    XQueryComponent component = context.getComponent("xquery", XQueryComponent.class);
    XQueryEndpoint endpoint = null;
    assertNotNull(configuration);
    assertNotNull(properties);
    for (Endpoint ep : context.getEndpoints()) {
        if (ep instanceof XQueryEndpoint) {
            endpoint = (XQueryEndpoint) ep;
            break;
        }
    }
    assertNotNull(component);
    assertNotNull(endpoint);
    assertNull(component.getConfiguration());
    assertTrue(component.getConfigurationProperties().isEmpty());
    assertNotNull(endpoint.getConfiguration());
    assertNotNull(endpoint.getConfigurationProperties());
    assertEquals(configuration, endpoint.getConfiguration());
    assertEquals(properties, endpoint.getConfigurationProperties());
}
Also used : Configuration(net.sf.saxon.Configuration) Endpoint(org.apache.camel.Endpoint) Test(org.junit.Test)

Aggregations

Configuration (net.sf.saxon.Configuration)16 Processor (net.sf.saxon.s9api.Processor)7 NodeInfo (net.sf.saxon.om.NodeInfo)6 DocumentWrapper (org.sirix.saxon.wrapper.DocumentWrapper)5 SaxonApiException (net.sf.saxon.s9api.SaxonApiException)4 StreamSource (javax.xml.transform.stream.StreamSource)3 XQueryCompiler (net.sf.saxon.s9api.XQueryCompiler)3 XQueryExecutable (net.sf.saxon.s9api.XQueryExecutable)3 DocumentInfo (net.sf.saxon.om.DocumentInfo)2 Item (net.sf.saxon.om.Item)2 DynamicQueryContext (net.sf.saxon.query.DynamicQueryContext)2 Serializer (net.sf.saxon.s9api.Serializer)2 Test (org.junit.Test)2 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)2 ResourceConfiguration (org.sirix.access.conf.ResourceConfiguration)2 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)2 MapBasedXPathFunctionResolver (com.helger.xml.xpath.MapBasedXPathFunctionResolver)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 SQLException (java.sql.SQLException)1