KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > parser > XPP3Driver


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

40
41 import java.io.*;
42 import java.util.Properties JavaDoc;
43
44 import com.sun.japex.*;
45
46 import org.xmlpull.v1.XmlPullParser;
47 import org.xmlpull.v1.XmlPullParserException;
48 import org.xmlpull.v1.XmlPullParserFactory;
49
50 public class XPP3Driver extends JapexDriverBase {
51     
52     String JavaDoc _xmlFile;
53     byte[] _xmlFileByteArray;
54     ByteArrayInputStream _inputStream;
55     XmlPullParser _parser;
56     
57     public XPP3Driver() {
58     }
59
60     public void initializeDriver() {
61         try {
62             XmlPullParserFactory factory = XmlPullParserFactory.newInstance(
63                System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
64             factory.setNamespaceAware(true);
65             _parser = factory.newPullParser();
66         }
67         catch (Exception JavaDoc e) {
68             e.printStackTrace();
69         }
70     }
71     
72     public void prepare(TestCase testCase) {
73         _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             FileInputStream fis = new FileInputStream(new File(_xmlFile));
81             _xmlFileByteArray = com.sun.japex.Util.streamToByteArray(fis);
82             _inputStream = new ByteArrayInputStream(_xmlFileByteArray);
83             fis.close();
84         }
85         catch (IOException e) {
86             e.printStackTrace();
87         }
88     }
89     
90     public void warmup(TestCase testCase) {
91         try {
92             _inputStream.reset();
93             _parser.setInput(_inputStream, null); // unknown encoding
94
while (_parser.next() != XmlPullParser.END_DOCUMENT);
95         }
96         catch (Exception JavaDoc e) {
97             e.printStackTrace();
98         }
99     }
100     
101     public void run(TestCase testCase) {
102         try {
103             _inputStream.reset();
104             _parser.setInput(_inputStream, null); // unknown encoding
105
while (_parser.next() != XmlPullParser.END_DOCUMENT);
106         }
107         catch (Exception JavaDoc e) {
108             e.printStackTrace();
109         }
110     }
111     
112     public void terminateDriver() {
113     }
114 }
115
Popular Tags