// prepare XQuery pool
XQueryPool queryPool = new XQueryPool(
new PoolConfig(), new XQueryFactory(null, resolver));
ResultSequenceSerializer serializer = new ResultSequenceSerializer();
serializer.setAlgorithm(algorithm);
serializer.setEncoding(encoding);
serializer.setIndent(indent);
// now do the real work
long runsStart = System.currentTimeMillis();
for (int run=0; run < runs; run++) {
if (isBench) {
System.out.println("\n\n******************************************");
System.out.println("run = " + run + ":");
}
for (int i=0; i < queries.size(); i++) {
long start = System.currentTimeMillis();
long serializationTime = 0;
Object query = queries.get(i);
XQuery xquery;
if (query instanceof String) {
xquery = queryPool.getXQuery((String)query, baseURI);
} else if (query instanceof File) {
xquery = queryPool.getXQuery((File)query, baseURI);
} else {
xquery = null; // disable XQuery for benchmarking
}
if (isBench) {
System.out.println("query = " +query);
}
if (explain && run == 0 && xquery != null) {
System.out.println("explain = \n" + xquery.explain());
}
XQuery morpher;
if (update instanceof String) {
morpher = queryPool.getXQuery((String)update, null);
} else if (update instanceof File) {
morpher = queryPool.getXQuery((File)update, null);
} else {
morpher = null;
}
int numSerials = 0;
for (int j=0; j < inputFiles.length; j++) {
Document doc = null;
if (inputFiles[j] != null) {
doc = docPool.getDocument(new File(inputFiles[j]));
}
if (explain && doc != null) {
System.out.println("stats=" + toStatisticsString(doc));
}
for (int iter=0; iter < iterations; iter++) {
Document doc2 = doc;
if (morpher != null && doc2 != null) {
doc2 = new Document(doc2); // immutable for multiple iterations
}
// run the query
Nodes results;
if (xomXPath) {
if (doc2 == null) throw new UsageException(
"A context node is required by XOM's XPath engine, but missing.");
results = doc2.query((String)query);
} else if (xquery != null) {
results = xquery.execute(doc2, null, variables).toNodes();
} else {
results = new Nodes(); // disable XQuery for benchmarking
results.append(doc2);
}
if (morpher != null) {
// interpret --query as select, interpret --update as morpher
for (int k=0; doc2 == null && k < results.size(); k++) {
doc2 = results.get(k).getDocument();
}
XQueryUtil.update(results, morpher, null);
// serialize modified document if there is one
results = new Nodes();
if (doc2 != null) results.append(doc2);
}
// serialize results onto output, if any
File f = (File) outputFiles.get(j);
OutputStream out = System.out;
if (f != null) {
if (f.getAbsolutePath().equals("/dev/null")) continue;
out = new FileOutputStream(f);
}
long serializationStart = System.currentTimeMillis();
serializer.write(results, out);
if (out != System.out && out != System.err) out.close();
serializationTime += System.currentTimeMillis() - serializationStart;
numSerials++;
}
}