KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > serializer > dom > Util


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 serializer.dom;
41
42 import java.io.File JavaDoc;
43 import java.io.InputStream JavaDoc;
44 import java.io.FileInputStream JavaDoc;
45 import java.io.OutputStream 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.dom.DOMSource JavaDoc;
52 import javax.xml.transform.sax.SAXResult JavaDoc;
53 import javax.xml.transform.stream.StreamResult JavaDoc;
54
55 import javax.xml.parsers.DocumentBuilder JavaDoc;
56 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
57 import org.w3c.dom.Document JavaDoc;
58
59 import javax.xml.stream.XMLStreamWriter;
60 import javax.xml.stream.XMLOutputFactory;
61 import javax.xml.stream.XMLStreamException;
62 import com.sun.xml.fastinfoset.stax.StAXInputFactory;
63 import com.sun.xml.fastinfoset.stax.SAX2StAXWriter;
64
65 public class Util {
66     DocumentBuilder JavaDoc _docBuilder;
67             
68     public static final int STAX_SERIALIZER_RI = 1;
69     public static final int STAX_SERIALIZER_FI = 2;
70     public static final int STAX_SERIALIZER_SJSXP = 3;
71     
72     XMLOutputFactory factory = XMLOutputFactory.newInstance();
73     /** Creates a new instance of Util */
74     public Util() {
75         init();
76     }
77     
78     public Util(int outputFactory) {
79         if (outputFactory==STAX_SERIALIZER_FI) {
80             System.setProperty("javax.xml.stream.XMLOutputFactory",
81                        "com.sun.xml.fastinfoset.stax.StAXOutputFactory");
82         } else if (outputFactory==STAX_SERIALIZER_SJSXP) {
83             System.setProperty("javax.xml.stream.XMLOutputFactory",
84                        "com.sun.xml.stream.ZephyrWriterFactory");
85         }
86         init();
87     }
88     
89     void init() {
90         try {
91             factory = XMLOutputFactory.newInstance();
92             DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
93             dbf.setNamespaceAware(true);
94             _docBuilder = dbf.newDocumentBuilder();
95         } catch (Exception JavaDoc e) {
96             e.printStackTrace();
97         }
98         
99     }
100
101
102     public DOMSource JavaDoc getDOMSource(File JavaDoc input) {
103         try {
104             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(input);
105             Document JavaDoc document = _docBuilder.parse(fis);
106             fis.close();
107             return new DOMSource JavaDoc(document);
108         }
109         catch (Exception JavaDoc e) {
110             e.printStackTrace();
111         }
112         return null;
113     }
114
115     public SAXResult JavaDoc getSAXResult(OutputStream JavaDoc output) {
116         SAXResult JavaDoc _result = null;
117         try {
118             XMLStreamWriter serializer = factory.createXMLStreamWriter(output);
119             SAX2StAXWriter saxTostax = new SAX2StAXWriter(serializer);
120             
121             _result = new SAXResult JavaDoc();
122             _result.setHandler(saxTostax);
123             _result.setLexicalHandler(saxTostax);
124             
125         }
126         catch (Exception JavaDoc e) {
127             e.printStackTrace();
128         }
129         return _result;
130     }
131     public StreamResult JavaDoc getStreamResult(OutputStream JavaDoc output) {
132         StreamResult JavaDoc result = null;
133         try {
134             result = new StreamResult JavaDoc(output);
135         }
136         catch (Exception JavaDoc e) {
137             e.printStackTrace();
138         }
139         return result;
140     }
141 }
142
Popular Tags