KickJava   Java API By Example, From Geeks To Geeks.

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


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: SAX2StAXEventWriter.java,v 1.3 2005/11/03 17:53:11 jeffsuttor Exp $
23  * @(#)SAX2StAXEventWriter.java 1.7 06/01/27
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.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37
38 import javax.xml.stream.XMLEventFactory;
39 import javax.xml.stream.XMLStreamException;
40 import javax.xml.stream.events.*;
41 import javax.xml.stream.XMLEventWriter;
42
43 import org.xml.sax.Attributes JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45 import org.xml.sax.ext.Locator2 JavaDoc;
46
47 /**
48  * @author Sunitha Reddy
49  */

50 public class SAX2StAXEventWriter extends SAX2StAXBaseWriter {
51
52   
53     private XMLEventWriter writer;
54
55
56     private XMLEventFactory eventFactory;
57
58
59     private List JavaDoc namespaceStack = new ArrayList JavaDoc();
60     
61     
62     private boolean needToCallStartDocument = false;
63    
64
65     public SAX2StAXEventWriter() {
66
67         eventFactory = XMLEventFactory.newInstance();
68
69     }
70
71  
72     public SAX2StAXEventWriter(XMLEventWriter writer) {
73
74         this.writer = writer;
75         eventFactory = XMLEventFactory.newInstance();
76
77     }
78
79     public SAX2StAXEventWriter(XMLEventWriter writer,
80             XMLEventFactory factory) {
81
82         this.writer = writer;
83         if (factory != null) {
84
85             this.eventFactory = factory;
86
87         } else {
88
89             eventFactory = XMLEventFactory.newInstance();
90
91         }
92
93     }
94
95     public XMLEventWriter getEventWriter() {
96
97         return writer;
98
99     }
100
101
102     public void setEventWriter(XMLEventWriter writer) {
103
104         this.writer = writer;
105
106     }
107
108  
109     public XMLEventFactory getEventFactory() {
110
111         return eventFactory;
112
113     }
114
115
116     public void setEventFactory(XMLEventFactory factory) {
117
118         this.eventFactory = factory;
119
120     }
121
122     public void startDocument() throws SAXException JavaDoc {
123
124         super.startDocument();
125
126         namespaceStack.clear();
127
128         eventFactory.setLocation(getCurrentLocation());
129         
130         // Encoding and version info will be available only after startElement
131
// is called for first time. So, defer START_DOCUMENT event of StAX till
132
// that point of time.
133
needToCallStartDocument = true;
134         
135     }
136
137     public void endDocument() throws SAXException JavaDoc {
138
139         eventFactory.setLocation(getCurrentLocation());
140
141         try {
142
143             writer.add(eventFactory.createEndDocument());
144
145         } catch (XMLStreamException e) {
146
147             throw new SAXException JavaDoc(e);
148
149         }
150
151         super.endDocument();
152
153         // clear the namespaces
154
namespaceStack.clear();
155
156     }
157
158     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
159             Attributes JavaDoc attributes) throws SAXException JavaDoc {
160         
161         if (needToCallStartDocument){
162             try {
163                 
164                 if (docLocator == null)
165                     writer.add(eventFactory.createStartDocument());
166                 else {
167                     try{
168                         writer.add(eventFactory.createStartDocument(((Locator2 JavaDoc)docLocator).getEncoding(),((Locator2 JavaDoc)docLocator).getXMLVersion()));
169                     }catch(ClassCastException JavaDoc e){
170                         writer.add(eventFactory.createStartDocument());
171                     }
172                 }
173                 
174             } catch (XMLStreamException e) {
175
176                 throw new SAXException JavaDoc(e);
177
178             }
179             needToCallStartDocument = false;
180         }
181         
182         // set document location
183
eventFactory.setLocation(getCurrentLocation());
184
185         // create attribute and namespace events
186
Collection JavaDoc[] events = {null, null};
187         createStartEvents(attributes, events);
188
189
190         namespaceStack.add(events[0]);
191
192         try {
193
194             String JavaDoc[] qname = {null, null};
195             parseQName(qName, qname);
196
197             writer.add(eventFactory.createStartElement(qname[0], uri,
198                     qname[1], events[1].iterator(), events[0].iterator()));
199
200         } catch (XMLStreamException e) {
201
202             throw new SAXException JavaDoc(e);
203
204         } finally {
205
206             super.startElement(uri, localName, qName, attributes);
207
208         }
209
210     }
211
212     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
213             throws SAXException JavaDoc {
214
215         super.endElement(uri, localName, qName);
216
217         eventFactory.setLocation(getCurrentLocation());
218
219         // parse name
220
String JavaDoc[] qname = {null, null};
221         parseQName(qName, qname);
222
223         // get namespaces
224
Collection JavaDoc nsList = (Collection JavaDoc) namespaceStack.remove(namespaceStack.size() - 1);
225         Iterator JavaDoc nsIter = nsList.iterator();
226
227         try {
228
229             writer.add(eventFactory.createEndElement(qname[0], uri, qname[1],
230                     nsIter));
231
232         } catch (XMLStreamException e) {
233
234             throw new SAXException JavaDoc(e);
235
236         }
237
238     }
239
240     public void comment(char[] ch, int start, int length) throws SAXException JavaDoc {
241
242         super.comment(ch, start, length);
243
244         eventFactory.setLocation(getCurrentLocation());
245         try {
246
247             writer.add(eventFactory.createComment(new String JavaDoc(ch, start,
248                     length)));
249
250         } catch (XMLStreamException e) {
251
252             throw new SAXException JavaDoc(e);
253
254         }
255
256     }
257
258     public void characters(char[] ch, int start, int length)
259             throws SAXException JavaDoc {
260
261         super.characters(ch, start, length);
262
263         try {
264
265             if (!isCDATA) {
266
267                 eventFactory.setLocation(getCurrentLocation());
268                 writer.add(eventFactory.createCharacters(new String JavaDoc(ch,
269                         start, length)));
270
271             }
272
273         } catch (XMLStreamException e) {
274
275             throw new SAXException JavaDoc(e);
276
277         }
278
279     }
280
281     public void ignorableWhitespace(char[] ch, int start, int length)
282             throws SAXException JavaDoc {
283
284         super.ignorableWhitespace(ch, start, length);
285         characters(ch, start, length);
286
287     }
288
289     public void processingInstruction(String JavaDoc target, String JavaDoc data)
290             throws SAXException JavaDoc {
291
292         super.processingInstruction(target, data);
293         try {
294
295             writer.add(eventFactory.createProcessingInstruction(target, data));
296
297         } catch (XMLStreamException e) {
298
299             throw new SAXException JavaDoc(e);
300
301         }
302
303     }
304
305     public void endCDATA() throws SAXException JavaDoc {
306
307         eventFactory.setLocation(getCurrentLocation());
308         try {
309
310             writer.add(eventFactory.createCData(CDATABuffer.toString()));
311
312         } catch (XMLStreamException e) {
313
314             throw new SAXException JavaDoc(e);
315
316         }
317
318         super.endCDATA();
319
320     }
321
322
323     protected void createStartEvents(Attributes JavaDoc attributes, Collection JavaDoc[] events) {
324
325         Map JavaDoc nsMap = null;
326         List JavaDoc attrs = null;
327
328         // create namespaces
329
if (namespaces != null) {
330
331             final int nDecls = namespaces.size();
332                 for (int i = 0; i < nDecls; i++) {
333                     final String JavaDoc prefix = (String JavaDoc) namespaces.elementAt(i);
334                     String JavaDoc uri = (String JavaDoc) namespaces.elementAt(i++);
335                 Namespace ns = createNamespace(prefix, uri);
336                 if (nsMap == null) {
337
338                     nsMap = new HashMap JavaDoc();
339
340                 }
341                 nsMap.put(prefix, ns);
342
343                 
344             }
345
346         }
347
348         // create attributes
349
String JavaDoc[] qname = {null, null};
350         for (int i = 0, s = attributes.getLength(); i < s; i++) {
351
352             parseQName(attributes.getQName(i), qname);
353
354             String JavaDoc attrPrefix = qname[0];
355             String JavaDoc attrLocal = qname[1];
356
357             String JavaDoc attrQName = attributes.getQName(i);
358             String JavaDoc attrValue = attributes.getValue(i);
359             String JavaDoc attrURI = attributes.getURI(i);
360
361             if ("xmlns".equals(attrQName) || "xmlns".equals(attrPrefix)) {
362
363                 // namespace declaration disguised as an attribute. If the
364
// namespace has already been declared, skip it, otherwise
365
// write it as an namespace
366

367                 if (!nsMap.containsKey(attrPrefix)) {
368
369                     Namespace ns = createNamespace(attrPrefix, attrValue);
370                     if (nsMap == null) {
371
372                         nsMap = new HashMap JavaDoc();
373
374                     }
375                     nsMap.put(attrPrefix, ns);
376
377                 }
378
379             } else {
380
381                 Attribute attribute;
382                 if (attrPrefix.length() > 0) {
383
384                     attribute = eventFactory.createAttribute(attrPrefix,
385                             attrURI, attrLocal, attrValue);
386
387                 } else {
388
389                     attribute = eventFactory.createAttribute(attrLocal,
390                             attrValue);
391
392                 }
393
394                 if (attrs == null) {
395
396                     attrs = new ArrayList JavaDoc();
397
398                 }
399                 attrs.add(attribute);
400
401             }
402
403         }
404
405         events[0] = (nsMap == null ? Collections.EMPTY_LIST : nsMap.values());
406         events[1] = (attrs == null ? Collections.EMPTY_LIST : attrs);
407
408     }
409
410     protected Namespace createNamespace(String JavaDoc prefix, String JavaDoc uri) {
411
412         if (prefix == null || prefix.length() == 0) {
413
414             return eventFactory.createNamespace(uri);
415
416         } else {
417
418             return eventFactory.createNamespace(prefix, uri);
419
420         }
421
422     }
423
424 }
425
Popular Tags