1 package com4j.tlbimp; 2 3 import java.io.IOException ; 4 import java.io.OutputStream ; 5 import java.io.PrintWriter ; 6 import java.io.Writer ; 7 import java.io.FilterWriter ; 8 import java.util.Stack ; 9 10 23 public class IndentingWriter extends PrintWriter { 24 private int indent=0; 25 private boolean newLine=true; 26 27 public IndentingWriter(Writer out) { 28 super(new CancellableWriter(out)); 29 } 30 31 public IndentingWriter(Writer out, boolean autoFlush) { 32 super(new CancellableWriter(out), autoFlush); 33 } 34 35 private CancellableWriter getOut() { 36 return (CancellableWriter)out; 37 } 38 39 40 public void startBuffering() { 46 try { 47 getOut().mark(); 48 } catch (IOException e) { 49 } 50 } 51 52 public void cancel() { 53 getOut().cancel(); 54 } 55 56 public void commit() { 57 try { 58 getOut().commit(); 59 } catch (IOException e) { 60 } 61 } 62 63 71 public void in() { 72 indent++; 73 } 74 75 78 public void out() { 79 indent--; 80 } 81 82 private void printIndent() { 83 try { 84 for( int i=0; i<indent; i++ ) 85 out.write(" "); 86 } catch( IOException e ) { 87 } 88 } 89 90 private void checkIndent() { 91 if(newLine) 92 printIndent(); 93 newLine = false; 94 } 95 96 97 105 private boolean needsComma; 106 107 private final Stack <Boolean > commaStack = new Stack <Boolean >(); 108 109 112 public void beginCommaMode() { 113 commaStack.push(needsComma); 114 needsComma = false; 115 } 116 117 120 public void endCommaMode() { 121 needsComma |= (boolean)commaStack.pop(); 122 } 123 124 128 public void comma() { 129 if(needsComma) { 130 print(','); 131 needsComma = false; 132 } 133 } 134 135 136 137 public void println() { 143 super.println(); 144 newLine = true; 145 } 146 147 public void write(int c) { 148 checkIndent(); 149 needsComma = true; 150 super.write(c); 151 } 152 153 public void write(char buf[], int off, int len) { 154 checkIndent(); 155 needsComma = true; 156 super.write(buf, off, len); 157 } 158 159 public void write(String s, int off, int len) { 160 checkIndent(); 161 needsComma = true; 162 super.write(s, off, len); 163 } 164 } 165 166 class CancellableWriter extends FilterWriter { 167 170 private final StringBuffer buffer = new StringBuffer (); 171 172 private boolean marked; 173 174 178 public void mark() throws IOException { 179 if(marked) commit(); 180 marked = true; 181 } 182 183 186 public void cancel() { 187 if(!marked) throw new IllegalStateException (); 188 marked = false; 189 buffer.setLength(0); 190 } 191 192 195 public void commit() throws IOException { 196 if(!marked) throw new IllegalStateException (); 197 marked = false; 198 super.append(buffer); 199 buffer.setLength(0); 200 } 201 202 203 public CancellableWriter(Writer out) { 204 super(out); 205 } 206 207 public void write(int c) throws IOException { 208 if(marked) 209 buffer.append( (char)c ); 210 else 211 super.write(c); 212 } 213 214 public void write(char[] cbuf, int off, int len) throws IOException { 215 if(marked) 216 buffer.append(cbuf,off,len); 217 else 218 super.write(cbuf, off, len); 219 } 220 221 public void write(String str, int off, int len) throws IOException { 222 if(marked) 223 buffer.append(str,off,len); 224 else 225 super.write(str, off, len); 226 } 227 228 public void flush() throws IOException { 229 super.flush(); 230 } 231 232 public void close() throws IOException { 233 if(marked) 234 commit(); 235 super.close(); 236 } 237 } | Popular Tags |