01: /*
02: * Copyright (C) 2007 Júlio Vilmar Gesser.
03: *
04: * This file is part of Java 1.5 parser and Abstract Syntax Tree.
05: *
06: * Java 1.5 parser and Abstract Syntax Tree is free software: you can redistribute it and/or modify
07: * it under the terms of the GNU Lesser General Public License as published by
08: * the Free Software Foundation, either version 3 of the License, or
09: * (at your option) any later version.
10: *
11: * Java 1.5 parser and Abstract Syntax Tree is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public License
17: * along with Java 1.5 parser and Abstract Syntax Tree. If not, see <http://www.gnu.org/licenses/>.
18: */
19: /*
20: * Created on 08/10/2006
21: */
22: package japa.parser.ast.visitor;
23:
24: /**
25: * @author Julio Vilmar Gesser
26: */
27: public final class SourcePrinter {
28:
29: private int level = 0;
30:
31: private boolean indented = false;
32:
33: private final StringBuilder buf = new StringBuilder();
34:
35: public void indent() {
36: level++;
37: }
38:
39: public void unindent() {
40: level--;
41: }
42:
43: private void makeIndent() {
44: for (int i = 0; i < level; i++) {
45: buf.append(" ");
46: }
47: }
48:
49: public void print(String arg) {
50: if (!indented) {
51: makeIndent();
52: indented = true;
53: }
54: buf.append(arg);
55: }
56:
57: public void printLn(String arg) {
58: print(arg);
59: printLn();
60: }
61:
62: public void printLn() {
63: buf.append("\n");
64: indented = false;
65: }
66:
67: public String getSource() {
68: return buf.toString();
69: }
70:
71: @Override
72: public String toString() {
73: return getSource();
74: }
75: }
|