if (mode.equals("xom-NNF")) {
builder = new Builder(XOMUtil.getNullNodeFactory());
}
// saxon
StaticQueryContext context = null;
Transformer saxonSerializer = null;
if (mode.equals("saxon")) {
context = new StaticQueryContext(new Configuration());
String clazz = "net.sf.saxon.TransformerFactoryImpl";
System.setProperty("javax.xml.transform.TransformerFactory", clazz);
saxonSerializer = TransformerFactory.newInstance().newTransformer();
}
// DOM
DocumentBuilder domBuilder = null;
Transformer domSerializer = null;
if (mode.equals("dom")) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion", Boolean.FALSE);
domBuilder = factory.newDocumentBuilder();
String clazz = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
// String clazz = "org.apache.xalan.processor.TransformerFactoryImpl";
System.setProperty("javax.xml.transform.TransformerFactory", clazz);
domSerializer = TransformerFactory.newInstance().newTransformer();
System.err.println(domSerializer.getClass().getName());
}
// FastInfoSet
Object fiSerializer = null;
// XMLStreamWriter fiStaxSerializer = null;
Builder fiBuilder = null;
Method fiMethod = null;
XMLStreamReader fistaxReader = null;
Method fistaxMethod = null;
if (mode.startsWith("fi")) {
NodeFactory factory = null;
if (mode.indexOf("NNF") >= 0) factory = XOMUtil.getNullNodeFactory();
XMLReader parser = (XMLReader) Class.forName("com.sun.xml.fastinfoset.sax.SAXDocumentParser").newInstance();
fiBuilder = new Builder(parser, false, factory);
if (mode.indexOf("stax") >= 0) {
fiSerializer = (XMLStreamWriter) Class.forName("com.sun.xml.fastinfoset.stax.StAXDocumentSerializer").newInstance();
} else {
fiSerializer = (ContentHandler) Class.forName("com.sun.xml.fastinfoset.sax.SAXDocumentSerializer").newInstance();
}
if (mode.startsWith("fi1")) { // enable "full indexing"
Method method;
method = fiSerializer.getClass().getMethod(
"setAttributeValueSizeLimit", new Class[] {Integer.TYPE});
method.invoke(fiSerializer, new Object[] {new Integer(Integer.MAX_VALUE)});
method = fiSerializer.getClass().getMethod(
"setCharacterContentChunkSizeLimit", new Class[] {Integer.TYPE});
method.invoke(fiSerializer, new Object[] {new Integer(Integer.MAX_VALUE)});
}
fiMethod = fiSerializer.getClass().getMethod(
"setOutputStream", new Class[] { OutputStream.class});
if (mode.indexOf("stax") >= 0) {
fistaxReader = (XMLStreamReader) Class.forName("com.sun.xml.fastinfoset.stax.StAXDocumentParser").newInstance();
fistaxMethod = fistaxReader.getClass().getMethod(
"setInputStream", new Class[] { InputStream.class});
}
}
// StAX
Builder staxBuilder = null;
if (mode.indexOf("stax") >= 0) {
NodeFactory factory = null;
if (mode.indexOf("NNF") >= 0) factory = XOMUtil.getNullNodeFactory();
staxBuilder = StaxUtil.createBuilder(staxInputFactory, factory);
}
for (int j=5; j < args.length; j++) {
try {
File[] files = IOTestUtil.listXMLFiles(args[j]);
for (int k=0; k < files.length; k++) {
File file = files[k];
if (bogus(file) || ignore(file) || file.isDirectory()) {
System.out.println("\n" + ": IGNORING " + file + " ...");
continue;
}
System.out.println("now processing " + file);
// System.out.print(".");
// prepare
// file:/path/to/file --> file:///path/to/file
String baseURI = file.toURI().toASCIIString();
if (baseURI.startsWith("file:/")) {
baseURI = baseURI.substring("file:/".length());
if (!baseURI.startsWith("//")) baseURI = "//" + baseURI;
baseURI = "file:/" + baseURI;
}
// System.out.println("baseURI: " + baseURI);
Document doc = new Builder().build(file);
byte[] data = codec.serialize(doc, compressionLevel);
if (!cmd.equals("test")) doc = new BinaryXMLCodec().deserialize(data); // use "interned" strings
byte[] fileData = FileUtil.toByteArray(new FileInputStream(file));
long fileLength = file.length();
int encodedSize = 0;
org.w3c.dom.Document domDoc = null;
if (mode.equals("dom")) {
domDoc = domBuilder.parse(file);
}
NodeInfo saxonDoc = null;
if (mode.equals("saxon")) {
saxonDoc = context.buildDocument(new StreamSource(new ByteArrayInputStream(fileData)));
}
if (mode.startsWith("fi")) {
if (mode.indexOf("stax") >= 0) {
// data = serializeWithStax(doc, staxOutputFactory);
data = serializeWithFastInfosetStax(doc, (XMLStreamWriter)fiSerializer, fiMethod, new ByteArrayOutputStream());
} else {
data = serializeWithFastInfoset(doc, (ContentHandler)fiSerializer, fiMethod, new ByteArrayOutputStream());
}
}
// run the benchmark
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
try {
// serialize
if (cmd.equals("ser") || cmd.equals("serdeser") || cmd.equals("test")) {
ByteArrayOutputStream out = createOutputStream(cmd.equals("ser"));
if (mode.startsWith("bnux")) {
if (mode.indexOf("stream") < 0) {
codec.serialize(doc, compressionLevel, out);
data = out.toByteArray();
} else {
data = serializeWithStreamingBnux(doc, compressionLevel, out);
}
} else if (mode.startsWith("xom")) {
if (mode.indexOf("stax") >= 0) {
data = serializeWithStax(doc, staxOutputFactory, out);
} else if (mode.indexOf("stream") < 0) {
data = serializeWithXOM(doc, out);
} else {
data = serializeWithStreamingXOM(doc, out);
}
} else if (mode.equals("saxon")) {
saxonSerializer.transform(saxonDoc, new StreamResult(out));
data = out.toByteArray();
} else if (mode.equals("dom")) {
domSerializer.transform(new DOMSource(domDoc), new StreamResult(out));
data = out.toByteArray();
} else if (mode.startsWith("fi")) {
if (mode.indexOf("stax") >= 0) {
// data = serializeWithStax(doc, staxOutputFactory);
data = serializeWithFastInfosetStax(doc, (XMLStreamWriter)fiSerializer, fiMethod, out);
} else {
data = serializeWithFastInfoset(doc, (ContentHandler)fiSerializer, fiMethod, out);
}
} else {
throw new IllegalArgumentException("illegal mode");
}
checksum += data.length;
}
encodedSize = data.length;
doneEncoded += encodedSize;
// deserialize
Document doc2 = null;
if (cmd.equals("deser") || cmd.equals("serdeser") || cmd.equals("test")) {
if (mode.startsWith("bnux")) {
doc2 = codec.deserialize(new ByteArrayInputStream(data), bnuxFactory);
} else if (mode.startsWith("xom") && mode.indexOf("stax") >= 0) {
doc2 = staxBuilder.build(new ByteArrayInputStream(fileData));
} else if (mode.startsWith("xom")) {
if (mode.indexOf("stream") < 0) {
doc2 = builder.build(new ByteArrayInputStream(fileData), baseURI);
} else {
doc2 = builder.build(new ByteArrayInputStream(data), baseURI);
}
} else if (mode.equals("saxon")) { // just for deser comparison
context.buildDocument(new StreamSource(new ByteArrayInputStream(fileData)));
} else if (mode.equals("dom")) {
domDoc = domBuilder.parse(new ByteArrayInputStream(fileData));
// System.err.println(domDoc.getClass().getName());
} else if (mode.startsWith("fi") && mode.indexOf("stax") >=0 ) {
fistaxMethod.invoke(fistaxReader, new Object[] {new ByteArrayInputStream(data)});