KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > transform > XMLToFastInfosetSAXSerializer


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package samples.transform;
41
42
43 import java.io.File JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.io.FileInputStream JavaDoc;
46 import java.io.FileOutputStream JavaDoc;
47 import java.io.BufferedInputStream JavaDoc;
48 import java.io.BufferedOutputStream JavaDoc;
49 import java.io.FileNotFoundException JavaDoc;
50
51 import javax.xml.transform.*;
52 import javax.xml.transform.dom.*;
53 import javax.xml.transform.stream.*;
54 import javax.xml.parsers.*;
55 import org.w3c.dom.*;
56 import javax.xml.transform.sax.SAXResult JavaDoc;
57
58 import javax.xml.stream.XMLStreamWriter;
59 import javax.xml.stream.XMLOutputFactory;
60 import javax.xml.stream.XMLStreamException;
61 import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer;
62
63
64
65 /** <p>Serializes an XML input stream into FI document using
66  * SAXDocumentSerializer defined in the fastinfoset.sax package.</p>
67  * The sample demonstrates how to use SAXDocumentSerializer as a SAX handler and JAXP
68  * transformer to convert an XML file into a FI document. As shown in the sample,
69  * transforming a DOM source to SAX Result involves very little coding. However, the process
70  * may not be efficient due to the construction of DOM source.
71  *
72  * In the sample, a DOMSource is constructed out of an XML file input (see method getDOMSource)
73  * and a SAXResult instantiated using an instance of SAXDocumentSerializer as the
74  * handler which takes a FI document as OutputStream (see method getSAXResult).
75  * The sample then calls transformer's tranform method to convert the XML file into the FI
76  * document.
77  */

78 public class XMLToFastInfosetSAXSerializer {
79     Transformer _transformer;
80     DocumentBuilder _docBuilder;
81     DOMSource _source = null;
82     SAXResult JavaDoc _result = null;
83     
84     /** Creates a new instance of FISerializer */
85     public XMLToFastInfosetSAXSerializer() {
86         try {
87             // get a transformer and document builder
88
_transformer = TransformerFactory.newInstance().newTransformer();
89             _docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
90         } catch (Exception JavaDoc e) {
91             e.printStackTrace();
92         }
93     }
94     
95  
96      /** Construct a DOMSource with a file.
97      *
98      * @param input the XML file input
99      */

100     void getDOMSource(File JavaDoc input) {
101         try {
102             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(input);
103             Document document = _docBuilder.parse(fis);
104             fis.close();
105             _source = new DOMSource(document);
106         }
107         catch (Exception JavaDoc e) {
108             e.printStackTrace();
109         }
110         
111     }
112     
113     /** Initialize a SAXResult and set its handers.
114      *
115      * @param output FI document output
116      */

117     void getSAXResult(File JavaDoc output) {
118         try {
119             BufferedOutputStream JavaDoc fos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(output));
120             SAXDocumentSerializer serializer = new SAXDocumentSerializer();
121             serializer.setOutputStream(fos);
122             
123             _result = new SAXResult JavaDoc();
124             _result.setHandler(serializer);
125             _result.setLexicalHandler(serializer);
126             
127         }
128         catch (Exception JavaDoc e) {
129             e.printStackTrace();
130         }
131     }
132     
133     /** Transform an XML file into a FI document.
134      *
135      * @param input an XML file input
136      * @param output the FI document output
137      */

138     public void write(File JavaDoc input, File JavaDoc output) {
139         // construct a DOMSource from the input file
140
getDOMSource(input);
141         // Initialize a SAXResult object
142
getSAXResult(output);
143         
144         if (_source != null && _result != null) {
145             try {
146                 System.out.println("Transforming "+input.getName()+ " into " + output.getName());
147                 // Transform the XML input file into a FI document
148
_transformer.transform(_source, _result);
149             } catch (Exception JavaDoc e) {
150                 e.printStackTrace();
151             }
152             System.out.println("\ndone.");
153         } else {
154             System.out.println("Source or Result could not be null.");
155         }
156     }
157     
158     /** Starts the sample
159      * @param args XML input file name and FI output document name
160      */

161     public static void main(String JavaDoc[] args) {
162         if (args.length < 1 || args.length > 4) {
163             displayUsageAndExit();
164         }
165         
166         try {
167             //XML input file, such as ./data/inv100.xml
168
File JavaDoc input = new File JavaDoc(args[0]);
169             //FastInfoset output file, such as ./data/inv100_sax.finf.
170
File JavaDoc ouput = new File JavaDoc(args[1]);
171             XMLToFastInfosetSAXSerializer docSerializer = new XMLToFastInfosetSAXSerializer();
172             docSerializer.write(input, ouput);
173         } catch (Exception JavaDoc e) {
174             e.printStackTrace();
175         }
176     }
177
178     private static void displayUsageAndExit() {
179         System.err.println("Usage: ant FISAXSerialixer or samples.sax.FISerializer XML_input_file FI_output_file");
180         System.exit(1);
181     }
182     
183 }
184
Popular Tags