in src/main/java/org/apache/xmlbeans/impl/tool/RunXQuery.java [45:223]
public static void main(String[] args) throws Exception {
Set<String> flags = new HashSet<>();
flags.add("h");
flags.add("help");
flags.add("usage");
flags.add("license");
flags.add("version");
flags.add("verbose");
flags.add("pretty");
CommandLine cl =
new CommandLine(
args, flags,
Arrays.asList("q", "qf"));
if (cl.getOpt("h") != null || cl.getOpt("help") != null || cl.getOpt("usage") != null) {
printUsage();
System.exit(0);
return;
}
String[] badopts = cl.getBadOpts();
if (badopts.length > 0) {
for (String badopt : badopts) {
System.out.println("Unrecognized option: " + badopt);
}
printUsage();
System.exit(0);
return;
}
if (cl.getOpt("license") != null) {
CommandLine.printLicense();
System.exit(0);
return;
}
if (cl.getOpt("version") != null) {
CommandLine.printVersion();
System.exit(0);
return;
}
args = cl.args();
if (args.length == 0) {
printUsage();
System.exit(0);
return;
}
boolean verbose = cl.getOpt("verbose") != null;
boolean pretty = cl.getOpt("pretty") != null;
//
// Get and compile the query
//
String query = cl.getOpt("q");
String queryfile = cl.getOpt("qf");
if (query == null && queryfile == null) {
System.err.println("No query specified");
System.exit(0);
return;
}
if (query != null && queryfile != null) {
System.err.println("Specify -qf or -q, not both.");
System.exit(0);
return;
}
try {
if (queryfile != null) {
File queryFile = new File(queryfile);
InputStream is = Files.newInputStream(queryFile.toPath());
StringBuilder sb = new StringBuilder();
try (InputStreamReader r = new InputStreamReader(is, StandardCharsets.ISO_8859_1)) {
for (; ; ) {
int ch = r.read();
if (ch < 0) {
break;
}
sb.append((char) ch);
}
}
is.close();
query = sb.toString();
}
} catch (Throwable e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
System.err.println("Cannot read query file: " + e.getMessage());
System.exit(1);
return;
}
if (verbose) {
System.out.println("Compile Query:");
System.out.println(query);
System.out.println();
}
try {
query = XmlBeans.compileQuery(query);
} catch (Exception e) {
System.err.println("Error compiling query: " + e.getMessage());
System.exit(1);
return;
}
//
// Get the instance
//
File[] files = cl.getFiles();
for (File file : files) {
XmlObject x;
try {
if (verbose) {
try(InputStream is = Files.newInputStream(file.toPath())) {
for (; ; ) {
int ch = is.read();
if (ch < 0) {
break;
}
System.out.write(ch);
}
}
System.out.println();
}
x = XmlObject.Factory.parse(file);
} catch (Throwable e) {
System.err.println("Error parsing instance: " + e.getMessage());
System.exit(1);
return;
}
if (verbose) {
System.out.println("Executing Query...");
System.err.println();
}
XmlObject[] result;
try {
result = x.execQuery(query);
} catch (Throwable e) {
System.err.println("Error executing query: " + e.getMessage());
System.exit(1);
return;
}
if (verbose) {
System.out.println("Query Result:");
}
XmlOptions opts = new XmlOptions();
opts.setSaveOuter();
if (pretty) {
opts.setSavePrettyPrint();
}
for (XmlObject xmlObject : result) {
xmlObject.save(System.out, opts);
System.out.println();
}
}
}