| 1 | |
package org.webslinger.io; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.FilterWriter; |
| 5 | |
import java.io.Writer; |
| 6 | |
|
| 7 | |
public class IndentingWriter extends FilterWriter { |
| 8 | 14 | protected final StringBuilder indent = new StringBuilder(); |
| 9 | |
protected final boolean doSpace; |
| 10 | |
protected final boolean doNewline; |
| 11 | |
protected boolean lastWasNewline; |
| 12 | |
|
| 13 | |
public IndentingWriter(Writer out, boolean doSpace, boolean doNewline) { |
| 14 | 14 | super(out); |
| 15 | 14 | this.doSpace = doSpace; |
| 16 | 14 | this.doNewline = doNewline; |
| 17 | 14 | } |
| 18 | |
|
| 19 | |
public IndentingWriter(Writer out) { |
| 20 | 13 | this(out, true, true); |
| 21 | 13 | } |
| 22 | |
|
| 23 | |
public IndentingWriter newline() throws IOException { |
| 24 | 13 | lastWasNewline = true; |
| 25 | 13 | if (doNewline) super.write('\n'); |
| 26 | 13 | return this; |
| 27 | |
} |
| 28 | |
|
| 29 | |
protected void checkAfterNewline() throws IOException { |
| 30 | 166 | if (lastWasNewline) { |
| 31 | 13 | if (doSpace) { |
| 32 | 13 | if (doNewline) { |
| 33 | 8 | super.write(indent.toString(), 0, indent.length()); |
| 34 | |
} else { |
| 35 | 5 | super.write(' '); |
| 36 | |
} |
| 37 | |
} |
| 38 | 13 | lastWasNewline = false; |
| 39 | |
} |
| 40 | 166 | } |
| 41 | |
|
| 42 | |
public IndentingWriter push() { |
| 43 | 4 | indent.append(' '); |
| 44 | 4 | return this; |
| 45 | |
} |
| 46 | |
|
| 47 | |
public IndentingWriter pop() { |
| 48 | 4 | indent.setLength(indent.length() - 1); |
| 49 | 4 | return this; |
| 50 | |
} |
| 51 | |
|
| 52 | |
public IndentingWriter space() throws IOException { |
| 53 | 1 | checkAfterNewline(); |
| 54 | 1 | if (doSpace) super.write(' '); |
| 55 | 1 | return this; |
| 56 | |
} |
| 57 | |
|
| 58 | |
public void write(char[] buf) throws IOException { |
| 59 | 0 | write(buf, 0, buf.length); |
| 60 | 0 | } |
| 61 | |
|
| 62 | |
public void write(char[] buf, int offset, int length) throws IOException { |
| 63 | |
int i; |
| 64 | 0 | for (i = offset; i < length; i++) { |
| 65 | 0 | if (buf[i] == '\n') { |
| 66 | 0 | checkAfterNewline(); |
| 67 | 0 | super.write(buf, offset, i - offset + 1); |
| 68 | 0 | offset = i + 1; |
| 69 | 0 | lastWasNewline = true; |
| 70 | |
} |
| 71 | |
} |
| 72 | 0 | checkAfterNewline(); |
| 73 | 0 | super.write(buf, offset, i - offset); |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
public void write(int c) throws IOException { |
| 77 | 142 | if (c == '\n') { |
| 78 | 0 | lastWasNewline =true; |
| 79 | |
} else { |
| 80 | 142 | checkAfterNewline(); |
| 81 | |
} |
| 82 | 142 | super.write(c); |
| 83 | 142 | } |
| 84 | |
|
| 85 | |
public void write(String s) throws IOException { |
| 86 | 23 | write(s, 0, s.length()); |
| 87 | 23 | } |
| 88 | |
|
| 89 | |
public void write(String s, int offset, int length) throws IOException { |
| 90 | |
int i; |
| 91 | 90 | for (i = offset; i < length; i++) { |
| 92 | 67 | if (s.charAt(i) == '\n') { |
| 93 | 0 | checkAfterNewline(); |
| 94 | 0 | super.write(s, offset, i - offset + 1); |
| 95 | 0 | offset = i + 1; |
| 96 | 0 | lastWasNewline = true; |
| 97 | |
} |
| 98 | |
} |
| 99 | 23 | checkAfterNewline(); |
| 100 | 23 | super.write(s, offset, i - offset); |
| 101 | 23 | } |
| 102 | |
} |