KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > impl > llom > OMSerializerUtil


1 package org.apache.axis2.om.impl.llom;
2
3 import org.apache.axis2.om.OMAttribute;
4 import org.apache.axis2.om.OMNamespace;
5 import org.apache.axis2.om.OMNode;
6 import org.apache.axis2.om.OMOutput;
7 import org.apache.axis2.om.impl.llom.serialize.StreamingOMSerializer;
8
9 import javax.xml.stream.XMLStreamException;
10 import javax.xml.stream.XMLStreamWriter;
11 import java.util.Iterator JavaDoc;
12
13 /*
14 * Copyright 2004,2005 The Apache Software Foundation.
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License");
17 * you may not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 *
28 *
29 */

30 public class OMSerializerUtil {
31
32     /**
33      * Method serializeEndpart
34      *
35      * @param writer
36      * @throws javax.xml.stream.XMLStreamException
37      */

38     static void serializeEndpart(OMOutput omOutput)
39             throws XMLStreamException {
40         omOutput.getXmlStreamWriter().writeEndElement();
41     }
42
43     /**
44      * Method serializeAttribute
45      *
46      * @param attr
47      * @param writer
48      * @throws XMLStreamException
49      */

50     static void serializeAttribute(OMAttribute attr, OMOutput omOutput)
51             throws XMLStreamException {
52
53         // first check whether the attribute is associated with a namespace
54
OMNamespace ns = attr.getNamespace();
55         String JavaDoc prefix = null;
56         String JavaDoc namespaceName = null;
57         XMLStreamWriter writer = omOutput.getXmlStreamWriter();
58         if (ns != null) {
59
60             // add the prefix if it's availble
61
prefix = ns.getPrefix();
62             namespaceName = ns.getName();
63             if (prefix != null) {
64                 writer.writeAttribute(prefix, namespaceName,
65                         attr.getLocalName(), attr.getValue());
66             } else {
67                 writer.writeAttribute(namespaceName, attr.getLocalName(),
68                         attr.getValue());
69             }
70         } else {
71             writer.writeAttribute(attr.getLocalName(), attr.getValue());
72         }
73     }
74
75     /**
76      * Method serializeNamespace
77      *
78      * @param namespace
79      * @param writer
80      * @throws XMLStreamException
81      */

82     static void serializeNamespace(OMNamespace namespace,OMOutput omOutput)
83             throws XMLStreamException {
84         
85         if (namespace != null) {
86             XMLStreamWriter writer = omOutput.getXmlStreamWriter();
87             String JavaDoc uri = namespace.getName();
88             String JavaDoc prefix = writer.getPrefix(uri);
89             String JavaDoc ns_prefix = namespace.getPrefix();
90             if (prefix == null) {
91                 writer.writeNamespace(ns_prefix, namespace.getName());
92                 writer.setPrefix(ns_prefix, uri);
93             }
94         }
95     }
96
97
98     /**
99      * Method serializeStartpart
100      *
101      * @param writer
102      * @throws XMLStreamException
103      */

104     static void serializeStartpart(OMElementImpl element,OMOutput omOutput)
105             throws XMLStreamException {
106         String JavaDoc nameSpaceName = null;
107         String JavaDoc writer_prefix = null;
108         String JavaDoc prefix = null;
109         XMLStreamWriter writer = omOutput.getXmlStreamWriter();
110         if (element.ns != null) {
111             nameSpaceName = element.ns.getName();
112             writer_prefix = writer.getPrefix(nameSpaceName);
113             prefix = element.ns.getPrefix();
114             if (nameSpaceName != null) {
115                 if (writer_prefix != null) {
116                     writer.writeStartElement(nameSpaceName,
117                             element.getLocalName());
118                 } else {
119                     if (prefix != null) {
120                         writer.writeStartElement(prefix, element.getLocalName(),
121                                 nameSpaceName);
122                         writer.writeNamespace(prefix, nameSpaceName);
123                         writer.setPrefix(prefix, nameSpaceName);
124                     } else {
125                         writer.writeStartElement(nameSpaceName,
126                                 element.getLocalName());
127                         writer.writeDefaultNamespace(nameSpaceName);
128                         writer.setDefaultNamespace(nameSpaceName);
129                     }
130                 }
131             } else {
132                 writer.writeStartElement(element.getLocalName());
133 // throw new OMException(
134
// "Non namespace qualified elements are not allowed");
135
}
136         } else {
137             writer.writeStartElement(element.getLocalName());
138 // throw new OMException(
139
// "Non namespace qualified elements are not allowed");
140
}
141
142         // add the elements attributes
143
serializeAttributes(element,omOutput);
144
145         // add the namespaces
146
serializeNamespaces(element, omOutput);
147     }
148
149     public static void serializeNamespaces(OMElementImpl element, OMOutput omOutput) throws XMLStreamException {
150         Iterator JavaDoc namespaces = element.getAllDeclaredNamespaces();
151         if (namespaces != null) {
152             while (namespaces.hasNext()) {
153                 serializeNamespace((OMNamespace) namespaces.next(), omOutput);
154             }
155         }
156     }
157
158     public static void serializeAttributes(OMElementImpl element, OMOutput omOutput) throws XMLStreamException {
159         if (element.getAttributes() != null) {
160             Iterator JavaDoc attributesList = element.getAttributes();
161             while (attributesList.hasNext()) {
162                 serializeAttribute((OMAttribute) attributesList.next(), omOutput);
163             }
164         }
165     }
166
167
168     /**
169      * Method serializeNormal
170      *
171      * @param writer
172      * @param cache
173      * @throws XMLStreamException
174      */

175     static void serializeNormal(OMElementImpl element,OMOutput omOutput, boolean cache)
176             throws XMLStreamException {
177         
178         if (cache){
179             element.build();
180         }
181
182         serializeStartpart(element,omOutput);
183         OMNode firstChild = element.firstChild;
184         if (firstChild != null) {
185             if (cache){
186                 firstChild.serializeWithCache(omOutput);
187             }else{
188                 firstChild.serialize(omOutput);
189             }
190         }
191         serializeEndpart(omOutput);
192     }
193
194     static void serializeByPullStream(OMElementImpl element,OMOutput omOutput) throws XMLStreamException {
195         StreamingOMSerializer streamingOMSerializer = new StreamingOMSerializer();
196         streamingOMSerializer.serialize(element.getXMLStreamReaderWithoutCaching(),
197                 omOutput);
198         return;
199     }
200 }
201
Popular Tags