| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon; |
| 9 |
|
| 10 |
import net.sf.saxon.event.Receiver; |
| 11 |
import net.sf.saxon.expr.instruct.TerminationException; |
| 12 |
import net.sf.saxon.lib.*; |
| 13 |
import net.sf.saxon.om.*; |
| 14 |
import net.sf.saxon.query.QueryReader; |
| 15 |
import net.sf.saxon.query.QueryResult; |
| 16 |
import net.sf.saxon.query.UpdateAgent; |
| 17 |
import net.sf.saxon.query.XQueryExpression; |
| 18 |
import net.sf.saxon.s9api.*; |
| 19 |
import net.sf.saxon.serialize.SerializationProperties; |
| 20 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 21 |
import net.sf.saxon.trace.TimingCodeInjector; |
| 22 |
import net.sf.saxon.trace.TimingTraceListener; |
| 23 |
import net.sf.saxon.trace.XQueryTraceListener; |
| 24 |
import net.sf.saxon.trans.CommandLineOptions; |
| 25 |
import net.sf.saxon.trans.LicenseException; |
| 26 |
import net.sf.saxon.trans.XPathException; |
| 27 |
import net.sf.saxon.type.ConversionResult; |
| 28 |
import net.sf.saxon.type.SchemaException; |
| 29 |
import net.sf.saxon.type.Type; |
| 30 |
import net.sf.saxon.value.DateTimeValue; |
| 31 |
import org.xml.sax.InputSource; |
| 32 |
|
| 33 |
import javax.xml.transform.Source; |
| 34 |
import javax.xml.transform.TransformerException; |
| 35 |
import javax.xml.transform.TransformerFactoryConfigurationError; |
| 36 |
import javax.xml.transform.sax.SAXSource; |
| 37 |
import javax.xml.transform.stream.StreamResult; |
| 38 |
import javax.xml.transform.stream.StreamSource; |
| 39 |
import java.io.*; |
| 40 |
import java.net.URI; |
| 41 |
import java.net.URISyntaxException; |
| 42 |
import java.util.ArrayList; |
| 43 |
import java.util.List; |
| 44 |
import java.util.Set; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class Query { |
| 54 |
|
| 55 |
protected Processor processor; |
| 56 |
protected Configuration config; |
| 57 |
protected boolean showTime = false; |
| 58 |
protected int repeat = 1; |
| 59 |
protected String sourceFileName = null; |
| 60 |
protected String queryFileName = null; |
| 61 |
protected boolean useURLs = false; |
| 62 |
protected String outputFileName = null; |
| 63 |
protected String moduleURIResolverClass = null; |
| 64 |
protected final String uriResolverClass = null; |
| 65 |
protected boolean explain = false; |
| 66 |
protected boolean wrap = false; |
| 67 |
protected boolean projection = false; |
| 68 |
protected boolean streaming = false; |
| 69 |
protected boolean updating = false; |
| 70 |
protected boolean writeback = false; |
| 71 |
protected boolean backup = true; |
| 72 |
protected String explainOutputFileName = null; |
| 73 |
private Logger traceDestination = new StandardLogger(); |
| 74 |
private boolean closeTraceDestination = false; |
| 75 |
private boolean allowExit = true; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
protected Configuration getConfiguration() { |
| 85 |
return config; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static void main(String args[]) { |
| 100 |
|
| 101 |
new Query().doQuery(args, "java net.sf.saxon.Query"); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public void setPermittedOptions(CommandLineOptions options) { |
| 112 |
options.addRecognizedOption("backup", CommandLineOptions.TYPE_BOOLEAN, |
| 113 |
"Save updated documents before overwriting"); |
| 114 |
options.addRecognizedOption("catalog", CommandLineOptions.TYPE_FILENAME | CommandLineOptions.VALUE_REQUIRED, |
| 115 |
"Use specified catalog file to resolve URIs"); |
| 116 |
options.addRecognizedOption("config", CommandLineOptions.TYPE_FILENAME | CommandLineOptions.VALUE_REQUIRED, |
| 117 |
"Use specified configuration file"); |
| 118 |
options.addRecognizedOption("cr", CommandLineOptions.TYPE_CLASSNAME | CommandLineOptions.VALUE_REQUIRED, |
| 119 |
"Use specified collection URI resolver class"); |
| 120 |
options.addRecognizedOption("dtd", CommandLineOptions.TYPE_ENUMERATION, |
| 121 |
"Validate using DTD"); |
| 122 |
options.setPermittedValues("dtd", new String[]{"on", "off", "recover"}, "on"); |
| 123 |
options.addRecognizedOption("expand", CommandLineOptions.TYPE_BOOLEAN, |
| 124 |
"Expand attribute defaults from DTD or Schema"); |
| 125 |
options.addRecognizedOption("explain", CommandLineOptions.TYPE_FILENAME, |
| 126 |
"Display compiled expression tree and optimization decisions"); |
| 127 |
options.addRecognizedOption("ext", CommandLineOptions.TYPE_BOOLEAN, |
| 128 |
"Allow calls to Java extension functions and xsl:result-document"); |
| 129 |
options.addRecognizedOption("init", CommandLineOptions.TYPE_CLASSNAME, |
| 130 |
"User-supplied net.sf.saxon.lib.Initializer class to initialize the Saxon Configuration"); |
| 131 |
options.addRecognizedOption("l", CommandLineOptions.TYPE_BOOLEAN, |
| 132 |
"Maintain line numbers for source documents"); |
| 133 |
options.addRecognizedOption("mr", CommandLineOptions.TYPE_CLASSNAME | CommandLineOptions.VALUE_REQUIRED, |
| 134 |
"Use named ModuleURIResolver class"); |
| 135 |
options.addRecognizedOption("now", CommandLineOptions.TYPE_DATETIME | CommandLineOptions.VALUE_REQUIRED, |
| 136 |
"Run with specified current date/time"); |
| 137 |
options.addRecognizedOption("o", CommandLineOptions.TYPE_FILENAME | CommandLineOptions.VALUE_REQUIRED, |
| 138 |
"Use specified file for primary output"); |
| 139 |
options.addRecognizedOption("opt", CommandLineOptions.TYPE_STRING | CommandLineOptions.VALUE_REQUIRED, |
| 140 |
"Enable/disable optimization options [-]cfgklmsvwx"); |
| 141 |
options.addRecognizedOption("outval", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, |
| 142 |
"Action when validation of output file fails"); |
| 143 |
options.setPermittedValues("outval", new String[]{"recover", "fatal"}, null); |
| 144 |
options.addRecognizedOption("p", CommandLineOptions.TYPE_BOOLEAN, |
| 145 |
"Recognize query parameters in URI passed to doc()"); |
| 146 |
options.addRecognizedOption("projection", CommandLineOptions.TYPE_BOOLEAN, |
| 147 |
"Use source document projection"); |
| 148 |
options.addRecognizedOption("q", CommandLineOptions.TYPE_FILENAME | CommandLineOptions.VALUE_REQUIRED, |
| 149 |
"Query filename"); |
| 150 |
options.addRecognizedOption("qs", CommandLineOptions.TYPE_STRING | CommandLineOptions.VALUE_REQUIRED, |
| 151 |
"Query string (usually in quotes)"); |
| 152 |
options.addRecognizedOption("quit", CommandLineOptions.TYPE_BOOLEAN | CommandLineOptions.VALUE_REQUIRED, |
| 153 |
"Quit JVM if query fails"); |
| 154 |
options.addRecognizedOption("r", CommandLineOptions.TYPE_CLASSNAME | CommandLineOptions.VALUE_REQUIRED, |
| 155 |
"Use named URIResolver class"); |
| 156 |
options.addRecognizedOption("repeat", CommandLineOptions.TYPE_INTEGER | CommandLineOptions.VALUE_REQUIRED, |
| 157 |
"Run N times for performance measurement"); |
| 158 |
options.addRecognizedOption("s", CommandLineOptions.TYPE_FILENAME | CommandLineOptions.VALUE_REQUIRED, |
| 159 |
"Source file for primary input"); |
| 160 |
options.addRecognizedOption("sa", CommandLineOptions.TYPE_BOOLEAN, |
| 161 |
"Run in schema-aware mode"); |
| 162 |
options.addRecognizedOption("scmin", CommandLineOptions.TYPE_FILENAME, |
| 163 |
"Pre-load schema in SCM format"); |
| 164 |
options.addRecognizedOption("stream", CommandLineOptions.TYPE_BOOLEAN, |
| 165 |
"Execute in streamed mode"); |
| 166 |
options.addRecognizedOption("strip", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, |
| 167 |
"Handling of whitespace text nodes in source documents"); |
| 168 |
options.setPermittedValues("strip", new String[]{"none", "all", "ignorable"}, null); |
| 169 |
options.addRecognizedOption("t", CommandLineOptions.TYPE_BOOLEAN, |
| 170 |
"Display version and timing information"); |
| 171 |
options.addRecognizedOption("T", CommandLineOptions.TYPE_CLASSNAME, |
| 172 |
"Use named TraceListener class, or standard TraceListener"); |
| 173 |
options.addRecognizedOption("TB", CommandLineOptions.TYPE_FILENAME, |
| 174 |
"Trace hotspot bytecode generation to specified XML file"); |
| 175 |
options.addRecognizedOption("TJ", CommandLineOptions.TYPE_BOOLEAN, |
| 176 |
"Debug binding and execution of extension functions"); |
| 177 |
options.setPermittedValues("TJ", new String[]{"on", "off"}, "on"); |
| 178 |
options.addRecognizedOption("tree", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, |
| 179 |
"Use specified tree model for source documents"); |
| 180 |
options.addRecognizedOption("Tlevel", CommandLineOptions.TYPE_STRING, |
| 181 |
"Level of detail for trace listener output"); |
| 182 |
options.setPermittedValues("Tlevel", new String[]{"none", "low", "normal", "high"}, "normal"); |
| 183 |
options.addRecognizedOption("Tout", CommandLineOptions.TYPE_FILENAME, |
| 184 |
"File for trace listener output"); |
| 185 |
options.addRecognizedOption("TP", CommandLineOptions.TYPE_FILENAME, |
| 186 |
"Use profiling trace listener, with specified output file"); |
| 187 |
options.addRecognizedOption("traceout", CommandLineOptions.TYPE_FILENAME | CommandLineOptions.VALUE_REQUIRED, |
| 188 |
"File for output of trace() messages"); |
| 189 |
options.setPermittedValues("tree", new String[]{"linked", "tiny", "tinyc"}, null); |
| 190 |
options.addRecognizedOption("u", CommandLineOptions.TYPE_BOOLEAN, |
| 191 |
"Interpret filename arguments as URIs"); |
| 192 |
options.setPermittedValues("u", new String[]{"on", "off"}, "on"); |
| 193 |
options.addRecognizedOption("update", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, |
| 194 |
"Enable or disable XQuery updates, or enable the syntax but discard the updates"); |
| 195 |
options.setPermittedValues("update", new String[]{"on", "off", "discard"}, null); |
| 196 |
options.addRecognizedOption("val", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, |
| 197 |
"Apply validation to source documents"); |
| 198 |
options.setPermittedValues("val", new String[]{"strict", "lax"}, "strict"); |
| 199 |
options.addRecognizedOption("wrap", CommandLineOptions.TYPE_BOOLEAN, |
| 200 |
"Wrap result sequence in XML elements"); |
| 201 |
options.addRecognizedOption("x", CommandLineOptions.TYPE_CLASSNAME | CommandLineOptions.VALUE_REQUIRED, |
| 202 |
"Use named XMLReader class for parsing source documents"); |
| 203 |
options.addRecognizedOption("xi", CommandLineOptions.TYPE_BOOLEAN, |
| 204 |
"Expand XInclude directives in source documents"); |
| 205 |
options.addRecognizedOption("xmlversion", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, |
| 206 |
"Indicate whether XML 1.1 is supported"); |
| 207 |
options.setPermittedValues("xmlversion", new String[]{"1.0", "1.1"}, null); |
| 208 |
options.addRecognizedOption("xsd", CommandLineOptions.TYPE_FILENAME_LIST | CommandLineOptions.VALUE_REQUIRED, |
| 209 |
"List of schema documents to be preloaded"); |
| 210 |
options.addRecognizedOption("xsdversion", CommandLineOptions.TYPE_ENUMERATION | CommandLineOptions.VALUE_REQUIRED, |
| 211 |
"Indicate whether XSD 1.1 is supported"); |
| 212 |
options.setPermittedValues("xsdversion", new String[]{"1.0", "1.1"}, null); |
| 213 |
options.addRecognizedOption("xsiloc", CommandLineOptions.TYPE_BOOLEAN, |
| 214 |
"Load schemas named in xsi:schemaLocation (default on)"); |
| 215 |
options.addRecognizedOption("?", CommandLineOptions.VALUE_PROHIBITED, |
| 216 |
"Display command line help text"); |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
@SuppressWarnings("ResultOfMethodCallIgnored") |
| 230 |
protected void doQuery(String args[], String command) { |
| 231 |
|
| 232 |
CommandLineOptions options = new CommandLineOptions(); |
| 233 |
setPermittedOptions(options); |
| 234 |
try { |
| 235 |
options.setActualOptions(args); |
| 236 |
} catch (XPathException err) { |
| 237 |
quit(err.getMessage(), 2); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
boolean schemaAware = false; |
| 242 |
String configFile = options.getOptionValue("config"); |
| 243 |
if (configFile != null) { |
| 244 |
try { |
| 245 |
config = Configuration.readConfiguration(new StreamSource(configFile)); |
| 246 |
schemaAware = config.isLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XQUERY); |
| 247 |
} catch (XPathException e) { |
| 248 |
quit(e.getMessage(), 2); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if (config == null && !schemaAware) { |
| 253 |
schemaAware = options.testIfSchemaAware(); |
| 254 |
} |
| 255 |
|
| 256 |
if (config == null) { |
| 257 |
config = Configuration.newConfiguration(); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
processor = new Processor(config); |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
try { |
| 267 |
parseOptions(options); |
| 268 |
|
| 269 |
XQueryCompiler compiler = processor.newXQueryCompiler(); |
| 270 |
compiler.setSchemaAware(schemaAware); |
| 271 |
|
| 272 |
if (updating) { |
| 273 |
compiler.setUpdatingEnabled(true); |
| 274 |
} |
| 275 |
if (config.getTraceListener() != null) { |
| 276 |
compiler.setCompileWithTracing(true); |
| 277 |
} |
| 278 |
|
| 279 |
if (moduleURIResolverClass != null) { |
| 280 |
Object mr = config.getInstance(moduleURIResolverClass, null); |
| 281 |
if (!(mr instanceof ModuleURIResolver)) { |
| 282 |
badUsage(moduleURIResolverClass + " is not a ModuleURIResolver"); |
| 283 |
} |
| 284 |
compiler.setModuleURIResolver((ModuleURIResolver) mr); |
| 285 |
} |
| 286 |
|
| 287 |
if (uriResolverClass != null) { |
| 288 |
config.setURIResolver(config.makeURIResolver(uriResolverClass)); |
| 289 |
} |
| 290 |
|
| 291 |
config.displayLicenseMessage(); |
| 292 |
if (schemaAware && !config.isLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XQUERY)) { |
| 293 |
if ("EE".equals(config.getEditionCode())) { |
| 294 |
quit("Installed license does not allow schema-aware query", 2); |
| 295 |
} else { |
| 296 |
quit("Schema-aware query requires Saxon Enterprise Edition", 2); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
if (explain) { |
| 301 |
config.setBooleanProperty(Feature.TRACE_OPTIMIZER_DECISIONS, true); |
| 302 |
} |
| 303 |
|
| 304 |
compiler.setStreaming(streaming); |
| 305 |
|
| 306 |
Source sourceInput = null; |
| 307 |
|
| 308 |
if (sourceFileName != null) { |
| 309 |
sourceInput = processSourceFile(sourceFileName, useURLs); |
| 310 |
} |
| 311 |
|
| 312 |
long startTime = System.nanoTime(); |
| 313 |
if (showTime) { |
| 314 |
System.err.println("Analyzing query from " + queryFileName); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
XQueryExecutable exp = null; |
| 320 |
try { |
| 321 |
exp = compileQuery(compiler, queryFileName, useURLs); |
| 322 |
|
| 323 |
if (showTime) { |
| 324 |
long endTime = System.nanoTime(); |
| 325 |
System.err.println("Analysis time: " + ((endTime - startTime) / 1e6) + " milliseconds"); |
| 326 |
startTime = endTime; |
| 327 |
} |
| 328 |
|
| 329 |
} catch (SaxonApiException e) { |
| 330 |
if (e.getCause() instanceof XPathException) { |
| 331 |
XPathException err = (XPathException) e.getCause(); |
| 332 |
int line = -1; |
| 333 |
String module = null; |
| 334 |
if (err.getLocator() != null) { |
| 335 |
line = err.getLocator().getLineNumber(); |
| 336 |
module = err.getLocator().getSystemId(); |
| 337 |
} |
| 338 |
if (err.hasBeenReported()) { |
| 339 |
quit("Static error(s) in query", 2); |
| 340 |
} else { |
| 341 |
if (line == -1) { |
| 342 |
System.err.println("Static error in query: " + err.getMessage()); |
| 343 |
} else { |
| 344 |
System.err.println("Static error at line " + line + " of " + module + ':'); |
| 345 |
System.err.println(err.getMessage()); |
| 346 |
} |
| 347 |
} |
| 348 |
exp = null; |
| 349 |
if (allowExit) { |
| 350 |
System.exit(2); |
| 351 |
} else { |
| 352 |
throw new RuntimeException(err); |
| 353 |
} |
| 354 |
} else { |
| 355 |
quit(e.getMessage(), 2); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
if (explain && exp != null) { |
| 360 |
Serializer out; |
| 361 |
if (explainOutputFileName == null || explainOutputFileName.equals("")) { |
| 362 |
out = processor.newSerializer(System.err); |
| 363 |
} else { |
| 364 |
out = processor.newSerializer(new File(explainOutputFileName)); |
| 365 |
} |
| 366 |
out.setOutputProperty(Serializer.Property.METHOD, "xml"); |
| 367 |
out.setOutputProperty(Serializer.Property.INDENT, "yes"); |
| 368 |
out.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes"); |
| 369 |
if (!"HE".equals(processor.getSaxonEdition())) { |
| 370 |
out.setOutputProperty(Serializer.Property.SAXON_INDENT_SPACES, "2"); |
| 371 |
} |
| 372 |
exp.explain(out); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
exp.getUnderlyingCompiledQuery().setAllowDocumentProjection(projection); |
| 378 |
final XQueryEvaluator evaluator = exp.load(); |
| 379 |
evaluator.setTraceFunctionDestination(traceDestination); |
| 380 |
if (options.getOptionValue("now") != null) { |
| 381 |
String now = options.getOptionValue("now"); |
| 382 |
ConversionResult dt = DateTimeValue.makeDateTimeValue(now, config.getConversionRules()); |
| 383 |
if (dt instanceof DateTimeValue) { |
| 384 |
evaluator.getUnderlyingQueryContext().setCurrentDateTime((DateTimeValue) dt); |
| 385 |
} else { |
| 386 |
System.err.println("Invalid dateTime: " + now + " (ignored)"); |
| 387 |
} |
| 388 |
} |
| 389 |
if (uriResolverClass != null) { |
| 390 |
evaluator.setURIResolver(config.makeURIResolver(uriResolverClass)); |
| 391 |
} |
| 392 |
processSource(sourceInput, exp, evaluator); |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
options.setParams(processor, evaluator::setExternalVariable); |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
startTime = System.nanoTime(); |
| 401 |
long totalTime = 0; |
| 402 |
int r; |
| 403 |
for (r = 0; r < repeat; r++) { |
| 404 |
try { |
| 405 |
OutputStream out; |
| 406 |
if (outputFileName != null) { |
| 407 |
File outputFile = new File(outputFileName); |
| 408 |
if (outputFile.isDirectory()) { |
| 409 |
quit("Output is a directory", 2); |
| 410 |
} |
| 411 |
createFileIfNecessary(outputFile); |
| 412 |
out = new FileOutputStream(outputFile); |
| 413 |
} else { |
| 414 |
out = System.out; |
| 415 |
} |
| 416 |
Serializer serializer = processor.newSerializer(out); |
| 417 |
try { |
| 418 |
options.setSerializationProperties(serializer); |
| 419 |
} catch (IllegalArgumentException e) { |
| 420 |
quit(e.getMessage(), 2); |
| 421 |
} |
| 422 |
if (updating && exp.isUpdateQuery()) { |
| 423 |
serializer.setOutputProperties( |
| 424 |
exp.getUnderlyingCompiledQuery().getExecutable().getPrimarySerializationProperties().getProperties()); |
| 425 |
runUpdate(exp, evaluator, serializer); |
| 426 |
} else { |
| 427 |
runQuery(exp, evaluator, sourceInput, serializer); |
| 428 |
} |
| 429 |
} catch (SaxonApiException err) { |
| 430 |
if (err.getCause() instanceof XPathException && ((XPathException) err.getCause()).hasBeenReported()) { |
| 431 |
String category = ((XPathException) err.getCause()).isTypeError() ? "type" : "dynamic"; |
| 432 |
quit("Query failed with " + category + " error: " + err.getCause().getMessage(), 2); |
| 433 |
} else { |
| 434 |
throw err; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
if (showTime) { |
| 439 |
long endTime = System.nanoTime(); |
| 440 |
if (r >= 3) { |
| 441 |
totalTime += endTime - startTime; |
| 442 |
} |
| 443 |
if (repeat < 100) { |
| 444 |
System.err.println("Execution time: " + CommandLineOptions.showExecutionTimeNano(endTime - startTime)); |
| 445 |
System.err.println("Memory used: " + CommandLineOptions.showMemoryUsed()); |
| 446 |
} else if (totalTime > 1000000000000L) { |
| 447 |
|
| 448 |
break; |
| 449 |
} |
| 450 |
startTime = endTime; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
if (repeat > 3) { |
| 455 |
System.err.println("Average execution time: " + |
| 456 |
CommandLineOptions.showExecutionTimeNano(totalTime / (r - 3))); |
| 457 |
} |
| 458 |
if (options.getOptionValue("TB") != null) { |
| 459 |
|
| 460 |
config.createByteCodeReport(options.getOptionValue("TB")); |
| 461 |
} |
| 462 |
|
| 463 |
} catch (TerminationException err) { |
| 464 |
quit(err.getMessage(), 1); |
| 465 |
} catch (SchemaException err) { |
| 466 |
quit("Schema processing failed: " + err.getMessage(), 2); |
| 467 |
} catch (XPathException | LicenseException | SaxonApiException err) { |
| 468 |
quit("Query processing failed: " + err.getMessage(), 2); |
| 469 |
} catch (TransformerFactoryConfigurationError err) { |
| 470 |
err.printStackTrace(); |
| 471 |
quit("Query processing failed", 2); |
| 472 |
} catch (Exception err2) { |
| 473 |
err2.printStackTrace(); |
| 474 |
quit("Fatal error during query: " + err2.getClass().getName() + ": " + |
| 475 |
(err2.getMessage() == null ? " (no message)" : err2.getMessage()), 2); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
protected void parseOptions(CommandLineOptions options) |
| 487 |
throws TransformerException { |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
options.applyToConfiguration(processor); |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
allowExit = !"off".equals(options.getOptionValue("quit")); |
| 496 |
|
| 497 |
backup = "on".equals(options.getOptionValue("backup")); |
| 498 |
explainOutputFileName = options.getOptionValue("explain"); |
| 499 |
explain = explainOutputFileName != null; |
| 500 |
moduleURIResolverClass = options.getOptionValue("mr"); |
| 501 |
outputFileName = options.getOptionValue("o"); |
| 502 |
streaming = "on".equals(options.getOptionValue("stream")); |
| 503 |
|
| 504 |
String value = options.getOptionValue("p"); |
| 505 |
if ("on".equals(value)) { |
| 506 |
config.setParameterizedURIResolver(); |
| 507 |
useURLs = true; |
| 508 |
} |
| 509 |
|
| 510 |
projection = "on".equals(options.getOptionValue("projection")); |
| 511 |
|
| 512 |
|
| 513 |
value = options.getOptionValue("q"); |
| 514 |
if (value != null) { |
| 515 |
queryFileName = value; |
| 516 |
} |
| 517 |
|
| 518 |
value = options.getOptionValue("qs"); |
| 519 |
if (value != null) { |
| 520 |
queryFileName = "{" + value + "}"; |
| 521 |
} |
| 522 |
|
| 523 |
String qv = options.getOptionValue("qversion"); |
| 524 |
if (qv != null && !"3.1".equals(qv)) { |
| 525 |
System.err.println("-qversion ignored: 3.1 is assumed"); |
| 526 |
} |
| 527 |
|
| 528 |
value = options.getOptionValue("repeat"); |
| 529 |
if (value != null) { |
| 530 |
repeat = Integer.parseInt(value); |
| 531 |
} |
| 532 |
|
| 533 |
sourceFileName = options.getOptionValue("s"); |
| 534 |
|
| 535 |
value = options.getOptionValue("t"); |
| 536 |
if ("on".equals(value)) { |
| 537 |
System.err.println(config.getProductTitle()); |
| 538 |
System.err.println(Version.platform.getPlatformVersion()); |
| 539 |
config.setTiming(true); |
| 540 |
showTime = true; |
| 541 |
} |
| 542 |
|
| 543 |
value = options.getOptionValue("traceout"); |
| 544 |
if (value != null) { |
| 545 |
switch (value) { |
| 546 |
case "#err": |
| 547 |
|
| 548 |
break; |
| 549 |
case "#out": |
| 550 |
traceDestination = new StandardLogger(System.out); |
| 551 |
break; |
| 552 |
case "#null": |
| 553 |
traceDestination = null; |
| 554 |
break; |
| 555 |
default: |
| 556 |
try { |
| 557 |
traceDestination = new StandardLogger(new File(value)); |
| 558 |
closeTraceDestination = true; |
| 559 |
} catch (FileNotFoundException e) { |
| 560 |
badUsage("Trace output file " + value + " cannot be created"); |
| 561 |
} |
| 562 |
break; |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
value = options.getOptionValue("T"); |
| 567 |
if (value != null) { |
| 568 |
if ("".equals(value)) { |
| 569 |
makeXQueryTraceListener(options); |
| 570 |
} else { |
| 571 |
config.setTraceListenerClass(value); |
| 572 |
} |
| 573 |
config.setLineNumbering(true); |
| 574 |
} |
| 575 |
|
| 576 |
value = options.getOptionValue("Tout"); |
| 577 |
if (value != null) { |
| 578 |
config.setTraceListenerOutputFile(value); |
| 579 |
if (options.getOptionValue("T") == null) { |
| 580 |
makeXQueryTraceListener(options); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
value = options.getOptionValue("TB"); |
| 585 |
if (value != null) { |
| 586 |
|
| 587 |
config.setBooleanProperty(Feature.MONITOR_HOT_SPOT_BYTE_CODE, true); |
| 588 |
} |
| 589 |
|
| 590 |
value = options.getOptionValue("TP"); |
| 591 |
if (value != null) { |
| 592 |
TimingTraceListener listener = new TimingTraceListener(); |
| 593 |
config.setTraceListener(listener); |
| 594 |
config.setLineNumbering(true); |
| 595 |
config.getDefaultStaticQueryContext().setCodeInjector(new TimingCodeInjector()); |
| 596 |
if (!value.isEmpty()) { |
| 597 |
try { |
| 598 |
listener.setOutputDestination( |
| 599 |
new StandardLogger(new File(value))); |
| 600 |
} catch (FileNotFoundException e) { |
| 601 |
badUsage("Trace output file " + value + " cannot be created"); |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
value = options.getOptionValue("u"); |
| 607 |
if (value != null) { |
| 608 |
useURLs = "on".equals(value); |
| 609 |
} |
| 610 |
|
| 611 |
value = options.getOptionValue("update"); |
| 612 |
if (value != null) { |
| 613 |
if (!"off".equals(value)) { |
| 614 |
updating = true; |
| 615 |
} |
| 616 |
writeback = !"discard".equals(value); |
| 617 |
} |
| 618 |
|
| 619 |
wrap = "on".equals(options.getOptionValue("wrap")); |
| 620 |
|
| 621 |
value = options.getOptionValue("x"); |
| 622 |
if (value != null) { |
| 623 |
config.setSourceParserClass(value); |
| 624 |
} |
| 625 |
|
| 626 |
String additionalSchemas = options.getOptionValue("xsd"); |
| 627 |
|
| 628 |
value = options.getOptionValue("?"); |
| 629 |
if (value != null) { |
| 630 |
badUsage(""); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
applyLocalOptions(options, config); |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
List<String> positional = options.getPositionalOptions(); |
| 640 |
int currentPositionalOption = 0; |
| 641 |
|
| 642 |
if (queryFileName == null) { |
| 643 |
if (positional.size() == currentPositionalOption) { |
| 644 |
badUsage("No query file name"); |
| 645 |
} |
| 646 |
queryFileName = positional.get(currentPositionalOption++); |
| 647 |
} |
| 648 |
|
| 649 |
if (currentPositionalOption < positional.size()) { |
| 650 |
badUsage("Unrecognized option: " + positional.get(currentPositionalOption)); |
| 651 |
} |
| 652 |
|
| 653 |
String scmInput = options.getOptionValue("scmin"); |
| 654 |
if (scmInput != null) { |
| 655 |
config.importComponents(new StreamSource(scmInput)); |
| 656 |
} |
| 657 |
|
| 658 |
if (additionalSchemas != null) { |
| 659 |
CommandLineOptions.loadAdditionalSchemas(config, additionalSchemas); |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
private void makeXQueryTraceListener(CommandLineOptions options) { |
| 664 |
XQueryTraceListener listener = new XQueryTraceListener(); |
| 665 |
String value = options.getOptionValue("Tout"); |
| 666 |
if (value != null) { |
| 667 |
try { |
| 668 |
listener.setOutputDestination(new StandardLogger(new PrintStream(value))); |
| 669 |
} catch (FileNotFoundException e) { |
| 670 |
badUsage("Cannot write to " + value); |
| 671 |
} |
| 672 |
} |
| 673 |
value = options.getOptionValue("Tlevel"); |
| 674 |
if (value != null) { |
| 675 |
switch (value) { |
| 676 |
case "none": |
| 677 |
listener.setLevelOfDetail(0); |
| 678 |
break; |
| 679 |
case "low": |
| 680 |
listener.setLevelOfDetail(1); |
| 681 |
break; |
| 682 |
case "normal": |
| 683 |
listener.setLevelOfDetail(2); |
| 684 |
break; |
| 685 |
case "high": |
| 686 |
listener.setLevelOfDetail(3); |
| 687 |
break; |
| 688 |
} |
| 689 |
} |
| 690 |
config.setTraceListener(listener); |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
@SuppressWarnings("EmptyMethod") |
| 701 |
protected void applyLocalOptions(CommandLineOptions options, Configuration config) { |
| 702 |
|
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
protected Source processSourceFile(String sourceFileName, boolean useURLs) throws TransformerException { |
| 707 |
Source sourceInput; |
| 708 |
if (useURLs || CommandLineOptions.isImplicitURI(sourceFileName)) { |
| 709 |
sourceInput = config.getURIResolver().resolve(sourceFileName, null); |
| 710 |
if (sourceInput == null) { |
| 711 |
sourceInput = config.getSystemURIResolver().resolve(sourceFileName, null); |
| 712 |
} |
| 713 |
} else if (sourceFileName.equals("-")) { |
| 714 |
|
| 715 |
String sysID = new File(System.getProperty("user.dir")).toURI().toASCIIString(); |
| 716 |
sourceInput = new StreamSource(System.in, sysID); |
| 717 |
} else { |
| 718 |
File sourceFile = new File(sourceFileName); |
| 719 |
if (!sourceFile.exists()) { |
| 720 |
quit("Source file " + sourceFile + " does not exist", 2); |
| 721 |
} |
| 722 |
|
| 723 |
if (Version.platform.isJava()) { |
| 724 |
InputSource eis = new InputSource(sourceFile.toURI().toString()); |
| 725 |
|
| 726 |
|
| 727 |
sourceInput = new SAXSource(eis); |
| 728 |
} else { |
| 729 |
sourceInput = new StreamSource(sourceFile.toURI().toString()); |
| 730 |
} |
| 731 |
} |
| 732 |
return sourceInput; |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
protected XQueryExecutable compileQuery(XQueryCompiler compiler, String queryFileName, boolean useURLs) |
| 748 |
throws SaxonApiException, IOException { |
| 749 |
XQueryExecutable exp; |
| 750 |
if (queryFileName.equals("-")) { |
| 751 |
Reader queryReader = new InputStreamReader(System.in); |
| 752 |
compiler.setBaseURI(new File(System.getProperty("user.dir")).toURI()); |
| 753 |
exp = compiler.compile(queryReader); |
| 754 |
} else if (queryFileName.startsWith("{") && queryFileName.endsWith("}")) { |
| 755 |
|
| 756 |
String q = queryFileName.substring(1, queryFileName.length() - 1); |
| 757 |
compiler.setBaseURI(new File(System.getProperty("user.dir")).toURI()); |
| 758 |
exp = compiler.compile(q); |
| 759 |
} else if (useURLs || CommandLineOptions.isImplicitURI(queryFileName)) { |
| 760 |
ModuleURIResolver resolver = compiler.getModuleURIResolver(); |
| 761 |
boolean isStandardResolver = false; |
| 762 |
if (resolver == null) { |
| 763 |
resolver = getConfiguration().getStandardModuleURIResolver(); |
| 764 |
isStandardResolver = true; |
| 765 |
} |
| 766 |
while (true) { |
| 767 |
String[] locations = {queryFileName}; |
| 768 |
Source[] sources; |
| 769 |
try { |
| 770 |
sources = resolver.resolve(null, null, locations); |
| 771 |
} catch (Exception e) { |
| 772 |
if (e instanceof XPathException) { |
| 773 |
throw new SaxonApiException(e); |
| 774 |
} else { |
| 775 |
XPathException xe = new XPathException("Exception in ModuleURIResolver: ", e); |
| 776 |
xe.setErrorCode("XQST0059"); |
| 777 |
throw new SaxonApiException(xe); |
| 778 |
} |
| 779 |
} |
| 780 |
if (sources == null) { |
| 781 |
if (isStandardResolver) { |
| 782 |
|
| 783 |
quit("System problem: standard ModuleURIResolver returned null", 4); |
| 784 |
} else { |
| 785 |
resolver = getConfiguration().getStandardModuleURIResolver(); |
| 786 |
isStandardResolver = true; |
| 787 |
} |
| 788 |
} else { |
| 789 |
if (sources.length != 1 || !(sources[0] instanceof StreamSource)) { |
| 790 |
quit("Module URI Resolver must return a single StreamSource", 2); |
| 791 |
} |
| 792 |
try { |
| 793 |
String queryText = QueryReader.readSourceQuery((StreamSource) sources[0], config.getValidCharacterChecker()); |
| 794 |
exp = compiler.compile(queryText); |
| 795 |
} catch (XPathException e) { |
| 796 |
throw new SaxonApiException(e); |
| 797 |
} |
| 798 |
break; |
| 799 |
} |
| 800 |
} |
| 801 |
} else { |
| 802 |
try (InputStream queryStream = new FileInputStream(queryFileName)) { |
| 803 |
compiler.setBaseURI(new File(queryFileName).toURI()); |
| 804 |
exp = compiler.compile(queryStream); |
| 805 |
} |
| 806 |
} |
| 807 |
return exp; |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
protected void explain(XQueryExpression exp) throws FileNotFoundException, XPathException { |
| 819 |
OutputStream explainOutput; |
| 820 |
if (explainOutputFileName == null || "".equals(explainOutputFileName)) { |
| 821 |
explainOutput = System.err; |
| 822 |
} else { |
| 823 |
explainOutput = new FileOutputStream(new File(explainOutputFileName)); |
| 824 |
} |
| 825 |
SerializationProperties props = ExpressionPresenter.makeDefaultProperties(config); |
| 826 |
Receiver diag = config.getSerializerFactory().getReceiver( |
| 827 |
new StreamResult(explainOutput), props); |
| 828 |
ExpressionPresenter expressionPresenter = new ExpressionPresenter(config, diag); |
| 829 |
exp.explain(expressionPresenter); |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
protected void processSource( Source sourceInput, XQueryExecutable exp, XQueryEvaluator evaluator) throws SaxonApiException { |
| 842 |
if (sourceInput != null && !streaming) { |
| 843 |
DocumentBuilder builder = processor.newDocumentBuilder(); |
| 844 |
if (exp.isUpdateQuery()) { |
| 845 |
builder.setTreeModel(TreeModel.LINKED_TREE); |
| 846 |
} |
| 847 |
if (showTime) { |
| 848 |
System.err.println("Processing " + sourceInput.getSystemId()); |
| 849 |
} |
| 850 |
if (!exp.getUnderlyingCompiledQuery().usesContextItem()) { |
| 851 |
System.err.println("Source document ignored - query does not access the context item"); |
| 852 |
return; |
| 853 |
} |
| 854 |
if (projection) { |
| 855 |
builder.setDocumentProjectionQuery(exp); |
| 856 |
if (explain) { |
| 857 |
exp.getUnderlyingCompiledQuery().explainPathMap(); |
| 858 |
} |
| 859 |
} |
| 860 |
builder.setDTDValidation(getConfiguration().getBooleanProperty(Feature.DTD_VALIDATION)); |
| 861 |
if (getConfiguration().getBooleanProperty(Feature.DTD_VALIDATION_RECOVERABLE)) { |
| 862 |
sourceInput = new AugmentedSource(sourceInput, getConfiguration().getParseOptions()); |
| 863 |
} |
| 864 |
XdmNode doc = builder.build(sourceInput); |
| 865 |
evaluator.setContextItem(doc); |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
protected void runQuery(XQueryExecutable exp, XQueryEvaluator evaluator, Source input, Destination destination) |
| 879 |
throws SaxonApiException { |
| 880 |
try { |
| 881 |
if (wrap) { |
| 882 |
try { |
| 883 |
XQueryExpression e = exp.getUnderlyingCompiledQuery(); |
| 884 |
SequenceIterator results = e.iterator(evaluator.getUnderlyingQueryContext()); |
| 885 |
DocumentInfo resultDoc = QueryResult.wrap(results, config); |
| 886 |
XdmValue wrappedResultDoc = XdmValue.wrap(resultDoc.getUnderlyingNode()); |
| 887 |
processor.writeXdmValue(wrappedResultDoc, destination); |
| 888 |
destination.closeAndNotify(); |
| 889 |
} catch (XPathException e1) { |
| 890 |
throw new SaxonApiException(e1); |
| 891 |
} |
| 892 |
} else if (streaming) { |
| 893 |
evaluator.runStreamed(input, destination); |
| 894 |
} else { |
| 895 |
evaluator.run(destination); |
| 896 |
} |
| 897 |
} finally { |
| 898 |
if (closeTraceDestination && traceDestination != null) { |
| 899 |
traceDestination.close(); |
| 900 |
} |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
protected void runUpdate(XQueryExecutable exp, XQueryEvaluator evaluator, final Serializer serializer) |
| 913 |
throws SaxonApiException { |
| 914 |
|
| 915 |
try { |
| 916 |
if (serializer.getOutputProperty(Serializer.Property.METHOD) == null) { |
| 917 |
serializer.setOutputProperty(Serializer.Property.METHOD, "xml"); |
| 918 |
} |
| 919 |
if (writeback) { |
| 920 |
final List<SaxonApiException> errors = new ArrayList<>(3); |
| 921 |
UpdateAgent agent = (node, controller) -> { |
| 922 |
try { |
| 923 |
DocumentPool pool = controller.getDocumentPool(); |
| 924 |
String documentURI = pool.getDocumentURI(node); |
| 925 |
if (documentURI != null) { |
| 926 |
rewriteToDisk(node, serializer, backup, showTime ? System.err : null); |
| 927 |
} else if (showTime) { |
| 928 |
System.err.println("Updated document discarded because it was not read using doc()"); |
| 929 |
} |
| 930 |
} catch (SaxonApiException err) { |
| 931 |
System.err.println(err.getMessage()); |
| 932 |
errors.add(err); |
| 933 |
} |
| 934 |
}; |
| 935 |
evaluator.run(); |
| 936 |
try { |
| 937 |
exp.getUnderlyingCompiledQuery().runUpdate(evaluator.getUnderlyingQueryContext(), agent); |
| 938 |
} catch (XPathException e) { |
| 939 |
throw new SaxonApiException(e); |
| 940 |
} |
| 941 |
|
| 942 |
if (!errors.isEmpty()) { |
| 943 |
throw errors.get(0); |
| 944 |
} |
| 945 |
} else { |
| 946 |
try { |
| 947 |
if (evaluator.getContextItem() != null) { |
| 948 |
Set<MutableNodeInfo> affectedDocuments = |
| 949 |
exp.getUnderlyingCompiledQuery().runUpdate(evaluator.getUnderlyingQueryContext()); |
| 950 |
Item initial = evaluator.getContextItem().getUnderlyingValue().head(); |
| 951 |
|
| 952 |
if (initial instanceof NodeInfo && affectedDocuments.contains(initial)) { |
| 953 |
processor.writeXdmValue(evaluator.getContextItem(), serializer); |
| 954 |
} |
| 955 |
} |
| 956 |
} catch (XPathException e) { |
| 957 |
throw new SaxonApiException(e); |
| 958 |
} |
| 959 |
} |
| 960 |
} finally { |
| 961 |
if (closeTraceDestination && traceDestination != null) { |
| 962 |
traceDestination.close(); |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
@SuppressWarnings("ResultOfMethodCallIgnored") |
| 983 |
private static void rewriteToDisk(NodeInfo doc, Serializer serializer, boolean backup, PrintStream log) |
| 984 |
throws SaxonApiException { |
| 985 |
switch (doc.getNodeKind()) { |
| 986 |
case Type.DOCUMENT: |
| 987 |
|
| 988 |
break; |
| 989 |
case Type.ELEMENT: |
| 990 |
NodeInfo parent = doc.getParent(); |
| 991 |
if (parent != null && parent.getNodeKind() != Type.DOCUMENT) { |
| 992 |
throw new SaxonApiException("Cannot rewrite an element node unless it is top-level"); |
| 993 |
} |
| 994 |
break; |
| 995 |
default: |
| 996 |
throw new SaxonApiException("Node to be rewritten must be a document or element node"); |
| 997 |
} |
| 998 |
String uri = doc.getSystemId(); |
| 999 |
if (uri == null || uri.isEmpty()) { |
| 1000 |
throw new SaxonApiException("Cannot rewrite a document with no known URI"); |
| 1001 |
} |
| 1002 |
URI u; |
| 1003 |
try { |
| 1004 |
u = new URI(uri); |
| 1005 |
} catch (URISyntaxException e) { |
| 1006 |
throw new SaxonApiException("SystemId of updated document is not a valid URI: " + uri); |
| 1007 |
} |
| 1008 |
File existingFile = new File(u); |
| 1009 |
File dir = existingFile.getParentFile(); |
| 1010 |
if (backup && existingFile.exists()) { |
| 1011 |
File backupFile = new File(dir, existingFile.getName() + ".bak"); |
| 1012 |
if (log != null) { |
| 1013 |
log.println("Creating backup file " + backupFile); |
| 1014 |
} |
| 1015 |
boolean success = existingFile.renameTo(backupFile); |
| 1016 |
if (!success) { |
| 1017 |
throw new SaxonApiException("Failed to create backup file of " + backupFile); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
if (!existingFile.exists()) { |
| 1021 |
if (log != null) { |
| 1022 |
log.println("Creating file " + existingFile); |
| 1023 |
} |
| 1024 |
try { |
| 1025 |
existingFile.createNewFile(); |
| 1026 |
} catch (IOException e) { |
| 1027 |
throw new SaxonApiException("Failed to create new file " + existingFile); |
| 1028 |
} |
| 1029 |
} else { |
| 1030 |
if (log != null) { |
| 1031 |
log.println("Overwriting file " + existingFile); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
serializer.setOutputFile(existingFile); |
| 1035 |
serializer.getProcessor().writeXdmValue(XdmValue.wrap(doc), serializer); |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
protected void quit(String message, int code) { |
| 1047 |
System.err.println(message); |
| 1048 |
if (allowExit) { |
| 1049 |
System.exit(code); |
| 1050 |
} else { |
| 1051 |
throw new RuntimeException(message); |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
protected void badUsage(String message) { |
| 1061 |
if (!"".equals(message)) { |
| 1062 |
System.err.println(message); |
| 1063 |
} |
| 1064 |
if (!showTime) { |
| 1065 |
System.err.println(config.getProductTitle()); |
| 1066 |
} |
| 1067 |
System.err.println("Usage: see http://www.saxonica.com/documentation/index.html#!using-xquery/commandline"); |
| 1068 |
System.err.println("Format: " + CommandLineOptions.getCommandName(this) + " options params"); |
| 1069 |
CommandLineOptions options = new CommandLineOptions(); |
| 1070 |
setPermittedOptions(options); |
| 1071 |
System.err.println("Options available:" + options.displayPermittedOptions()); |
| 1072 |
System.err.println("Use -XYZ:? for details of option XYZ"); |
| 1073 |
System.err.println("Params: "); |
| 1074 |
System.err.println(" param=value Set query string parameter"); |
| 1075 |
System.err.println(" +param=filename Set query document parameter"); |
| 1076 |
System.err.println(" ?param=expression Set query parameter using XPath"); |
| 1077 |
System.err.println(" !param=value Set serialization parameter"); |
| 1078 |
if (allowExit) { |
| 1079 |
System.exit("".equals(message) ? 0 : 2); |
| 1080 |
} else { |
| 1081 |
throw new RuntimeException(message); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
@SuppressWarnings("ResultOfMethodCallIgnored") |
| 1093 |
public static void createFileIfNecessary(File file) throws IOException { |
| 1094 |
if (!file.exists()) { |
| 1095 |
File directory = file.getParentFile(); |
| 1096 |
if (directory != null && !directory.exists()) { |
| 1097 |
directory.mkdirs(); |
| 1098 |
} |
| 1099 |
file.createNewFile(); |
| 1100 |
} |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|