KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > util > DefaultTextOutput


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.util;
17
18 import java.io.PrintWriter JavaDoc;
19 import java.io.StringWriter JavaDoc;
20 import java.util.Arrays JavaDoc;
21
22 /**
23  * Adapts {@link TextOutput} to a print writer.
24  */

25 public final class DefaultTextOutput implements TextOutput {
26
27   private final boolean compact;
28   private int identLevel = 0;
29   private int indentGranularity = 2;
30   private char[][] indents = new char[][] {new char[0]};
31   private boolean justNewlined;
32   private final PrintWriter JavaDoc p;
33   private final StringWriter JavaDoc sw;
34
35   public DefaultTextOutput(boolean compact) {
36     this.compact = compact;
37     sw = new StringWriter JavaDoc();
38     p = new PrintWriter JavaDoc(sw, false);
39   }
40
41   public void indentIn() {
42     ++identLevel;
43     if (identLevel >= indents.length) {
44       // Cache a new level of indentation string.
45
//
46
char[] newIndentLevel = new char[identLevel * indentGranularity];
47       Arrays.fill(newIndentLevel, ' ');
48       char[][] newIndents = new char[indents.length + 1][];
49       System.arraycopy(indents, 0, newIndents, 0, indents.length);
50       newIndents[identLevel] = newIndentLevel;
51       indents = newIndents;
52     }
53   }
54   
55   public void indentOut() {
56     --identLevel;
57   }
58
59   public void newline() {
60     if (compact) {
61       p.print('\n');
62     } else {
63       p.println();
64     }
65     justNewlined = true;
66   }
67
68   public void newlineOpt() {
69     if (!compact) {
70       p.println();
71       justNewlined = true;
72     }
73   }
74
75   public void print(char c) {
76     maybeIndent();
77     p.print(c);
78     justNewlined = false;
79   }
80
81   public void print(char[] s) {
82     maybeIndent();
83     p.print(s);
84     justNewlined = false;
85   }
86
87   public void print(String JavaDoc s) {
88     maybeIndent();
89     p.print(s);
90     justNewlined = false;
91   }
92
93   public void printOpt(char c) {
94     if (!compact) {
95       maybeIndent();
96       p.print(c);
97     }
98   }
99
100   public void printOpt(char[] s) {
101     if (!compact) {
102       maybeIndent();
103       p.print(s);
104     }
105   }
106
107   public void printOpt(String JavaDoc s) {
108     if (!compact) {
109       maybeIndent();
110       p.print(s);
111     }
112   }
113
114   public String JavaDoc toString() {
115     p.flush();
116     return sw.toString();
117   }
118
119   private void maybeIndent() {
120     if (justNewlined && !compact) {
121       p.print(indents[identLevel]);
122       justNewlined = false;
123     }
124   }
125 }
126
Popular Tags