KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > impl > llom > serialize > StreamWriterToContentHandlerConverter


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.om.impl.llom.serialize;
17
18 import org.apache.axis2.om.OMOutput;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.ContentHandler JavaDoc;
23 import org.xml.sax.Locator JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25
26 import javax.xml.stream.XMLStreamException;
27 import javax.xml.stream.XMLStreamWriter;
28
29 /**
30  * Class StreamWriterToContentHandlerConverter
31  */

32 public class StreamWriterToContentHandlerConverter implements ContentHandler JavaDoc {
33     /**
34      * Field log
35      */

36     private Log log = LogFactory.getLog(getClass());
37
38     /**
39      * Field writer
40      */

41     private XMLStreamWriter writer;
42
43     /**
44      * Constructor StreamWriterToContentHandlerConverter
45      *
46      * @param writer
47      */

48     public StreamWriterToContentHandlerConverter(OMOutput omOutput) {
49         this.writer = omOutput.getXmlStreamWriter();
50     }
51
52     /**
53      * Method endDocument
54      *
55      * @throws SAXException
56      */

57     public void endDocument() throws SAXException JavaDoc {
58
59         // do nothing
60
}
61
62     /**
63      * Method startDocument
64      *
65      * @throws SAXException
66      */

67     public void startDocument() throws SAXException JavaDoc {
68
69         //
70
}
71
72     /**
73      * Method characters
74      *
75      * @param ch
76      * @param start
77      * @param length
78      * @throws SAXException
79      */

80     public void characters(char ch[], int start, int length)
81             throws SAXException JavaDoc {
82         try {
83             writer.writeCharacters(ch, start, length);
84         } catch (XMLStreamException e) {
85             throw new SAXException JavaDoc(e);
86         }
87     }
88
89     /**
90      * Method ignorableWhitespace
91      *
92      * @param ch
93      * @param start
94      * @param length
95      * @throws SAXException
96      */

97     public void ignorableWhitespace(char ch[], int start, int length)
98             throws SAXException JavaDoc {
99
100         // throw new UnsupportedOperationException();
101
}
102
103     /**
104      * Method endPrefixMapping
105      *
106      * @param prefix
107      * @throws SAXException
108      */

109     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
110
111         // throw new UnsupportedOperationException();
112
}
113
114     /**
115      * Method skippedEntity
116      *
117      * @param name
118      * @throws SAXException
119      */

120     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
121
122         // throw new UnsupportedOperationException();
123
}
124
125     /**
126      * Method setDocumentLocator
127      *
128      * @param locator
129      */

130     public void setDocumentLocator(Locator JavaDoc locator) {
131
132         // throw new UnsupportedOperationException();
133
}
134
135     /**
136      * Method processingInstruction
137      *
138      * @param target
139      * @param data
140      * @throws SAXException
141      */

142     public void processingInstruction(String JavaDoc target, String JavaDoc data)
143             throws SAXException JavaDoc {
144
145         // throw new UnsupportedOperationException();
146
}
147
148     /**
149      * Method startPrefixMapping
150      *
151      * @param prefix
152      * @param uri
153      * @throws SAXException
154      */

155     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
156             throws SAXException JavaDoc {
157         try {
158             writer.writeNamespace(prefix, uri);
159             writer.setPrefix(prefix, uri);
160         } catch (XMLStreamException e) {
161             throw new SAXException JavaDoc(e);
162         }
163     }
164
165     /**
166      * Method endElement
167      *
168      * @param namespaceURI
169      * @param localName
170      * @param qName
171      * @throws SAXException
172      */

173     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
174             throws SAXException JavaDoc {
175         try {
176             writer.writeEndElement();
177         } catch (XMLStreamException e) {
178             throw new SAXException JavaDoc(e);
179         }
180     }
181
182     /**
183      * Method getPrefix
184      *
185      * @param qName
186      * @return
187      */

188     private String JavaDoc getPrefix(String JavaDoc qName) {
189         if (qName != null) {
190             return qName.substring(0, qName.indexOf(":"));
191         }
192         return null;
193     }
194
195     /**
196      * Method startElement
197      *
198      * @param namespaceURI
199      * @param localName
200      * @param qName
201      * @param atts
202      * @throws SAXException
203      */

204     public void startElement(
205             String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
206             throws SAXException JavaDoc {
207         try {
208             log.info("writing element {" + namespaceURI + '}' + localName
209                             + " directly to stream ");
210             String JavaDoc prefix = getPrefix(qName);
211
212             // it is only the prefix we want to learn from the QName! so we can get rid of the
213
// spliting QName
214
if (prefix == null) {
215                 writer.writeStartElement(namespaceURI, localName);
216             } else {
217                 writer.writeStartElement(prefix, localName, namespaceURI);
218             }
219             if (atts != null) {
220                 int attCount = atts.getLength();
221                 for (int i = 0; i < attCount; i++) {
222                     writer.writeAttribute(atts.getURI(i), localName,
223                             atts.getValue(i));
224                 }
225             }
226         } catch (XMLStreamException e) {
227             throw new SAXException JavaDoc(e);
228         }
229     }
230 }
231
Popular Tags