001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: package com.sun.xml.stream.buffer.stax;
021:
022: import com.sun.xml.stream.buffer.MutableXMLStreamBuffer;
023: import org.jvnet.staxex.Base64Data;
024: import org.jvnet.staxex.NamespaceContextEx;
025: import org.jvnet.staxex.XMLStreamWriterEx;
026:
027: import javax.activation.DataHandler;
028: import javax.xml.namespace.NamespaceContext;
029: import javax.xml.stream.XMLStreamException;
030: import javax.xml.stream.XMLStreamWriter;
031: import java.io.OutputStream;
032:
033: /**
034: * {@link XMLStreamWriter} that fills {@link MutableXMLStreamBuffer}.
035: * <p>
036: * TODO: need to retain all attributes/namespaces and then store all namespaces
037: * before the attributes. Currently it is necessary for the caller to ensure
038: * all namespaces are written before attributes and the caller must not intermix
039: * calls to the writeNamespace and writeAttribute methods.
040: *
041: */
042: public class StreamWriterBufferCreator extends StreamBufferCreator
043: implements XMLStreamWriterEx {
044: private final NamespaceContexHelper namespaceContext = new NamespaceContexHelper();
045:
046: /**
047: * Nesting depth of the element.
048: * This field is ultimately used to keep track of the # of trees we created in
049: * the buffer.
050: */
051: private int depth = 0;
052:
053: public StreamWriterBufferCreator() {
054: setXMLStreamBuffer(new MutableXMLStreamBuffer());
055: }
056:
057: public StreamWriterBufferCreator(MutableXMLStreamBuffer buffer) {
058: setXMLStreamBuffer(buffer);
059: }
060:
061: // XMLStreamWriter
062:
063: public Object getProperty(String str)
064: throws IllegalArgumentException {
065: return null; //return null for all the property names instead of
066: //throwing unsupported operation exception.
067: }
068:
069: public void close() throws XMLStreamException {
070: }
071:
072: public void flush() throws XMLStreamException {
073: }
074:
075: public NamespaceContextEx getNamespaceContext() {
076: return namespaceContext;
077: }
078:
079: public void setNamespaceContext(NamespaceContext namespaceContext)
080: throws XMLStreamException {
081: /*
082: * It is really unclear from the JavaDoc how to implement this method.
083: */
084: throw new UnsupportedOperationException();
085: }
086:
087: public void setDefaultNamespace(String namespaceURI)
088: throws XMLStreamException {
089: setPrefix("", namespaceURI);
090: }
091:
092: public void setPrefix(String prefix, String namespaceURI)
093: throws XMLStreamException {
094: namespaceContext.declareNamespace(prefix, namespaceURI);
095: }
096:
097: public String getPrefix(String namespaceURI)
098: throws XMLStreamException {
099: return namespaceContext.getPrefix(namespaceURI);
100: }
101:
102: public void writeStartDocument() throws XMLStreamException {
103: writeStartDocument("", "");
104: }
105:
106: public void writeStartDocument(String version)
107: throws XMLStreamException {
108: writeStartDocument("", "");
109: }
110:
111: public void writeStartDocument(String encoding, String version)
112: throws XMLStreamException {
113: namespaceContext.resetContexts();
114:
115: storeStructure(T_DOCUMENT);
116: }
117:
118: public void writeEndDocument() throws XMLStreamException {
119: storeStructure(T_END);
120: }
121:
122: public void writeStartElement(String localName)
123: throws XMLStreamException {
124: namespaceContext.pushContext();
125: depth++;
126:
127: final String defaultNamespaceURI = namespaceContext
128: .getNamespaceURI("");
129:
130: if (defaultNamespaceURI == null)
131: storeQualifiedName(T_ELEMENT_LN, null, null, localName);
132: else
133: storeQualifiedName(T_ELEMENT_LN, null, defaultNamespaceURI,
134: localName);
135: }
136:
137: public void writeStartElement(String namespaceURI, String localName)
138: throws XMLStreamException {
139: namespaceContext.pushContext();
140: depth++;
141:
142: final String prefix = namespaceContext.getPrefix(namespaceURI);
143: if (prefix == null) {
144: throw new XMLStreamException();
145: }
146:
147: namespaceContext.pushContext();
148: storeQualifiedName(T_ELEMENT_LN, prefix, namespaceURI,
149: localName);
150: }
151:
152: public void writeStartElement(String prefix, String localName,
153: String namespaceURI) throws XMLStreamException {
154: namespaceContext.pushContext();
155: depth++;
156:
157: storeQualifiedName(T_ELEMENT_LN, prefix, namespaceURI,
158: localName);
159: }
160:
161: public void writeEmptyElement(String localName)
162: throws XMLStreamException {
163: writeStartElement(localName);
164: writeEndElement();
165: }
166:
167: public void writeEmptyElement(String namespaceURI, String localName)
168: throws XMLStreamException {
169: writeStartElement(namespaceURI, localName);
170: writeEndElement();
171: }
172:
173: public void writeEmptyElement(String prefix, String localName,
174: String namespaceURI) throws XMLStreamException {
175: writeStartElement(prefix, localName, namespaceURI);
176: writeEndElement();
177: }
178:
179: public void writeEndElement() throws XMLStreamException {
180: namespaceContext.popContext();
181:
182: storeStructure(T_END);
183: if (--depth == 0)
184: increaseTreeCount();
185: }
186:
187: public void writeDefaultNamespace(String namespaceURI)
188: throws XMLStreamException {
189: storeNamespaceAttribute(null, namespaceURI);
190: }
191:
192: public void writeNamespace(String prefix, String namespaceURI)
193: throws XMLStreamException {
194: if ("xmlns".equals(prefix))
195: prefix = null;
196: storeNamespaceAttribute(prefix, namespaceURI);
197: }
198:
199: public void writeAttribute(String localName, String value)
200: throws XMLStreamException {
201: storeAttribute(null, null, localName, "CDATA", value);
202: }
203:
204: public void writeAttribute(String namespaceURI, String localName,
205: String value) throws XMLStreamException {
206: final String prefix = namespaceContext.getPrefix(namespaceURI);
207: if (prefix == null) {
208: // TODO
209: throw new XMLStreamException();
210: }
211:
212: writeAttribute(prefix, namespaceURI, localName, value);
213: }
214:
215: public void writeAttribute(String prefix, String namespaceURI,
216: String localName, String value) throws XMLStreamException {
217: storeAttribute(prefix, namespaceURI, localName, "CDATA", value);
218: }
219:
220: public void writeCData(String data) throws XMLStreamException {
221: storeStructure(T_TEXT_AS_STRING);
222: storeContentString(data);
223: }
224:
225: public void writeCharacters(String charData)
226: throws XMLStreamException {
227: storeStructure(T_TEXT_AS_STRING);
228: storeContentString(charData);
229: }
230:
231: public void writeCharacters(char[] buf, int start, int len)
232: throws XMLStreamException {
233: storeContentCharacters(T_TEXT_AS_CHAR_ARRAY, buf, start, len);
234: }
235:
236: public void writeComment(String str) throws XMLStreamException {
237: storeStructure(T_COMMENT_AS_STRING);
238: storeContentString(str);
239: }
240:
241: public void writeDTD(String str) throws XMLStreamException {
242: // not support. just ignore.
243: }
244:
245: public void writeEntityRef(String str) throws XMLStreamException {
246: storeStructure(T_UNEXPANDED_ENTITY_REFERENCE);
247: storeContentString(str);
248: }
249:
250: public void writeProcessingInstruction(String target)
251: throws XMLStreamException {
252: writeProcessingInstruction(target, "");
253: }
254:
255: public void writeProcessingInstruction(String target, String data)
256: throws XMLStreamException {
257: storeProcessingInstruction(target, data);
258: }
259:
260: // XMLStreamWriterEx
261:
262: public void writePCDATA(CharSequence charSequence)
263: throws XMLStreamException {
264: if (charSequence instanceof Base64Data) {
265: storeStructure(T_TEXT_AS_OBJECT);
266: storeContentObject(((Base64Data) charSequence).clone());
267: } else {
268: writeCharacters(charSequence.toString());
269: }
270: }
271:
272: public void writeBinary(byte[] bytes, int offset, int length,
273: String endpointURL) throws XMLStreamException {
274: Base64Data d = new Base64Data();
275: byte b[] = new byte[length];
276: System.arraycopy(bytes, offset, b, 0, length);
277: d.set(b, length, null, true);
278: storeStructure(T_TEXT_AS_OBJECT);
279: storeContentObject(d);
280: }
281:
282: public void writeBinary(DataHandler dataHandler)
283: throws XMLStreamException {
284: Base64Data d = new Base64Data();
285: d.set(dataHandler);
286: storeStructure(T_TEXT_AS_OBJECT);
287: storeContentObject(d);
288: }
289:
290: public OutputStream writeBinary(String endpointURL)
291: throws XMLStreamException {
292: // TODO
293: throw new UnsupportedOperationException();
294: }
295: }
|