summaryrefslogtreecommitdiff
path: root/sources/meta/util/TextWriter.java
blob: 2884848e83eb6d3fcdd3629c6886b509aa2d070e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package meta.util;

/** A string generator with support for indentation and spacing. */
public class TextWriter {

    //########################################################################
    // Private Constants

    /** The line separator */
    private static final String LINE = System.getProperty("line.separator");

    /** The default width of an indentation level */
    private static final String STEP = "    ";

    //########################################################################
    // Private Fields

    /** The output buffer */
    private final StringBuffer buffer;

    /** The width of an indentation level */
    private final String step;

    /** The current indentation level */
    private int level;

    /** Do we need to align? */
    private boolean align;

    /** Do we need to print a white space? */
    private boolean space;

    /** Is there already a empty line? */
    private boolean line;

    //########################################################################
    // Public Constructors

    /** Creates a new TextWriter. */
    public TextWriter() {
        this(new StringBuffer());
    }

    /** Creates a new TextWriter. */
    public TextWriter(String step) {
        this(new StringBuffer(), step);
    }

    /** Creates a new TextWriter. */
    public TextWriter(String step, int level) {
        this(new StringBuffer(), step, level);
    }

    /** Creates a new TextWriter. */
    public TextWriter(StringBuffer buffer) {
        this(buffer, STEP, 0);
    }

    /** Creates a new TextWriter. */
    public TextWriter(StringBuffer buffer, String step) {
        this(buffer, step, 0);
    }

    /** Creates a new TextWriter. */
    public TextWriter(StringBuffer buffer, String step, int level) {
        this.buffer = buffer;
        this.step = step;
        this.level = level;
        this.align = false;
        this.space = false;
        this.line = false;
    }

    //########################################################################
    // Public Methods - Printing

    /** Prints a new line. */
    public TextWriter println() {
        return line();
    }

    /** Prints the boolean value followed by a new line. */
    public TextWriter println(boolean value) {
        return print(value).line();
    }

    /** Prints the byte value followed by a new line. */
    public TextWriter println(byte value) {
        return print(value).line();
    }

    /** Prints the short value followed by a new line. */
    public TextWriter println(short value) {
        return print(value).line();
    }

    /** Prints the char value followed by a new line. */
    public TextWriter println(char value) {
        return print(value).line();
    }

    /** Prints the int value followed by a new line. */
    public TextWriter println(int value) {
        return print(value).line();
    }

    /** Prints the long value followed by a new line. */
    public TextWriter println(long value) {
        return print(value).line();
    }

    /** Prints the float value followed by a new line. */
    public TextWriter println(float value) {
        return print(value).line();
    }

    /** Prints the double value followed by a new line. */
    public TextWriter println(double value) {
        return print(value).line();
    }

    /** Prints the string followed by a new line. */
    public TextWriter println(String value) {
        return print(value).line();
    }

    /** Prints the boolean value. */
    public TextWriter print(boolean value) {
        return print(String.valueOf(value));
    }

    /** Prints the byte value. */
    public TextWriter print(byte value) {
        return print(String.valueOf(value));
    }

    /** Prints the short value. */
    public TextWriter print(short value) {
        return print(String.valueOf(value));
    }

    /** Prints the char value. */
    public TextWriter print(char value) {
        return print(String.valueOf(value));
    }

    /** Prints the int value. */
    public TextWriter print(int value) {
        return print(String.valueOf(value));
    }

    /** Prints the long value. */
    public TextWriter print(long value) {
        return print(String.valueOf(value));
    }

    /** Prints the float value. */
    public TextWriter print(float value) {
        return print(String.valueOf(value));
    }

    /** Prints the long value. */
    public TextWriter print(double value) {
        return print(String.valueOf(value));
    }

    /** Prints the string. */
    public TextWriter print(String value) {
        if (align) for (int i = 0; i < level; i++) buffer.append(step);
        if (space) buffer.append(' ');
        buffer.append(value);
        align = false;
        space = false;
        line = false;
        return this;
    }

    //########################################################################
    // Public Methods - Formating

    /** Returns the indentation width. */
    public int getIndentWidth() {
        return step == null ? -1 : step.length();
    }

    /** Returns the indentation level. */
    public int getIndentLevel() {
        return level;
    }

    /** Sets the indentation level. */
    public TextWriter setIndentLevel(int level) {
        this.level = level;
        return this;
    }

    /** Increases the indentation level by one. */
    public TextWriter indent() {
        level++;
        return this;
    }

    /** Decreases the indentation level by one. */
    public TextWriter undent() {
        level--;
        return this;
    }

    /** Starts a new line. */
    public TextWriter line() {
        if (step == null) return space();
        if (line) return this;
        buffer.append(LINE);
        line = align;
        align = true;
        space = false;
        return this;
    }

    /** Inserts a white space. */
    public TextWriter space() {
        space = !align;
        return this;
    }

    //########################################################################
    // Public Methods - Accessing

    /** Returns the underlying StringBuffer. */
    public StringBuffer getBuffer() {
        return buffer;
    }

    /** Returns the generated string. */
    public String toString() {
        return buffer.toString();
    }

    //########################################################################
}