//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Copyright (c) 2018 Saxonica Limited. // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// package net.sf.saxon; import net.sf.saxon.event.Receiver; import net.sf.saxon.expr.instruct.TerminationException; import net.sf.saxon.lib.*; import net.sf.saxon.om.*; import net.sf.saxon.query.QueryReader; import net.sf.saxon.query.QueryResult; import net.sf.saxon.query.UpdateAgent; import net.sf.saxon.query.XQueryExpression; import net.sf.saxon.s9api.*; import net.sf.saxon.serialize.SerializationProperties; import net.sf.saxon.trace.ExpressionPresenter; import net.sf.saxon.trace.TimingCodeInjector; import net.sf.saxon.trace.TimingTraceListener; import net.sf.saxon.trace.XQueryTraceListener; import net.sf.saxon.trans.CommandLineOptions; import net.sf.saxon.trans.LicenseException; import net.sf.saxon.trans.XPathException; import net.sf.saxon.type.ConversionResult; import net.sf.saxon.type.SchemaException; import net.sf.saxon.type.Type; import net.sf.saxon.value.DateTimeValue; import org.xml.sax.InputSource; import javax.xml.transform.Source; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.*; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Set; /** * This Query class provides a command-line interface to the Saxon XQuery processor. *
The XQuery syntax supported conforms to the W3C XQuery 1.0 drafts.
* * @author Michael H. Kay */ public class Query { protected Processor processor; protected Configuration config; protected boolean showTime = false; protected int repeat = 1; /*@Nullable*/ protected String sourceFileName = null; /*@Nullable*/ protected String queryFileName = null; protected boolean useURLs = false; /*@Nullable*/ protected String outputFileName = null; /*@Nullable*/ protected String moduleURIResolverClass = null; /*@Nullable*/ protected final String uriResolverClass = null; protected boolean explain = false; protected boolean wrap = false; protected boolean projection = false; protected boolean streaming = false; protected boolean updating = false; protected boolean writeback = false; protected boolean backup = true; /*@Nullable*/ protected String explainOutputFileName = null; private Logger traceDestination = new StandardLogger(); private boolean closeTraceDestination = false; private boolean allowExit = true; /** * Get the configuration in use * * @return the configuration */ protected Configuration getConfiguration() { return config; } /** * Main program, can be used directly from the command line. *The format is:
*java net.sf.saxon.Query [options] query-file >output-file
*followed by any number of parameters in the form {keyword=value}... which can be * referenced from within the query.
*This program executes the query in query-file.
* * @param args List of arguments supplied on operating system command line */ public static void main(String args[]) { // the real work is delegated to another routine so that it can be used in a subclass new Query().doQuery(args, "java net.sf.saxon.Query"); } /** * Set the options that are recognized on the command line. This method can be overridden in a subclass * to define additional command line options. * * @param options the CommandLineOptions in which the recognized options are to be registered. */ public void setPermittedOptions(CommandLineOptions options) { options.addRecognizedOption("backup", CommandLineOptions.TYPE_BOOLEAN, "Save updated documents before overwriting"); options.addRecognizedOption("catalog", CommandLineOptions.TYPE_FILENAME | CommandLineOptions.VALUE_REQUIRED, "Use specified catalog file to resolve URIs"); options.addRecognizedOption("config", CommandLineOptions.TYPE_FILENAME | CommandLineOptions.VALUE_REQUIRED, "Use specified configuration file"); options.addRecognizedOption("cr", CommandLineOptions.TYPE_CLASSNAME | CommandLineOptions.VALUE_REQUIRED, "Use specified collection URI resolver class"); options.addRecognizedOption("dtd", CommandLineOptions.TYPE_ENUMERATION, "Validate using DTD"); options.setPermittedValues("dtd", new String[]{"on", "off", "recover"}, "on"); options.addRecognizedOption("expand", CommandLineOptions.TYPE_BOOLEAN, "Expand attribute defaults from DTD or Schema"); options.addRecognizedOption("explain", CommandLineOptions.TYPE_FILENAME, "Display compiled expression tree and optimization decisions"); options.addRecognizedOption("ext", CommandLineOptions.TYPE_BOOLEAN, "Allow calls to Java extension functions and xsl:result-document"); options.addRecognizedOption("init", CommandLineOptions.TYPE_CLASSNAME, "User-supplied net.sf.saxon.lib.Initializer class to initialize the Saxon Configuration"); options.addRecognizedOption("l", CommandLineOptions.TYPE_BOOLEAN, "Maintain line numbers for source documents"); options.addRecognizedOption("mr", CommandLineOptions.TYPE_CLASSNAME | CommandLineOptions.VALUE_REQUIRED, "Use named ModuleURIResolver class"); options.addRecognizedOption("now", CommandLineOptions.TYPE_DATETIME | CommandLineOptions.VALUE_REQUIRED, "Run with specified current date/time"); options.addRecognizedOption("o", CommandLineOptions.TYPE_FILENAME | CommandLineOptions.VALUE_REQUIRED, "Use specified file for primary output"); options.addRecognizedOption("opt", CommandLineOptions.TYPE_STRING | CommandLineOptions.VALUE_REQUIRED, "Enable/disable optimization options [-]cfgklmsvwx"); options.addRecognizedOption("outval", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, "Action when validation of output file fails"); options.setPermittedValues("outval", new String[]{"recover", "fatal"}, null); options.addRecognizedOption("p", CommandLineOptions.TYPE_BOOLEAN, "Recognize query parameters in URI passed to doc()"); options.addRecognizedOption("projection", CommandLineOptions.TYPE_BOOLEAN, "Use source document projection"); options.addRecognizedOption("q", CommandLineOptions.TYPE_FILENAME | CommandLineOptions.VALUE_REQUIRED, "Query filename"); options.addRecognizedOption("qs", CommandLineOptions.TYPE_STRING | CommandLineOptions.VALUE_REQUIRED, "Query string (usually in quotes)"); options.addRecognizedOption("quit", CommandLineOptions.TYPE_BOOLEAN | CommandLineOptions.VALUE_REQUIRED, "Quit JVM if query fails"); options.addRecognizedOption("r", CommandLineOptions.TYPE_CLASSNAME | CommandLineOptions.VALUE_REQUIRED, "Use named URIResolver class"); options.addRecognizedOption("repeat", CommandLineOptions.TYPE_INTEGER | CommandLineOptions.VALUE_REQUIRED, "Run N times for performance measurement"); options.addRecognizedOption("s", CommandLineOptions.TYPE_FILENAME | CommandLineOptions.VALUE_REQUIRED, "Source file for primary input"); options.addRecognizedOption("sa", CommandLineOptions.TYPE_BOOLEAN, "Run in schema-aware mode"); options.addRecognizedOption("scmin", CommandLineOptions.TYPE_FILENAME, "Pre-load schema in SCM format"); options.addRecognizedOption("stream", CommandLineOptions.TYPE_BOOLEAN, "Execute in streamed mode"); options.addRecognizedOption("strip", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, "Handling of whitespace text nodes in source documents"); options.setPermittedValues("strip", new String[]{"none", "all", "ignorable"}, null); options.addRecognizedOption("t", CommandLineOptions.TYPE_BOOLEAN, "Display version and timing information"); options.addRecognizedOption("T", CommandLineOptions.TYPE_CLASSNAME, "Use named TraceListener class, or standard TraceListener"); options.addRecognizedOption("TB", CommandLineOptions.TYPE_FILENAME, "Trace hotspot bytecode generation to specified XML file"); options.addRecognizedOption("TJ", CommandLineOptions.TYPE_BOOLEAN, "Debug binding and execution of extension functions"); options.setPermittedValues("TJ", new String[]{"on", "off"}, "on"); options.addRecognizedOption("tree", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, "Use specified tree model for source documents"); options.addRecognizedOption("Tlevel", CommandLineOptions.TYPE_STRING, "Level of detail for trace listener output"); options.setPermittedValues("Tlevel", new String[]{"none", "low", "normal", "high"}, "normal"); options.addRecognizedOption("Tout", CommandLineOptions.TYPE_FILENAME, "File for trace listener output"); options.addRecognizedOption("TP", CommandLineOptions.TYPE_FILENAME, "Use profiling trace listener, with specified output file"); options.addRecognizedOption("traceout", CommandLineOptions.TYPE_FILENAME | CommandLineOptions.VALUE_REQUIRED, "File for output of trace() messages"); options.setPermittedValues("tree", new String[]{"linked", "tiny", "tinyc"}, null); options.addRecognizedOption("u", CommandLineOptions.TYPE_BOOLEAN, "Interpret filename arguments as URIs"); options.setPermittedValues("u", new String[]{"on", "off"}, "on"); options.addRecognizedOption("update", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, "Enable or disable XQuery updates, or enable the syntax but discard the updates"); options.setPermittedValues("update", new String[]{"on", "off", "discard"}, null); options.addRecognizedOption("val", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, "Apply validation to source documents"); options.setPermittedValues("val", new String[]{"strict", "lax"}, "strict"); options.addRecognizedOption("wrap", CommandLineOptions.TYPE_BOOLEAN, "Wrap result sequence in XML elements"); options.addRecognizedOption("x", CommandLineOptions.TYPE_CLASSNAME | CommandLineOptions.VALUE_REQUIRED, "Use named XMLReader class for parsing source documents"); options.addRecognizedOption("xi", CommandLineOptions.TYPE_BOOLEAN, "Expand XInclude directives in source documents"); options.addRecognizedOption("xmlversion", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, "Indicate whether XML 1.1 is supported"); options.setPermittedValues("xmlversion", new String[]{"1.0", "1.1"}, null); options.addRecognizedOption("xsd", CommandLineOptions.TYPE_FILENAME_LIST | CommandLineOptions.VALUE_REQUIRED, "List of schema documents to be preloaded"); options.addRecognizedOption("xsdversion", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, "Indicate whether XSD 1.1 is supported"); options.setPermittedValues("xsdversion", new String[]{"1.0", "1.1"}, null); options.addRecognizedOption("xsiloc", CommandLineOptions.TYPE_BOOLEAN, "Load schemas named in xsi:schemaLocation (default on)"); options.addRecognizedOption("?", CommandLineOptions.VALUE_PROHIBITED, "Display command line help text"); } /** * Support method for main program. This support method can also be invoked from subclasses * that support the same command line interface * * @param args the command-line arguments * @param command name of the class, to be used in error messages */ @SuppressWarnings("ResultOfMethodCallIgnored") protected void doQuery(String args[], String command) { CommandLineOptions options = new CommandLineOptions(); setPermittedOptions(options); try { options.setActualOptions(args); } catch (XPathException err) { quit(err.getMessage(), 2); } boolean schemaAware = false; String configFile = options.getOptionValue("config"); if (configFile != null) { try { config = Configuration.readConfiguration(new StreamSource(configFile)); schemaAware = config.isLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XQUERY); } catch (XPathException e) { quit(e.getMessage(), 2); } } if (config == null && !schemaAware) { schemaAware = options.testIfSchemaAware(); } if (config == null) { config = Configuration.newConfiguration(); } //config.setHostLanguage(Configuration.XQUERY); processor = new Processor(config); // Check the command-line arguments. try { parseOptions(options); XQueryCompiler compiler = processor.newXQueryCompiler(); compiler.setSchemaAware(schemaAware); if (updating) { compiler.setUpdatingEnabled(true); } if (config.getTraceListener() != null) { compiler.setCompileWithTracing(true); } if (moduleURIResolverClass != null) { Object mr = config.getInstance(moduleURIResolverClass, null); if (!(mr instanceof ModuleURIResolver)) { badUsage(moduleURIResolverClass + " is not a ModuleURIResolver"); } compiler.setModuleURIResolver((ModuleURIResolver) mr); } if (uriResolverClass != null) { config.setURIResolver(config.makeURIResolver(uriResolverClass)); } config.displayLicenseMessage(); if (schemaAware && !config.isLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XQUERY)) { if ("EE".equals(config.getEditionCode())) { quit("Installed license does not allow schema-aware query", 2); } else { quit("Schema-aware query requires Saxon Enterprise Edition", 2); } } if (explain) { config.setBooleanProperty(Feature.TRACE_OPTIMIZER_DECISIONS, true); } compiler.setStreaming(streaming); Source sourceInput = null; if (sourceFileName != null) { sourceInput = processSourceFile(sourceFileName, useURLs); } long startTime = System.nanoTime(); if (showTime) { System.err.println("Analyzing query from " + queryFileName); } // Compile the query XQueryExecutable exp = null; try { exp = compileQuery(compiler, queryFileName, useURLs); if (showTime) { long endTime = System.nanoTime(); System.err.println("Analysis time: " + ((endTime - startTime) / 1e6) + " milliseconds"); startTime = endTime; } } catch (SaxonApiException e) { if (e.getCause() instanceof XPathException) { XPathException err = (XPathException) e.getCause(); int line = -1; String module = null; if (err.getLocator() != null) { line = err.getLocator().getLineNumber(); module = err.getLocator().getSystemId(); } if (err.hasBeenReported()) { quit("Static error(s) in query", 2); } else { if (line == -1) { System.err.println("Static error in query: " + err.getMessage()); } else { System.err.println("Static error at line " + line + " of " + module + ':'); System.err.println(err.getMessage()); } } exp = null; if (allowExit) { System.exit(2); } else { throw new RuntimeException(err); } } else { quit(e.getMessage(), 2); } } if (explain && exp != null) { Serializer out; if (explainOutputFileName == null || explainOutputFileName.equals("")) { out = processor.newSerializer(System.err); } else { out = processor.newSerializer(new File(explainOutputFileName)); } out.setOutputProperty(Serializer.Property.METHOD, "xml"); out.setOutputProperty(Serializer.Property.INDENT, "yes"); out.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes"); if (!"HE".equals(processor.getSaxonEdition())) { out.setOutputProperty(Serializer.Property.SAXON_INDENT_SPACES, "2"); } exp.explain(out); } // Load the source file (applying document projection if requested) exp.getUnderlyingCompiledQuery().setAllowDocumentProjection(projection); final XQueryEvaluator evaluator = exp.load(); evaluator.setTraceFunctionDestination(traceDestination); if (options.getOptionValue("now") != null) { String now = options.getOptionValue("now"); ConversionResult dt = DateTimeValue.makeDateTimeValue(now, config.getConversionRules()); if (dt instanceof DateTimeValue) { evaluator.getUnderlyingQueryContext().setCurrentDateTime((DateTimeValue) dt); } else { System.err.println("Invalid dateTime: " + now + " (ignored)"); } } if (uriResolverClass != null) { evaluator.setURIResolver(config.makeURIResolver(uriResolverClass)); } processSource(sourceInput, exp, evaluator); // Supply query parameters options.setParams(processor, evaluator::setExternalVariable); // Run the query (repeatedly, if the -repeat option was set) startTime = System.nanoTime(); long totalTime = 0; int r; for (r = 0; r < repeat; r++) { // repeat is for internal testing/timing try { OutputStream out; if (outputFileName != null) { File outputFile = new File(outputFileName); if (outputFile.isDirectory()) { quit("Output is a directory", 2); } createFileIfNecessary(outputFile); out = new FileOutputStream(outputFile); } else { out = System.out; } Serializer serializer = processor.newSerializer(out); try { options.setSerializationProperties(serializer); } catch (IllegalArgumentException e) { quit(e.getMessage(), 2); } if (updating && exp.isUpdateQuery()) { serializer.setOutputProperties( exp.getUnderlyingCompiledQuery().getExecutable().getPrimarySerializationProperties().getProperties()); runUpdate(exp, evaluator, serializer); } else { runQuery(exp, evaluator, sourceInput, serializer); } } catch (SaxonApiException err) { if (err.getCause() instanceof XPathException && ((XPathException) err.getCause()).hasBeenReported()) { String category = ((XPathException) err.getCause()).isTypeError() ? "type" : "dynamic"; quit("Query failed with " + category + " error: " + err.getCause().getMessage(), 2); } else { throw err; } } if (showTime) { long endTime = System.nanoTime(); if (r >= 3) { totalTime += endTime - startTime; } if (repeat < 100) { System.err.println("Execution time: " + CommandLineOptions.showExecutionTimeNano(endTime - startTime)); System.err.println("Memory used: " + CommandLineOptions.showMemoryUsed()); } else if (totalTime > 1000000000000L) { // quit after 1000 seconds break; } startTime = endTime; } } if (repeat > 3) { System.err.println("Average execution time: " + CommandLineOptions.showExecutionTimeNano(totalTime / (r - 3))); } if (options.getOptionValue("TB") != null) { // report on hotspot bytecode generation config.createByteCodeReport(options.getOptionValue("TB")); } } catch (TerminationException err) { quit(err.getMessage(), 1); } catch (SchemaException err) { quit("Schema processing failed: " + err.getMessage(), 2); } catch (XPathException | LicenseException | SaxonApiException err) { quit("Query processing failed: " + err.getMessage(), 2); } catch (TransformerFactoryConfigurationError err) { err.printStackTrace(); quit("Query processing failed", 2); } catch (Exception err2) { err2.printStackTrace(); quit("Fatal error during query: " + err2.getClass().getName() + ": " + (err2.getMessage() == null ? " (no message)" : err2.getMessage()), 2); } } /** * Parse the options supplied on the command line * * @param options the command line arguments * @throws TransformerException if failures occur. Note, the method may also invoke System.exit(). */ protected void parseOptions(CommandLineOptions options) throws TransformerException { // Apply those options which simply update the Configuration options.applyToConfiguration(processor); // Apply options that are processed locally allowExit = !"off".equals(options.getOptionValue("quit")); backup = "on".equals(options.getOptionValue("backup")); explainOutputFileName = options.getOptionValue("explain"); explain = explainOutputFileName != null; moduleURIResolverClass = options.getOptionValue("mr"); outputFileName = options.getOptionValue("o"); streaming = "on".equals(options.getOptionValue("stream")); String value = options.getOptionValue("p"); if ("on".equals(value)) { config.setParameterizedURIResolver(); useURLs = true; } projection = "on".equals(options.getOptionValue("projection")); value = options.getOptionValue("q"); if (value != null) { queryFileName = value; } value = options.getOptionValue("qs"); if (value != null) { queryFileName = "{" + value + "}"; } String qv = options.getOptionValue("qversion"); if (qv != null && !"3.1".equals(qv)) { System.err.println("-qversion ignored: 3.1 is assumed"); } value = options.getOptionValue("repeat"); if (value != null) { repeat = Integer.parseInt(value); } sourceFileName = options.getOptionValue("s"); value = options.getOptionValue("t"); if ("on".equals(value)) { System.err.println(config.getProductTitle()); System.err.println(Version.platform.getPlatformVersion()); config.setTiming(true); showTime = true; } value = options.getOptionValue("traceout"); if (value != null) { switch (value) { case "#err": // no action, this is the default break; case "#out": traceDestination = new StandardLogger(System.out); break; case "#null": traceDestination = null; break; default: try { traceDestination = new StandardLogger(new File(value)); closeTraceDestination = true; } catch (FileNotFoundException e) { badUsage("Trace output file " + value + " cannot be created"); } break; } } value = options.getOptionValue("T"); if (value != null) { if ("".equals(value)) { makeXQueryTraceListener(options); } else { config.setTraceListenerClass(value); } config.setLineNumbering(true); } value = options.getOptionValue("Tout"); if (value != null) { config.setTraceListenerOutputFile(value); if (options.getOptionValue("T") == null) { makeXQueryTraceListener(options); } } value = options.getOptionValue("TB"); if (value != null) { // Trace hotspot byte code generation and produce a report. config.setBooleanProperty(Feature.MONITOR_HOT_SPOT_BYTE_CODE, true); } value = options.getOptionValue("TP"); if (value != null) { TimingTraceListener listener = new TimingTraceListener(); config.setTraceListener(listener); config.setLineNumbering(true); config.getDefaultStaticQueryContext().setCodeInjector(new TimingCodeInjector()); if (!value.isEmpty()) { try { listener.setOutputDestination( new StandardLogger(new File(value))); } catch (FileNotFoundException e) { badUsage("Trace output file " + value + " cannot be created"); } } } value = options.getOptionValue("u"); if (value != null) { useURLs = "on".equals(value); } value = options.getOptionValue("update"); if (value != null) { if (!"off".equals(value)) { updating = true; } writeback = !"discard".equals(value); } wrap = "on".equals(options.getOptionValue("wrap")); value = options.getOptionValue("x"); if (value != null) { config.setSourceParserClass(value); } String additionalSchemas = options.getOptionValue("xsd"); value = options.getOptionValue("?"); if (value != null) { badUsage(""); } // Apply options defined locally in a subclass applyLocalOptions(options, config); // Apply positional options List