@Override public ValidationResult validate(final String subject, final String input, final ValidationContext validationContext) { try { final Processor proc = new Processor(false); final XQueryCompiler comp = proc.newXQueryCompiler(); String error = null; try { comp.compile(input); } catch (final Exception e) { error = e.toString(); } return new ValidationResult.Builder().input(input).subject(subject).valid(error == null).explanation(error).build(); } catch (final Exception e) { return new ValidationResult.Builder().input(input).subject(subject).valid(false) .explanation("Unable to initialize XQuery engine due to " + e.toString()).build(); } } }
Processor qtproc = runtime.getProcessor(); XQueryCompiler xqcomp = qtproc.newXQueryCompiler(); xqcomp.setBaseURI(root.getBaseURI()); if (xqcomp.getModuleURIResolver() == null) { xqcomp.setModuleURIResolver(runtime.getResolver()); XQueryExecutable xqexec = xqcomp.compile(queryString); XQueryEvaluator xqeval = xqexec.load(); if (document != null) {
public static void serialize(XProcRuntime xproc, Vector<XdmNode> nodes, Serializer serializer) throws SaxonApiException { Processor qtproc = xproc.getProcessor(); XQueryCompiler xqcomp = qtproc.newXQueryCompiler(); xqcomp.setModuleURIResolver(xproc.getResolver()); xqcomp.getUnderlyingStaticContext().setSchemaAware( xqcomp.getProcessor().getUnderlyingConfiguration().isLicensedFeature( Configuration.LicenseFeature.ENTERPRISE_XQUERY)); XQueryExecutable xqexec = xqcomp.compile("."); XQueryEvaluator xqeval = xqexec.load(); if (xproc.getHtmlSerializer() && "html".equals(serializer.getOutputProperty(Serializer.Property.METHOD))) {
compiler.setErrorListener(new DefaultErrorListener()); compiler.setCompileWithTracing(isTracing); compiler.setLanguageVersion(getConfigs().getString(config, "languageVersion", "1.0")); executable = compiler.compile(query); executable = compiler.compile(new File(queryFile));
if (queryFileName.equals("-")) { Reader queryReader = new InputStreamReader(System.in); exp = compiler.compile(queryReader); } else if (queryFileName.startsWith("{") && queryFileName.endsWith("}")) { compiler.setBaseURI(new File(System.getProperty("user.dir")).toURI()); exp = compiler.compile(q); } else if (useURLs || CommandLineOptions.isImplicitURI(queryFileName)) { ModuleURIResolver resolver = compiler.getModuleURIResolver(); boolean isStandardResolver = false; if (resolver == null) { exp = compiler.compile(queryText); } catch (XPathException e) { throw new SaxonApiException(e); compiler.setBaseURI(new File(queryFileName).toURI()); exp = compiler.compile(queryStream);
XQueryCompiler xQueryCompiler = getXQueryCompiler(); if (errorListener != null) { xQueryCompiler.setErrorListener(errorListener); xQueryCompiler.setBaseURI(baseURI); xquery = xQueryCompiler.compile(exprString); } catch (SaxonApiException e) { throw new LuxException (e); xquery = xQueryCompiler.compile(queryString); } catch (SaxonApiException e) { throw new LuxException (e);
@Override public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException { String query = arguments[0].head().getStringValue(); Evaluator eval = SearchBase.getEvaluator(context); XQueryCompiler xqueryCompiler = eval.getCompiler().getXQueryCompiler(); xqueryCompiler.setErrorListener(eval.getErrorListener()); try { // TODO: cache compiled xslt somewhere XQueryExecutable xqueryExec= xqueryCompiler.compile(query); evaluator = xqueryExec.load(); evaluator.setErrorListener(eval.getErrorListener()); if (arguments.length > 1) { bindParameters(arguments[1]); } XdmValue result = evaluator.evaluate(); ArrayList<TransformerException> runtimeErrors = eval.getErrorListener().getErrors(); if (!runtimeErrors.isEmpty()) { throw new XPathException(runtimeErrors.get(0).getMessage(), runtimeErrors.get(0).getLocator(), runtimeErrors.get(0)); } return result.getUnderlyingValue(); } catch (SaxonApiException e) { throw new XPathException (e); } }
/** * Evaluates an XQuery 1.0 expression using the Saxon s9api interfaces. * * @param source The XML Source. * @param query The query expression. * @param nsBindings A collection of namespace bindings required to evaluate * the query, where each entry maps a namespace URI (key) to a prefix * (value). * @return An XdmValue object representing a value in the XDM data model. * @throws SaxonApiException If an error occurs while evaluating the query * (this always wraps some other underlying exception). */ public static XdmValue evaluateXQuery(Source source, String query, Map<String, String> nsBindings) throws SaxonApiException { Processor proc = new Processor(false); XQueryCompiler xqCompiler = proc.newXQueryCompiler(); if (null != nsBindings) { for (String nsURI : nsBindings.keySet()) { xqCompiler.declareNamespace(nsBindings.get(nsURI), nsURI); } } XQueryExecutable xqExec = xqCompiler.compile(query); XQueryEvaluator xqEval = xqExec.load(); xqEval.setSource(source); return xqEval.evaluate(); }
xqcomp.setModuleURIResolver(runtime.getResolver()); XQueryExecutable xqexec = xqcomp.compile(queryexpr); XQueryEvaluator xqeval = xqexec.load(); xqeval.setContextItem(srcDoc);
@Test public void testFormatNumber() throws SaxonApiException { Processor proc = new Processor(true); XQueryCompiler comp = proc.newXQueryCompiler(); comp.setLanguageVersion("3.0"); XQueryExecutable exp = comp.compile("xquery version \"3.0\"; format-number(number('123.4'),'#.00000000')"); XQueryEvaluator qe = exp.load(); Serializer out = new Serializer(new StringWriter()); qe.run(out); for(XdmItem i : qe) { System.out.println(i.getStringValue()); } }
xqueryCompiler.setEncoding(CharEncoding.UTF_8); exp = xqueryCompiler.compile(xQueryExpression); eval = exp.load();
compiler.setSchemaAware(schemaAware); compiler.setUpdatingEnabled(true); compiler.setCompileWithTracing(true); badUsage(moduleURIResolverClass + " is not a ModuleURIResolver"); compiler.setModuleURIResolver((ModuleURIResolver) mr); compiler.setStreaming(streaming);
/** * Say whether the query should be compiled and evaluated to use streaming. * This affects subsequent calls on the compile() methods. This option requires * Saxon-EE. * * @param option if true, the compiler will attempt to compile a query to be * capable of executing in streaming mode. If the query cannot be streamed, * a compile-time exception is reported. In streaming mode, the source * document is supplied as a stream, and no tree is built in memory. The default * is false. * <p> * <p>When setStreaming(true) is specified, this has the additional side-effect of setting the required * context item type to "document-node()" * @since 9.6 */ public void setStreaming(boolean option) { env.setStreaming(option); // We check this again more securely, but it's good to give the error as soon as possible if (option && !processor.getUnderlyingConfiguration().isLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XQUERY)) { throw new UnsupportedOperationException("Streaming requires a Saxon-EE license"); } if (option) { setRequiredContextItemType(new ConstructedItemType(NodeKindTest.DOCUMENT, getProcessor())); } }
/** * Import a compiled XQuery library. This makes pre-compiled XQuery library modules available * to the <code>saxon:import-query</code> declaration. * @param queryCompiler An XQueryCompiler that has been used to compile a library of XQuery functions * (by using one of the overloaded methods named <code>compileLibrary</code>). */ public void importXQueryEnvironment(XQueryCompiler queryCompiler) { compilerInfo.setXQueryLibraries(queryCompiler.getUnderlyingStaticContext().getCompiledLibraries()); }
public XQueryCompiler getXQueryCompiler () { XQueryCompiler xqueryCompiler = processor.newXQueryCompiler(); for (java.util.Map.Entry<String, String> binding : namespaceBindings.entrySet()) { xqueryCompiler.declareNamespace(binding.getKey(), binding.getValue()); } xqueryCompiler.declareNamespace("lux", FunCall.LUX_NAMESPACE); return xqueryCompiler; }
/** * Request fast compilation. Fast compilation will generally be achieved at the expense of run-time performance * and quality of diagnostics. Fast compilation is a good trade-off if (a) the expression is known to be correct, * and (b) once compiled, the expression is only executed once against a document of modest size. * <p><i>The current implementation is equivalent to switching off all optimizations. Setting this option, however, * indicates an intent rather than a mechanism, and the implementation details may change in future to reflect * the intent.</i></p> * * @param fast set to true to request fast compilation; set to false to revert to the optimization options * defined in the Configuration. * @since 9.9 */ public void setFastCompilation(boolean fast) { if (fast) { env.setOptimizerOptions(new OptimizerOptions(0)); } else { env.setOptimizerOptions(getProcessor().getUnderlyingConfiguration().getOptimizerOptions()); } }
Processor qtproc = runtime.getProcessor(); XQueryCompiler xqcomp = qtproc.newXQueryCompiler(); xqcomp.setBaseURI(root.getBaseURI()); if (xqcomp.getModuleURIResolver() == null) { xqcomp.setModuleURIResolver(runtime.getResolver()); XQueryExecutable xqexec = xqcomp.compile(queryString); XQueryEvaluator xqeval = xqexec.load(); if (document != null) {
public static void serialize(XProcRuntime xproc, Vector<XdmNode> nodes, Serializer serializer) throws SaxonApiException { Processor qtproc = xproc.getProcessor(); XQueryCompiler xqcomp = qtproc.newXQueryCompiler(); xqcomp.setModuleURIResolver(xproc.getResolver()); xqcomp.getUnderlyingStaticContext().setSchemaAware( xqcomp.getProcessor().getUnderlyingConfiguration().isLicensedFeature( Configuration.LicenseFeature.ENTERPRISE_XQUERY)); XQueryExecutable xqexec = xqcomp.compile("."); XQueryEvaluator xqeval = xqexec.load(); if (xproc.getHtmlSerializer() && "html".equals(serializer.getOutputProperty(Serializer.Property.METHOD))) {
compiler.setErrorListener(new DefaultErrorListener()); compiler.setCompileWithTracing(isTracing); compiler.setLanguageVersion(getConfigs().getString(config, "languageVersion", "1.0")); executable = compiler.compile(query); executable = compiler.compile(new File(queryFile));
if (queryFileName.equals("-")) { Reader queryReader = new InputStreamReader(System.in); exp = compiler.compile(queryReader); } else if (queryFileName.startsWith("{") && queryFileName.endsWith("}")) { compiler.setBaseURI(new File(System.getProperty("user.dir")).toURI()); exp = compiler.compile(q); } else if (useURLs || CommandLineOptions.isImplicitURI(queryFileName)) { ModuleURIResolver resolver = compiler.getModuleURIResolver(); boolean isStandardResolver = false; if (resolver == null) { exp = compiler.compile(queryText); } catch (XPathException e) { throw new SaxonApiException(e); compiler.setBaseURI(new File(queryFileName).toURI()); exp = compiler.compile(queryStream);