1 23 24 package org.enhydra.xml.xmlc.codegen; 25 26 import java.io.BufferedOutputStream ; 27 import java.io.File ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.OutputStreamWriter ; 31 import java.io.PrintWriter ; 32 import java.io.Writer ; 33 import java.util.ArrayList ; 34 35 import org.enhydra.xml.xmlc.XMLCError; 36 37 41 public class IndentWriter extends PrintWriter { 42 45 private boolean fAtBOLN = true; 46 47 50 private static int INDENT_SIZE = 4; 51 52 55 private int fIndent = 0; 56 57 60 private int fAdditionalIndent = 0; 61 62 66 private ArrayList fAdditionalIndentStack; 67 68 71 private boolean fZeroCheck = true; 72 73 76 private String fIndentPrefix = ""; 77 78 83 private static String fIndentSourceString 84 = " "; 85 86 89 public IndentWriter(Writer writer) { 90 super(writer); 91 } 92 93 96 public IndentWriter(Writer writer, 97 boolean autoflush) { 98 super(writer, autoflush); 99 } 100 101 105 public void setZeroCheck(boolean value) { 106 fZeroCheck = value; 107 } 108 109 112 public boolean getZeroCheck() { 113 return fZeroCheck; 114 } 115 116 119 private static Writer openFile(File src, String enc) throws IOException { 120 String parent = src.getParent(); 122 if (parent != null) { 123 new File (parent).mkdirs(); 124 } 125 if (enc == null) { 126 return new PrintWriter (new BufferedOutputStream (new FileOutputStream (src))); 127 } else { 128 return new PrintWriter (new OutputStreamWriter ( 129 new BufferedOutputStream (new FileOutputStream (src)), enc)); 130 } 131 132 133 } 134 135 139 public IndentWriter(File src) throws IOException { 140 super(openFile(src, null)); 141 } 142 143 147 public IndentWriter(File src, String enc) throws IOException { 148 super(openFile(src, enc)); 149 } 150 151 159 public void close(boolean ignoreErrors) { 160 if (ignoreErrors) { 161 try { 162 super.close(); 163 } catch (Throwable err) { 164 } 166 } else { 167 super.close(); 168 checkZero(); 169 } 170 } 171 172 175 public void flush() { 176 super.flush(); 177 checkZero(); 178 } 179 180 183 private static String getIndentString(int numChars) { 184 while (fIndentSourceString.length() < numChars) { 186 fIndentSourceString += fIndentSourceString; 187 } 188 return fIndentSourceString.substring(0, numChars); 189 } 190 191 194 private void setIndentPrefix() { 195 fIndentPrefix = getIndentString(fIndent); 196 } 197 198 201 private void checkZero() { 202 if (fZeroCheck && (fIndent != 0)) { 203 throw new XMLCError("code indentation not zero at end"); 205 } 206 } 207 208 211 public final void enter() { 212 fIndent += INDENT_SIZE; 213 setIndentPrefix(); 214 } 215 216 219 public final void leave() { 220 fIndent -= INDENT_SIZE; 221 if (fIndent < 0) { 222 throw new XMLCError("identation level underflow"); 223 } 224 setIndentPrefix(); 225 } 226 227 230 public final int getIndentLevel() { 231 return fIndent; 232 } 233 234 239 final public void pushAdditionalIndent(int numChars) { 240 if (fAdditionalIndentStack == null) { 241 fAdditionalIndentStack = new ArrayList (); 242 } 243 fAdditionalIndentStack.add(new Integer (numChars)); 244 fIndent += numChars; 245 setIndentPrefix(); 246 } 247 248 251 public final void popAdditionalIndent() { 252 if ((fAdditionalIndentStack == null) || (fAdditionalIndentStack.size() == 0)) { 253 throw new XMLCError("additional identation stack underflow"); 254 } 255 Integer numChars = (Integer )fAdditionalIndentStack.remove(fAdditionalIndentStack.size()-1); 256 fIndent -= numChars.intValue(); 257 if (fIndent < 0) { 258 throw new XMLCError("identation level underflow"); 259 } 260 setIndentPrefix(); 261 } 262 263 266 public boolean atBOLN() { 267 return fAtBOLN; 268 } 269 270 273 private void printIndent() { 274 super.print(fIndentPrefix); 275 fAtBOLN = false; 276 } 277 278 282 protected void printPrefix(String prefix) { 283 if (!fAtBOLN) { 284 println(); 285 } 286 super.print(prefix); 287 printIndent(); 288 } 289 290 294 public void print(boolean b) { 295 if (fAtBOLN) { 296 printIndent(); 297 } 298 super.print(b); 299 } 300 301 305 public void print(char c) { 306 if (fAtBOLN) { 307 printIndent(); 308 } 309 super.print(c); 310 } 311 312 316 public void print(long l) { 317 if (fAtBOLN) { 318 printIndent(); 319 } 320 super.print(l); 321 } 322 323 327 public void print(float f) { 328 if (fAtBOLN) { 329 printIndent(); 330 } 331 super.print(f); 332 } 333 334 338 public void print(double d) { 339 if (fAtBOLN) { 340 printIndent(); 341 } 342 super.print(d); 343 } 344 345 350 public void print(char[] str) { 351 int idx = 0; 352 int strlen = str.length; 353 int nlIdx; 354 355 while (idx < strlen) { 357 if (fAtBOLN) { 358 printIndent(); 359 } 360 for (nlIdx = idx; (nlIdx < strlen) && (str[nlIdx] != '\n'); nlIdx++) { 362 } 363 if (nlIdx < strlen) { 364 write(str, idx, nlIdx-idx); 365 println(); } else { 367 write(str, idx, strlen-idx); 368 } 369 idx = nlIdx+1; 370 fAtBOLN = true; } 372 } 373 374 379 public void print(String str) { 380 int idx = 0; 381 int strlen = str.length(); 382 int nlIdx; 383 384 while ((idx < strlen) && ((nlIdx = str.indexOf('\n', idx)) >= 0)) { 386 if (fAtBOLN) { 387 printIndent(); 388 } 389 write(str, idx, nlIdx-idx); 390 println(); idx = nlIdx+1; 392 } 393 394 if (idx < strlen) { 397 if (fAtBOLN) { 398 printIndent(); 399 } 400 write(str, idx, strlen-idx); 401 } 402 } 403 404 408 public void print(Object obj) { 409 if (fAtBOLN) { 410 printIndent(); 411 } 412 super.print(obj); 413 } 414 415 419 public void println() { 420 super.println(); 421 fAtBOLN = true; 422 } 423 424 429 public void println(String [] strs) { 430 for (int i = 0; i < strs.length; i++) { 431 println(strs[i]); 432 } 433 } 434 } 435 | Popular Tags |