KickJava   Java API By Example, From Geeks To Geeks.

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


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.axis2.om.OMSerializer;
20
21 import javax.xml.stream.XMLStreamConstants;
22 import javax.xml.stream.XMLStreamException;
23 import javax.xml.stream.XMLStreamReader;
24 import javax.xml.stream.XMLStreamWriter;
25
26 /**
27  * Class StreamingOMSerializer
28  */

29 public class StreamingOMSerializer implements XMLStreamConstants, OMSerializer {
30
31     /*
32      * The behavior of the serializer is such that it returns when it encounters the
33      * starting element for the second time. The depth variable tracks the depth of the
34      * serilizer and tells it when to return.
35      * Note that it is assumed that this serilization starts on an Element
36      */

37
38     /**
39      * Field depth
40      */

41     private int depth = 0;
42
43     /**
44      * Method serializeWithCache
45      *
46      * @param obj
47      * @param omOutput
48      * @throws XMLStreamException
49      */

50     public void serialize(Object JavaDoc obj, OMOutput omOutput)
51             throws XMLStreamException {
52         if (!(obj instanceof XMLStreamReader)) {
53             throw new UnsupportedOperationException JavaDoc(
54                     "Unsupported input object. Must be of the the type XMLStreamReader");
55         }
56         XMLStreamReader node = (XMLStreamReader) obj;
57         serializeNode(node, omOutput);
58     }
59
60     /**
61      * Method serializeNode
62      *
63      * @param reader
64      * @param omOutput
65      * @throws XMLStreamException
66      */

67     protected void serializeNode(XMLStreamReader reader, OMOutput omOutput)
68             throws XMLStreamException {
69         //TODO We get the StAXWriter at this point and uses it hereafter assuming that this is the only entry point to this class.
70
// If there can be other classes calling methodes of this we might need to change methode signatures to OMOutputer
71
XMLStreamWriter writer = omOutput.getXmlStreamWriter();
72         while (reader.hasNext()) {
73             int event = reader.next();
74             if (event == START_ELEMENT) {
75                 serializeElement(reader, writer);
76                 depth++;
77             } else if (event == ATTRIBUTE) {
78                 serializeAttributes(reader, writer);
79             } else if (event == CHARACTERS) {
80                 serializeText(reader, writer);
81             } else if (event == COMMENT) {
82                 serializeComment(reader, writer);
83             } else if (event == CDATA) {
84                 serializeCData(reader, writer);
85             } else if (event == END_ELEMENT) {
86                 serializeEndElement(writer);
87                 depth--;
88             } else if (event == END_DOCUMENT) {
89                 try {
90                     serializeEndElement(writer);
91                 } catch (Exception JavaDoc e) {
92                     //TODO: log exceptions
93
}
94             }
95             if (depth == 0) {
96                 break;
97             }
98         }
99     }
100
101     /**
102      * @param reader
103      * @param writer
104      * @throws XMLStreamException
105      */

106     protected void serializeElement(
107             XMLStreamReader reader, XMLStreamWriter writer)
108             throws XMLStreamException {
109         String JavaDoc prefix = reader.getPrefix();
110         String JavaDoc nameSpaceName = reader.getNamespaceURI();
111         String JavaDoc writer_prefix = writer.getPrefix(nameSpaceName);
112         if (nameSpaceName != null) {
113             if (writer_prefix != null) {
114                 writer.writeStartElement(nameSpaceName, reader.getLocalName());
115             } else {
116                 if (prefix != null) {
117                     writer.writeStartElement(prefix, reader.getLocalName(),
118                             nameSpaceName);
119                     writer.writeNamespace(prefix, nameSpaceName);
120                     writer.setPrefix(prefix, nameSpaceName);
121                 } else {
122                     writer.writeStartElement(nameSpaceName,
123                             reader.getLocalName());
124                     writer.writeDefaultNamespace(nameSpaceName);
125                     writer.setDefaultNamespace(nameSpaceName);
126                 }
127             }
128         } else {
129 // throw new OMException(
130
// "Non namespace qualified elements are not allowed");
131
}
132
133         // add attributes
134
serializeAttributes(reader, writer);
135
136         // add the namespaces
137
int count = reader.getNamespaceCount();
138         for (int i = 0; i < count; i++) {
139             serializeNamespace(reader.getNamespacePrefix(i),
140                     reader.getNamespaceURI(i), writer);
141         }
142     }
143
144     /**
145      * Method serializeEndElement
146      *
147      * @param writer
148      * @throws XMLStreamException
149      */

150     protected void serializeEndElement(XMLStreamWriter writer)
151             throws XMLStreamException {
152         writer.writeEndElement();
153     }
154
155     /**
156      * @param reader
157      * @param writer
158      * @throws XMLStreamException
159      */

160     protected void serializeText(XMLStreamReader reader, XMLStreamWriter writer)
161             throws XMLStreamException {
162         writer.writeCharacters(reader.getText());
163     }
164
165     /**
166      * Method serializeCData
167      *
168      * @param reader
169      * @param writer
170      * @throws XMLStreamException
171      */

172     protected void serializeCData(
173             XMLStreamReader reader, XMLStreamWriter writer)
174             throws XMLStreamException {
175         writer.writeCData(reader.getText());
176     }
177
178     /**
179      * Method serializeComment
180      *
181      * @param reader
182      * @param writer
183      * @throws XMLStreamException
184      */

185     protected void serializeComment(
186             XMLStreamReader reader, XMLStreamWriter writer)
187             throws XMLStreamException {
188         writer.writeComment(reader.getText());
189     }
190
191     /**
192      * @param reader
193      * @param writer
194      * @throws XMLStreamException
195      */

196     protected void serializeAttributes(
197             XMLStreamReader reader, XMLStreamWriter writer)
198             throws XMLStreamException {
199         int count = reader.getAttributeCount();
200         String JavaDoc prefix = null;
201         String JavaDoc namespaceName = null;
202         for (int i = 0; i < count; i++) {
203             prefix = reader.getAttributePrefix(i);
204             namespaceName = reader.getAttributeNamespace(i);
205             if ((prefix != null) && !namespaceName.equals("")) {
206                 writer.writeAttribute(prefix, namespaceName,
207                         reader.getAttributeLocalName(i),
208                         reader.getAttributeValue(i));
209             } else {
210                 writer.writeAttribute(reader.getAttributeLocalName(i),
211                         reader.getAttributeValue(i));
212             }
213         }
214     }
215
216     /**
217      * Method serializeNamespace
218      *
219      * @param prefix
220      * @param URI
221      * @param writer
222      * @throws XMLStreamException
223      */

224     private void serializeNamespace(
225             String JavaDoc prefix, String JavaDoc URI, XMLStreamWriter writer)
226             throws XMLStreamException {
227         String JavaDoc prefix1 = writer.getPrefix(URI);
228         if (prefix1 == null) {
229             writer.writeNamespace(prefix, URI);
230             writer.setPrefix(prefix, URI);
231         }
232     }
233 }
234
Popular Tags