KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > trax > SAX2StAXStreamWriter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://jaxp.dev.java.net/CDDLv1.0.html.
9  * See the License for the specific language governing
10  * permissions and limitations under the License.
11  *
12  * When distributing Covered Code, include this CDDL
13  * HEADER in each file and include the License file at
14  * https://jaxp.dev.java.net/CDDLv1.0.html
15  * If applicable add the following below this CDDL HEADER
16  * with the fields enclosed by brackets "[]" replaced with
17  * your own identifying information: Portions Copyright
18  * [year] [name of copyright owner]
19  */

20
21 /*
22  * $Id: SAX2StAXStreamWriter.java,v 1.7 2006/04/12 08:52:20 sunithareddy Exp $
23  * @(#)SAX2StAXStreamWriter.java 1.10 06/06/21
24  *
25  * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.org.apache.xalan.internal.xsltc.trax;
29
30 import java.util.Iterator JavaDoc;
31
32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.stream.XMLStreamWriter;
34 import javax.xml.stream.XMLEventWriter;
35
36 import org.xml.sax.Attributes JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38 import org.xml.sax.ext.Locator2 JavaDoc;
39
40 /**
41  * @author Sunitha Reddy
42  */

43
44 public class SAX2StAXStreamWriter extends SAX2StAXBaseWriter {
45
46
47     private XMLStreamWriter writer;
48         
49         private boolean needToCallStartDocument = false;
50         
51     public SAX2StAXStreamWriter() {
52
53     }
54
55     public SAX2StAXStreamWriter(XMLStreamWriter writer) {
56
57         this.writer = writer;
58
59     }
60
61
62     public XMLStreamWriter getStreamWriter() {
63
64         return writer;
65
66     }
67
68
69     public void setStreamWriter(XMLStreamWriter writer) {
70
71         this.writer = writer;
72
73     }
74
75     public void startDocument() throws SAXException JavaDoc {
76
77         super.startDocument();
78                 // Encoding and version info will be available only after startElement
79
// is called for first time. So, defer START_DOCUMENT event of StAX till
80
// that point of time.
81
needToCallStartDocument = true;
82         }
83
84     public void endDocument() throws SAXException JavaDoc {
85
86         try {
87
88             writer.writeEndDocument();
89
90         } catch (XMLStreamException e) {
91
92             throw new SAXException JavaDoc(e);
93
94         }
95
96         super.endDocument();
97
98     }
99
100     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
101             Attributes JavaDoc attributes) throws SAXException JavaDoc {
102                 
103                 if (needToCallStartDocument) {
104                     try {
105                         if (docLocator == null)
106                             writer.writeStartDocument();
107                         else {
108                             try{
109                                 writer.writeStartDocument(((Locator2 JavaDoc)docLocator).getXMLVersion());
110                             }catch(ClassCastException JavaDoc e){
111                                 writer.writeStartDocument();
112                             }
113                         }
114
115                     } catch (XMLStreamException e) {
116
117                             throw new SAXException JavaDoc(e);
118
119                     }
120                     needToCallStartDocument = false;
121                 }
122                 
123         try {
124
125             String JavaDoc[] qname = {null, null};
126             parseQName(qName, qname);
127                         //Do not call writeStartElement with prefix and namespaceURI, as it writes out
128
//namespace declaration.
129
//writer.writeStartElement(qname[0], qname[1], uri);
130
writer.writeStartElement(qName);
131                        
132
133             // No need to write namespaces, as they are written as part of attributes.
134
/*if (namespaces != null) {
135
136                             final int nDecls = namespaces.size();
137                             for (int i = 0; i < nDecls; i++) {
138                                 final String prefix = (String) namespaces.elementAt(i);
139                                 if (prefix.length() == 0) {
140                                     writer.setDefaultNamespace((String)namespaces.elementAt(++i));
141                                 } else {
142                                     writer.setPrefix(prefix, (String) namespaces.elementAt(++i));
143                                 }
144                                 
145                                 writer.writeNamespace(prefix, (String)namespaces.elementAt(i));
146                             }
147
148                                                  
149             }*/

150
151             // write attributes
152
for (int i = 0, s = attributes.getLength(); i < s; i++) {
153
154                 parseQName(attributes.getQName(i), qname);
155
156                 String JavaDoc attrPrefix = qname[0];
157                 String JavaDoc attrLocal = qname[1];
158
159                 String JavaDoc attrQName = attributes.getQName(i);
160                 String JavaDoc attrValue = attributes.getValue(i);
161                 String JavaDoc attrURI = attributes.getURI(i);
162
163                 if ("xmlns".equals(attrPrefix) || "xmlns".equals(attrQName)) {
164
165                     // namespace declaration disguised as an attribute.
166
// write it as an namespace
167

168                                         if (attrLocal.length() == 0) {
169
170                                             writer.setDefaultNamespace(attrValue);
171
172                                         } else {
173
174                                             writer.setPrefix(attrLocal, attrValue);
175
176                                         }
177
178                                         writer.writeNamespace(attrLocal, attrValue);
179
180                 } else if (attrPrefix.length() > 0) {
181
182                     writer.writeAttribute(attrPrefix, attrURI, attrLocal,
183                             attrValue);
184
185                 } else {
186                                         writer.writeAttribute(attrQName, attrValue);
187                                 }
188
189             }
190
191         } catch (XMLStreamException e) {
192                         throw new SAXException JavaDoc(e);
193
194         } finally {
195
196             super.startElement(uri, localName, qName, attributes);
197
198         }
199
200     }
201         
202     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
203             throws SAXException JavaDoc {
204
205         try {
206
207             writer.writeEndElement();
208
209         } catch (XMLStreamException e) {
210
211             throw new SAXException JavaDoc(e);
212
213         } finally {
214
215             super.endElement(uri, localName, qName);
216
217         }
218
219     }
220
221     public void comment(char[] ch, int start, int length) throws SAXException JavaDoc {
222
223         super.comment(ch, start, length);
224         try {
225
226             writer.writeComment(new String JavaDoc(ch, start, length));
227
228         } catch (XMLStreamException e) {
229
230             throw new SAXException JavaDoc(e);
231
232         }
233
234     }
235
236     public void characters(char[] ch, int start, int length)
237             throws SAXException JavaDoc {
238
239         super.characters(ch, start, length);
240         try {
241
242             if (!isCDATA) {
243
244                 writer.writeCharacters(ch, start, length);
245
246             }
247
248         } catch (XMLStreamException e) {
249
250             throw new SAXException JavaDoc(e);
251
252         }
253
254     }
255
256     public void endCDATA() throws SAXException JavaDoc {
257
258         try {
259
260             writer.writeCData(CDATABuffer.toString());
261
262         } catch (XMLStreamException e) {
263
264             throw new SAXException JavaDoc(e);
265
266         }
267
268         super.endCDATA();
269
270     }
271
272     public void ignorableWhitespace(char[] ch, int start, int length)
273             throws SAXException JavaDoc {
274
275         super.ignorableWhitespace(ch, start, length);
276         try {
277
278             writer.writeCharacters(ch, start, length);
279
280         } catch (XMLStreamException e) {
281
282             throw new SAXException JavaDoc(e);
283
284         }
285
286     }
287
288     public void processingInstruction(String JavaDoc target, String JavaDoc data)
289             throws SAXException JavaDoc {
290
291         super.processingInstruction(target, data);
292         try {
293
294             writer.writeProcessingInstruction(target, data);
295
296         } catch (XMLStreamException e) {
297
298             throw new SAXException JavaDoc(e);
299
300         }
301
302     }
303
304 }
305
Popular Tags