1 /*------------------------------------------------------------------------------
  2 Name:      DomQueryTest.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:    With key oid:
 16              java DomQueryTest AgentBig.xml xmlBlaster/key/AGENT[@id=\"192.168.124.10\"] xmlBlaster/key/AGENT/DRIVER[@id=\"FileProof\"] xmlBlaster/key[@oid=\"2\"]
 17            Normal XPath checks:
 18              java -DtestAgentNavigation=false DomQueryTest Qos.xml /qos/expiration
 19 Version:   $Id: DomQueryTest.java 14846 2006-03-07 17:14:22Z ruff $
 20 ------------------------------------------------------------------------------*/
 21 
 22 import com.jclark.xsl.om.*;
 23 
 24 import java.io.File;
 25 import java.io.IOException;
 26 
 27 import org.xmlBlaster.util.StopWatch;
 28 
 29 import java.util.Properties;
 30 import java.util.Enumeration;
 31 
 32 import org.xml.sax.InputSource;
 33 import org.xml.sax.SAXException;
 34 
 35 import com.jclark.xsl.dom.XMLProcessorImpl;
 36 import com.jclark.xsl.dom.SunXMLProcessorImpl;
 37 
 38 import org.w3c.dom.Document;
 39 import org.w3c.dom.Element;
 40 import org.w3c.dom.NamedNodeMap;
 41 import org.w3c.dom.Attr;
 42 
 43 import com.fujitsu.xml.omquery.DomQueryMgr;
 44 
 45 class DomQueryTest
 46 {
 47    final String ME = "DomQueryTester";
 48    boolean testAgentNavigation = true;  // only to be true with Agent.xml or AgentBig.xml
 49 
 50    public DomQueryTest(String argv[])
 51    {
 52       Object tmp = System.getProperty("testAgentNavigation");
 53       if (tmp != null) {
 54          testAgentNavigation = (Boolean.valueOf((String)tmp)).booleanValue();
 55       }
 56       if (argv.length < 2) {
 57          System.out.println("Usage:\n\n   java DomQueryTest <XML-file> <Query-String>\n\nExample:\n   java DomQueryTest Agent.xml xmlBlaster/key/AGENT[@id=\\\"192.168.124.10\\\"]\n");
 58          System.exit(1);
 59       }
 60 
 61       boolean dumpIt = false;
 62       if (argv.length == 2) dumpIt = true;
 63 
 64       try
 65       {
 66          Document doc;
 67          DomQueryMgr query_mgr;
 68          Enumeration iter;
 69          int num_nodes;
 70 
 71          // Query: xmlBlaster/key/AGENT[@id=\"192.168.124.10\"]  xmlBlaster/key/AGENT/DRIVER[@id=\"FileProof\"]  xmlBlaster/key[@oid=\"2\"]
 72          // Time 1: For 7 <key> blocks on 266 MHz AMD Linux, JDK 1.2
 73          // Time 2: For 600 <key> blocks on 266 MHz AMD Linux, JDK 1.2
 74 
 75          StopWatch inputTime = new StopWatch();
 76          InputSource input = new InputSource(createURL(argv[0]));       // [ 20 millis ]
 77          System.out.println("Read file" + inputTime.nice());
 78 
 79          StopWatch xmlprocTime = new StopWatch();
 80          XMLProcessorImpl xmlproc = new SunXMLProcessorImpl();          // [ 75 millis ] [ 60 millis ]
 81          System.out.println("Instantiate SunXMLProcessorImpl" + xmlprocTime.nice());
 82 
 83          {
 84             StopWatch docTime = new StopWatch();
 85             doc = xmlproc.load(input);
 86             System.out.println("Create DOM - Document" + docTime.nice());     // [ 1 sec 608 millis ] [ 3 sec 69 millis ]
 87 
 88             StopWatch mgrTime = new StopWatch();
 89             query_mgr = new DomQueryMgr(doc);
 90             System.out.println("Instantiate DomQueryMgr" + mgrTime.nice());   // [ 240 millis ] [ 204 millis ]
 91 
 92             if (argv.length > 1) {
 93                StopWatch queryTime = new StopWatch();
 94                iter = query_mgr.getNodesByXPath(doc, argv[1]);          // [ 2 sec 630 millis ] [ 2 sec 516 millis ]
 95                System.out.println("Query time" + queryTime.nice());
 96 
 97                num_nodes = getNumNodes(iter, dumpIt);
 98                System.out.println(num_nodes + " nodes matches for XPath " + "\"" + argv[1] + "\"");
 99             }
100 
101             if (dumpIt) {
102                StopWatch queryTime = new StopWatch();
103                iter = query_mgr.getNodesByXPath(doc, argv[1]);
104                System.out.println("Query a second time encreases performance to" + queryTime.nice());
105                System.exit(0);
106             }
107 
108             if (argv.length > 2) {
109                StopWatch queryTime2 = new StopWatch();
110                iter = query_mgr.getNodesByXPath(doc, argv[2]);          // [ 2 millis ] [ 2 millis ]
111                System.out.println("Query time" + queryTime2.nice());
112 
113                num_nodes = getNumNodes(iter, dumpIt);
114                System.out.println(num_nodes + " nodes matches for XPath " + "\"" + argv[2] + "\"");
115             }
116 
117             if (argv.length > 3) {
118                StopWatch queryTime2 = new StopWatch();
119                iter = query_mgr.getNodesByXPath(doc, argv[3]);          // [ 1 millis ] [ 1 millis ]
120                System.out.println("Query time" + queryTime2.nice());
121 
122                num_nodes = getNumNodes(iter, dumpIt);
123                System.out.println(num_nodes + " nodes matches for XPath " + "\"" + argv[3] + "\"");
124             }
125          }
126 
127          {
128             StopWatch docTime = new StopWatch();
129             doc = xmlproc.load(input);
130             System.out.println("Create DOM - Document" + docTime.nice());     // [ 28 millis ] [ 1 sec 487 millis ]
131 
132             StopWatch mgrTime = new StopWatch();
133             query_mgr = new DomQueryMgr(doc);
134             System.out.println("Instantiate DomQueryMgr" + mgrTime.nice());   // [ 1 millis ] [ 1 millis ]
135 
136             if (argv.length > 1) {
137                StopWatch queryTime = new StopWatch();
138                iter = query_mgr.getNodesByXPath(doc, argv[1]);          // [ 1 millis ] [ 1 millis ]
139                System.out.println("Query time" + queryTime.nice());
140 
141                num_nodes = getNumNodes(iter, dumpIt);
142                System.out.println(num_nodes + " nodes matches for XPath " + "\"" + argv[1] + "\"");
143             }
144          }
145       }
146       catch (IOException e)
147       {
148          System.err.println(e.getMessage());
149          e.printStackTrace();
150       }
151       catch (SAXException e)
152       {
153          System.err.println(e.getMessage());
154          e.printStackTrace();
155       }
156       catch (XSLException e)
157       {
158          System.err.println(e.getMessage());
159          e.printStackTrace();
160       }
161    }
162 
163    private int getNumNodes(Enumeration nodeIter, boolean dumpIt) throws XSLException
164    {
165       int n = 0;
166 
167       while (nodeIter.hasMoreElements())
168       {
169          n++;
170          Object obj = nodeIter.nextElement();
171          if (dumpIt) {
172             if (obj instanceof String) {
173                String val = (String)obj;
174                System.out.println("TagValue="+val);
175             }
176             /* Does not compile with JDK 1.3, need 1.4:
177             else if (obj instanceof org.apache.crimson.tree.AttributeNode) {
178                org.apache.crimson.tree.AttributeNode attr = (org.apache.crimson.tree.AttributeNode)obj;
179                System.out.println("AttibuteValue="+attr.getValue());
180             }
181             */
182             else {
183                System.out.println(obj.toString());
184             }
185 
186             if (testAgentNavigation) {
187                try {
188                   Element node = (Element)obj;
189                   System.out.println("Found key oid=\"" + getKeyOID(node) + "\"\n");
190                } catch (Exception e) {
191                   System.out.println("ERROR: Found no xmlBlaster key oid. " + e.toString());
192                }
193             }
194          }
195 
196          /*
197          System.out.println("Processing nodeName=" + node.getNodeName() + ", " +
198                               "localName=" + node.getLocalName() + ", " +
199                               "tagName=" + node.getTagName() + ", " +
200                               "" + node.toString()
201                               );
202             */
203       }
204 
205       return n;
206    }
207 
208    private String getKeyOID(org.w3c.dom.Node node) throws Exception
209    {
210       if (node == null)
211          throw new Exception("no parent node found");
212 
213       String nodeName = node.getNodeName();
214 
215       if (nodeName.equals("xmlBlaster"))       // ERROR: the root node, must be specialy handled
216          throw new Exception("xmlBlaster node not allowed");
217 
218       if (!nodeName.equals("key")) {
219          return getKeyOID(node.getParentNode());  // w3c: getParentNode() sun: getParentImpl()
220       }
221 
222       /* !!! Element
223       org.w3c.dom.Attr keyOIDAttr = node.getAttributeNode("oid");
224       if (keyOIDAttr != null)
225          return keyOIDAttr.getValue();
226       */
227 
228       // w3c conforming code:
229       NamedNodeMap attributes = node.getAttributes();
230       if (attributes != null && attributes.getLength() > 0) {
231          int attributeCount = attributes.getLength();
232          for (int i = 0; i < attributeCount; i++) {
233             Attr attribute = (Attr)attributes.item(i);
234             if (attribute.getNodeName().equals("oid")) {
235                String val = attribute.getNodeValue();
236                return val;
237             }
238          }
239       }
240 
241       throw new Exception("Internal getKeyOID() error");
242    }
243 
244    private String createURL(String path)
245    {
246       File f = new File(path);
247       String uri = f.getAbsolutePath();
248 
249       char sep = System.getProperty("file.separator").charAt(0);
250       uri = uri.replace(sep, '/');
251       if (uri.charAt(0) != '/')
252       uri = '/' + uri;
253 
254       uri = "file://" + uri;
255 
256       return uri;
257    }
258 
259    public static void main(String argv[])
260    {
261       new DomQueryTest(argv);
262    }
263 }


syntax highlighted by Code2HTML, v. 0.9.1