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