aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.runtime.output; aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: import java.io.OutputStream; aoqi@0: aoqi@0: import java.io.StringWriter; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.DatatypeConverterImpl; aoqi@0: import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.Name; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.MarshallerImpl; aoqi@0: aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * {@link XmlOutput} implementation specialized for UTF-8. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: * @author Paul Sandoz aoqi@0: */ aoqi@0: public class UTF8XmlOutput extends XmlOutputAbstractImpl { aoqi@0: protected final OutputStream out; aoqi@0: aoqi@0: /** prefixes encoded. */ aoqi@0: private Encoded[] prefixes = new Encoded[8]; aoqi@0: aoqi@0: /** aoqi@0: * Of the {@link #prefixes}, number of filled entries. aoqi@0: * This is almost the same as {@link NamespaceContextImpl#count()}, aoqi@0: * except that it allows us to handle contextual in-scope namespace bindings correctly. aoqi@0: */ aoqi@0: private int prefixCount; aoqi@0: aoqi@0: /** local names encoded in UTF-8. All entries are pre-filled. */ aoqi@0: private final Encoded[] localNames; aoqi@0: aoqi@0: /** Temporary buffer used to encode text. */ aoqi@0: /* aoqi@0: * TODO aoqi@0: * The textBuffer could write directly to the _octetBuffer aoqi@0: * when encoding a string if Encoder is modified. aoqi@0: * This will avoid an additional memory copy. aoqi@0: */ aoqi@0: private final Encoded textBuffer = new Encoded(); aoqi@0: aoqi@0: /** Buffer of octets for writing. */ aoqi@0: // TODO: Obtain buffer size from property on the JAXB context aoqi@0: protected final byte[] octetBuffer = new byte[1024]; aoqi@0: aoqi@0: /** Index in buffer to write to. */ aoqi@0: protected int octetBufferIndex; aoqi@0: aoqi@0: /** aoqi@0: * Set to true to indicate that we need to write '>' aoqi@0: * to close a start tag. Deferring the write of this char aoqi@0: * allows us to write "/>" for empty elements. aoqi@0: */ aoqi@0: protected boolean closeStartTagPending = false; aoqi@0: aoqi@0: /** aoqi@0: * @see MarshallerImpl#header aoqi@0: */ aoqi@0: private String header; aoqi@0: aoqi@0: private CharacterEscapeHandler escapeHandler = null; aoqi@0: aoqi@0: /** aoqi@0: * aoqi@0: * @param localNames aoqi@0: * local names encoded in UTF-8. aoqi@0: */ aoqi@0: public UTF8XmlOutput(OutputStream out, Encoded[] localNames, CharacterEscapeHandler escapeHandler) { aoqi@0: this.out = out; aoqi@0: this.localNames = localNames; aoqi@0: for( int i=0; i' to close the start tag, if necessary. aoqi@0: */ aoqi@0: protected final void closeStartTag() throws IOException { aoqi@0: if(closeStartTagPending) { aoqi@0: write('>'); aoqi@0: closeStartTagPending = false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void beginStartTag(int prefix, String localName) throws IOException { aoqi@0: closeStartTag(); aoqi@0: int base= pushNsDecls(); aoqi@0: write('<'); aoqi@0: writeName(prefix,localName); aoqi@0: writeNsDecls(base); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void beginStartTag(Name name) throws IOException { aoqi@0: closeStartTag(); aoqi@0: int base = pushNsDecls(); aoqi@0: write('<'); aoqi@0: writeName(name); aoqi@0: writeNsDecls(base); aoqi@0: } aoqi@0: aoqi@0: private int pushNsDecls() { aoqi@0: int total = nsContext.count(); aoqi@0: NamespaceContextImpl.Element ns = nsContext.getCurrent(); aoqi@0: aoqi@0: if(total > prefixes.length) { aoqi@0: // reallocate aoqi@0: int m = Math.max(total,prefixes.length*2); aoqi@0: Encoded[] buf = new Encoded[m]; aoqi@0: System.arraycopy(prefixes,0,buf,0,prefixes.length); aoqi@0: for( int i=prefixes.length; i'); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void endTag(int prefix, String localName) throws IOException { aoqi@0: if(closeStartTagPending) { aoqi@0: write(EMPTY_TAG); aoqi@0: closeStartTagPending = false; aoqi@0: } else { aoqi@0: write(CLOSE_TAG); aoqi@0: writeName(prefix,localName); aoqi@0: write('>'); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public void text(String value, boolean needSP) throws IOException { aoqi@0: closeStartTag(); aoqi@0: if(needSP) aoqi@0: write(' '); aoqi@0: doText(value,false); aoqi@0: } aoqi@0: aoqi@0: public void text(Pcdata value, boolean needSP) throws IOException { aoqi@0: closeStartTag(); aoqi@0: if(needSP) aoqi@0: write(' '); aoqi@0: value.writeTo(this); aoqi@0: } aoqi@0: aoqi@0: private void doText(String value,boolean isAttribute) throws IOException { aoqi@0: if (escapeHandler != null) { aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: escapeHandler.escape(value.toCharArray(), 0, value.length(), isAttribute, sw); aoqi@0: textBuffer.set(sw.toString()); aoqi@0: } else { aoqi@0: textBuffer.setEscape(value, isAttribute); aoqi@0: } aoqi@0: textBuffer.write(this); aoqi@0: } aoqi@0: aoqi@0: public final void text(int value) throws IOException { aoqi@0: closeStartTag(); aoqi@0: /* aoqi@0: * TODO aoqi@0: * Change to use the octet buffer directly aoqi@0: */ aoqi@0: aoqi@0: // max is -2147483648 and 11 digits aoqi@0: boolean minus = (value<0); aoqi@0: textBuffer.ensureSize(11); aoqi@0: byte[] buf = textBuffer.buf; aoqi@0: int idx = 11; aoqi@0: aoqi@0: do { aoqi@0: int r = value%10; aoqi@0: if(r<0) r = -r; aoqi@0: buf[--idx] = (byte)('0'|r); // really measn 0x30+r but 0<=r<10, so bit-OR would do. aoqi@0: value /= 10; aoqi@0: } while(value!=0); aoqi@0: aoqi@0: if(minus) buf[--idx] = (byte)'-'; aoqi@0: aoqi@0: write(buf,idx,11-idx); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Writes the given byte[] as base64 encoded binary to the output. aoqi@0: * aoqi@0: *

aoqi@0: * Being defined on this class allows this method to access the buffer directly, aoqi@0: * which translates to a better performance. aoqi@0: */ aoqi@0: public void text(byte[] data, int dataLen) throws IOException { aoqi@0: closeStartTag(); aoqi@0: aoqi@0: int start = 0; aoqi@0: aoqi@0: while(dataLen>0) { aoqi@0: // how many bytes (in data) can we write without overflowing the buffer? aoqi@0: int batchSize = Math.min(((octetBuffer.length-octetBufferIndex)/4)*3,dataLen); aoqi@0: aoqi@0: // write the batch aoqi@0: octetBufferIndex = DatatypeConverterImpl._printBase64Binary(data,start,batchSize,octetBuffer,octetBufferIndex); aoqi@0: aoqi@0: if(batchSize aoqi@0: * This method can be used somewhat like the {@code text} method, aoqi@0: * but it doesn't perform character escaping. aoqi@0: */ aoqi@0: public final void write(int i) throws IOException { aoqi@0: if (octetBufferIndex < octetBuffer.length) { aoqi@0: octetBuffer[octetBufferIndex++] = (byte)i; aoqi@0: } else { aoqi@0: out.write(octetBuffer); aoqi@0: octetBufferIndex = 1; aoqi@0: octetBuffer[0] = (byte)i; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected final void write(byte[] b) throws IOException { aoqi@0: write(b, 0, b.length); aoqi@0: } aoqi@0: aoqi@0: protected final void write(byte[] b, int start, int length) throws IOException { aoqi@0: if ((octetBufferIndex + length) < octetBuffer.length) { aoqi@0: System.arraycopy(b, start, octetBuffer, octetBufferIndex, length); aoqi@0: octetBufferIndex += length; aoqi@0: } else { aoqi@0: out.write(octetBuffer, 0, octetBufferIndex); aoqi@0: out.write(b, start, length); aoqi@0: octetBufferIndex = 0; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected final void flushBuffer() throws IOException { aoqi@0: out.write(octetBuffer, 0, octetBufferIndex); aoqi@0: octetBufferIndex = 0; aoqi@0: } aoqi@0: aoqi@0: static byte[] toBytes(String s) { aoqi@0: byte[] buf = new byte[s.length()]; aoqi@0: for( int i=s.length()-1; i>=0; i-- ) aoqi@0: buf[i] = (byte)s.charAt(i); aoqi@0: return buf; aoqi@0: } aoqi@0: aoqi@0: // per instance copy to prevent an attack where malicious OutputStream aoqi@0: // rewrites the byte array. aoqi@0: private final byte[] XMLNS_EQUALS = _XMLNS_EQUALS.clone(); aoqi@0: private final byte[] XMLNS_COLON = _XMLNS_COLON.clone(); aoqi@0: private final byte[] EQUALS = _EQUALS.clone(); aoqi@0: private final byte[] CLOSE_TAG = _CLOSE_TAG.clone(); aoqi@0: private final byte[] EMPTY_TAG = _EMPTY_TAG.clone(); aoqi@0: private final byte[] XML_DECL = _XML_DECL.clone(); aoqi@0: aoqi@0: // masters aoqi@0: private static final byte[] _XMLNS_EQUALS = toBytes(" xmlns=\""); aoqi@0: private static final byte[] _XMLNS_COLON = toBytes(" xmlns:"); aoqi@0: private static final byte[] _EQUALS = toBytes("=\""); aoqi@0: private static final byte[] _CLOSE_TAG = toBytes(""); aoqi@0: private static final byte[] _XML_DECL = toBytes(""); aoqi@0: aoqi@0: // no need to copy aoqi@0: private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; aoqi@0: }