01: // You can redistribute this software and/or modify it under the terms of
02: // the Infozone Software License version 2 published by the Infozone Group
03: // (http://www.infozone-group.org).
04: //
05: // Copyright (C) @year@ by The Infozone Group. All rights reserved.
06: //
07: // $Id: XPathTest.java,v 1.1 2002/05/10 08:59:12 per_nyfelt Exp $
08:
09: package org.infozone.tools.xml.queries.test;
10:
11: import org.w3c.dom.Document;
12: import org.w3c.dom.DocumentFragment;
13: import org.w3c.dom.NodeList;
14:
15: import org.infozone.tools.xml.queries.*;
16:
17: import javax.xml.parsers.*;
18:
19: /**
20: * @version $Revision: 1.1 $ $Date: 2002/05/10 08:59:12 $
21: * @author <a href="http://www.softwarebuero.de">SMB</a>
22: */
23: public class XPathTest {
24:
25: public static void main(String[] args) throws Exception {
26:
27: // System.getProperties().put( "org.infozone.tools.xml.queries.XPathQueryFactory",
28: // "org.infozone.tools.xml.queries.xt.XPathQueryFactoryImpl");
29:
30: XPathQueryFactory factory = XPathQueryFactory.newInstance();
31:
32: DocumentBuilderFactory builderFactory = DocumentBuilderFactory
33: .newInstance();
34: DocumentBuilder docBuilder = builderFactory
35: .newDocumentBuilder();
36: Document doc = docBuilder.parse(args[0]);
37:
38: long start = System.currentTimeMillis();
39: XPathQuery query = factory.newXPathQuery();
40: System.out.println(query.getClass().getName());
41: query.setQString("/*");
42: System.out.println("init: "
43: + (System.currentTimeMillis() - start));
44:
45: start = System.currentTimeMillis();
46: XObject result = query.execute(doc);
47: System.out.println("execute: "
48: + (System.currentTimeMillis() - start));
49:
50: printResult(result);
51:
52: result = query.execute(doc);
53: printResult(result);
54: }
55:
56: protected static void printResult(XObject result) throws Exception {
57: if (result == null) {
58: System.out.println("XPath query: result: null");
59: } else {
60: System.out.print("XPATH query: result: ");
61: // cast the query result to
62: switch (result.getType()) {
63: case XObject.CLASS_BOOLEAN:
64: System.out.println("(Boolean): " + result.bool());
65: break;
66: case XObject.CLASS_NUMBER:
67: System.out.println("(Number): " + result.num());
68: break;
69: case XObject.CLASS_STRING:
70: System.out.println("(String): " + result.str());
71: break;
72: case XObject.CLASS_RTREEFRAG:
73: System.out.println("(DocumentFragment): -");
74: break;
75: case XObject.CLASS_NODESET:
76: NodeList nodeList = result.nodeset();
77: System.out.println("(NodeList): "
78: + nodeList.getLength() + " Entries");
79:
80: for (int i = 0; i < nodeList.getLength(); i++) {
81: System.out.print(i + 1 + " Entry: ");
82: System.out.println(" value="
83: + nodeList.item(i).getNodeName());
84: }
85: break;
86: default:
87: System.out.println("(Unknown): -");
88: break;
89: }
90: }
91: }
92:
93: }
|