KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > parser > dom > XercesDOMDeferredParserDriver


1 /*
2  * Japex ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * This Software is distributed under the following terms:
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, is permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistribution in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based names,
19  * nor the names of contributors may be used to endorse or promote products
20  * derived from this Software without specific prior written permission.
21  *
22  * The Software is provided "AS IS," without a warranty of any kind. ALL
23  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
24  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25  * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS
26  * SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE
27  * AS A RESULT OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE
28  * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE
29  * LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
30  * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED
31  * AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
32  * INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGES.
34  *
35  * You acknowledge that the Software is not designed, licensed or intended
36  * for use in the design, construction, operation or maintenance of any
37  * nuclear facility.
38  */

39
40 package parser.dom;
41
42 import java.io.File JavaDoc;
43 import java.io.ByteArrayInputStream JavaDoc;
44 import java.io.FileInputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import com.sun.japex.*;
47 import org.w3c.dom.Document JavaDoc;
48 import com.sun.org.apache.xerces.internal.parsers.DOMParser;
49 import org.w3c.dom.NamedNodeMap JavaDoc;
50 import org.w3c.dom.Node JavaDoc;
51 import org.w3c.dom.NodeList JavaDoc;
52 import org.xml.sax.InputSource JavaDoc;
53
54 public class XercesDOMDeferredParserDriver extends JapexDriverBase {
55
56     ByteArrayInputStream JavaDoc _inputStream;
57     
58     DOMParser _parser;
59        
60     public void initializeDriver() {
61         try {
62             _parser = new DOMParser();
63             _parser.setFeature("http://xml.org/sax/features/namespaces", true);
64             _parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
65         } catch (Exception JavaDoc e) {
66             e.printStackTrace();
67             try { Thread.currentThread().sleep(5000); } catch (Exception JavaDoc ee) {}
68         }
69     }
70     
71     public void prepare(TestCase testCase) {
72         String JavaDoc xmlFile = testCase.getParam("xmlfile");
73         if (xmlFile == null) {
74             throw new RuntimeException JavaDoc("xmlfile not specified");
75         }
76         // Load file into byte array to factor out IO
77
try {
78             // TODO must use URL here
79
FileInputStream JavaDoc fis = new FileInputStream JavaDoc(new File JavaDoc(xmlFile));
80             byte[] xmlFileByteArray = com.sun.japex.Util.streamToByteArray(fis);
81             _inputStream = new ByteArrayInputStream JavaDoc(xmlFileByteArray);
82             fis.close();
83         }
84         catch (IOException JavaDoc e) {
85             e.printStackTrace();
86         }
87
88     }
89     
90     public void warmup(TestCase testCase) {
91         try {
92             _inputStream.reset();
93             _parser.parse(new InputSource JavaDoc(_inputStream));
94             Document JavaDoc d = _parser.getDocument();
95             // traverse(d);
96
}
97         catch (Exception JavaDoc e) {
98             e.printStackTrace();
99         }
100     }
101     
102     public void run(TestCase testCase) {
103         try {
104             _inputStream.reset();
105             _parser.parse(new InputSource JavaDoc(_inputStream));
106             Document JavaDoc d = _parser.getDocument();
107             // traverse(d);
108
}
109         catch (Exception JavaDoc e) {
110             e.printStackTrace();
111         }
112     }
113     
114     public void finish(TestCase testCase) {
115     }
116     
117     public void terminateDriver() {
118     }
119     
120     public final void traverse(Node JavaDoc n) {
121         Object JavaDoc o = n.getNodeValue();
122         
123         if (n.hasAttributes()) {
124             NamedNodeMap JavaDoc nnm = n.getAttributes();
125             for (int i = 0; i < nnm.getLength(); i++) {
126                 Node JavaDoc an = nnm.item(i);
127                 o = an.getNodeValue();
128             }
129         }
130         
131         if (n.hasChildNodes()) {
132             NodeList JavaDoc nl = n.getChildNodes();
133             for (int i = 0; i < nl.getLength(); i++) {
134                 traverse(nl.item(i));
135             }
136         }
137     }
138 }
139
Popular Tags