src/share/classes/com/sun/xml/internal/bind/v2/runtime/output/UTF8XmlOutput.java

changeset 1
0961a4a21176
child 42
99fc62f032a7
child 45
31822b475baa
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/xml/internal/bind/v2/runtime/output/UTF8XmlOutput.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,401 @@
     1.4 +/*
     1.5 + * Copyright 2006 Sun Microsystems, Inc.  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.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.v2.runtime.output;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.io.OutputStream;
    1.33 +
    1.34 +import javax.xml.stream.XMLStreamException;
    1.35 +
    1.36 +import com.sun.xml.internal.bind.DatatypeConverterImpl;
    1.37 +import com.sun.xml.internal.bind.v2.runtime.Name;
    1.38 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
    1.39 +
    1.40 +import org.xml.sax.SAXException;
    1.41 +
    1.42 +/**
    1.43 + * {@link XmlOutput} implementation specialized for UTF-8.
    1.44 + *
    1.45 + * @author Kohsuke Kawaguchi
    1.46 + * @author Paul Sandoz
    1.47 + */
    1.48 +public class UTF8XmlOutput extends XmlOutputAbstractImpl {
    1.49 +    protected final OutputStream out;
    1.50 +
    1.51 +    /** prefixes encoded. */
    1.52 +    private Encoded[] prefixes = new Encoded[8];
    1.53 +
    1.54 +    /**
    1.55 +     * Of the {@link #prefixes}, number of filled entries.
    1.56 +     * This is almost the same as {@link NamespaceContextImpl#count()},
    1.57 +     * except that it allows us to handle contextual in-scope namespace bindings correctly.
    1.58 +     */
    1.59 +    private int prefixCount;
    1.60 +
    1.61 +    /** local names encoded in UTF-8. All entries are pre-filled. */
    1.62 +    private final Encoded[] localNames;
    1.63 +
    1.64 +    /** Temporary buffer used to encode text. */
    1.65 +    /*
    1.66 +     * TODO
    1.67 +     * The textBuffer could write directly to the _octetBuffer
    1.68 +     * when encoding a string if Encoder is modified.
    1.69 +     * This will avoid an additional memory copy.
    1.70 +     */
    1.71 +    private final Encoded textBuffer = new Encoded();
    1.72 +
    1.73 +    /** Buffer of octets for writing. */
    1.74 +    // TODO: Obtain buffer size from property on the JAXB context
    1.75 +    protected final byte[] octetBuffer = new byte[1024];
    1.76 +
    1.77 +    /** Index in buffer to write to. */
    1.78 +    protected int octetBufferIndex;
    1.79 +
    1.80 +    /**
    1.81 +     * Set to true to indicate that we need to write '>'
    1.82 +     * to close a start tag. Deferring the write of this char
    1.83 +     * allows us to write "/>" for empty elements.
    1.84 +     */
    1.85 +    protected boolean closeStartTagPending = false;
    1.86 +
    1.87 +    /**
    1.88 +     *
    1.89 +     * @param localNames
    1.90 +     *      local names encoded in UTF-8.
    1.91 +     */
    1.92 +    public UTF8XmlOutput(OutputStream out, Encoded[] localNames) {
    1.93 +        this.out = out;
    1.94 +        this.localNames = localNames;
    1.95 +        for( int i=0; i<prefixes.length; i++ )
    1.96 +            prefixes[i] = new Encoded();
    1.97 +    }
    1.98 +
    1.99 +    @Override
   1.100 +    public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException {
   1.101 +        super.startDocument(serializer, fragment,nsUriIndex2prefixIndex,nsContext);
   1.102 +
   1.103 +        octetBufferIndex = 0;
   1.104 +        if(!fragment) {
   1.105 +            write(XML_DECL);
   1.106 +        }
   1.107 +    }
   1.108 +
   1.109 +    public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
   1.110 +        flushBuffer();
   1.111 +        super.endDocument(fragment);
   1.112 +    }
   1.113 +
   1.114 +    /**
   1.115 +     * Writes '>' to close the start tag, if necessary.
   1.116 +     */
   1.117 +    protected final void closeStartTag() throws IOException {
   1.118 +        if(closeStartTagPending) {
   1.119 +            write('>');
   1.120 +            closeStartTagPending = false;
   1.121 +        }
   1.122 +    }
   1.123 +
   1.124 +    public void beginStartTag(int prefix, String localName) throws IOException {
   1.125 +        closeStartTag();
   1.126 +        int base= pushNsDecls();
   1.127 +        write('<');
   1.128 +        writeName(prefix,localName);
   1.129 +        writeNsDecls(base);
   1.130 +    }
   1.131 +
   1.132 +    public void beginStartTag(Name name) throws IOException {
   1.133 +        closeStartTag();
   1.134 +        int base = pushNsDecls();
   1.135 +        write('<');
   1.136 +        writeName(name);
   1.137 +        writeNsDecls(base);
   1.138 +    }
   1.139 +
   1.140 +    private int pushNsDecls() {
   1.141 +        int total = nsContext.count();
   1.142 +        NamespaceContextImpl.Element ns = nsContext.getCurrent();
   1.143 +
   1.144 +        if(total > prefixes.length) {
   1.145 +            // reallocate
   1.146 +            int m = Math.max(total,prefixes.length*2);
   1.147 +            Encoded[] buf = new Encoded[m];
   1.148 +            System.arraycopy(prefixes,0,buf,0,prefixes.length);
   1.149 +            for( int i=prefixes.length; i<buf.length; i++ )
   1.150 +                buf[i] = new Encoded();
   1.151 +            prefixes = buf;
   1.152 +        }
   1.153 +
   1.154 +        int base = Math.min(prefixCount,ns.getBase());
   1.155 +        int size = nsContext.count();
   1.156 +        for( int i=base; i<size; i++ ) {
   1.157 +            String p = nsContext.getPrefix(i);
   1.158 +
   1.159 +            Encoded e = prefixes[i];
   1.160 +
   1.161 +            if(p.length()==0) {
   1.162 +                e.buf = EMPTY_BYTE_ARRAY;
   1.163 +                e.len = 0;
   1.164 +            } else {
   1.165 +                e.set(p);
   1.166 +                e.append(':');
   1.167 +            }
   1.168 +        }
   1.169 +        prefixCount = size;
   1.170 +        return base;
   1.171 +    }
   1.172 +
   1.173 +    protected void writeNsDecls(int base) throws IOException {
   1.174 +        NamespaceContextImpl.Element ns = nsContext.getCurrent();
   1.175 +        int size = nsContext.count();
   1.176 +
   1.177 +        for( int i=ns.getBase(); i<size; i++ )
   1.178 +            writeNsDecl(i);
   1.179 +    }
   1.180 +
   1.181 +    /**
   1.182 +     * Writes a single namespace declaration for the specified prefix.
   1.183 +     */
   1.184 +    protected final void writeNsDecl(int prefixIndex) throws IOException {
   1.185 +        String p = nsContext.getPrefix(prefixIndex);
   1.186 +
   1.187 +        if(p.length()==0) {
   1.188 +            if(nsContext.getCurrent().isRootElement()
   1.189 +            && nsContext.getNamespaceURI(prefixIndex).length()==0)
   1.190 +                return;     // no point in declaring xmlns="" on the root element
   1.191 +            write(XMLNS_EQUALS);
   1.192 +        } else {
   1.193 +            Encoded e = prefixes[prefixIndex];
   1.194 +            write(XMLNS_COLON);
   1.195 +            write(e.buf,0,e.len-1); // skip the trailing ':'
   1.196 +            write(EQUALS);
   1.197 +        }
   1.198 +        doText(nsContext.getNamespaceURI(prefixIndex),true);
   1.199 +        write('\"');
   1.200 +    }
   1.201 +
   1.202 +    private void writePrefix(int prefix) throws IOException {
   1.203 +        prefixes[prefix].write(this);
   1.204 +    }
   1.205 +
   1.206 +    private void writeName(Name name) throws IOException {
   1.207 +        writePrefix(nsUriIndex2prefixIndex[name.nsUriIndex]);
   1.208 +        localNames[name.localNameIndex].write(this);
   1.209 +    }
   1.210 +
   1.211 +    private void writeName(int prefix, String localName) throws IOException {
   1.212 +        writePrefix(prefix);
   1.213 +        textBuffer.set(localName);
   1.214 +        textBuffer.write(this);
   1.215 +    }
   1.216 +
   1.217 +    @Override
   1.218 +    public void attribute(Name name, String value) throws IOException {
   1.219 +        write(' ');
   1.220 +        if(name.nsUriIndex==-1) {
   1.221 +            localNames[name.localNameIndex].write(this);
   1.222 +        } else
   1.223 +            writeName(name);
   1.224 +        write(EQUALS);
   1.225 +        doText(value,true);
   1.226 +        write('\"');
   1.227 +    }
   1.228 +
   1.229 +    public void attribute(int prefix, String localName, String value) throws IOException {
   1.230 +        write(' ');
   1.231 +        if(prefix==-1) {
   1.232 +            textBuffer.set(localName);
   1.233 +            textBuffer.write(this);
   1.234 +        } else
   1.235 +            writeName(prefix,localName);
   1.236 +        write(EQUALS);
   1.237 +        doText(value,true);
   1.238 +        write('\"');
   1.239 +    }
   1.240 +
   1.241 +    public void endStartTag() throws IOException {
   1.242 +        closeStartTagPending = true;
   1.243 +    }
   1.244 +
   1.245 +    @Override
   1.246 +    public void endTag(Name name) throws IOException {
   1.247 +        if(closeStartTagPending) {
   1.248 +            write(EMPTY_TAG);
   1.249 +            closeStartTagPending = false;
   1.250 +        } else {
   1.251 +            write(CLOSE_TAG);
   1.252 +            writeName(name);
   1.253 +            write('>');
   1.254 +        }
   1.255 +    }
   1.256 +
   1.257 +    public void endTag(int prefix, String localName) throws IOException {
   1.258 +        if(closeStartTagPending) {
   1.259 +            write(EMPTY_TAG);
   1.260 +            closeStartTagPending = false;
   1.261 +        } else {
   1.262 +            write(CLOSE_TAG);
   1.263 +            writeName(prefix,localName);
   1.264 +            write('>');
   1.265 +        }
   1.266 +    }
   1.267 +
   1.268 +    public void text(String value, boolean needSP) throws IOException {
   1.269 +        closeStartTag();
   1.270 +        if(needSP)
   1.271 +            write(' ');
   1.272 +        doText(value,false);
   1.273 +    }
   1.274 +
   1.275 +    public void text(Pcdata value, boolean needSP) throws IOException {
   1.276 +        closeStartTag();
   1.277 +        if(needSP)
   1.278 +            write(' ');
   1.279 +        value.writeTo(this);
   1.280 +    }
   1.281 +
   1.282 +    private void doText(String value,boolean isAttribute) throws IOException {
   1.283 +        textBuffer.setEscape(value,isAttribute);
   1.284 +        textBuffer.write(this);
   1.285 +    }
   1.286 +
   1.287 +    public final void text(int value) throws IOException {
   1.288 +        closeStartTag();
   1.289 +        /*
   1.290 +         * TODO
   1.291 +         * Change to use the octet buffer directly
   1.292 +         */
   1.293 +
   1.294 +        // max is -2147483648 and 11 digits
   1.295 +        boolean minus = (value<0);
   1.296 +        textBuffer.ensureSize(11);
   1.297 +        byte[] buf = textBuffer.buf;
   1.298 +        int idx = 11;
   1.299 +
   1.300 +        do {
   1.301 +            int r = value%10;
   1.302 +            if(r<0) r = -r;
   1.303 +            buf[--idx] = (byte)('0'|r);    // really measn 0x30+r but 0<=r<10, so bit-OR would do.
   1.304 +            value /= 10;
   1.305 +        } while(value!=0);
   1.306 +
   1.307 +        if(minus)   buf[--idx] = (byte)'-';
   1.308 +
   1.309 +        write(buf,idx,11-idx);
   1.310 +    }
   1.311 +
   1.312 +    /**
   1.313 +     * Writes the given byte[] as base64 encoded binary to the output.
   1.314 +     *
   1.315 +     * <p>
   1.316 +     * Being defined on this class allows this method to access the buffer directly,
   1.317 +     * which translates to a better performance.
   1.318 +     */
   1.319 +    public void text(byte[] data, int dataLen) throws IOException {
   1.320 +        closeStartTag();
   1.321 +
   1.322 +        int start = 0;
   1.323 +
   1.324 +        while(dataLen>0) {
   1.325 +            // how many bytes (in data) can we write without overflowing the buffer?
   1.326 +            int batchSize = Math.min(((octetBuffer.length-octetBufferIndex)/4)*3,dataLen);
   1.327 +
   1.328 +            // write the batch
   1.329 +            octetBufferIndex = DatatypeConverterImpl._printBase64Binary(data,start,batchSize,octetBuffer,octetBufferIndex);
   1.330 +
   1.331 +            if(batchSize<dataLen)
   1.332 +                flushBuffer();
   1.333 +
   1.334 +            start += batchSize;
   1.335 +            dataLen -= batchSize;
   1.336 +
   1.337 +        }
   1.338 +    }
   1.339 +
   1.340 +//
   1.341 +//
   1.342 +// series of the write method that places bytes to the output
   1.343 +// (by doing some buffering internal to this class)
   1.344 +//
   1.345 +
   1.346 +    /**
   1.347 +     * Writes one byte directly into the buffer.
   1.348 +     *
   1.349 +     * <p>
   1.350 +     * This method can be used somewhat like the {@code text} method,
   1.351 +     * but it doesn't perform character escaping.
   1.352 +     */
   1.353 +    public final void write(int i) throws IOException {
   1.354 +        if (octetBufferIndex < octetBuffer.length) {
   1.355 +            octetBuffer[octetBufferIndex++] = (byte)i;
   1.356 +        } else {
   1.357 +            out.write(octetBuffer);
   1.358 +            octetBufferIndex = 1;
   1.359 +            octetBuffer[0] = (byte)i;
   1.360 +        }
   1.361 +    }
   1.362 +
   1.363 +    protected final void write(byte[] b) throws IOException {
   1.364 +        write(b, 0,  b.length);
   1.365 +    }
   1.366 +
   1.367 +    protected final void write(byte[] b, int start, int length) throws IOException {
   1.368 +        if ((octetBufferIndex + length) < octetBuffer.length) {
   1.369 +            System.arraycopy(b, start, octetBuffer, octetBufferIndex, length);
   1.370 +            octetBufferIndex += length;
   1.371 +        } else {
   1.372 +            out.write(octetBuffer, 0, octetBufferIndex);
   1.373 +            out.write(b, start, length);
   1.374 +            octetBufferIndex = 0;
   1.375 +        }
   1.376 +    }
   1.377 +
   1.378 +    protected final void flushBuffer() throws IOException {
   1.379 +        out.write(octetBuffer, 0, octetBufferIndex);
   1.380 +        octetBufferIndex = 0;
   1.381 +    }
   1.382 +
   1.383 +    public void flush() throws IOException {
   1.384 +        flushBuffer();
   1.385 +        out.flush();
   1.386 +    }
   1.387 +
   1.388 +
   1.389 +
   1.390 +    static byte[] toBytes(String s) {
   1.391 +        byte[] buf = new byte[s.length()];
   1.392 +        for( int i=s.length()-1; i>=0; i-- )
   1.393 +            buf[i] = (byte)s.charAt(i);
   1.394 +        return buf;
   1.395 +    }
   1.396 +
   1.397 +    private static final byte[] XMLNS_EQUALS = toBytes(" xmlns=\"");
   1.398 +    private static final byte[] XMLNS_COLON = toBytes(" xmlns:");
   1.399 +    private static final byte[] EQUALS = toBytes("=\"");
   1.400 +    private static final byte[] CLOSE_TAG = toBytes("</");
   1.401 +    private static final byte[] EMPTY_TAG = toBytes("/>");
   1.402 +    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
   1.403 +    private static final byte[] XML_DECL = toBytes("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
   1.404 +}

mercurial