/** * Compile a string containing a source query * transformations * * @param context the XPath dynamic evaluation context * @param query a string containing the query to be compiled * @return the compiled query */ public static XQueryExpression compileQuery(XPathContext context, String query) throws XPathException { if (query == null) { return null; } StaticQueryContext sqc = new StaticQueryContext(context.getConfiguration()); return sqc.compileQuery(query); }
/** * Set the default element namespace * * @param uri the namespace URI to be used as the default namespace for elements and types * @since 8.4 */ public void setDefaultElementNamespace(String uri) { defaultElementNamespace = uri; declareNamespace("", uri); }
/** * Load a query library module * @return the QueryModule object representing the loaded library module */ private QueryModule loadModule() throws XPathException { // Create a dummy main query module and compile it StaticQueryContext sqc = new StaticQueryContext(getConfiguration()); sqc.setExecutable(getExecutable()); sqc.setBaseURI(getBaseURI()); String mainModule = "import module namespace m = \"" + moduleURI + "\" at \"" + href + "\"; ()"; XQueryExpression exp = sqc.compileQuery(mainModule); return exp.getStaticContext(); }
/** * Static method used to create the default static context: intended for internal use only */ public static StaticQueryContext makeDefaultStaticQueryContext(Configuration config) { StaticQueryContext sqc = new StaticQueryContext(); sqc.config = config; sqc.namePool = config.getNamePool(); sqc.reset(); return sqc; }
/** * Compile a library module supplied as a file. The code generated by compiling the library is available * for importing by all subsequent compilations using the same XQueryCompiler; it is identified by an * "import module" declaration that specifies the module URI of the library module. No module location * hint is required, and if one is present, it is ignored. * <p>The encoding of the input stream may be specified using {@link #setEncoding(String)}; * if this has not been set, the compiler determines the encoding from the version header of the * query, and if that too is absent, it assumes UTF-8.</p> * <p>Separate compilation of library modules is supported only under Saxon-EE</p> * * @param query the file containing the query. The URI corresponding to this file will be used as the * base URI of the query, overriding any URI supplied using {@link #setBaseURI(java.net.URI)} (but not * overriding any base URI specified within the query prolog) * @throws SaxonApiException if the query compilation fails with a static error * @throws IOException if the file does not exist or cannot be read * @since 9.2 */ public void compileLibrary(File query) throws SaxonApiException, IOException { try (FileInputStream stream = new FileInputStream(query)) { String savedBaseUri = env.getBaseURI(); env.setBaseURI(query.toURI().toString()); env.compileLibrary(stream, encoding); env.setBaseURI(savedBaseUri); } catch (XPathException e) { throw new SaxonApiException(e); } }
sqc.setSchemaAware(false); String value = props.getProperty(pn_xqj_baseURI); if (value != null && !value.isEmpty()) { sqc.setBaseURI(value); sqc.setPreserveBoundarySpace(String.valueOf(BOUNDARY_SPACE_PRESERVE).equals(value)); sqc.setConstructionMode(Validation.PRESERVE); } else { sqc.setConstructionMode(Validation.STRIP); sqc.declareDefaultCollation(value); sqc.setDefaultElementNamespace(value); sqc.setDefaultFunctionNamespace(value); sqc.setEmptyLeast(String.valueOf(DEFAULT_ORDER_FOR_EMPTY_SEQUENCES_LEAST).equals(value)); sqc.setInheritNamespaces(String.valueOf(COPY_NAMESPACES_MODE_INHERIT).equals(value)); sqc.setPreserveNamespaces(String.valueOf(COPY_NAMESPACES_MODE_PRESERVE).equals(value)); sqc.clearNamespaces(); StringTokenizer st = new StringTokenizer(value, " "); while (st.hasMoreTokens()) { String namespace = st.nextToken(); int idx = namespace.indexOf(":"); sqc.declareNamespace(namespace.substring(0, idx), namespace.substring(idx + 1));
return compileQuery(sb.toString());
protected void setStaticContext(StaticQueryContext sqc, XQStaticContext ctx) throws XQException { sqc.setSchemaAware(false); sqc.setBaseURI(ctx.getBaseURI()); sqc.setPreserveBoundarySpace(ctx.getBoundarySpacePolicy() == BOUNDARY_SPACE_PRESERVE); if (ctx.getConstructionMode() == CONSTRUCTION_MODE_PRESERVE) { sqc.setConstructionMode(Validation.PRESERVE); } else { sqc.setConstructionMode(Validation.STRIP); sqc.setInheritNamespaces(ctx.getCopyNamespacesModeInherit() == COPY_NAMESPACES_MODE_INHERIT); sqc.setPreserveNamespaces(ctx.getCopyNamespacesModePreserve() == COPY_NAMESPACES_MODE_PRESERVE); sqc.declareDefaultCollation(ctx.getDefaultCollation()); sqc.setDefaultElementNamespace(ctx.getDefaultElementTypeNamespace()); sqc.setDefaultFunctionNamespace(ctx.getDefaultFunctionNamespace()); sqc.setEmptyLeast(ctx.getDefaultOrderForEmptySequences() == DEFAULT_ORDER_FOR_EMPTY_SEQUENCES_LEAST); sqc.clearNamespaces(); String[] prefixes = ctx.getNamespacePrefixes(); for (String prefix: prefixes) { sqc.declareNamespace(prefix, ctx.getNamespaceURI(prefix)); sqc.setLanguageVersion(saxon_xquery_version);
if (sqc != null) { inheritNamespaces = sqc.isInheritNamespaces(); preserveNamespaces = sqc.isPreserveNamespaces(); preserveSpace = sqc.isPreserveBoundarySpace(); defaultEmptyLeast = sqc.isEmptyLeast(); defaultFunctionNamespace = sqc.getDefaultFunctionNamespace(); defaultElementNamespace = sqc.getDefaultElementNamespace(); defaultCollationName = sqc.getDefaultCollationName(); constructionMode = sqc.getConstructionMode(); if (constructionMode == Validation.PRESERVE && !sqc.isSchemaAware()) { requiredContextItemType = sqc.getRequiredContextItemType(); isUpdating = sqc.isUpdatingEnabled(); codeInjector = sqc.getCodeInjector(); optimizerOptions = sqc.getOptimizerOptions();
return (T)Boolean.valueOf(getDefaultStaticQueryContext().isUpdatingEnabled()); return (T)Integer.valueOf(getDefaultStaticQueryContext().getConstructionMode()); return (T)getDefaultStaticQueryContext().getDefaultElementNamespace(); return (T)getDefaultStaticQueryContext().getDefaultFunctionNamespace(); return (T) Boolean.valueOf(getDefaultStaticQueryContext().isEmptyLeast()); return (T) Boolean.valueOf(getDefaultStaticQueryContext().isInheritNamespaces()); return (T) Boolean.valueOf(getDefaultStaticQueryContext().isPreserveBoundarySpace()); return (T) Boolean.valueOf(getDefaultStaticQueryContext().isPreserveNamespaces()); return (T) getDefaultStaticQueryContext().getRequiredContextItemType(); return (T) Boolean.valueOf(getDefaultStaticQueryContext().isSchemaAware()); return (T)getDefaultStaticQueryContext().getErrorListener().getClass().getName();
explicitPrologNamespaces = new HashMap(10); if (sqc != null) { executable = sqc.getExecutable(); inheritNamespaces = sqc.isInheritNamespaces(); preserveNamespaces = sqc.isPreserveNamespaces(); preserveSpace = sqc.isPreserveBoundarySpace(); defaultEmptyLeast = sqc.isEmptyLeast(); defaultFunctionNamespace = sqc.getDefaultFunctionNamespace(); defaultElementNamespace = sqc.getDefaultElementNamespace(); defaultCollationName = sqc.getDefaultCollationName(); constructionMode = sqc.getConstructionMode(); isUpdating = sqc.isUpdatingEnabled();
if (item.getPrefix() == null) { if (item.getUri() == null) { context.setDefaultElementNamespace(EMPTY_STRING); ic.setDefaultElementNamespace(EMPTY_STRING); } else { context.setDefaultElementNamespace(item.getUri()); ic.setDefaultElementNamespace(item.getUri()); namespaceMap.put(EMPTY_STRING, item.getUri()); context.declareNamespace(item.getPrefix(), item.getUri()); ic.declareNamespace(item.getPrefix(), item.getUri()); namespaceMap.put(item.getPrefix(), item.getUri()); context.declareGlobalVariable(StructuredQName.fromClarkName(derivedColumn.getAlias()), SequenceType.ANY_SEQUENCE, EmptySequence.getInstance(), true); } catch (XPathException e) { this.xQuery = context.compileQuery(xQueryString); } catch (XPathException e) { throw new QueryResolverException(QueryPlugin.Event.TEIID30154, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30154, xQueryString));
/** * Main method for testing * @param args Takes one argument, the XPath expression to be analyzed * @throws Exception */ public static void main(String[] args) throws Exception { Configuration config = new Configuration(); Expression exp; if (args[0].equals("xpath")) { XPathEvaluator xpath = new XPathEvaluator(config); XPathExpression xpexp = xpath.createExpression(args[1]); exp = xpexp.getInternalExpression(); } else if (args[0].equals("xquery")) { StaticQueryContext sqc = new StaticQueryContext(config); sqc.setBaseURI(new File(args[1]).toURI().toString()); XQueryExpression xqe = sqc.compileQuery(new FileReader(args[1])); exp = xqe.getExpression(); } else { throw new IllegalArgumentException("first argument must be xpath or xquery"); } exp.explain(System.err); PathMap initialPath = new PathMap(exp); initialPath.diagnosticDump(System.err); PathMapRoot[] roots = initialPath.getPathMapRoots(); for (int i=0; i<roots.length; i++) { initialPath.reduceToDownwardsAxes(roots[i]); } System.err.println("AFTER REDUCTION:"); initialPath.diagnosticDump(System.err); }
if (queryFileName.equals("-")) { Reader queryReader = new InputStreamReader(System.in); exp = staticEnv.compileQuery(queryReader); } else if (queryFileName.startsWith("{") && queryFileName.endsWith("}")) { exp = staticEnv.compileQuery(q); } else if (useURLs || queryFileName.startsWith("http:") || queryFileName.startsWith("file:")) { ModuleURIResolver resolver = staticEnv.getModuleURIResolver(); if (resolver == null) { resolver = staticEnv.getConfiguration().getStandardModuleURIResolver(); exp = staticEnv.compileQuery(queryText); } else { InputStream queryStream = new FileInputStream(queryFileName); staticEnv.setBaseURI(new File(queryFileName).toURI().toString()); exp = staticEnv.compileQuery(queryStream, null);
StaticQueryContext staticEnv = new StaticQueryContext(config); try { ItemType t = resolveType(itf, rootNodeType); __log.debug("resolved context item type " + t); staticEnv.setRequiredContextItemType(t); staticEnv.setBaseURI(baseResourceURI.toString()); XQueryExpression exp = staticEnv.compileQuery(query);
/** * Compile a query supplied as a file * * @param query the file containing the query. The URI corresponding to this file will be used as the * base URI of the query, overriding any URI supplied using {@link #setBaseURI(java.net.URI)} (but not * overriding any base URI specified within the query prolog) * @return an XQueryExecutable representing the compiled query * @throws SaxonApiException if the query compilation fails with a static error * @throws IOException if the file does not exist or cannot be read * @since 9.1 */ public XQueryExecutable compile(File query) throws SaxonApiException, IOException { try (FileInputStream stream = new FileInputStream(query)) { String savedBaseUri = env.getBaseURI(); env.setBaseURI(query.toURI().toString()); XQueryExecutable exec = new XQueryExecutable(processor, env.compileQuery(stream, encoding)); env.setBaseURI(savedBaseUri); return exec; } catch (XPathException e) { throw new SaxonApiException(e); } }
config = sqc.getConfiguration(); isMainModule = true; topModule = this; activeNamespaces = new Stack<>(); baseURI = sqc.getBaseURI(); defaultCollationName = sqc.getDefaultCollationName(); try { locationURI = baseURI == null ? null : new URI(baseURI); throw new XPathException("Invalid location URI: " + baseURI); executable = sqc.makeExecutable(); importers = null; init(sqc); packageData = pd; for (Iterator<GlobalVariable> vars = sqc.iterateDeclaredGlobalVariables(); vars.hasNext(); ) { GlobalVariable var = vars.next(); declareVariable(var); if (sqc.getModuleLocation() == null) { moduleLocation = new ExplicitLocation(sqc.getSystemId(), 1, -1); } else { moduleLocation = sqc.getModuleLocation(); optimizerOptions = sqc.getOptimizerOptions();
StaticQueryContext staticContext = new StaticQueryContext(config); DynamicQueryContext context = new DynamicQueryContext(config); try { XQueryExpression exp = staticContext.compileQuery(script); StreamSource doc = new StreamSource(new StringReader(inputDoc)); DocumentInfo docInfo = staticContext.buildDocument(doc); context.setContextNode(docInfo);
final StaticQueryContext aStaticQueryCtx = aConfiguration.newStaticQueryContext (); aStaticQueryCtx.setBaseURI (m_sBaseURL); final XQueryExpression exp = aStaticQueryCtx.compileQuery (aXQueryIS, sEncoding); final Controller aXQController = exp.newController (aDynamicQueryContext);
XQueryCompiler xqcomp = processor.newXQueryCompiler(); StaticQueryContext sqc = xqcomp.getUnderlyingStaticContext(); sqc.compileLibrary(connection.getInputStream(), "utf-8"); XQueryExpression xqe = sqc.compileQuery("import module namespace f='" + ns + "'; ."); QueryModule qm = xqe.getMainModule(); fl = qm.getGlobalFunctionLibrary();