}
}
private DocumentPool createDocumentPool(final boolean isBench) {
// prepare BuilderPool
PoolConfig config = new PoolConfig();
if (noBuilderPool) config.setMaxEntries(0);
final BuilderPool builderPool;
if (filter == null) {
builderPool = new BuilderPool(config, new BuilderFactory());
} else {
BuilderFactory builderFactory = new BuilderFactory() {
protected Builder newBuilder(XMLReader parser, boolean validate) {
StreamingTransform myTransform = new StreamingTransform() {
public Nodes transform(Element subtree) {
return XQueryUtil.xquery(subtree, filterQuery);
}
};
return new Builder(parser, validate, filter.createNodeFactory(null, myTransform));
}
};
builderPool = new BuilderPool(config, builderFactory);
}
// prepare DocumentFactory and DocumentPool
DocumentFactory docFactory = new DocumentFactory() {
public Document createDocument(InputStream input, URI baseURI)
throws ParsingException, IOException {
long start = System.currentTimeMillis();
Document doc;
if (baseURI != null && baseURI.getPath().endsWith(".bnux")) {
if (filter == null) {
doc = getBinaryXMLFactory().createDocument(input, baseURI);
} else {
StreamingTransform myTransform = new StreamingTransform() {
public Nodes transform(Element subtree) {
return XQueryUtil.xquery(subtree, filterQuery);
}
};
if (input == null && baseURI == null)
throw new IllegalArgumentException("input and baseURI must not both be null");
if (input == null) input = baseURI.toURL().openStream();
try {
doc = new BinaryXMLCodec().deserialize(input, filter.createNodeFactory(null, myTransform));
if (baseURI != null) doc.setBaseURI(baseURI.toASCIIString());
} finally {
input.close(); // do what SAX XML parsers do
}
}
} else {
doc = super.createDocument(input, baseURI);
}
if (xinclude) {
try {
XIncluder.resolveInPlace(doc, newBuilder());
} catch (XIncludeException e) {
throw new ParsingException(e.getMessage(), e);
}
}
if (stripWhitespace) XOMUtil.Normalizer.STRIP.normalize(doc);
long end = System.currentTimeMillis();
if (isBench || explain) System.out.println(baseURI + " parse [ms]=" + (end-start));
return doc;
}
protected Builder newBuilder() {
if (validate.equals("wf")) {
return builderPool.getBuilder(false);
} else if (validate.equals("dtd")) {
if (schema == null) return builderPool.getBuilder(true);
EntityResolver resolver;
try {
resolver = new BuilderFactory().createResolver(
new FileInputStream(schema));
} catch (IOException e) {
throw new UsageException(e);
}
return builderPool.getDTDBuilder(resolver);
} else if (validate.equals("schema")) {
HashMap map = new HashMap();
if (schema != null) map.put(schema, namespace);
// return new BuilderFactory().createW3CBuilder(map);
return builderPool.getW3CBuilder(map);
} else if (validate.equals("relaxng")) {
if (schema == null) throw new UsageException(
"Missing required argument --schema");
return builderPool.getMSVBuilder(schema.toURI());
} else if (validate.equals("html")) {
XMLReader parser;
try {
parser = (XMLReader) Class.forName("org.ccil.cowan.tagsoup.Parser").newInstance();
} catch (Exception e) {
throw new UsageException(e);
}
return new Builder(parser);
} else {
throw new UsageException("Illegal validate option: " + validate);
}
}
};
return new DocumentPool(
new DocumentMap(
new PoolConfig().
setCompressionLevel(docPoolCompression).
setCapacity(docPoolCapacity)),
docFactory);
}