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/IndentingUTF8XmlOutput.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,156 @@ 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 com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler; 1.32 +import java.io.IOException; 1.33 +import java.io.OutputStream; 1.34 + 1.35 +import javax.xml.stream.XMLStreamException; 1.36 + 1.37 +import com.sun.xml.internal.bind.v2.runtime.Name; 1.38 + 1.39 +import org.xml.sax.SAXException; 1.40 + 1.41 +/** 1.42 + * {@link UTF8XmlOutput} with indentation. 1.43 + * 1.44 + * TODO: not sure if it's a good idea to move the indenting functionality to another class. 1.45 + * 1.46 + * Doesn't have to be final, but it helps the JVM. 1.47 + * 1.48 + * @author Kohsuke Kawaguchi 1.49 + */ 1.50 +public final class IndentingUTF8XmlOutput extends UTF8XmlOutput { 1.51 + 1.52 + /** 1.53 + * Null if the writer should perform no indentation. 1.54 + * 1.55 + * Otherwise this will keep the 8 copies of the string for indentation. 1.56 + * (so that we can write 8 indentation at once.) 1.57 + */ 1.58 + private final Encoded indent8; 1.59 + 1.60 + /** 1.61 + * Length of one indentation. 1.62 + */ 1.63 + private final int unitLen; 1.64 + 1.65 + private int depth = 0; 1.66 + 1.67 + private boolean seenText = false; 1.68 + 1.69 + /** 1.70 + * 1.71 + * @param indentStr 1.72 + * set to null for no indentation and optimal performance. 1.73 + * otherwise the string is used for indentation. 1.74 + */ 1.75 + public IndentingUTF8XmlOutput(OutputStream out, String indentStr, Encoded[] localNames, CharacterEscapeHandler escapeHandler) { 1.76 + super(out, localNames, escapeHandler); 1.77 + 1.78 + if(indentStr!=null) { 1.79 + Encoded e = new Encoded(indentStr); 1.80 + indent8 = new Encoded(); 1.81 + indent8.ensureSize(e.len*8); 1.82 + unitLen = e.len; 1.83 + for( int i=0; i<8; i++ ) 1.84 + System.arraycopy(e.buf, 0, indent8.buf, unitLen*i, unitLen); 1.85 + } else { 1.86 + this.indent8 = null; 1.87 + this.unitLen = 0; 1.88 + } 1.89 + } 1.90 + 1.91 + @Override 1.92 + public void beginStartTag(int prefix, String localName) throws IOException { 1.93 + indentStartTag(); 1.94 + super.beginStartTag(prefix, localName); 1.95 + } 1.96 + 1.97 + @Override 1.98 + public void beginStartTag(Name name) throws IOException { 1.99 + indentStartTag(); 1.100 + super.beginStartTag(name); 1.101 + } 1.102 + 1.103 + private void indentStartTag() throws IOException { 1.104 + closeStartTag(); 1.105 + if(!seenText) 1.106 + printIndent(); 1.107 + depth++; 1.108 + seenText = false; 1.109 + } 1.110 + 1.111 + @Override 1.112 + public void endTag(Name name) throws IOException { 1.113 + indentEndTag(); 1.114 + super.endTag(name); 1.115 + } 1.116 + 1.117 + @Override 1.118 + public void endTag(int prefix, String localName) throws IOException { 1.119 + indentEndTag(); 1.120 + super.endTag(prefix, localName); 1.121 + } 1.122 + 1.123 + private void indentEndTag() throws IOException { 1.124 + depth--; 1.125 + if(!closeStartTagPending && !seenText) 1.126 + printIndent(); 1.127 + seenText = false; 1.128 + } 1.129 + 1.130 + private void printIndent() throws IOException { 1.131 + write('\n'); 1.132 + int i = depth%8; 1.133 + 1.134 + write( indent8.buf, 0, i*unitLen ); 1.135 + 1.136 + i>>=3; // really i /= 8; 1.137 + 1.138 + for( ; i>0; i-- ) 1.139 + indent8.write(this); 1.140 + } 1.141 + 1.142 + @Override 1.143 + public void text(String value, boolean needSP) throws IOException { 1.144 + seenText = true; 1.145 + super.text(value, needSP); 1.146 + } 1.147 + 1.148 + @Override 1.149 + public void text(Pcdata value, boolean needSP) throws IOException { 1.150 + seenText = true; 1.151 + super.text(value, needSP); 1.152 + } 1.153 + 1.154 + @Override 1.155 + public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException { 1.156 + write('\n'); 1.157 + super.endDocument(fragment); 1.158 + } 1.159 +}