1 17 18 22 23 package org.apache.geronimo.system.configuration; 24 25 import java.io.Writer ; 26 import java.io.StringWriter ; 27 import java.io.IOException ; 28 29 30 37 public class IndentPrinter 38 extends Printer 39 { 40 41 42 46 private StringBuffer _line; 47 48 49 54 private StringBuffer _text; 55 56 57 62 private int _spaces; 63 64 65 69 private int _thisIndent; 70 71 72 76 private int _nextIndent; 77 78 79 IndentPrinter( Writer writer, OutputFormat format) 80 { 81 super( writer, format ); 82 _line = new StringBuffer ( 80 ); 84 _text = new StringBuffer ( 20 ); 85 _spaces = 0; 86 _thisIndent = _nextIndent = 0; 87 } 88 89 90 98 public void enterDTD() 99 { 100 if ( dtdWriter == null ) { 103 _line.append( _text ); 104 _text = new StringBuffer ( 20 ); 105 flushLine( false ); 106 dtdWriter = new StringWriter (); 107 docWriter = writer; 108 writer = dtdWriter; 109 } 110 } 111 112 113 118 public String leaveDTD() 119 { 120 if ( writer == dtdWriter ) { 122 _line.append( _text ); 123 _text = new StringBuffer ( 20 ); 124 flushLine( false ); 125 writer = docWriter; 126 return dtdWriter.toString(); 127 } else 128 return null; 129 } 130 131 132 141 public void printText( String text ) 142 { 143 _text.append( text ); 144 } 145 146 147 public void printText( StringBuffer text ) 148 { 149 _text.append( text ); 150 } 151 152 153 public void printText( char ch ) 154 { 155 _text.append( ch ); 156 } 157 158 159 public void printText( char[] chars, int start, int length ) 160 { 161 _text.append( chars, start, length ); 162 } 163 164 165 173 public void printSpace() 174 { 175 186 if ( _text.length() > 0 ) { 190 if ( format.getLineWidth() > 0 && 195 _thisIndent + _line.length() + _spaces + _text.length() > format.getLineWidth() ) { 196 flushLine( false ); 197 try { 198 writer.write( format.getLineSeparator() ); 200 } catch ( IOException except ) { 201 if ( exception == null ) 204 exception = except; 205 } 206 } 207 208 while ( _spaces > 0 ) { 211 _line.append( ' ' ); 212 --_spaces; 213 } 214 _line.append( _text ); 215 _text = new StringBuffer ( 20 ); 216 } 217 ++_spaces; 220 } 221 222 223 230 public void breakLine() 231 { 232 breakLine( false ); 233 } 234 235 236 public void breakLine( boolean preserveSpace ) 237 { 238 if ( _text.length() > 0 ) { 240 while ( _spaces > 0 ) { 241 _line.append( ' ' ); 242 --_spaces; 243 } 244 _line.append( _text ); 245 _text = new StringBuffer ( 20 ); 246 } 247 flushLine( preserveSpace ); 248 try { 249 writer.write( format.getLineSeparator() ); 251 } catch ( IOException except ) { 252 if ( exception == null ) 255 exception = except; 256 } 257 } 258 259 260 267 public void flushLine( boolean preserveSpace ) 268 { 269 int indent; 270 271 if ( _line.length() > 0 ) { 272 try { 273 274 if ( format.getIndenting() && ! preserveSpace ) { 275 indent = _thisIndent; 277 if ( ( 2 * indent ) > format.getLineWidth() && format.getLineWidth() > 0 ) 278 indent = format.getLineWidth() / 2; 279 while ( indent > 0 ) { 282 writer.write( ' ' ); 283 --indent; 284 } 285 } 286 _thisIndent = _nextIndent; 287 288 _spaces = 0; 292 writer.write( _line.toString() ); 293 294 _line = new StringBuffer ( 40 ); 295 } catch ( IOException except ) { 296 if ( exception == null ) 299 exception = except; 300 } 301 } 302 } 303 304 305 309 public void flush() 310 { 311 if ( _line.length() > 0 || _text.length() > 0 ) 312 breakLine(); 313 try { 314 writer.flush(); 315 } catch ( IOException except ) { 316 if ( exception == null ) 319 exception = except; 320 } 321 } 322 323 324 327 public void indent() 328 { 329 _nextIndent += format.getIndent(); 330 } 331 332 333 336 public void unindent() 337 { 338 _nextIndent -= format.getIndent(); 339 if ( _nextIndent < 0 ) 340 _nextIndent = 0; 341 if ( ( _line.length() + _spaces + _text.length() ) == 0 ) 344 _thisIndent = _nextIndent; 345 } 346 347 348 public int getNextIndent() 349 { 350 return _nextIndent; 351 } 352 353 354 public void setNextIndent( int indent ) 355 { 356 _nextIndent = indent; 357 } 358 359 360 public void setThisIndent( int indent ) 361 { 362 _thisIndent = indent; 363 } 364 365 366 } 367 | Popular Tags |