|
1 /* |
|
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.ws.streaming; |
|
27 |
|
28 import com.sun.istack.internal.Nullable; |
|
29 import com.sun.xml.internal.ws.encoding.HasEncoding; |
|
30 import com.sun.xml.internal.ws.encoding.SOAPBindingCodec; |
|
31 |
|
32 import javax.xml.namespace.QName; |
|
33 import javax.xml.stream.XMLStreamWriter; |
|
34 import javax.xml.stream.XMLStreamException; |
|
35 import java.util.Map; |
|
36 import java.io.OutputStream; |
|
37 |
|
38 /** |
|
39 * <p>XMLStreamWriterUtil provides some utility methods intended to be used |
|
40 * in conjunction with a StAX XMLStreamWriter. </p> |
|
41 * |
|
42 * @author Santiago.PericasGeertsen@sun.com |
|
43 */ |
|
44 public class XMLStreamWriterUtil { |
|
45 |
|
46 private XMLStreamWriterUtil() { |
|
47 } |
|
48 |
|
49 /** |
|
50 * Gives the underlying stream for XMLStreamWriter. It closes any start elements, and returns the stream so |
|
51 * that JAXB can write infoset directly to the stream. |
|
52 * |
|
53 * @param writer XMLStreamWriter for which stream is required |
|
54 * @return underlying OutputStream, null if writer doesn't provide a way to get it |
|
55 * @throws XMLStreamException if any of writer operations throw the exception |
|
56 */ |
|
57 public static @Nullable OutputStream getOutputStream(XMLStreamWriter writer) throws XMLStreamException { |
|
58 Object obj = null; |
|
59 |
|
60 // Hack for JDK6's SJSXP |
|
61 if (writer instanceof Map) { |
|
62 obj = ((Map) writer).get("sjsxp-outputstream"); |
|
63 } |
|
64 |
|
65 // woodstox |
|
66 if (obj == null) { |
|
67 try { |
|
68 obj = writer.getProperty("com.ctc.wstx.outputUnderlyingStream"); |
|
69 } catch(Exception ie) { |
|
70 // Catch all exceptions. SJSXP in JDK throws NPE |
|
71 // nothing to do here |
|
72 } |
|
73 } |
|
74 |
|
75 // SJSXP |
|
76 if (obj == null) { |
|
77 try { |
|
78 obj = writer.getProperty("http://java.sun.com/xml/stream/properties/outputstream"); |
|
79 } catch(Exception ie) { |
|
80 // Catch all exceptions. SJSXP in JDK throws NPE |
|
81 // nothing to do here |
|
82 } |
|
83 } |
|
84 |
|
85 |
|
86 if (obj != null) { |
|
87 writer.writeCharacters(""); // Force completion of open elems |
|
88 writer.flush(); |
|
89 return (OutputStream)obj; |
|
90 } |
|
91 return null; |
|
92 } |
|
93 |
|
94 /** |
|
95 * Gives the encoding with which XMLStreamWriter is created. |
|
96 * |
|
97 * @param writer XMLStreamWriter for which encoding is required |
|
98 * @return null if cannot be found, else the encoding |
|
99 */ |
|
100 public static @Nullable String getEncoding(XMLStreamWriter writer) { |
|
101 /* |
|
102 * TODO Add reflection logic to handle woodstox writer |
|
103 * as it implements XMLStreamWriter2#getEncoding() |
|
104 * It's not that important since woodstox writer is typically wrapped |
|
105 * in a writer with HasEncoding |
|
106 */ |
|
107 return (writer instanceof HasEncoding) |
|
108 ? ((HasEncoding)writer).getEncoding() |
|
109 : null; |
|
110 } |
|
111 |
|
112 public static String encodeQName(XMLStreamWriter writer, QName qname, |
|
113 PrefixFactory prefixFactory) |
|
114 { |
|
115 // NOTE: Here it is assumed that we do not serialize using default |
|
116 // namespace declarations and therefore that writer.getPrefix will |
|
117 // never return "" |
|
118 |
|
119 try { |
|
120 String namespaceURI = qname.getNamespaceURI(); |
|
121 String localPart = qname.getLocalPart(); |
|
122 |
|
123 if (namespaceURI == null || namespaceURI.equals("")) { |
|
124 return localPart; |
|
125 } |
|
126 else { |
|
127 String prefix = writer.getPrefix(namespaceURI); |
|
128 if (prefix == null) { |
|
129 prefix = prefixFactory.getPrefix(namespaceURI); |
|
130 writer.writeNamespace(prefix, namespaceURI); |
|
131 } |
|
132 return prefix + ":" + localPart; |
|
133 } |
|
134 } |
|
135 catch (XMLStreamException e) { |
|
136 throw new RuntimeException(e); |
|
137 } |
|
138 } |
|
139 } |