src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/output/IndentingUTF8XmlOutput.java

changeset 0
373ffda63c9a
equal deleted inserted replaced
-1:000000000000 0:373ffda63c9a
1 /*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.xml.internal.bind.v2.runtime.output;
27
28 import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;
29 import java.io.IOException;
30 import java.io.OutputStream;
31
32 import javax.xml.stream.XMLStreamException;
33
34 import com.sun.xml.internal.bind.v2.runtime.Name;
35
36 import org.xml.sax.SAXException;
37
38 /**
39 * {@link UTF8XmlOutput} with indentation.
40 *
41 * TODO: not sure if it's a good idea to move the indenting functionality to another class.
42 *
43 * Doesn't have to be final, but it helps the JVM.
44 *
45 * @author Kohsuke Kawaguchi
46 */
47 public final class IndentingUTF8XmlOutput extends UTF8XmlOutput {
48
49 /**
50 * Null if the writer should perform no indentation.
51 *
52 * Otherwise this will keep the 8 copies of the string for indentation.
53 * (so that we can write 8 indentation at once.)
54 */
55 private final Encoded indent8;
56
57 /**
58 * Length of one indentation.
59 */
60 private final int unitLen;
61
62 private int depth = 0;
63
64 private boolean seenText = false;
65
66 /**
67 *
68 * @param indentStr
69 * set to null for no indentation and optimal performance.
70 * otherwise the string is used for indentation.
71 */
72 public IndentingUTF8XmlOutput(OutputStream out, String indentStr, Encoded[] localNames, CharacterEscapeHandler escapeHandler) {
73 super(out, localNames, escapeHandler);
74
75 if(indentStr!=null) {
76 Encoded e = new Encoded(indentStr);
77 indent8 = new Encoded();
78 indent8.ensureSize(e.len*8);
79 unitLen = e.len;
80 for( int i=0; i<8; i++ )
81 System.arraycopy(e.buf, 0, indent8.buf, unitLen*i, unitLen);
82 } else {
83 this.indent8 = null;
84 this.unitLen = 0;
85 }
86 }
87
88 @Override
89 public void beginStartTag(int prefix, String localName) throws IOException {
90 indentStartTag();
91 super.beginStartTag(prefix, localName);
92 }
93
94 @Override
95 public void beginStartTag(Name name) throws IOException {
96 indentStartTag();
97 super.beginStartTag(name);
98 }
99
100 private void indentStartTag() throws IOException {
101 closeStartTag();
102 if(!seenText)
103 printIndent();
104 depth++;
105 seenText = false;
106 }
107
108 @Override
109 public void endTag(Name name) throws IOException {
110 indentEndTag();
111 super.endTag(name);
112 }
113
114 @Override
115 public void endTag(int prefix, String localName) throws IOException {
116 indentEndTag();
117 super.endTag(prefix, localName);
118 }
119
120 private void indentEndTag() throws IOException {
121 depth--;
122 if(!closeStartTagPending && !seenText)
123 printIndent();
124 seenText = false;
125 }
126
127 private void printIndent() throws IOException {
128 write('\n');
129 int i = depth%8;
130
131 write( indent8.buf, 0, i*unitLen );
132
133 i>>=3; // really i /= 8;
134
135 for( ; i>0; i-- )
136 indent8.write(this);
137 }
138
139 @Override
140 public void text(String value, boolean needSP) throws IOException {
141 seenText = true;
142 super.text(value, needSP);
143 }
144
145 @Override
146 public void text(Pcdata value, boolean needSP) throws IOException {
147 seenText = true;
148 super.text(value, needSP);
149 }
150
151 @Override
152 public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException {
153 write('\n');
154 super.endDocument(fragment);
155 }
156 }

mercurial