KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > parser > dom > FIDOMDocumentParserDriver


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
41 package parser.dom;
42
43 import com.sun.japex.JapexDriverBase;
44 import com.sun.japex.TestCase;
45 import com.sun.xml.fastinfoset.dom.DOMDocumentParser;
46 import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer;
47 import java.io.ByteArrayInputStream JavaDoc;
48 import java.io.ByteArrayOutputStream JavaDoc;
49 import java.io.File JavaDoc;
50 import java.io.FileInputStream JavaDoc;
51 import javax.xml.parsers.DocumentBuilder JavaDoc;
52 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
53 import javax.xml.parsers.SAXParser JavaDoc;
54 import javax.xml.parsers.SAXParserFactory JavaDoc;
55
56 public class FIDOMDocumentParserDriver extends JapexDriverBase {
57     
58     ByteArrayInputStream JavaDoc _inputStream;
59
60     DOMDocumentParser _fiddp;
61     DocumentBuilder JavaDoc _db;
62     
63     public void initializeDriver() {
64     }
65
66
67     public void prepare(TestCase testCase) {
68         String JavaDoc xmlFile = testCase.getParam("xmlfile");
69         if (xmlFile == null) {
70             throw new RuntimeException JavaDoc("xmlfile not specified");
71         }
72         
73         // Load file into byte array to factor out IO
74
try {
75         SAXParserFactory JavaDoc spf = SAXParserFactory.newInstance();
76             spf.setNamespaceAware(true);
77             SAXParser JavaDoc parser = spf.newSAXParser();
78
79         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
80         SAXDocumentSerializer ds = new SAXDocumentSerializer();
81             
82             ds.setOutputStream(baos);
83
84             // TODO must use URL here
85
FileInputStream JavaDoc fis = new FileInputStream JavaDoc(new File JavaDoc(xmlFile));
86             parser.parse(fis, ds);
87             fis.close();
88
89             _inputStream = new ByteArrayInputStream JavaDoc(baos.toByteArray());
90             
91             DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
92             dbf.setNamespaceAware(true);
93             _db = dbf.newDocumentBuilder();
94             
95             _fiddp = new DOMDocumentParser();
96         }
97         catch (Exception JavaDoc e) {
98         }
99     }
100
101     public void warmup(TestCase testCase) {
102         try {
103             _inputStream.reset();
104             _fiddp.parse(_db.newDocument(), _inputStream);
105         }
106         catch (Exception JavaDoc e) {
107             e.printStackTrace();
108         }
109     }
110     
111     public void run(TestCase testCase) {
112         try {
113             _inputStream.reset();
114             _fiddp.parse(_db.newDocument(), _inputStream);
115         }
116         catch (Exception JavaDoc e) {
117             e.printStackTrace();
118         }
119     }
120     
121     public void finish(TestCase testCase) {
122     }
123     
124     public void terminateDriver() {
125     }
126     
127 }
128
Popular Tags