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);