KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > stax > SAX2StAXWriter


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 com.sun.xml.fastinfoset.stax;
41
42 import com.sun.xml.fastinfoset.QualifiedName;
43 import java.util.ArrayList JavaDoc;
44 import javax.xml.stream.XMLStreamException;
45 import javax.xml.stream.XMLStreamWriter;
46 import org.xml.sax.Attributes JavaDoc;
47 import org.xml.sax.Locator JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
49 import org.xml.sax.ext.LexicalHandler JavaDoc;
50 import org.xml.sax.helpers.DefaultHandler JavaDoc;
51
52 public class SAX2StAXWriter extends DefaultHandler JavaDoc implements LexicalHandler JavaDoc {
53         
54     /**
55      * XML stream writer where events are pushed.
56      */

57     XMLStreamWriter _writer;
58     
59     /**
60      * List of namespace decl for upcoming element.
61      */

62     ArrayList JavaDoc _namespaces = new ArrayList JavaDoc();
63     
64     public SAX2StAXWriter(XMLStreamWriter writer) {
65         _writer = writer;
66     }
67     
68     public XMLStreamWriter getWriter() {
69         return _writer;
70     }
71     
72     public void startDocument() throws SAXException JavaDoc {
73         try {
74             _writer.writeStartDocument();
75         }
76         catch (XMLStreamException e) {
77             throw new SAXException JavaDoc(e);
78         }
79     }
80     
81     public void endDocument() throws SAXException JavaDoc {
82         try {
83             _writer.writeEndDocument();
84             _writer.flush();
85         }
86         catch (XMLStreamException e) {
87             throw new SAXException JavaDoc(e);
88         }
89     }
90     
91     public void characters(char[] ch, int start, int length)
92         throws SAXException JavaDoc
93     {
94         try {
95             _writer.writeCharacters(ch, start, length);
96         }
97         catch (XMLStreamException e) {
98             throw new SAXException JavaDoc(e);
99         }
100     }
101     
102     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
103         String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc
104     {
105         try {
106             int k = qName.indexOf(':');
107             String JavaDoc prefix = (k > 0) ? qName.substring(0, k) : "";
108             _writer.writeStartElement(prefix, localName, namespaceURI);
109             
110             int length = _namespaces.size();
111             for (int i = 0; i < length; i++) {
112                 QualifiedName nsh = (QualifiedName) _namespaces.get(i);
113                 _writer.writeNamespace(nsh.prefix, nsh.namespaceName);
114             }
115             _namespaces.clear();
116             
117             length = atts.getLength();
118             for (int i = 0; i < length; i++) {
119                 _writer.writeAttribute(atts.getURI(i),
120                                        atts.getLocalName(i),
121                                        atts.getValue(i));
122             }
123         }
124         catch (XMLStreamException e) {
125             throw new SAXException JavaDoc(e);
126         }
127     }
128     
129     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
130         String JavaDoc qName) throws SAXException JavaDoc
131     {
132         try {
133             _writer.writeEndElement();
134         }
135         catch (XMLStreamException e) {
136             e.printStackTrace();
137             throw new SAXException JavaDoc(e);
138         }
139     }
140     
141     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
142         throws SAXException JavaDoc
143     {
144         try {
145             _writer.setPrefix(prefix, uri);
146             _namespaces.add(new QualifiedName(prefix, uri));
147         }
148         catch (XMLStreamException e) {
149             throw new SAXException JavaDoc(e);
150         }
151     }
152     
153     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
154     }
155     
156     public void ignorableWhitespace(char[] ch, int start, int length)
157         throws SAXException JavaDoc
158     {
159         characters(ch, start, length);
160     }
161     
162     public void processingInstruction(String JavaDoc target, String JavaDoc data)
163         throws SAXException JavaDoc
164     {
165         try {
166             _writer.writeProcessingInstruction(target, data);
167         }
168         catch (XMLStreamException e) {
169             throw new SAXException JavaDoc(e);
170         }
171     }
172     
173     public void setDocumentLocator(Locator JavaDoc locator) {
174     }
175     
176     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
177     }
178     
179     public void comment(char[] ch, int start, int length)
180         throws SAXException JavaDoc
181     {
182         try {
183             _writer.writeComment(new String JavaDoc(ch, start, length));
184         }
185         catch (XMLStreamException e) {
186             throw new SAXException JavaDoc(e);
187         }
188     }
189     
190     public void endCDATA() throws SAXException JavaDoc {
191     }
192     
193     public void endDTD() throws SAXException JavaDoc {
194     }
195     
196     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
197     }
198     
199     public void startCDATA() throws SAXException JavaDoc {
200     }
201     
202     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
203     }
204     
205     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
206     }
207     
208 }
209
210
Popular Tags