1 package org.apache.beehive.controls.runtime.generator; 2 19 20 import java.io.IOException ; 21 import java.io.Writer ; 22 23 26 public class IndentingWriter extends Writer 27 { 28 41 protected int depth = 0; 42 43 public IndentingWriter(Writer delegate) 44 { 45 this(delegate, 46 Integer.getInteger("org.apache.beehive.controls.runtime.generator.indentLevel", 4).intValue()); 47 } 48 49 public IndentingWriter(Writer delegate, int indentLevel) 50 { 51 _out = delegate; 52 this._indentLevel = indentLevel; 53 } 54 55 public void write(char cbuf[], int off, int len) throws IOException 56 { 57 if (off < 0 || off + len > cbuf.length) 58 throw new ArrayIndexOutOfBoundsException (); 59 60 for (int i = off; i < off + len; i++) 61 { 62 char c = cbuf[i]; 63 if (c == '}') 64 { 65 decrDepth(); 66 } 67 else if ((c == ' ' || c == '\t') && _needIndent) 68 { 69 continue; 70 } 71 72 if (_needIndent) 73 indent(); 74 _out.write(c); 75 76 if (c == '\n') 77 { 78 _needIndent = true; 79 } 80 else if (c == '{') 81 { 82 incrDepth(); 83 } 84 } 85 } 86 87 public void flush() throws IOException 88 { 89 _out.flush(); 90 } 91 92 public void close() throws IOException 93 { 94 _out.close(); 95 } 96 97 private void indent() throws IOException 98 { 99 for (int i = 0; i < depth; i++) 100 { 101 _out.write(' '); 102 } 103 _needIndent = false; 104 } 105 106 private void incrDepth() 107 { 108 depth += _indentLevel; 109 } 110 111 private void decrDepth() 112 { 113 depth -= _indentLevel; 114 if (depth < 0) 115 depth = 0; 116 } 117 118 protected Writer _out; 119 protected int _indentLevel; 120 private boolean _needIndent = false; 121 } 122 | Popular Tags |