KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > jaxp > XMLStreamHelper


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

17 package org.apache.servicemix.jbi.jaxp;
18
19 import javax.xml.XMLConstants JavaDoc;
20 import javax.xml.namespace.QName JavaDoc;
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  * Utility methods for working with an XMLStreamWriter. Maybe push this back into
28  * stax-utils project.
29  *
30  * Code borrowed to XFire project.
31  *
32  * @version $Revision: 1.16 $
33  */

34 public class XMLStreamHelper implements XMLStreamConstants {
35
36     /**
37      * Copies the reader to the writer. The start and end document
38      * methods must be handled on the writer manually.
39      *
40      * TODO: if the namespace on the reader has been declared previously
41      * to where we are in the stream, this probably won't work.
42      *
43      * @param reader
44      * @param writer
45      * @throws XMLStreamException
46      */

47     public static void copy( XMLStreamReader reader, XMLStreamWriter writer )
48         throws XMLStreamException
49     {
50         int read = 0; // number of elements read in
51
int event = reader.getEventType();
52         
53         while ( reader.hasNext() )
54         {
55             switch( event )
56             {
57                 case XMLStreamConstants.START_ELEMENT:
58                     read++;
59                     writeStartElement( reader, writer );
60                     break;
61                 case XMLStreamConstants.END_ELEMENT:
62                     writer.writeEndElement();
63                     read--;
64                     if ( read <= 0 )
65                         return;
66                     break;
67                 case XMLStreamConstants.CHARACTERS:
68                     writer.writeCharacters( reader.getText() );
69                     break;
70                 case XMLStreamConstants.START_DOCUMENT:
71                 case XMLStreamConstants.END_DOCUMENT:
72                 case XMLStreamConstants.ATTRIBUTE:
73                 case XMLStreamConstants.NAMESPACE:
74                     break;
75                 default:
76                     break;
77             }
78             event = reader.next();
79         }
80     }
81
82     private static void writeStartElement(XMLStreamReader reader, XMLStreamWriter writer)
83         throws XMLStreamException
84     {
85         String JavaDoc local = reader.getLocalName();
86         String JavaDoc uri = reader.getNamespaceURI();
87         String JavaDoc prefix = reader.getPrefix();
88         if (prefix == null)
89         {
90             prefix = "";
91         }
92         if (uri == null)
93         {
94             uri = "";
95         }
96         
97         String JavaDoc boundPrefix = writer.getPrefix(uri);
98         boolean writeElementNS = false;
99         if ( boundPrefix == null || !prefix.equals(boundPrefix) )
100         {
101             writeElementNS = true;
102         }
103         
104         // Write out the element name
105
if (uri != null)
106         {
107             if (prefix.length() == 0)
108             {
109                 
110                 writer.writeStartElement(local);
111                 writer.setDefaultNamespace(uri);
112                 
113             }
114             else
115             {
116                 writer.writeStartElement(prefix, local, uri);
117                 writer.setPrefix(prefix, uri);
118             }
119         }
120         else
121         {
122             writer.writeStartElement( local );
123         }
124
125         // Write out the namespaces
126
for ( int i = 0; i < reader.getNamespaceCount(); i++ )
127         {
128             String JavaDoc nsURI = reader.getNamespaceURI(i);
129             String JavaDoc nsPrefix = reader.getNamespacePrefix(i);
130             if (nsPrefix == null) nsPrefix = "";
131             
132             if ( nsPrefix.length() == 0 )
133             {
134                 writer.writeDefaultNamespace(nsURI);
135             }
136             else
137             {
138                 writer.writeNamespace(nsPrefix, nsURI);
139             }
140
141             if (nsURI.equals(uri) && nsPrefix.equals(prefix))
142             {
143                 writeElementNS = false;
144             }
145         }
146         
147         // Check if the namespace still needs to be written.
148
// We need this check because namespace writing works
149
// different on Woodstox and the RI.
150
if (writeElementNS)
151         {
152             if ( prefix == null || prefix.length() == 0 )
153             {
154                 writer.writeDefaultNamespace(uri);
155             }
156             else
157             {
158                 writer.writeNamespace(prefix, uri);
159             }
160         }
161
162         // Write out attributes
163
for ( int i = 0; i < reader.getAttributeCount(); i++ )
164         {
165             String JavaDoc ns = reader.getAttributeNamespace(i);
166             String JavaDoc nsPrefix = reader.getAttributePrefix(i);
167             if ( ns == null || ns.length() == 0 )
168             {
169                 writer.writeAttribute(
170                         reader.getAttributeLocalName(i),
171                         reader.getAttributeValue(i));
172             }
173             else if (nsPrefix == null || nsPrefix.length() == 0)
174             {
175                 writer.writeAttribute(
176                     reader.getAttributeNamespace(i),
177                     reader.getAttributeLocalName(i),
178                     reader.getAttributeValue(i));
179             }
180             else
181             {
182                 writer.writeAttribute(reader.getAttributePrefix(i),
183                                       reader.getAttributeNamespace(i),
184                                       reader.getAttributeLocalName(i),
185                                       reader.getAttributeValue(i));
186             }
187             
188             
189         }
190     }
191     
192     /**
193      * Write a start element with the specified parameters
194      * @param writer
195      * @param uri
196      * @param local
197      * @param prefix
198      * @throws XMLStreamException
199      */

200     public static void writeStartElement( XMLStreamWriter writer, String JavaDoc uri, String JavaDoc local, String JavaDoc prefix )
201         throws XMLStreamException
202     {
203         if (prefix == null)
204         {
205             prefix = "";
206         }
207         if (uri == null)
208         {
209             uri = "";
210         }
211         
212         String JavaDoc boundPrefix = writer.getPrefix(uri);
213         boolean writeElementNS = false;
214         if ( boundPrefix == null || !prefix.equals(boundPrefix) )
215         {
216             writeElementNS = true;
217         }
218         
219         // Write out the element name
220
if (uri != null)
221         {
222             if (prefix.length() == 0)
223             {
224                 
225                 writer.writeStartElement(local);
226                 writer.setDefaultNamespace(uri);
227                 
228             }
229             else
230             {
231                 writer.writeStartElement(prefix, local, uri);
232                 writer.setPrefix(prefix, uri);
233             }
234         }
235         else
236         {
237             writer.writeStartElement( local );
238         }
239
240         // Check if the namespace still needs to be written.
241
// We need this check because namespace writing works
242
// different on Woodstox and the RI.
243
if (writeElementNS)
244         {
245             if ( prefix.length() == 0 )
246             {
247                 writer.writeDefaultNamespace(uri);
248             }
249             else
250             {
251                 writer.writeNamespace(prefix, uri);
252             }
253         }
254     }
255
256     /**
257      * Write a start element with the given QName.
258      * However, if a namespace has already been bound to a prefix,
259      * use the existing one, else default to the prefix
260      * in the QName (if specified). Else, a prefix is generated.
261      *
262      * @param writer
263      * @param name
264      * @throws XMLStreamException
265      */

266     public static void writeStartElement(XMLStreamWriter writer, QName JavaDoc name) throws XMLStreamException {
267         String JavaDoc prefix = choosePrefix(writer, name, false);
268         writeStartElement(writer, name.getNamespaceURI(), name.getLocalPart(), prefix);
269     }
270
271     /**
272      *
273      * @param out
274      * @param name
275      * @throws XMLStreamException
276      */

277     public static void writeTextQName(XMLStreamWriter out, QName JavaDoc name) throws XMLStreamException {
278         String JavaDoc prefix = choosePrefix(out, name, true);
279         if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
280             out.writeCharacters(name.getLocalPart());
281         } else {
282             out.writeCharacters(prefix + ":" + name.getLocalPart());
283         }
284     }
285     
286     protected static String JavaDoc choosePrefix(XMLStreamWriter out, QName JavaDoc name, boolean declare) throws XMLStreamException {
287         String JavaDoc uri = name.getNamespaceURI();
288         // If no namespace
289
if (uri == null || XMLConstants.NULL_NS_URI.equals(uri)) {
290             if (!XMLConstants.NULL_NS_URI.equals(out.getNamespaceContext().getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX))) {
291                 out.setPrefix(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.NULL_NS_URI);
292             }
293             return XMLConstants.DEFAULT_NS_PREFIX;
294         // Need to write a prefix
295
} else {
296             String JavaDoc defPrefix = name.getPrefix();
297             // A prefix is specified
298
if (defPrefix != null && !XMLConstants.DEFAULT_NS_PREFIX.equals(defPrefix)) {
299                 // if the uri is bound to the specified prefix, good, else
300
if (!uri.equals(out.getNamespaceContext().getNamespaceURI(defPrefix))) {
301                     // if there is a prefix bound to the uri, use it
302
if (out.getNamespaceContext().getPrefix(uri) != null) {
303                         defPrefix = out.getNamespaceContext().getPrefix(uri);
304                     // get prefix from the writer
305
} else if (out.getPrefix(uri) != null) {
306                         defPrefix = out.getPrefix(uri);
307                     // we need to bind the prefix
308
} else if (declare) {
309                         out.setPrefix(defPrefix, uri);
310                         out.writeNamespace(defPrefix, uri);
311                     }
312                 }
313             // No prefix specified
314
} else {
315                 // if there is a prefix bound to the uri, use it
316
if (out.getNamespaceContext().getPrefix(uri) != null) {
317                     defPrefix = out.getNamespaceContext().getPrefix(uri);
318                 // get prefix from the writer
319
} else if (out.getPrefix(uri) != null) {
320                     defPrefix = out.getPrefix(uri);
321                 // we need to generate a prefix
322
} else {
323                     defPrefix = getUniquePrefix(out);
324                     if (declare) {
325                         out.setPrefix(defPrefix, uri);
326                         out.writeNamespace(defPrefix, uri);
327                     }
328                 }
329             }
330             return defPrefix;
331         }
332     }
333     
334     protected static String JavaDoc getUniquePrefix(XMLStreamWriter writer) {
335         int n = 1;
336         while (true) {
337             String JavaDoc nsPrefix = "ns" + n;
338             if (writer.getNamespaceContext().getNamespaceURI(nsPrefix) == null) {
339                 return nsPrefix;
340             }
341             n++;
342         }
343     }
344
345 }
346
Popular Tags