001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.runtime.output;
038:
039: import java.io.IOException;
040: import java.io.OutputStream;
041:
042: import javax.xml.stream.XMLStreamException;
043:
044: import com.sun.xml.bind.v2.runtime.Name;
045:
046: import org.xml.sax.SAXException;
047:
048: /**
049: * {@link UTF8XmlOutput} with indentation.
050: *
051: * TODO: not sure if it's a good idea to move the indenting functionality to another class.
052: *
053: * Doesn't have to be final, but it helps the JVM.
054: *
055: * @author Kohsuke Kawaguchi
056: */
057: public final class IndentingUTF8XmlOutput extends UTF8XmlOutput {
058:
059: /**
060: * Null if the writer should perform no indentation.
061: *
062: * Otherwise this will keep the 8 copies of the string for indentation.
063: * (so that we can write 8 indentation at once.)
064: */
065: private final Encoded indent8;
066:
067: /**
068: * Length of one indentation.
069: */
070: private final int unitLen;
071:
072: private int depth = 0;
073:
074: private boolean seenText = false;
075:
076: /**
077: *
078: * @param indentStr
079: * set to null for no indentation and optimal performance.
080: * otherwise the string is used for indentation.
081: */
082: public IndentingUTF8XmlOutput(OutputStream out, String indentStr,
083: Encoded[] localNames) {
084: super (out, localNames);
085:
086: if (indentStr != null) {
087: Encoded e = new Encoded(indentStr);
088: indent8 = new Encoded();
089: indent8.ensureSize(e.len * 8);
090: unitLen = e.len;
091: for (int i = 0; i < 8; i++)
092: System.arraycopy(e.buf, 0, indent8.buf, unitLen * i,
093: unitLen);
094: } else {
095: this .indent8 = null;
096: this .unitLen = 0;
097: }
098: }
099:
100: @Override
101: public void beginStartTag(int prefix, String localName)
102: throws IOException {
103: indentStartTag();
104: super .beginStartTag(prefix, localName);
105: }
106:
107: @Override
108: public void beginStartTag(Name name) throws IOException {
109: indentStartTag();
110: super .beginStartTag(name);
111: }
112:
113: private void indentStartTag() throws IOException {
114: closeStartTag();
115: if (!seenText)
116: printIndent();
117: depth++;
118: seenText = false;
119: }
120:
121: @Override
122: public void endTag(Name name) throws IOException {
123: indentEndTag();
124: super .endTag(name);
125: }
126:
127: @Override
128: public void endTag(int prefix, String localName) throws IOException {
129: indentEndTag();
130: super .endTag(prefix, localName);
131: }
132:
133: private void indentEndTag() throws IOException {
134: depth--;
135: if (!closeStartTagPending && !seenText)
136: printIndent();
137: seenText = false;
138: }
139:
140: private void printIndent() throws IOException {
141: write('\n');
142: int i = depth % 8;
143:
144: write(indent8.buf, 0, i * unitLen);
145:
146: i >>= 3; // really i /= 8;
147:
148: for (; i > 0; i--)
149: indent8.write(this );
150: }
151:
152: @Override
153: public void text(String value, boolean needSP) throws IOException {
154: seenText = true;
155: super .text(value, needSP);
156: }
157:
158: @Override
159: public void text(Pcdata value, boolean needSP) throws IOException {
160: seenText = true;
161: super .text(value, needSP);
162: }
163:
164: @Override
165: public void endDocument(boolean fragment) throws IOException,
166: SAXException, XMLStreamException {
167: write('\n');
168: super.endDocument(fragment);
169: }
170: }
|