01: /*
02: * Janino - An embedded Java[TM] compiler
03: *
04: * Copyright (c) 2006, Arno Unkrig
05: * All rights reserved.
06: *
07: * Redistribution and use in source and binary forms, with or without
08: * modification, are permitted provided that the following conditions
09: * are met:
10: *
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above
14: * copyright notice, this list of conditions and the following
15: * disclaimer in the documentation and/or other materials
16: * provided with the distribution.
17: * 3. The name of the author may not be used to endorse or promote
18: * products derived from this software without specific prior
19: * written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32: */
33:
34: package org.codehaus.janino.util;
35:
36: import java.io.*;
37:
38: /**
39: * A {@link java.io.FilterWriter} that automatically indents lines by looking at
40: * trailing opening braces ('{') and leading closing braces ('}').
41: */
42: public class AutoIndentWriter extends FilterWriter {
43: private static final String LINE_SEPARATOR = System
44: .getProperty("line.separator");
45: private int previousChar = -1;
46: private int indentation = 0;
47: private String prefix = null;
48:
49: public AutoIndentWriter(Writer out) {
50: super (out);
51: }
52:
53: public void write(int c) throws IOException {
54: if (AutoIndentWriter.isLineSeparatorChar(c)) {
55: if (this .previousChar == '{')
56: this .indent();
57: } else if (AutoIndentWriter
58: .isLineSeparatorChar(this .previousChar)) {
59: if (c == '}')
60: this .unindent();
61: for (int i = 0; i < this .indentation; ++i)
62: this .out.write(" ");
63: if (this .prefix != null)
64: this .out.write(this .prefix);
65: }
66: super .write(c);
67: this .previousChar = c;
68: }
69:
70: public void unindent() {
71: --this .indentation;
72: }
73:
74: public void indent() {
75: ++this .indentation;
76: }
77:
78: /**
79: * The prefix, if non-null, is printed between the indentation space and
80: * the line data.
81: */
82: public void setPrefix(String prefix) {
83: this .prefix = prefix;
84: }
85:
86: public void write(char[] cbuf, int off, int len) throws IOException {
87: for (; len > 0; --len)
88: this .write(cbuf[off++]);
89: }
90:
91: public void write(String str, int off, int len) throws IOException {
92: for (; len > 0; --len)
93: this .write(str.charAt(off++));
94: }
95:
96: private static boolean isLineSeparatorChar(int c) {
97: return AutoIndentWriter.LINE_SEPARATOR.indexOf(c) != -1;
98: }
99: }
|