Advertisement
Guest User

SaxonTest.java

a guest
Oct 9th, 2013
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. package saxontest;
  2.  
  3. import java.util.ArrayList;
  4. import javax.xml.xpath.*;
  5. import net.sf.saxon.Configuration;
  6. import net.sf.saxon.event.PipelineConfiguration;
  7. import net.sf.saxon.om.NodeInfo;
  8. import net.sf.saxon.om.NoNamespaceName;
  9. import net.sf.saxon.om.Sequence;
  10. import net.sf.saxon.tree.linked.LinkedTreeBuilder;
  11. import net.sf.saxon.type.AnyType;
  12. import net.sf.saxon.type.BuiltInAtomicType;
  13. import net.sf.saxon.xpath.XPathFactoryImpl;
  14.  
  15. public class SaxonTest {
  16.  
  17.     public static void eval(XPathExpression compiledExpr, NodeInfo node) throws Exception {
  18.         Object res = compiledExpr.evaluate(node, XPathConstants.NODESET);
  19.         ArrayList<NodeInfo> al = (ArrayList<NodeInfo>) res;
  20.         if (al.size() != 1) System.out.println(al.size());
  21.     }
  22.  
  23.     public static NodeInfo addRow(LinkedTreeBuilder tb, NoNamespaceName parentName, NoNamespaceName childName) throws Exception {
  24.         tb.startElement(parentName, AnyType.getInstance(), 0, 0);
  25.         tb.startContent();
  26.  
  27.             NodeInfo rowNode = tb.getCurrentParentNode();
  28.  
  29.             tb.startElement(childName, BuiltInAtomicType.STRING, 0, 0);
  30.             tb.startContent();
  31.                 tb.characters("1", 0, 0);
  32.             tb.endElement();
  33.  
  34.             tb.startElement(childName, BuiltInAtomicType.STRING, 0, 0);
  35.             tb.startContent();
  36.                 tb.characters("2", 0, 0);
  37.             tb.endElement();
  38.  
  39.             tb.startElement(childName, BuiltInAtomicType.STRING, 0, 0);
  40.             tb.startContent();
  41.                 tb.characters("3", 0, 0);
  42.             tb.endElement();
  43.  
  44.         tb.endElement();
  45.  
  46.         return rowNode;
  47.     }
  48.  
  49.     public static void main(String[] args) throws Exception {
  50.         if (args.length != 1) {
  51.             System.out.println("Argument required");
  52.             System.exit(1);
  53.         }
  54.  
  55.         int numNodes = 0;
  56.  
  57.         try {
  58.             numNodes = Integer.parseInt(args[0]);
  59.         } catch(Exception e) {
  60.             System.out.println("Integer argument required");
  61.             System.exit(1);
  62.         }
  63.  
  64.         XPathFactoryImpl factory = new XPathFactoryImpl();
  65.         XPath xpath = factory.newXPath();
  66.         XPathExpression compiledExpr = xpath.compile("../header[1]/title[3]");
  67.  
  68.         Configuration config = factory.getConfiguration();
  69.         PipelineConfiguration pc = config.makePipelineConfiguration();
  70.  
  71.         NoNamespaceName rootName = new NoNamespaceName("root");
  72.         NoNamespaceName headerName = new NoNamespaceName("header");
  73.         NoNamespaceName titleName = new NoNamespaceName("title");
  74.         NoNamespaceName rowName = new NoNamespaceName("row");
  75.         NoNamespaceName itemName = new NoNamespaceName("item");
  76.  
  77.         NodeInfo lastNode = null;
  78.  
  79.         LinkedTreeBuilder tb = new LinkedTreeBuilder(pc);
  80.  
  81.         tb.open();
  82.         tb.startDocument(0);
  83.  
  84.         tb.startElement(rootName, AnyType.getInstance(), 0, 0);
  85.         tb.startContent();
  86.  
  87.             addRow(tb, headerName, titleName);
  88.  
  89.             for (int x = 0; x < numNodes; x++) {
  90.                 lastNode = addRow(tb, rowName, itemName);
  91.             }
  92.  
  93.         tb.endElement();
  94.  
  95.         tb.endDocument();
  96.         tb.close();
  97.  
  98.         long startTime = System.nanoTime();
  99.         for (int x = 0; x < 10000; x++) {
  100.             eval(compiledExpr, lastNode);
  101.         }
  102.         long endTime = System.nanoTime();
  103.         long duration = endTime - startTime;
  104.         System.out.println(numNodes + " nodes -> " + (duration / 1000000) + " ms");
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement