src/share/jaxws_classes/com/sun/xml/internal/stream/buffer/stax/StreamWriterBufferCreator.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 637
9c07ef4934dd
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.stream.buffer.stax;
aoqi@0 27
aoqi@0 28 import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer;
aoqi@0 29 import com.sun.xml.internal.org.jvnet.staxex.Base64Data;
aoqi@0 30 import com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx;
aoqi@0 31 import com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx;
aoqi@0 32
aoqi@0 33 import javax.activation.DataHandler;
aoqi@0 34 import javax.xml.namespace.NamespaceContext;
aoqi@0 35 import javax.xml.stream.XMLStreamException;
aoqi@0 36 import javax.xml.stream.XMLStreamWriter;
aoqi@0 37 import java.io.OutputStream;
aoqi@0 38
aoqi@0 39 /**
aoqi@0 40 * {@link XMLStreamWriter} that fills {@link MutableXMLStreamBuffer}.
aoqi@0 41 * <p>
aoqi@0 42 * TODO: need to retain all attributes/namespaces and then store all namespaces
aoqi@0 43 * before the attributes. Currently it is necessary for the caller to ensure
aoqi@0 44 * all namespaces are written before attributes and the caller must not intermix
aoqi@0 45 * calls to the writeNamespace and writeAttribute methods.
aoqi@0 46 *
aoqi@0 47 */
aoqi@0 48 public class StreamWriterBufferCreator extends StreamBufferCreator implements XMLStreamWriterEx {
aoqi@0 49 private final NamespaceContexHelper namespaceContext = new NamespaceContexHelper();
aoqi@0 50
aoqi@0 51 /**
aoqi@0 52 * Nesting depth of the element.
aoqi@0 53 * This field is ultimately used to keep track of the # of trees we created in
aoqi@0 54 * the buffer.
aoqi@0 55 */
aoqi@0 56 private int depth=0;
aoqi@0 57
aoqi@0 58 public StreamWriterBufferCreator() {
aoqi@0 59 setXMLStreamBuffer(new MutableXMLStreamBuffer());
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 public StreamWriterBufferCreator(MutableXMLStreamBuffer buffer) {
aoqi@0 63 setXMLStreamBuffer(buffer);
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 // XMLStreamWriter
aoqi@0 67
aoqi@0 68 public Object getProperty(String str) throws IllegalArgumentException {
aoqi@0 69 return null; //return null for all the property names instead of
aoqi@0 70 //throwing unsupported operation exception.
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 public void close() throws XMLStreamException {
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 public void flush() throws XMLStreamException {
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 public NamespaceContextEx getNamespaceContext() {
aoqi@0 80 return namespaceContext;
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 public void setNamespaceContext(NamespaceContext namespaceContext) throws XMLStreamException {
aoqi@0 84 /*
aoqi@0 85 * It is really unclear from the JavaDoc how to implement this method.
aoqi@0 86 */
aoqi@0 87 throw new UnsupportedOperationException();
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 public void setDefaultNamespace(String namespaceURI) throws XMLStreamException {
aoqi@0 91 setPrefix("", namespaceURI);
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 public void setPrefix(String prefix, String namespaceURI) throws XMLStreamException {
aoqi@0 95 namespaceContext.declareNamespace(prefix, namespaceURI);
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 public String getPrefix(String namespaceURI) throws XMLStreamException {
aoqi@0 99 return namespaceContext.getPrefix(namespaceURI);
aoqi@0 100 }
aoqi@0 101
aoqi@0 102
aoqi@0 103 public void writeStartDocument() throws XMLStreamException {
aoqi@0 104 writeStartDocument("", "");
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 public void writeStartDocument(String version) throws XMLStreamException {
aoqi@0 108 writeStartDocument("", "");
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 public void writeStartDocument(String encoding, String version) throws XMLStreamException {
aoqi@0 112 namespaceContext.resetContexts();
aoqi@0 113
aoqi@0 114 storeStructure(T_DOCUMENT);
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 public void writeEndDocument() throws XMLStreamException {
aoqi@0 118 storeStructure(T_END);
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 public void writeStartElement(String localName) throws XMLStreamException {
aoqi@0 122 namespaceContext.pushContext();
aoqi@0 123 depth++;
aoqi@0 124
aoqi@0 125 final String defaultNamespaceURI = namespaceContext.getNamespaceURI("");
aoqi@0 126
aoqi@0 127 if (defaultNamespaceURI == null)
aoqi@0 128 storeQualifiedName(T_ELEMENT_LN, null, null, localName);
aoqi@0 129 else
aoqi@0 130 storeQualifiedName(T_ELEMENT_LN, null, defaultNamespaceURI, localName);
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
aoqi@0 134 namespaceContext.pushContext();
aoqi@0 135 depth++;
aoqi@0 136
aoqi@0 137 final String prefix = namespaceContext.getPrefix(namespaceURI);
aoqi@0 138 if (prefix == null) {
aoqi@0 139 throw new XMLStreamException();
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 namespaceContext.pushContext();
aoqi@0 143 storeQualifiedName(T_ELEMENT_LN, prefix, namespaceURI, localName);
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
aoqi@0 147 namespaceContext.pushContext();
aoqi@0 148 depth++;
aoqi@0 149
aoqi@0 150 storeQualifiedName(T_ELEMENT_LN, prefix, namespaceURI, localName);
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 public void writeEmptyElement(String localName) throws XMLStreamException {
aoqi@0 154 writeStartElement(localName);
aoqi@0 155 writeEndElement();
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
aoqi@0 159 writeStartElement(namespaceURI, localName);
aoqi@0 160 writeEndElement();
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
aoqi@0 164 writeStartElement(prefix, localName, namespaceURI);
aoqi@0 165 writeEndElement();
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 public void writeEndElement() throws XMLStreamException {
aoqi@0 169 namespaceContext.popContext();
aoqi@0 170
aoqi@0 171 storeStructure(T_END);
aoqi@0 172 if(--depth==0)
aoqi@0 173 increaseTreeCount();
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
aoqi@0 177 storeNamespaceAttribute(null, namespaceURI);
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {
aoqi@0 181 if ("xmlns".equals(prefix))
aoqi@0 182 prefix = null;
aoqi@0 183 storeNamespaceAttribute(prefix, namespaceURI);
aoqi@0 184 }
aoqi@0 185
aoqi@0 186
aoqi@0 187 public void writeAttribute(String localName, String value) throws XMLStreamException {
aoqi@0 188 storeAttribute(null, null, localName, "CDATA", value);
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {
aoqi@0 192 final String prefix = namespaceContext.getPrefix(namespaceURI);
aoqi@0 193 if (prefix == null) {
aoqi@0 194 // TODO
aoqi@0 195 throw new XMLStreamException();
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 writeAttribute(prefix, namespaceURI, localName, value);
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
aoqi@0 202 storeAttribute(prefix, namespaceURI, localName, "CDATA", value);
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 public void writeCData(String data) throws XMLStreamException {
aoqi@0 206 storeStructure(T_TEXT_AS_STRING);
aoqi@0 207 storeContentString(data);
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 public void writeCharacters(String charData) throws XMLStreamException {
aoqi@0 211 storeStructure(T_TEXT_AS_STRING);
aoqi@0 212 storeContentString(charData);
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 public void writeCharacters(char[] buf, int start, int len) throws XMLStreamException {
aoqi@0 216 storeContentCharacters(T_TEXT_AS_CHAR_ARRAY, buf, start, len);
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 public void writeComment(String str) throws XMLStreamException {
aoqi@0 220 storeStructure(T_COMMENT_AS_STRING);
aoqi@0 221 storeContentString(str);
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 public void writeDTD(String str) throws XMLStreamException {
aoqi@0 225 // not support. just ignore.
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 public void writeEntityRef(String str) throws XMLStreamException {
aoqi@0 229 storeStructure(T_UNEXPANDED_ENTITY_REFERENCE);
aoqi@0 230 storeContentString(str);
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 public void writeProcessingInstruction(String target) throws XMLStreamException {
aoqi@0 234 writeProcessingInstruction(target, "");
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 public void writeProcessingInstruction(String target, String data) throws XMLStreamException {
aoqi@0 238 storeProcessingInstruction(target, data);
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 // XMLStreamWriterEx
aoqi@0 242
aoqi@0 243 public void writePCDATA(CharSequence charSequence) throws XMLStreamException {
aoqi@0 244 if (charSequence instanceof Base64Data) {
aoqi@0 245 storeStructure(T_TEXT_AS_OBJECT);
aoqi@0 246 storeContentObject(((Base64Data)charSequence).clone());
aoqi@0 247 } else {
aoqi@0 248 writeCharacters(charSequence.toString());
aoqi@0 249 }
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 public void writeBinary(byte[] bytes, int offset, int length, String endpointURL) throws XMLStreamException {
aoqi@0 253 Base64Data d = new Base64Data();
aoqi@0 254 byte b[] = new byte[length];
aoqi@0 255 System.arraycopy(bytes, offset, b, 0, length);
aoqi@0 256 d.set(b, length, null, true);
aoqi@0 257 storeStructure(T_TEXT_AS_OBJECT);
aoqi@0 258 storeContentObject(d);
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 public void writeBinary(DataHandler dataHandler) throws XMLStreamException {
aoqi@0 262 Base64Data d = new Base64Data();
aoqi@0 263 d.set(dataHandler);
aoqi@0 264 storeStructure(T_TEXT_AS_OBJECT);
aoqi@0 265 storeContentObject(d);
aoqi@0 266 }
aoqi@0 267
aoqi@0 268 public OutputStream writeBinary(String endpointURL) throws XMLStreamException {
aoqi@0 269 // TODO
aoqi@0 270 throw new UnsupportedOperationException();
aoqi@0 271 }
aoqi@0 272 }

mercurial