149 if (s == null) |
149 if (s == null) |
150 s = "null"; |
150 s = "null"; |
151 for (int i = 0; i < s.length(); i++) { |
151 for (int i = 0; i < s.length(); i++) { |
152 char c = s.charAt(i); |
152 char c = s.charAt(i); |
153 switch (c) { |
153 switch (c) { |
|
154 case ' ': |
|
155 pendingSpaces++; |
|
156 break; |
|
157 |
154 case '\n': |
158 case '\n': |
155 println(); |
159 println(); |
156 break; |
160 break; |
|
161 |
157 default: |
162 default: |
158 if (buffer.length() == 0) |
163 if (buffer.length() == 0) |
159 indent(); |
164 indent(); |
|
165 if (pendingSpaces > 0) { |
|
166 for (int sp = 0; sp < pendingSpaces; sp++) |
|
167 buffer.append(' '); |
|
168 pendingSpaces = 0; |
|
169 } |
160 buffer.append(c); |
170 buffer.append(c); |
161 } |
171 } |
162 } |
172 } |
163 |
173 |
164 } |
174 } |
165 |
175 |
166 protected void println() { |
176 protected void println() { |
|
177 // ignore/discard pending spaces |
|
178 pendingSpaces = 0; |
167 out.println(buffer); |
179 out.println(buffer); |
168 buffer.setLength(0); |
180 buffer.setLength(0); |
169 } |
181 } |
170 |
182 |
171 protected void indent(int delta) { |
183 protected void indent(int delta) { |
172 indentCount += delta; |
184 indentCount += delta; |
173 } |
185 } |
174 |
186 |
175 protected void tab() { |
187 protected void tab() { |
176 if (buffer.length() == 0) |
188 int col = indentCount * indentWidth + tabColumn; |
177 indent(); |
189 pendingSpaces += (col <= buffer.length() ? 1 : col - buffer.length()); |
178 space(indentCount * indentWidth + tabColumn - buffer.length()); |
|
179 } |
190 } |
180 |
191 |
181 private void indent() { |
192 private void indent() { |
182 space(indentCount * indentWidth); |
193 pendingSpaces += (indentCount * indentWidth); |
183 } |
194 } |
184 |
195 |
185 private void space(int n) { |
196 private final PrintWriter out; |
186 for (int i = 0; i < n; i++) |
197 private final StringBuilder buffer; |
187 buffer.append(' '); |
|
188 } |
|
189 |
|
190 private PrintWriter out; |
|
191 private StringBuilder buffer; |
|
192 private int indentCount; |
198 private int indentCount; |
193 private int indentWidth; |
199 private final int indentWidth; |
194 private int tabColumn; |
200 private final int tabColumn; |
195 private boolean pendingNewline; |
201 private boolean pendingNewline; |
|
202 private int pendingSpaces; |
196 } |
203 } |
197 } |
204 } |
198 |
205 |