Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package saxontest;
- import java.util.ArrayList;
- import javax.xml.xpath.*;
- import net.sf.saxon.Configuration;
- import net.sf.saxon.event.PipelineConfiguration;
- import net.sf.saxon.om.NodeInfo;
- import net.sf.saxon.om.NoNamespaceName;
- import net.sf.saxon.om.Sequence;
- import net.sf.saxon.tree.linked.LinkedTreeBuilder;
- import net.sf.saxon.type.AnyType;
- import net.sf.saxon.type.BuiltInAtomicType;
- import net.sf.saxon.xpath.XPathFactoryImpl;
- public class SaxonTest {
- public static void eval(XPathExpression compiledExpr, NodeInfo node) throws Exception {
- Object res = compiledExpr.evaluate(node, XPathConstants.NODESET);
- ArrayList<NodeInfo> al = (ArrayList<NodeInfo>) res;
- if (al.size() != 1) System.out.println(al.size());
- }
- public static NodeInfo addRow(LinkedTreeBuilder tb, NoNamespaceName parentName, NoNamespaceName childName) throws Exception {
- tb.startElement(parentName, AnyType.getInstance(), 0, 0);
- tb.startContent();
- NodeInfo rowNode = tb.getCurrentParentNode();
- tb.startElement(childName, BuiltInAtomicType.STRING, 0, 0);
- tb.startContent();
- tb.characters("1", 0, 0);
- tb.endElement();
- tb.startElement(childName, BuiltInAtomicType.STRING, 0, 0);
- tb.startContent();
- tb.characters("2", 0, 0);
- tb.endElement();
- tb.startElement(childName, BuiltInAtomicType.STRING, 0, 0);
- tb.startContent();
- tb.characters("3", 0, 0);
- tb.endElement();
- tb.endElement();
- return rowNode;
- }
- public static void main(String[] args) throws Exception {
- if (args.length != 1) {
- System.out.println("Argument required");
- System.exit(1);
- }
- int numNodes = 0;
- try {
- numNodes = Integer.parseInt(args[0]);
- } catch(Exception e) {
- System.out.println("Integer argument required");
- System.exit(1);
- }
- XPathFactoryImpl factory = new XPathFactoryImpl();
- XPath xpath = factory.newXPath();
- XPathExpression compiledExpr = xpath.compile("../header[1]/title[3]");
- Configuration config = factory.getConfiguration();
- PipelineConfiguration pc = config.makePipelineConfiguration();
- NoNamespaceName rootName = new NoNamespaceName("root");
- NoNamespaceName headerName = new NoNamespaceName("header");
- NoNamespaceName titleName = new NoNamespaceName("title");
- NoNamespaceName rowName = new NoNamespaceName("row");
- NoNamespaceName itemName = new NoNamespaceName("item");
- NodeInfo lastNode = null;
- LinkedTreeBuilder tb = new LinkedTreeBuilder(pc);
- tb.open();
- tb.startDocument(0);
- tb.startElement(rootName, AnyType.getInstance(), 0, 0);
- tb.startContent();
- addRow(tb, headerName, titleName);
- for (int x = 0; x < numNodes; x++) {
- lastNode = addRow(tb, rowName, itemName);
- }
- tb.endElement();
- tb.endDocument();
- tb.close();
- long startTime = System.nanoTime();
- for (int x = 0; x < 10000; x++) {
- eval(compiledExpr, lastNode);
- }
- long endTime = System.nanoTime();
- long duration = endTime - startTime;
- System.out.println(numNodes + " nodes -> " + (duration / 1000000) + " ms");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement