KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > parser > dom > JAXPSAXXercesDOMBuilderDriver


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 com.sun.japex.JapexDriverBase;
43 import com.sun.japex.TestCase;
44 import com.sun.org.apache.xml.internal.utils.DOMBuilder;
45 import com.sun.xml.fastinfoset.sax.Features;
46 import com.sun.xml.fastinfoset.sax.Properties;
47 import java.io.File JavaDoc;
48 import java.io.ByteArrayInputStream JavaDoc;
49 import java.io.FileInputStream JavaDoc;
50 import javax.xml.parsers.DocumentBuilder JavaDoc;
51 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
52
53 import javax.xml.parsers.SAXParserFactory JavaDoc;
54 import javax.xml.parsers.SAXParser JavaDoc;
55 import org.xml.sax.InputSource JavaDoc;
56 import org.xml.sax.XMLReader JavaDoc;
57
58
59
60 public class JAXPSAXXercesDOMBuilderDriver extends JapexDriverBase {
61     ByteArrayInputStream JavaDoc _inputStream;
62     InputSource JavaDoc _inputSource;
63     
64     SAXParser JavaDoc _parser;
65     XMLReader JavaDoc _reader;
66     DocumentBuilder JavaDoc _db;
67     
68     public void initializeDriver() {
69     }
70
71
72     public void prepare(TestCase testCase) {
73         String JavaDoc xmlFile = testCase.getParam("xmlfile");
74         if (xmlFile == null) {
75             throw new RuntimeException JavaDoc("xmlfile not specified");
76         }
77         
78         // Load file into byte array to factor out IO
79
try {
80         SAXParserFactory JavaDoc spf = SAXParserFactory.newInstance();
81             spf.setNamespaceAware(true);
82             spf.setFeature(Features.NAMESPACE_PREFIXES_FEATURE, true);
83             _parser = spf.newSAXParser();
84             _reader = _parser.getXMLReader();
85             
86             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(new File JavaDoc(xmlFile));
87             byte[] xmlFileByteArray = com.sun.japex.Util.streamToByteArray(fis);
88             _inputStream = new ByteArrayInputStream JavaDoc(xmlFileByteArray);
89             fis.close();
90             _inputSource = new InputSource JavaDoc(_inputStream);
91
92             DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
93             dbf.setNamespaceAware(true);
94             _db = dbf.newDocumentBuilder();
95         }
96         catch (Exception JavaDoc e) {
97         }
98     }
99
100     public void warmup(TestCase testCase) {
101         try {
102             _inputStream.reset();
103             DOMBuilder db = new DOMBuilder(_db.newDocument());
104             _reader.setContentHandler(db);
105             _reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, db);
106             _reader.parse(_inputSource);
107         }
108         catch (Exception JavaDoc e) {
109             e.printStackTrace();
110         }
111     }
112     
113     public void run(TestCase testCase) {
114         try {
115             _inputStream.reset();
116             DOMBuilder db = new DOMBuilder(_db.newDocument());
117             _reader.setContentHandler(db);
118             _reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, db);
119             _reader.parse(_inputSource);
120         }
121         catch (Exception JavaDoc e) {
122             e.printStackTrace();
123         }
124     }
125     
126     public void finish(TestCase testCase) {
127     }
128     
129     public void terminateDriver() {
130     }
131 }
132
Popular Tags