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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/stream/buffer/stax/StreamWriterBufferCreator.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,272 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.stream.buffer.stax;
    1.30 +
    1.31 +import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer;
    1.32 +import com.sun.xml.internal.org.jvnet.staxex.Base64Data;
    1.33 +import com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx;
    1.34 +import com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx;
    1.35 +
    1.36 +import javax.activation.DataHandler;
    1.37 +import javax.xml.namespace.NamespaceContext;
    1.38 +import javax.xml.stream.XMLStreamException;
    1.39 +import javax.xml.stream.XMLStreamWriter;
    1.40 +import java.io.OutputStream;
    1.41 +
    1.42 +/**
    1.43 + * {@link XMLStreamWriter} that fills {@link MutableXMLStreamBuffer}.
    1.44 + * <p>
    1.45 + * TODO: need to retain all attributes/namespaces and then store all namespaces
    1.46 + * before the attributes. Currently it is necessary for the caller to ensure
    1.47 + * all namespaces are written before attributes and the caller must not intermix
    1.48 + * calls to the writeNamespace and writeAttribute methods.
    1.49 + *
    1.50 + */
    1.51 +public class StreamWriterBufferCreator extends StreamBufferCreator implements XMLStreamWriterEx {
    1.52 +    private final NamespaceContexHelper namespaceContext = new NamespaceContexHelper();
    1.53 +
    1.54 +    /**
    1.55 +     * Nesting depth of the element.
    1.56 +     * This field is ultimately used to keep track of the # of trees we created in
    1.57 +     * the buffer.
    1.58 +     */
    1.59 +    private int depth=0;
    1.60 +
    1.61 +    public StreamWriterBufferCreator() {
    1.62 +        setXMLStreamBuffer(new MutableXMLStreamBuffer());
    1.63 +    }
    1.64 +
    1.65 +    public StreamWriterBufferCreator(MutableXMLStreamBuffer buffer) {
    1.66 +        setXMLStreamBuffer(buffer);
    1.67 +    }
    1.68 +
    1.69 +    // XMLStreamWriter
    1.70 +
    1.71 +    public Object getProperty(String str) throws IllegalArgumentException {
    1.72 +        return null; //return  null for all the property names instead of
    1.73 +                    //throwing unsupported operation exception.
    1.74 +    }
    1.75 +
    1.76 +    public void close() throws XMLStreamException {
    1.77 +    }
    1.78 +
    1.79 +    public void flush() throws XMLStreamException {
    1.80 +    }
    1.81 +
    1.82 +    public NamespaceContextEx getNamespaceContext() {
    1.83 +        return namespaceContext;
    1.84 +    }
    1.85 +
    1.86 +    public void setNamespaceContext(NamespaceContext namespaceContext) throws XMLStreamException {
    1.87 +        /*
    1.88 +         * It is really unclear from the JavaDoc how to implement this method.
    1.89 +         */
    1.90 +        throw new UnsupportedOperationException();
    1.91 +    }
    1.92 +
    1.93 +    public void setDefaultNamespace(String namespaceURI) throws XMLStreamException {
    1.94 +        setPrefix("", namespaceURI);
    1.95 +    }
    1.96 +
    1.97 +    public void setPrefix(String prefix, String namespaceURI) throws XMLStreamException {
    1.98 +        namespaceContext.declareNamespace(prefix, namespaceURI);
    1.99 +    }
   1.100 +
   1.101 +    public String getPrefix(String namespaceURI) throws XMLStreamException {
   1.102 +        return namespaceContext.getPrefix(namespaceURI);
   1.103 +    }
   1.104 +
   1.105 +
   1.106 +    public void writeStartDocument() throws XMLStreamException {
   1.107 +        writeStartDocument("", "");
   1.108 +    }
   1.109 +
   1.110 +    public void writeStartDocument(String version) throws XMLStreamException {
   1.111 +        writeStartDocument("", "");
   1.112 +    }
   1.113 +
   1.114 +    public void writeStartDocument(String encoding, String version) throws XMLStreamException {
   1.115 +        namespaceContext.resetContexts();
   1.116 +
   1.117 +        storeStructure(T_DOCUMENT);
   1.118 +    }
   1.119 +
   1.120 +    public void writeEndDocument() throws XMLStreamException {
   1.121 +        storeStructure(T_END);
   1.122 +    }
   1.123 +
   1.124 +    public void writeStartElement(String localName) throws XMLStreamException {
   1.125 +        namespaceContext.pushContext();
   1.126 +        depth++;
   1.127 +
   1.128 +        final String defaultNamespaceURI = namespaceContext.getNamespaceURI("");
   1.129 +
   1.130 +        if (defaultNamespaceURI == null)
   1.131 +            storeQualifiedName(T_ELEMENT_LN, null, null, localName);
   1.132 +        else
   1.133 +            storeQualifiedName(T_ELEMENT_LN, null, defaultNamespaceURI, localName);
   1.134 +    }
   1.135 +
   1.136 +    public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
   1.137 +        namespaceContext.pushContext();
   1.138 +        depth++;
   1.139 +
   1.140 +        final String prefix = namespaceContext.getPrefix(namespaceURI);
   1.141 +        if (prefix == null) {
   1.142 +            throw new XMLStreamException();
   1.143 +        }
   1.144 +
   1.145 +        namespaceContext.pushContext();
   1.146 +        storeQualifiedName(T_ELEMENT_LN, prefix, namespaceURI, localName);
   1.147 +    }
   1.148 +
   1.149 +    public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
   1.150 +        namespaceContext.pushContext();
   1.151 +        depth++;
   1.152 +
   1.153 +        storeQualifiedName(T_ELEMENT_LN, prefix, namespaceURI, localName);
   1.154 +    }
   1.155 +
   1.156 +    public void writeEmptyElement(String localName) throws XMLStreamException {
   1.157 +        writeStartElement(localName);
   1.158 +        writeEndElement();
   1.159 +    }
   1.160 +
   1.161 +    public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
   1.162 +        writeStartElement(namespaceURI, localName);
   1.163 +        writeEndElement();
   1.164 +    }
   1.165 +
   1.166 +    public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
   1.167 +        writeStartElement(prefix, localName, namespaceURI);
   1.168 +        writeEndElement();
   1.169 +    }
   1.170 +
   1.171 +    public void writeEndElement() throws XMLStreamException {
   1.172 +        namespaceContext.popContext();
   1.173 +
   1.174 +        storeStructure(T_END);
   1.175 +        if(--depth==0)
   1.176 +            increaseTreeCount();
   1.177 +    }
   1.178 +
   1.179 +    public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
   1.180 +        storeNamespaceAttribute(null, namespaceURI);
   1.181 +    }
   1.182 +
   1.183 +    public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {
   1.184 +        if ("xmlns".equals(prefix))
   1.185 +            prefix = null;
   1.186 +        storeNamespaceAttribute(prefix, namespaceURI);
   1.187 +    }
   1.188 +
   1.189 +
   1.190 +    public void writeAttribute(String localName, String value) throws XMLStreamException {
   1.191 +        storeAttribute(null, null, localName, "CDATA", value);
   1.192 +    }
   1.193 +
   1.194 +    public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {
   1.195 +        final String prefix = namespaceContext.getPrefix(namespaceURI);
   1.196 +        if (prefix == null) {
   1.197 +            // TODO
   1.198 +            throw new XMLStreamException();
   1.199 +        }
   1.200 +
   1.201 +        writeAttribute(prefix, namespaceURI, localName, value);
   1.202 +    }
   1.203 +
   1.204 +    public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
   1.205 +        storeAttribute(prefix, namespaceURI, localName, "CDATA", value);
   1.206 +    }
   1.207 +
   1.208 +    public void writeCData(String data) throws XMLStreamException {
   1.209 +        storeStructure(T_TEXT_AS_STRING);
   1.210 +        storeContentString(data);
   1.211 +    }
   1.212 +
   1.213 +    public void writeCharacters(String charData) throws XMLStreamException {
   1.214 +        storeStructure(T_TEXT_AS_STRING);
   1.215 +        storeContentString(charData);
   1.216 +    }
   1.217 +
   1.218 +    public void writeCharacters(char[] buf, int start, int len) throws XMLStreamException {
   1.219 +        storeContentCharacters(T_TEXT_AS_CHAR_ARRAY, buf, start, len);
   1.220 +    }
   1.221 +
   1.222 +    public void writeComment(String str) throws XMLStreamException {
   1.223 +        storeStructure(T_COMMENT_AS_STRING);
   1.224 +        storeContentString(str);
   1.225 +    }
   1.226 +
   1.227 +    public void writeDTD(String str) throws XMLStreamException {
   1.228 +        // not support. just ignore.
   1.229 +    }
   1.230 +
   1.231 +    public void writeEntityRef(String str) throws XMLStreamException {
   1.232 +        storeStructure(T_UNEXPANDED_ENTITY_REFERENCE);
   1.233 +        storeContentString(str);
   1.234 +    }
   1.235 +
   1.236 +    public void writeProcessingInstruction(String target) throws XMLStreamException {
   1.237 +        writeProcessingInstruction(target, "");
   1.238 +    }
   1.239 +
   1.240 +    public void writeProcessingInstruction(String target, String data) throws XMLStreamException {
   1.241 +        storeProcessingInstruction(target, data);
   1.242 +    }
   1.243 +
   1.244 +    // XMLStreamWriterEx
   1.245 +
   1.246 +    public void writePCDATA(CharSequence charSequence) throws XMLStreamException {
   1.247 +        if (charSequence instanceof Base64Data) {
   1.248 +            storeStructure(T_TEXT_AS_OBJECT);
   1.249 +            storeContentObject(((Base64Data)charSequence).clone());
   1.250 +        } else {
   1.251 +            writeCharacters(charSequence.toString());
   1.252 +        }
   1.253 +    }
   1.254 +
   1.255 +    public void writeBinary(byte[] bytes, int offset, int length, String endpointURL) throws XMLStreamException {
   1.256 +        Base64Data d = new Base64Data();
   1.257 +        byte b[] = new byte[length];
   1.258 +        System.arraycopy(bytes, offset, b, 0, length);
   1.259 +        d.set(b, length, null, true);
   1.260 +        storeStructure(T_TEXT_AS_OBJECT);
   1.261 +        storeContentObject(d);
   1.262 +    }
   1.263 +
   1.264 +    public void writeBinary(DataHandler dataHandler) throws XMLStreamException {
   1.265 +        Base64Data d = new Base64Data();
   1.266 +        d.set(dataHandler);
   1.267 +        storeStructure(T_TEXT_AS_OBJECT);
   1.268 +        storeContentObject(d);
   1.269 +    }
   1.270 +
   1.271 +    public OutputStream writeBinary(String endpointURL) throws XMLStreamException {
   1.272 +        // TODO
   1.273 +        throw new UnsupportedOperationException();
   1.274 +    }
   1.275 +}

mercurial