KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > size > FastInfosetSizeExtVocDriver


1 package size;
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.net.URI JavaDoc;
43 import org.xml.sax.InputSource JavaDoc;
44 import javax.xml.parsers.*;
45 import java.util.Properties JavaDoc;
46
47 import com.sun.xml.fastinfoset.sax.*;
48 import com.sun.xml.fastinfoset.vocab.*;
49
50 import com.sun.japex.*;
51
52 public class FastInfosetSizeExtVocDriver extends JapexDriverBase {
53     
54     protected String JavaDoc _xmlFile;
55     protected byte[] _fastInfosetByteArray;
56     
57     public FastInfosetSizeExtVocDriver() {
58     }
59
60     public void initializeDriver() {
61     }
62     
63     public void prepare(TestCase testCase) {
64         _xmlFile = testCase.getParam("xmlfile");
65         if (_xmlFile == null) {
66             throw new RuntimeException JavaDoc("xmlfile not specified");
67         }
68         
69         // Load file into byte array to factor out IO
70
try {
71             // Instantiate XML parser
72
SAXParserFactory spf = SAXParserFactory.newInstance();
73             spf.setNamespaceAware(true);
74             SAXParser parser = spf.newSAXParser();
75
76             // Create stream from XML file
77
FileInputStream fis = new FileInputStream(new File(_xmlFile));
78             byte[] xmlFileByteArray = Util.streamToByteArray(fis);
79             fis.close();
80             
81             // Create external vocabulary object
82
SerializerVocabulary externalVocabulary = new SerializerVocabulary();
83             externalVocabulary.attributeValueSizeConstraint
84                 = externalVocabulary.characterContentChunkSizeContraint = 0;
85
86             // Generate vocabulary from XML instance
87
VocabularyGenerator vocabularyGenerator = new VocabularyGenerator(externalVocabulary);
88             parser.parse(new ByteArrayInputStream(xmlFileByteArray),
89                          vocabularyGenerator);
90             
91             // Serialize document into FI using external vocabulary
92
ByteArrayOutputStream baos = new ByteArrayOutputStream();
93         SAXDocumentSerializer ds = new SAXDocumentSerializer();
94             
95             SerializerVocabulary initialVocabulary = new SerializerVocabulary();
96             initialVocabulary.setExternalVocabulary(
97                 new URI JavaDoc("file:///" + _xmlFile),
98                 externalVocabulary, false);
99             ds.setVocabulary(initialVocabulary);
100             ds.setOutputStream(baos);
101             parser.parse(new ByteArrayInputStream(xmlFileByteArray), ds);
102
103             _fastInfosetByteArray = baos.toByteArray();
104         }
105         catch (Exception JavaDoc e) {
106             e.printStackTrace();
107         }
108     }
109     
110     public void warmup(TestCase testCase) {
111     }
112     
113     public void run(TestCase testCase) {
114     }
115     
116     public void finish(TestCase testCase) {
117         testCase.setDoubleParam(Constants.RESULT_VALUE,
118             _fastInfosetByteArray.length / 1024.0);
119     }
120     
121     public void terminateDriver() {
122     }
123 }
124
Popular Tags