1 /*------------------------------------------------------------------------------
  2 Name:      XtOmQueryTest.java
  3 Project:   xmlBlaster.org
  4 Copyright: many people
  5 Comment:   Syntax for Query:
  6               XPath: http://www.w3.org/TR/xpath
  7 
  8            XT implementation:
  9               http://www.jclark.com/xml/xt.html
 10 
 11            XPath interface (contains everything):
 12               http://www.246.ne.jp/~kamiya/pub/omquery.zip
 13 
 14 Compile:   jikes *.java  (put local directory into CLASSPATH)
 15 Invoke:    java XtOmQueryTest Agent.xml xmlBlaster/key/AGENT[@id=\"192.168.124.10\"] xmlBlaster/key/AGENT/DRIVER[@id=\"FileProof\"] xmlBlaster/key[@oid=\"2\"]
 16 Version:   $Id: XtOmQueryTest.java 14846 2006-03-07 17:14:22Z ruff $
 17 ------------------------------------------------------------------------------*/
 18 
 19 import com.jclark.xsl.om.*;
 20 
 21 import java.util.logging.Logger;
 22 import java.util.logging.Level;
 23 import org.xmlBlaster.util.StopWatch;
 24 
 25 import java.io.File;
 26 import java.io.IOException;
 27 
 28 import java.util.Enumeration;
 29 import java.util.Properties;
 30 
 31 import org.xml.sax.InputSource;
 32 import org.xml.sax.SAXException;
 33 
 34 import com.fujitsu.xml.omquery.XtOmQueryMgr;
 35 
 36 class XtOmQueryTest
 37 {
 38    static private final String parser_name = "org.apache.crimson.parser.Parser2";
 39    static private final String ME = "XtOmQueryTest";
 40 
 41    public XtOmQueryTest(String argv[])
 42    {
 43       if (argv.length < 2) {
 44          System.out.println("Usage:\n\n   java XtOmQueryTest <XML-file> <Query-String>\n\nExample:\n   java XtOmQueryTest Agent.xml xmlBlaster/key/AGENT[@id=\\\"192.168.124.10\\\"]\n");
 45       }
 46 
 47       boolean dumpIt = false;
 48       if (argv.length == 2) dumpIt = true;
 49 
 50       Enumeration iter;
 51       int num_nodes;
 52       InputSource input;
 53 
 54       Properties prop = System.getProperties();
 55       prop.put("org.xml.sax.parser", parser_name);
 56       System.setProperties(prop);
 57 
 58       // Query: xmlBlaster/key/AGENT[@id=\"192.168.124.10\"]  xmlBlaster/key/AGENT/DRIVER[@id=\"FileProof\"]  xmlBlaster/key[@oid=\"2\"]
 59       // Time 1: For 7 <key> blocks on 266 MHz AMD Linux, JDK 1.2
 60       // Time 2: For 600 <key> blocks on 266 MHz AMD Linux, JDK 1.2
 61 
 62       try
 63       {
 64          StopWatch inputTime = new StopWatch();
 65          input = new InputSource(createURL(argv[0]));           // [ 29 millis ] [ 28 millis ]
 66          System.out.println("Read file" + inputTime.nice());
 67 
 68          StopWatch mgrTime = new StopWatch();
 69          XtOmQueryMgr query_mgr = new XtOmQueryMgr();           // [ 588 millis ] [ 612 millis ]
 70          System.out.println("Instantiate DomQueryMgr" + mgrTime.nice());
 71 
 72          {
 73             StopWatch loadTime = new StopWatch();
 74             com.jclark.xsl.om.Node node = query_mgr.load(input);// [ 738 millis ] [ 1 sec 987 millis ]
 75             System.out.println("Load nodes" + loadTime.nice());
 76 
 77             if (argv.length > 1) {
 78                StopWatch queryTime = new StopWatch();
 79                iter = query_mgr.getNodesByXPath(node, argv[1]); // [ 2 sec 422 millis ] [ 2 sec 577 millis ]
 80                System.out.println("Query time" + queryTime.nice());
 81 
 82                num_nodes = getNumNodes(iter, dumpIt);
 83                System.out.println(num_nodes + " nodes matches for XPath " + "\"" + argv[1] + "\"");
 84             }
 85 
 86             if (dumpIt)
 87                System.exit(1);
 88 
 89             if (argv.length > 2) {
 90                StopWatch queryTime2 = new StopWatch();
 91                iter = query_mgr.getNodesByXPath(node, argv[2]); // [ 3 millis ] [ 1 millis ]
 92                System.out.println("Query time" + queryTime2.nice());
 93 
 94                num_nodes = getNumNodes(iter, dumpIt);
 95                System.out.println(num_nodes + " nodes matches for XPath " + "\"" + argv[2] + "\"");
 96             }
 97 
 98             if (argv.length > 3) {
 99                StopWatch queryTime2 = new StopWatch();
100                iter = query_mgr.getNodesByXPath(node, argv[3]); // [ 1 millis ] [ 0 millis ]
101                System.out.println("Query time" + queryTime2.nice());
102 
103                num_nodes = getNumNodes(iter, dumpIt);
104                System.out.println(num_nodes + " nodes matches for XPath " + "\"" + argv[3] + "\"");
105             }
106          }
107 
108          {
109             StopWatch loadTime = new StopWatch();
110             Node node = query_mgr.load(input);                  // [ 22 millis ] [ 1 sec 211 millis ]
111             System.out.println("Load nodes" + loadTime.nice());
112 
113             StopWatch queryTime = new StopWatch();
114             iter = query_mgr.getNodesByXPath(node, argv[1]);    // [ 0 millis ] [ 1 millis ]
115             System.out.println("Query time" + queryTime.nice());
116 
117             num_nodes = getNumNodes(iter, dumpIt);
118             System.out.println(num_nodes + " nodes matches for XPath " + "\"" + argv[1] + "\"");
119          }
120       }
121       catch (IOException e)
122       {
123          System.err.println(e.getMessage());
124          e.printStackTrace();
125       }
126       catch (SAXException e)
127       {
128          System.err.println(e.getMessage());
129          e.printStackTrace();
130       }
131       catch (XSLException e)
132       {
133          System.err.println(e.getMessage());
134          e.printStackTrace();
135       }
136    }
137 
138    private int getNumNodes(Enumeration nodeIter, boolean dumpIt) throws XSLException
139    {
140       int n = 0;
141 
142       //com.jclark.xsl.om.Name key_id = new com.jclark.xsl.om.Name("oid");
143 
144       while (nodeIter.hasMoreElements())
145       {
146          n++;
147          Object obj = nodeIter.nextElement();
148          Node node = (Node)obj;
149          if (dumpIt) {
150             //NameTableImpl nti = (NameTableImpl) node.getCreator();
151 
152             System.out.println("Processing node " + node.getName() + ": " + node.getData());
153             System.out.println("Processing node " + node.toString());
154 
155             SafeNodeIterator siter = node.getAttributes();
156 
157             Object aobj = siter.next();
158             while (aobj != null) {
159                System.out.println("Attibutes:  " + aobj.toString());
160 
161                aobj = siter.next();
162             }
163 
164             com.jclark.xsl.om.Node parent = node.getParent();
165 
166             if (parent == null)
167                System.out.println("No parent");
168             else {
169                System.out.println("Got parent");
170             }
171          }
172       }
173 
174       return n;
175    }
176 
177    private String createURL(String path)
178    {
179       File f = new File(path);
180       String uri = f.getAbsolutePath();
181 
182       char sep = System.getProperty("file.separator").charAt(0);
183       uri = uri.replace(sep, '/');
184       if (uri.charAt(0) != '/')
185       uri = '/' + uri;
186 
187       uri = "file://" + uri;
188 
189       return uri;
190    }
191 
192    public static void main(String argv[])
193    {
194       new XtOmQueryTest(argv);
195    }
196 }


syntax highlighted by Code2HTML, v. 0.9.1