001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.util;
017:
018: import java.io.PrintWriter;
019: import java.io.StringWriter;
020: import java.util.Arrays;
021:
022: /**
023: * Adapts {@link TextOutput} to a print writer.
024: */
025: public final class DefaultTextOutput implements TextOutput {
026:
027: private final boolean compact;
028: private int identLevel = 0;
029: private int indentGranularity = 2;
030: private char[][] indents = new char[][] { new char[0] };
031: private boolean justNewlined;
032: private final PrintWriter p;
033: private final StringWriter sw;
034:
035: public DefaultTextOutput(boolean compact) {
036: this .compact = compact;
037: sw = new StringWriter();
038: p = new PrintWriter(sw, false);
039: }
040:
041: public void indentIn() {
042: ++identLevel;
043: if (identLevel >= indents.length) {
044: // Cache a new level of indentation string.
045: //
046: char[] newIndentLevel = new char[identLevel
047: * indentGranularity];
048: Arrays.fill(newIndentLevel, ' ');
049: char[][] newIndents = new char[indents.length + 1][];
050: System.arraycopy(indents, 0, newIndents, 0, indents.length);
051: newIndents[identLevel] = newIndentLevel;
052: indents = newIndents;
053: }
054: }
055:
056: public void indentOut() {
057: --identLevel;
058: }
059:
060: public void newline() {
061: if (compact) {
062: p.print('\n');
063: } else {
064: p.println();
065: }
066: justNewlined = true;
067: }
068:
069: public void newlineOpt() {
070: if (!compact) {
071: p.println();
072: justNewlined = true;
073: }
074: }
075:
076: public void print(char c) {
077: maybeIndent();
078: p.print(c);
079: justNewlined = false;
080: }
081:
082: public void print(char[] s) {
083: maybeIndent();
084: p.print(s);
085: justNewlined = false;
086: }
087:
088: public void print(String s) {
089: maybeIndent();
090: p.print(s);
091: justNewlined = false;
092: }
093:
094: public void printOpt(char c) {
095: if (!compact) {
096: maybeIndent();
097: p.print(c);
098: }
099: }
100:
101: public void printOpt(char[] s) {
102: if (!compact) {
103: maybeIndent();
104: p.print(s);
105: }
106: }
107:
108: public void printOpt(String s) {
109: if (!compact) {
110: maybeIndent();
111: p.print(s);
112: }
113: }
114:
115: public String toString() {
116: p.flush();
117: return sw.toString();
118: }
119:
120: private void maybeIndent() {
121: if (justNewlined && !compact) {
122: p.print(indents[identLevel]);
123: justNewlined = false;
124: }
125: }
126: }
|