|
1 /* |
|
2 * Copyright (c) 2005, 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.stream.buffer.stax; |
|
27 |
|
28 import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer; |
|
29 import com.sun.xml.internal.org.jvnet.staxex.Base64Data; |
|
30 import com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx; |
|
31 import com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx; |
|
32 |
|
33 import javax.activation.DataHandler; |
|
34 import javax.xml.namespace.NamespaceContext; |
|
35 import javax.xml.stream.XMLStreamException; |
|
36 import javax.xml.stream.XMLStreamWriter; |
|
37 import java.io.OutputStream; |
|
38 |
|
39 /** |
|
40 * {@link XMLStreamWriter} that fills {@link MutableXMLStreamBuffer}. |
|
41 * <p> |
|
42 * TODO: need to retain all attributes/namespaces and then store all namespaces |
|
43 * before the attributes. Currently it is necessary for the caller to ensure |
|
44 * all namespaces are written before attributes and the caller must not intermix |
|
45 * calls to the writeNamespace and writeAttribute methods. |
|
46 * |
|
47 */ |
|
48 public class StreamWriterBufferCreator extends StreamBufferCreator implements XMLStreamWriterEx { |
|
49 private final NamespaceContexHelper namespaceContext = new NamespaceContexHelper(); |
|
50 |
|
51 /** |
|
52 * Nesting depth of the element. |
|
53 * This field is ultimately used to keep track of the # of trees we created in |
|
54 * the buffer. |
|
55 */ |
|
56 private int depth=0; |
|
57 |
|
58 public StreamWriterBufferCreator() { |
|
59 setXMLStreamBuffer(new MutableXMLStreamBuffer()); |
|
60 } |
|
61 |
|
62 public StreamWriterBufferCreator(MutableXMLStreamBuffer buffer) { |
|
63 setXMLStreamBuffer(buffer); |
|
64 } |
|
65 |
|
66 // XMLStreamWriter |
|
67 |
|
68 public Object getProperty(String str) throws IllegalArgumentException { |
|
69 return null; //return null for all the property names instead of |
|
70 //throwing unsupported operation exception. |
|
71 } |
|
72 |
|
73 public void close() throws XMLStreamException { |
|
74 } |
|
75 |
|
76 public void flush() throws XMLStreamException { |
|
77 } |
|
78 |
|
79 public NamespaceContextEx getNamespaceContext() { |
|
80 return namespaceContext; |
|
81 } |
|
82 |
|
83 public void setNamespaceContext(NamespaceContext namespaceContext) throws XMLStreamException { |
|
84 /* |
|
85 * It is really unclear from the JavaDoc how to implement this method. |
|
86 */ |
|
87 throw new UnsupportedOperationException(); |
|
88 } |
|
89 |
|
90 public void setDefaultNamespace(String namespaceURI) throws XMLStreamException { |
|
91 setPrefix("", namespaceURI); |
|
92 } |
|
93 |
|
94 public void setPrefix(String prefix, String namespaceURI) throws XMLStreamException { |
|
95 namespaceContext.declareNamespace(prefix, namespaceURI); |
|
96 } |
|
97 |
|
98 public String getPrefix(String namespaceURI) throws XMLStreamException { |
|
99 return namespaceContext.getPrefix(namespaceURI); |
|
100 } |
|
101 |
|
102 |
|
103 public void writeStartDocument() throws XMLStreamException { |
|
104 writeStartDocument("", ""); |
|
105 } |
|
106 |
|
107 public void writeStartDocument(String version) throws XMLStreamException { |
|
108 writeStartDocument("", ""); |
|
109 } |
|
110 |
|
111 public void writeStartDocument(String encoding, String version) throws XMLStreamException { |
|
112 namespaceContext.resetContexts(); |
|
113 |
|
114 storeStructure(T_DOCUMENT); |
|
115 } |
|
116 |
|
117 public void writeEndDocument() throws XMLStreamException { |
|
118 storeStructure(T_END); |
|
119 } |
|
120 |
|
121 public void writeStartElement(String localName) throws XMLStreamException { |
|
122 namespaceContext.pushContext(); |
|
123 depth++; |
|
124 |
|
125 final String defaultNamespaceURI = namespaceContext.getNamespaceURI(""); |
|
126 |
|
127 if (defaultNamespaceURI == null) |
|
128 storeQualifiedName(T_ELEMENT_LN, null, null, localName); |
|
129 else |
|
130 storeQualifiedName(T_ELEMENT_LN, null, defaultNamespaceURI, localName); |
|
131 } |
|
132 |
|
133 public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException { |
|
134 namespaceContext.pushContext(); |
|
135 depth++; |
|
136 |
|
137 final String prefix = namespaceContext.getPrefix(namespaceURI); |
|
138 if (prefix == null) { |
|
139 throw new XMLStreamException(); |
|
140 } |
|
141 |
|
142 namespaceContext.pushContext(); |
|
143 storeQualifiedName(T_ELEMENT_LN, prefix, namespaceURI, localName); |
|
144 } |
|
145 |
|
146 public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { |
|
147 namespaceContext.pushContext(); |
|
148 depth++; |
|
149 |
|
150 storeQualifiedName(T_ELEMENT_LN, prefix, namespaceURI, localName); |
|
151 } |
|
152 |
|
153 public void writeEmptyElement(String localName) throws XMLStreamException { |
|
154 writeStartElement(localName); |
|
155 writeEndElement(); |
|
156 } |
|
157 |
|
158 public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException { |
|
159 writeStartElement(namespaceURI, localName); |
|
160 writeEndElement(); |
|
161 } |
|
162 |
|
163 public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { |
|
164 writeStartElement(prefix, localName, namespaceURI); |
|
165 writeEndElement(); |
|
166 } |
|
167 |
|
168 public void writeEndElement() throws XMLStreamException { |
|
169 namespaceContext.popContext(); |
|
170 |
|
171 storeStructure(T_END); |
|
172 if(--depth==0) |
|
173 increaseTreeCount(); |
|
174 } |
|
175 |
|
176 public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException { |
|
177 storeNamespaceAttribute(null, namespaceURI); |
|
178 } |
|
179 |
|
180 public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException { |
|
181 if ("xmlns".equals(prefix)) |
|
182 prefix = null; |
|
183 storeNamespaceAttribute(prefix, namespaceURI); |
|
184 } |
|
185 |
|
186 |
|
187 public void writeAttribute(String localName, String value) throws XMLStreamException { |
|
188 storeAttribute(null, null, localName, "CDATA", value); |
|
189 } |
|
190 |
|
191 public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException { |
|
192 final String prefix = namespaceContext.getPrefix(namespaceURI); |
|
193 if (prefix == null) { |
|
194 // TODO |
|
195 throw new XMLStreamException(); |
|
196 } |
|
197 |
|
198 writeAttribute(prefix, namespaceURI, localName, value); |
|
199 } |
|
200 |
|
201 public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException { |
|
202 storeAttribute(prefix, namespaceURI, localName, "CDATA", value); |
|
203 } |
|
204 |
|
205 public void writeCData(String data) throws XMLStreamException { |
|
206 storeStructure(T_TEXT_AS_STRING); |
|
207 storeContentString(data); |
|
208 } |
|
209 |
|
210 public void writeCharacters(String charData) throws XMLStreamException { |
|
211 storeStructure(T_TEXT_AS_STRING); |
|
212 storeContentString(charData); |
|
213 } |
|
214 |
|
215 public void writeCharacters(char[] buf, int start, int len) throws XMLStreamException { |
|
216 storeContentCharacters(T_TEXT_AS_CHAR_ARRAY, buf, start, len); |
|
217 } |
|
218 |
|
219 public void writeComment(String str) throws XMLStreamException { |
|
220 storeStructure(T_COMMENT_AS_STRING); |
|
221 storeContentString(str); |
|
222 } |
|
223 |
|
224 public void writeDTD(String str) throws XMLStreamException { |
|
225 // not support. just ignore. |
|
226 } |
|
227 |
|
228 public void writeEntityRef(String str) throws XMLStreamException { |
|
229 storeStructure(T_UNEXPANDED_ENTITY_REFERENCE); |
|
230 storeContentString(str); |
|
231 } |
|
232 |
|
233 public void writeProcessingInstruction(String target) throws XMLStreamException { |
|
234 writeProcessingInstruction(target, ""); |
|
235 } |
|
236 |
|
237 public void writeProcessingInstruction(String target, String data) throws XMLStreamException { |
|
238 storeProcessingInstruction(target, data); |
|
239 } |
|
240 |
|
241 // XMLStreamWriterEx |
|
242 |
|
243 public void writePCDATA(CharSequence charSequence) throws XMLStreamException { |
|
244 if (charSequence instanceof Base64Data) { |
|
245 storeStructure(T_TEXT_AS_OBJECT); |
|
246 storeContentObject(((Base64Data)charSequence).clone()); |
|
247 } else { |
|
248 writeCharacters(charSequence.toString()); |
|
249 } |
|
250 } |
|
251 |
|
252 public void writeBinary(byte[] bytes, int offset, int length, String endpointURL) throws XMLStreamException { |
|
253 Base64Data d = new Base64Data(); |
|
254 byte b[] = new byte[length]; |
|
255 System.arraycopy(bytes, offset, b, 0, length); |
|
256 d.set(b, length, null, true); |
|
257 storeStructure(T_TEXT_AS_OBJECT); |
|
258 storeContentObject(d); |
|
259 } |
|
260 |
|
261 public void writeBinary(DataHandler dataHandler) throws XMLStreamException { |
|
262 Base64Data d = new Base64Data(); |
|
263 d.set(dataHandler); |
|
264 storeStructure(T_TEXT_AS_OBJECT); |
|
265 storeContentObject(d); |
|
266 } |
|
267 |
|
268 public OutputStream writeBinary(String endpointURL) throws XMLStreamException { |
|
269 // TODO |
|
270 throw new UnsupportedOperationException(); |
|
271 } |
|
272 } |