jaxws/src/share/classes/com/sun/xml/internal/bind/v2/runtime/output/UTF8XmlOutput.java
author ramap
Mon, 09 Mar 2009 15:32:10 -0700
changeset 2675 91fc59916eb4
parent 8 474761f14bca
child 2679 1d6ff0427a13
permissions -rw-r--r--
6536193: Fix the flaw in UTF8XmlOutput Reviewed-by: tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
474761f14bca Initial load
duke
parents:
diff changeset
     1
/*
474761f14bca Initial load
duke
parents:
diff changeset
     2
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
474761f14bca Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
474761f14bca Initial load
duke
parents:
diff changeset
     4
 *
474761f14bca Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
474761f14bca Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
474761f14bca Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
474761f14bca Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
474761f14bca Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
474761f14bca Initial load
duke
parents:
diff changeset
    10
 *
474761f14bca Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
474761f14bca Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
474761f14bca Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
474761f14bca Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
474761f14bca Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
474761f14bca Initial load
duke
parents:
diff changeset
    16
 *
474761f14bca Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
474761f14bca Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
474761f14bca Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
474761f14bca Initial load
duke
parents:
diff changeset
    20
 *
474761f14bca Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
474761f14bca Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
474761f14bca Initial load
duke
parents:
diff changeset
    23
 * have any questions.
474761f14bca Initial load
duke
parents:
diff changeset
    24
 */
474761f14bca Initial load
duke
parents:
diff changeset
    25
package com.sun.xml.internal.bind.v2.runtime.output;
474761f14bca Initial load
duke
parents:
diff changeset
    26
474761f14bca Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
474761f14bca Initial load
duke
parents:
diff changeset
    28
import java.io.OutputStream;
474761f14bca Initial load
duke
parents:
diff changeset
    29
474761f14bca Initial load
duke
parents:
diff changeset
    30
import javax.xml.stream.XMLStreamException;
474761f14bca Initial load
duke
parents:
diff changeset
    31
474761f14bca Initial load
duke
parents:
diff changeset
    32
import com.sun.xml.internal.bind.DatatypeConverterImpl;
474761f14bca Initial load
duke
parents:
diff changeset
    33
import com.sun.xml.internal.bind.v2.runtime.Name;
474761f14bca Initial load
duke
parents:
diff changeset
    34
import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
2675
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
    35
import com.sun.xml.internal.bind.v2.runtime.MarshallerImpl;
8
474761f14bca Initial load
duke
parents:
diff changeset
    36
474761f14bca Initial load
duke
parents:
diff changeset
    37
import org.xml.sax.SAXException;
474761f14bca Initial load
duke
parents:
diff changeset
    38
474761f14bca Initial load
duke
parents:
diff changeset
    39
/**
474761f14bca Initial load
duke
parents:
diff changeset
    40
 * {@link XmlOutput} implementation specialized for UTF-8.
474761f14bca Initial load
duke
parents:
diff changeset
    41
 *
474761f14bca Initial load
duke
parents:
diff changeset
    42
 * @author Kohsuke Kawaguchi
474761f14bca Initial load
duke
parents:
diff changeset
    43
 * @author Paul Sandoz
474761f14bca Initial load
duke
parents:
diff changeset
    44
 */
474761f14bca Initial load
duke
parents:
diff changeset
    45
public class UTF8XmlOutput extends XmlOutputAbstractImpl {
474761f14bca Initial load
duke
parents:
diff changeset
    46
    protected final OutputStream out;
474761f14bca Initial load
duke
parents:
diff changeset
    47
474761f14bca Initial load
duke
parents:
diff changeset
    48
    /** prefixes encoded. */
474761f14bca Initial load
duke
parents:
diff changeset
    49
    private Encoded[] prefixes = new Encoded[8];
474761f14bca Initial load
duke
parents:
diff changeset
    50
474761f14bca Initial load
duke
parents:
diff changeset
    51
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    52
     * Of the {@link #prefixes}, number of filled entries.
474761f14bca Initial load
duke
parents:
diff changeset
    53
     * This is almost the same as {@link NamespaceContextImpl#count()},
474761f14bca Initial load
duke
parents:
diff changeset
    54
     * except that it allows us to handle contextual in-scope namespace bindings correctly.
474761f14bca Initial load
duke
parents:
diff changeset
    55
     */
474761f14bca Initial load
duke
parents:
diff changeset
    56
    private int prefixCount;
474761f14bca Initial load
duke
parents:
diff changeset
    57
474761f14bca Initial load
duke
parents:
diff changeset
    58
    /** local names encoded in UTF-8. All entries are pre-filled. */
474761f14bca Initial load
duke
parents:
diff changeset
    59
    private final Encoded[] localNames;
474761f14bca Initial load
duke
parents:
diff changeset
    60
474761f14bca Initial load
duke
parents:
diff changeset
    61
    /** Temporary buffer used to encode text. */
474761f14bca Initial load
duke
parents:
diff changeset
    62
    /*
474761f14bca Initial load
duke
parents:
diff changeset
    63
     * TODO
474761f14bca Initial load
duke
parents:
diff changeset
    64
     * The textBuffer could write directly to the _octetBuffer
474761f14bca Initial load
duke
parents:
diff changeset
    65
     * when encoding a string if Encoder is modified.
474761f14bca Initial load
duke
parents:
diff changeset
    66
     * This will avoid an additional memory copy.
474761f14bca Initial load
duke
parents:
diff changeset
    67
     */
474761f14bca Initial load
duke
parents:
diff changeset
    68
    private final Encoded textBuffer = new Encoded();
474761f14bca Initial load
duke
parents:
diff changeset
    69
474761f14bca Initial load
duke
parents:
diff changeset
    70
    /** Buffer of octets for writing. */
474761f14bca Initial load
duke
parents:
diff changeset
    71
    // TODO: Obtain buffer size from property on the JAXB context
474761f14bca Initial load
duke
parents:
diff changeset
    72
    protected final byte[] octetBuffer = new byte[1024];
474761f14bca Initial load
duke
parents:
diff changeset
    73
474761f14bca Initial load
duke
parents:
diff changeset
    74
    /** Index in buffer to write to. */
474761f14bca Initial load
duke
parents:
diff changeset
    75
    protected int octetBufferIndex;
474761f14bca Initial load
duke
parents:
diff changeset
    76
474761f14bca Initial load
duke
parents:
diff changeset
    77
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    78
     * Set to true to indicate that we need to write '>'
474761f14bca Initial load
duke
parents:
diff changeset
    79
     * to close a start tag. Deferring the write of this char
474761f14bca Initial load
duke
parents:
diff changeset
    80
     * allows us to write "/>" for empty elements.
474761f14bca Initial load
duke
parents:
diff changeset
    81
     */
474761f14bca Initial load
duke
parents:
diff changeset
    82
    protected boolean closeStartTagPending = false;
474761f14bca Initial load
duke
parents:
diff changeset
    83
474761f14bca Initial load
duke
parents:
diff changeset
    84
    /**
2675
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
    85
     * @see MarshallerImpl#header
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
    86
     */
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
    87
    private String header;
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
    88
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
    89
    /**
8
474761f14bca Initial load
duke
parents:
diff changeset
    90
     *
474761f14bca Initial load
duke
parents:
diff changeset
    91
     * @param localNames
474761f14bca Initial load
duke
parents:
diff changeset
    92
     *      local names encoded in UTF-8.
474761f14bca Initial load
duke
parents:
diff changeset
    93
     */
474761f14bca Initial load
duke
parents:
diff changeset
    94
    public UTF8XmlOutput(OutputStream out, Encoded[] localNames) {
474761f14bca Initial load
duke
parents:
diff changeset
    95
        this.out = out;
474761f14bca Initial load
duke
parents:
diff changeset
    96
        this.localNames = localNames;
474761f14bca Initial load
duke
parents:
diff changeset
    97
        for( int i=0; i<prefixes.length; i++ )
474761f14bca Initial load
duke
parents:
diff changeset
    98
            prefixes[i] = new Encoded();
474761f14bca Initial load
duke
parents:
diff changeset
    99
    }
474761f14bca Initial load
duke
parents:
diff changeset
   100
2675
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   101
    public void setHeader(String header) {
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   102
        this.header = header;
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   103
    }
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   104
8
474761f14bca Initial load
duke
parents:
diff changeset
   105
    @Override
474761f14bca Initial load
duke
parents:
diff changeset
   106
    public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException {
474761f14bca Initial load
duke
parents:
diff changeset
   107
        super.startDocument(serializer, fragment,nsUriIndex2prefixIndex,nsContext);
474761f14bca Initial load
duke
parents:
diff changeset
   108
474761f14bca Initial load
duke
parents:
diff changeset
   109
        octetBufferIndex = 0;
474761f14bca Initial load
duke
parents:
diff changeset
   110
        if(!fragment) {
474761f14bca Initial load
duke
parents:
diff changeset
   111
            write(XML_DECL);
474761f14bca Initial load
duke
parents:
diff changeset
   112
        }
2675
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   113
        if(header!=null) {
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   114
            textBuffer.set(header);
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   115
            textBuffer.write(this);
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   116
        }
8
474761f14bca Initial load
duke
parents:
diff changeset
   117
    }
474761f14bca Initial load
duke
parents:
diff changeset
   118
474761f14bca Initial load
duke
parents:
diff changeset
   119
    public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
474761f14bca Initial load
duke
parents:
diff changeset
   120
        flushBuffer();
474761f14bca Initial load
duke
parents:
diff changeset
   121
        super.endDocument(fragment);
474761f14bca Initial load
duke
parents:
diff changeset
   122
    }
474761f14bca Initial load
duke
parents:
diff changeset
   123
474761f14bca Initial load
duke
parents:
diff changeset
   124
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   125
     * Writes '>' to close the start tag, if necessary.
474761f14bca Initial load
duke
parents:
diff changeset
   126
     */
474761f14bca Initial load
duke
parents:
diff changeset
   127
    protected final void closeStartTag() throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   128
        if(closeStartTagPending) {
474761f14bca Initial load
duke
parents:
diff changeset
   129
            write('>');
474761f14bca Initial load
duke
parents:
diff changeset
   130
            closeStartTagPending = false;
474761f14bca Initial load
duke
parents:
diff changeset
   131
        }
474761f14bca Initial load
duke
parents:
diff changeset
   132
    }
474761f14bca Initial load
duke
parents:
diff changeset
   133
474761f14bca Initial load
duke
parents:
diff changeset
   134
    public void beginStartTag(int prefix, String localName) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   135
        closeStartTag();
474761f14bca Initial load
duke
parents:
diff changeset
   136
        int base= pushNsDecls();
474761f14bca Initial load
duke
parents:
diff changeset
   137
        write('<');
474761f14bca Initial load
duke
parents:
diff changeset
   138
        writeName(prefix,localName);
474761f14bca Initial load
duke
parents:
diff changeset
   139
        writeNsDecls(base);
474761f14bca Initial load
duke
parents:
diff changeset
   140
    }
474761f14bca Initial load
duke
parents:
diff changeset
   141
474761f14bca Initial load
duke
parents:
diff changeset
   142
    public void beginStartTag(Name name) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   143
        closeStartTag();
474761f14bca Initial load
duke
parents:
diff changeset
   144
        int base = pushNsDecls();
474761f14bca Initial load
duke
parents:
diff changeset
   145
        write('<');
474761f14bca Initial load
duke
parents:
diff changeset
   146
        writeName(name);
474761f14bca Initial load
duke
parents:
diff changeset
   147
        writeNsDecls(base);
474761f14bca Initial load
duke
parents:
diff changeset
   148
    }
474761f14bca Initial load
duke
parents:
diff changeset
   149
474761f14bca Initial load
duke
parents:
diff changeset
   150
    private int pushNsDecls() {
474761f14bca Initial load
duke
parents:
diff changeset
   151
        int total = nsContext.count();
474761f14bca Initial load
duke
parents:
diff changeset
   152
        NamespaceContextImpl.Element ns = nsContext.getCurrent();
474761f14bca Initial load
duke
parents:
diff changeset
   153
474761f14bca Initial load
duke
parents:
diff changeset
   154
        if(total > prefixes.length) {
474761f14bca Initial load
duke
parents:
diff changeset
   155
            // reallocate
474761f14bca Initial load
duke
parents:
diff changeset
   156
            int m = Math.max(total,prefixes.length*2);
474761f14bca Initial load
duke
parents:
diff changeset
   157
            Encoded[] buf = new Encoded[m];
474761f14bca Initial load
duke
parents:
diff changeset
   158
            System.arraycopy(prefixes,0,buf,0,prefixes.length);
474761f14bca Initial load
duke
parents:
diff changeset
   159
            for( int i=prefixes.length; i<buf.length; i++ )
474761f14bca Initial load
duke
parents:
diff changeset
   160
                buf[i] = new Encoded();
474761f14bca Initial load
duke
parents:
diff changeset
   161
            prefixes = buf;
474761f14bca Initial load
duke
parents:
diff changeset
   162
        }
474761f14bca Initial load
duke
parents:
diff changeset
   163
474761f14bca Initial load
duke
parents:
diff changeset
   164
        int base = Math.min(prefixCount,ns.getBase());
474761f14bca Initial load
duke
parents:
diff changeset
   165
        int size = nsContext.count();
474761f14bca Initial load
duke
parents:
diff changeset
   166
        for( int i=base; i<size; i++ ) {
474761f14bca Initial load
duke
parents:
diff changeset
   167
            String p = nsContext.getPrefix(i);
474761f14bca Initial load
duke
parents:
diff changeset
   168
474761f14bca Initial load
duke
parents:
diff changeset
   169
            Encoded e = prefixes[i];
474761f14bca Initial load
duke
parents:
diff changeset
   170
474761f14bca Initial load
duke
parents:
diff changeset
   171
            if(p.length()==0) {
474761f14bca Initial load
duke
parents:
diff changeset
   172
                e.buf = EMPTY_BYTE_ARRAY;
474761f14bca Initial load
duke
parents:
diff changeset
   173
                e.len = 0;
474761f14bca Initial load
duke
parents:
diff changeset
   174
            } else {
474761f14bca Initial load
duke
parents:
diff changeset
   175
                e.set(p);
474761f14bca Initial load
duke
parents:
diff changeset
   176
                e.append(':');
474761f14bca Initial load
duke
parents:
diff changeset
   177
            }
474761f14bca Initial load
duke
parents:
diff changeset
   178
        }
474761f14bca Initial load
duke
parents:
diff changeset
   179
        prefixCount = size;
474761f14bca Initial load
duke
parents:
diff changeset
   180
        return base;
474761f14bca Initial load
duke
parents:
diff changeset
   181
    }
474761f14bca Initial load
duke
parents:
diff changeset
   182
474761f14bca Initial load
duke
parents:
diff changeset
   183
    protected void writeNsDecls(int base) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   184
        NamespaceContextImpl.Element ns = nsContext.getCurrent();
474761f14bca Initial load
duke
parents:
diff changeset
   185
        int size = nsContext.count();
474761f14bca Initial load
duke
parents:
diff changeset
   186
474761f14bca Initial load
duke
parents:
diff changeset
   187
        for( int i=ns.getBase(); i<size; i++ )
474761f14bca Initial load
duke
parents:
diff changeset
   188
            writeNsDecl(i);
474761f14bca Initial load
duke
parents:
diff changeset
   189
    }
474761f14bca Initial load
duke
parents:
diff changeset
   190
474761f14bca Initial load
duke
parents:
diff changeset
   191
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   192
     * Writes a single namespace declaration for the specified prefix.
474761f14bca Initial load
duke
parents:
diff changeset
   193
     */
474761f14bca Initial load
duke
parents:
diff changeset
   194
    protected final void writeNsDecl(int prefixIndex) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   195
        String p = nsContext.getPrefix(prefixIndex);
474761f14bca Initial load
duke
parents:
diff changeset
   196
474761f14bca Initial load
duke
parents:
diff changeset
   197
        if(p.length()==0) {
474761f14bca Initial load
duke
parents:
diff changeset
   198
            if(nsContext.getCurrent().isRootElement()
474761f14bca Initial load
duke
parents:
diff changeset
   199
            && nsContext.getNamespaceURI(prefixIndex).length()==0)
474761f14bca Initial load
duke
parents:
diff changeset
   200
                return;     // no point in declaring xmlns="" on the root element
474761f14bca Initial load
duke
parents:
diff changeset
   201
            write(XMLNS_EQUALS);
474761f14bca Initial load
duke
parents:
diff changeset
   202
        } else {
474761f14bca Initial load
duke
parents:
diff changeset
   203
            Encoded e = prefixes[prefixIndex];
474761f14bca Initial load
duke
parents:
diff changeset
   204
            write(XMLNS_COLON);
474761f14bca Initial load
duke
parents:
diff changeset
   205
            write(e.buf,0,e.len-1); // skip the trailing ':'
474761f14bca Initial load
duke
parents:
diff changeset
   206
            write(EQUALS);
474761f14bca Initial load
duke
parents:
diff changeset
   207
        }
474761f14bca Initial load
duke
parents:
diff changeset
   208
        doText(nsContext.getNamespaceURI(prefixIndex),true);
474761f14bca Initial load
duke
parents:
diff changeset
   209
        write('\"');
474761f14bca Initial load
duke
parents:
diff changeset
   210
    }
474761f14bca Initial load
duke
parents:
diff changeset
   211
474761f14bca Initial load
duke
parents:
diff changeset
   212
    private void writePrefix(int prefix) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   213
        prefixes[prefix].write(this);
474761f14bca Initial load
duke
parents:
diff changeset
   214
    }
474761f14bca Initial load
duke
parents:
diff changeset
   215
474761f14bca Initial load
duke
parents:
diff changeset
   216
    private void writeName(Name name) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   217
        writePrefix(nsUriIndex2prefixIndex[name.nsUriIndex]);
474761f14bca Initial load
duke
parents:
diff changeset
   218
        localNames[name.localNameIndex].write(this);
474761f14bca Initial load
duke
parents:
diff changeset
   219
    }
474761f14bca Initial load
duke
parents:
diff changeset
   220
474761f14bca Initial load
duke
parents:
diff changeset
   221
    private void writeName(int prefix, String localName) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   222
        writePrefix(prefix);
474761f14bca Initial load
duke
parents:
diff changeset
   223
        textBuffer.set(localName);
474761f14bca Initial load
duke
parents:
diff changeset
   224
        textBuffer.write(this);
474761f14bca Initial load
duke
parents:
diff changeset
   225
    }
474761f14bca Initial load
duke
parents:
diff changeset
   226
474761f14bca Initial load
duke
parents:
diff changeset
   227
    @Override
474761f14bca Initial load
duke
parents:
diff changeset
   228
    public void attribute(Name name, String value) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   229
        write(' ');
474761f14bca Initial load
duke
parents:
diff changeset
   230
        if(name.nsUriIndex==-1) {
474761f14bca Initial load
duke
parents:
diff changeset
   231
            localNames[name.localNameIndex].write(this);
474761f14bca Initial load
duke
parents:
diff changeset
   232
        } else
474761f14bca Initial load
duke
parents:
diff changeset
   233
            writeName(name);
474761f14bca Initial load
duke
parents:
diff changeset
   234
        write(EQUALS);
474761f14bca Initial load
duke
parents:
diff changeset
   235
        doText(value,true);
474761f14bca Initial load
duke
parents:
diff changeset
   236
        write('\"');
474761f14bca Initial load
duke
parents:
diff changeset
   237
    }
474761f14bca Initial load
duke
parents:
diff changeset
   238
474761f14bca Initial load
duke
parents:
diff changeset
   239
    public void attribute(int prefix, String localName, String value) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   240
        write(' ');
474761f14bca Initial load
duke
parents:
diff changeset
   241
        if(prefix==-1) {
474761f14bca Initial load
duke
parents:
diff changeset
   242
            textBuffer.set(localName);
474761f14bca Initial load
duke
parents:
diff changeset
   243
            textBuffer.write(this);
474761f14bca Initial load
duke
parents:
diff changeset
   244
        } else
474761f14bca Initial load
duke
parents:
diff changeset
   245
            writeName(prefix,localName);
474761f14bca Initial load
duke
parents:
diff changeset
   246
        write(EQUALS);
474761f14bca Initial load
duke
parents:
diff changeset
   247
        doText(value,true);
474761f14bca Initial load
duke
parents:
diff changeset
   248
        write('\"');
474761f14bca Initial load
duke
parents:
diff changeset
   249
    }
474761f14bca Initial load
duke
parents:
diff changeset
   250
474761f14bca Initial load
duke
parents:
diff changeset
   251
    public void endStartTag() throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   252
        closeStartTagPending = true;
474761f14bca Initial load
duke
parents:
diff changeset
   253
    }
474761f14bca Initial load
duke
parents:
diff changeset
   254
474761f14bca Initial load
duke
parents:
diff changeset
   255
    @Override
474761f14bca Initial load
duke
parents:
diff changeset
   256
    public void endTag(Name name) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   257
        if(closeStartTagPending) {
474761f14bca Initial load
duke
parents:
diff changeset
   258
            write(EMPTY_TAG);
474761f14bca Initial load
duke
parents:
diff changeset
   259
            closeStartTagPending = false;
474761f14bca Initial load
duke
parents:
diff changeset
   260
        } else {
474761f14bca Initial load
duke
parents:
diff changeset
   261
            write(CLOSE_TAG);
474761f14bca Initial load
duke
parents:
diff changeset
   262
            writeName(name);
474761f14bca Initial load
duke
parents:
diff changeset
   263
            write('>');
474761f14bca Initial load
duke
parents:
diff changeset
   264
        }
474761f14bca Initial load
duke
parents:
diff changeset
   265
    }
474761f14bca Initial load
duke
parents:
diff changeset
   266
474761f14bca Initial load
duke
parents:
diff changeset
   267
    public void endTag(int prefix, String localName) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   268
        if(closeStartTagPending) {
474761f14bca Initial load
duke
parents:
diff changeset
   269
            write(EMPTY_TAG);
474761f14bca Initial load
duke
parents:
diff changeset
   270
            closeStartTagPending = false;
474761f14bca Initial load
duke
parents:
diff changeset
   271
        } else {
474761f14bca Initial load
duke
parents:
diff changeset
   272
            write(CLOSE_TAG);
474761f14bca Initial load
duke
parents:
diff changeset
   273
            writeName(prefix,localName);
474761f14bca Initial load
duke
parents:
diff changeset
   274
            write('>');
474761f14bca Initial load
duke
parents:
diff changeset
   275
        }
474761f14bca Initial load
duke
parents:
diff changeset
   276
    }
474761f14bca Initial load
duke
parents:
diff changeset
   277
474761f14bca Initial load
duke
parents:
diff changeset
   278
    public void text(String value, boolean needSP) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   279
        closeStartTag();
474761f14bca Initial load
duke
parents:
diff changeset
   280
        if(needSP)
474761f14bca Initial load
duke
parents:
diff changeset
   281
            write(' ');
474761f14bca Initial load
duke
parents:
diff changeset
   282
        doText(value,false);
474761f14bca Initial load
duke
parents:
diff changeset
   283
    }
474761f14bca Initial load
duke
parents:
diff changeset
   284
474761f14bca Initial load
duke
parents:
diff changeset
   285
    public void text(Pcdata value, boolean needSP) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   286
        closeStartTag();
474761f14bca Initial load
duke
parents:
diff changeset
   287
        if(needSP)
474761f14bca Initial load
duke
parents:
diff changeset
   288
            write(' ');
474761f14bca Initial load
duke
parents:
diff changeset
   289
        value.writeTo(this);
474761f14bca Initial load
duke
parents:
diff changeset
   290
    }
474761f14bca Initial load
duke
parents:
diff changeset
   291
474761f14bca Initial load
duke
parents:
diff changeset
   292
    private void doText(String value,boolean isAttribute) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   293
        textBuffer.setEscape(value,isAttribute);
474761f14bca Initial load
duke
parents:
diff changeset
   294
        textBuffer.write(this);
474761f14bca Initial load
duke
parents:
diff changeset
   295
    }
474761f14bca Initial load
duke
parents:
diff changeset
   296
474761f14bca Initial load
duke
parents:
diff changeset
   297
    public final void text(int value) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   298
        closeStartTag();
474761f14bca Initial load
duke
parents:
diff changeset
   299
        /*
474761f14bca Initial load
duke
parents:
diff changeset
   300
         * TODO
474761f14bca Initial load
duke
parents:
diff changeset
   301
         * Change to use the octet buffer directly
474761f14bca Initial load
duke
parents:
diff changeset
   302
         */
474761f14bca Initial load
duke
parents:
diff changeset
   303
474761f14bca Initial load
duke
parents:
diff changeset
   304
        // max is -2147483648 and 11 digits
474761f14bca Initial load
duke
parents:
diff changeset
   305
        boolean minus = (value<0);
474761f14bca Initial load
duke
parents:
diff changeset
   306
        textBuffer.ensureSize(11);
474761f14bca Initial load
duke
parents:
diff changeset
   307
        byte[] buf = textBuffer.buf;
474761f14bca Initial load
duke
parents:
diff changeset
   308
        int idx = 11;
474761f14bca Initial load
duke
parents:
diff changeset
   309
474761f14bca Initial load
duke
parents:
diff changeset
   310
        do {
474761f14bca Initial load
duke
parents:
diff changeset
   311
            int r = value%10;
474761f14bca Initial load
duke
parents:
diff changeset
   312
            if(r<0) r = -r;
474761f14bca Initial load
duke
parents:
diff changeset
   313
            buf[--idx] = (byte)('0'|r);    // really measn 0x30+r but 0<=r<10, so bit-OR would do.
474761f14bca Initial load
duke
parents:
diff changeset
   314
            value /= 10;
474761f14bca Initial load
duke
parents:
diff changeset
   315
        } while(value!=0);
474761f14bca Initial load
duke
parents:
diff changeset
   316
474761f14bca Initial load
duke
parents:
diff changeset
   317
        if(minus)   buf[--idx] = (byte)'-';
474761f14bca Initial load
duke
parents:
diff changeset
   318
474761f14bca Initial load
duke
parents:
diff changeset
   319
        write(buf,idx,11-idx);
474761f14bca Initial load
duke
parents:
diff changeset
   320
    }
474761f14bca Initial load
duke
parents:
diff changeset
   321
474761f14bca Initial load
duke
parents:
diff changeset
   322
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   323
     * Writes the given byte[] as base64 encoded binary to the output.
474761f14bca Initial load
duke
parents:
diff changeset
   324
     *
474761f14bca Initial load
duke
parents:
diff changeset
   325
     * <p>
474761f14bca Initial load
duke
parents:
diff changeset
   326
     * Being defined on this class allows this method to access the buffer directly,
474761f14bca Initial load
duke
parents:
diff changeset
   327
     * which translates to a better performance.
474761f14bca Initial load
duke
parents:
diff changeset
   328
     */
474761f14bca Initial load
duke
parents:
diff changeset
   329
    public void text(byte[] data, int dataLen) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   330
        closeStartTag();
474761f14bca Initial load
duke
parents:
diff changeset
   331
474761f14bca Initial load
duke
parents:
diff changeset
   332
        int start = 0;
474761f14bca Initial load
duke
parents:
diff changeset
   333
474761f14bca Initial load
duke
parents:
diff changeset
   334
        while(dataLen>0) {
474761f14bca Initial load
duke
parents:
diff changeset
   335
            // how many bytes (in data) can we write without overflowing the buffer?
474761f14bca Initial load
duke
parents:
diff changeset
   336
            int batchSize = Math.min(((octetBuffer.length-octetBufferIndex)/4)*3,dataLen);
474761f14bca Initial load
duke
parents:
diff changeset
   337
474761f14bca Initial load
duke
parents:
diff changeset
   338
            // write the batch
474761f14bca Initial load
duke
parents:
diff changeset
   339
            octetBufferIndex = DatatypeConverterImpl._printBase64Binary(data,start,batchSize,octetBuffer,octetBufferIndex);
474761f14bca Initial load
duke
parents:
diff changeset
   340
474761f14bca Initial load
duke
parents:
diff changeset
   341
            if(batchSize<dataLen)
474761f14bca Initial load
duke
parents:
diff changeset
   342
                flushBuffer();
474761f14bca Initial load
duke
parents:
diff changeset
   343
474761f14bca Initial load
duke
parents:
diff changeset
   344
            start += batchSize;
474761f14bca Initial load
duke
parents:
diff changeset
   345
            dataLen -= batchSize;
474761f14bca Initial load
duke
parents:
diff changeset
   346
474761f14bca Initial load
duke
parents:
diff changeset
   347
        }
474761f14bca Initial load
duke
parents:
diff changeset
   348
    }
474761f14bca Initial load
duke
parents:
diff changeset
   349
474761f14bca Initial load
duke
parents:
diff changeset
   350
//
474761f14bca Initial load
duke
parents:
diff changeset
   351
//
474761f14bca Initial load
duke
parents:
diff changeset
   352
// series of the write method that places bytes to the output
474761f14bca Initial load
duke
parents:
diff changeset
   353
// (by doing some buffering internal to this class)
474761f14bca Initial load
duke
parents:
diff changeset
   354
//
474761f14bca Initial load
duke
parents:
diff changeset
   355
474761f14bca Initial load
duke
parents:
diff changeset
   356
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   357
     * Writes one byte directly into the buffer.
474761f14bca Initial load
duke
parents:
diff changeset
   358
     *
474761f14bca Initial load
duke
parents:
diff changeset
   359
     * <p>
474761f14bca Initial load
duke
parents:
diff changeset
   360
     * This method can be used somewhat like the {@code text} method,
474761f14bca Initial load
duke
parents:
diff changeset
   361
     * but it doesn't perform character escaping.
474761f14bca Initial load
duke
parents:
diff changeset
   362
     */
474761f14bca Initial load
duke
parents:
diff changeset
   363
    public final void write(int i) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   364
        if (octetBufferIndex < octetBuffer.length) {
474761f14bca Initial load
duke
parents:
diff changeset
   365
            octetBuffer[octetBufferIndex++] = (byte)i;
474761f14bca Initial load
duke
parents:
diff changeset
   366
        } else {
474761f14bca Initial load
duke
parents:
diff changeset
   367
            out.write(octetBuffer);
474761f14bca Initial load
duke
parents:
diff changeset
   368
            octetBufferIndex = 1;
474761f14bca Initial load
duke
parents:
diff changeset
   369
            octetBuffer[0] = (byte)i;
474761f14bca Initial load
duke
parents:
diff changeset
   370
        }
474761f14bca Initial load
duke
parents:
diff changeset
   371
    }
474761f14bca Initial load
duke
parents:
diff changeset
   372
474761f14bca Initial load
duke
parents:
diff changeset
   373
    protected final void write(byte[] b) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   374
        write(b, 0,  b.length);
474761f14bca Initial load
duke
parents:
diff changeset
   375
    }
474761f14bca Initial load
duke
parents:
diff changeset
   376
474761f14bca Initial load
duke
parents:
diff changeset
   377
    protected final void write(byte[] b, int start, int length) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   378
        if ((octetBufferIndex + length) < octetBuffer.length) {
474761f14bca Initial load
duke
parents:
diff changeset
   379
            System.arraycopy(b, start, octetBuffer, octetBufferIndex, length);
474761f14bca Initial load
duke
parents:
diff changeset
   380
            octetBufferIndex += length;
474761f14bca Initial load
duke
parents:
diff changeset
   381
        } else {
474761f14bca Initial load
duke
parents:
diff changeset
   382
            out.write(octetBuffer, 0, octetBufferIndex);
474761f14bca Initial load
duke
parents:
diff changeset
   383
            out.write(b, start, length);
474761f14bca Initial load
duke
parents:
diff changeset
   384
            octetBufferIndex = 0;
474761f14bca Initial load
duke
parents:
diff changeset
   385
        }
474761f14bca Initial load
duke
parents:
diff changeset
   386
    }
474761f14bca Initial load
duke
parents:
diff changeset
   387
474761f14bca Initial load
duke
parents:
diff changeset
   388
    protected final void flushBuffer() throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   389
        out.write(octetBuffer, 0, octetBufferIndex);
474761f14bca Initial load
duke
parents:
diff changeset
   390
        octetBufferIndex = 0;
474761f14bca Initial load
duke
parents:
diff changeset
   391
    }
474761f14bca Initial load
duke
parents:
diff changeset
   392
474761f14bca Initial load
duke
parents:
diff changeset
   393
    static byte[] toBytes(String s) {
474761f14bca Initial load
duke
parents:
diff changeset
   394
        byte[] buf = new byte[s.length()];
474761f14bca Initial load
duke
parents:
diff changeset
   395
        for( int i=s.length()-1; i>=0; i-- )
474761f14bca Initial load
duke
parents:
diff changeset
   396
            buf[i] = (byte)s.charAt(i);
474761f14bca Initial load
duke
parents:
diff changeset
   397
        return buf;
474761f14bca Initial load
duke
parents:
diff changeset
   398
    }
474761f14bca Initial load
duke
parents:
diff changeset
   399
2675
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   400
    // per instance copy to prevent an attack where malicious OutputStream
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   401
    // rewrites the byte array.
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   402
    private final byte[] XMLNS_EQUALS = _XMLNS_EQUALS.clone();
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   403
    private final byte[] XMLNS_COLON = _XMLNS_COLON.clone();
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   404
    private final byte[] EQUALS = _EQUALS.clone();
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   405
    private final byte[] CLOSE_TAG = _CLOSE_TAG.clone();
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   406
    private final byte[] EMPTY_TAG = _EMPTY_TAG.clone();
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   407
    private final byte[] XML_DECL = _XML_DECL.clone();
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   408
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   409
    // masters
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   410
    private static final byte[] _XMLNS_EQUALS = toBytes(" xmlns=\"");
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   411
    private static final byte[] _XMLNS_COLON = toBytes(" xmlns:");
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   412
    private static final byte[] _EQUALS = toBytes("=\"");
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   413
    private static final byte[] _CLOSE_TAG = toBytes("</");
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   414
    private static final byte[] _EMPTY_TAG = toBytes("/>");
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   415
    private static final byte[] _XML_DECL = toBytes("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   416
91fc59916eb4 6536193: Fix the flaw in UTF8XmlOutput
ramap
parents: 8
diff changeset
   417
    // no need to copy
8
474761f14bca Initial load
duke
parents:
diff changeset
   418
    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
474761f14bca Initial load
duke
parents:
diff changeset
   419
}