1 57 58 59 package com.sun.org.apache.xml.internal.serialize; 60 61 62 import java.io.Writer ; 63 import java.io.StringWriter ; 64 import java.io.IOException ; 65 66 67 74 public class IndentPrinter 75 extends Printer 76 { 77 78 79 83 private StringBuffer _line; 84 85 86 91 private StringBuffer _text; 92 93 94 99 private int _spaces; 100 101 102 106 private int _thisIndent; 107 108 109 113 private int _nextIndent; 114 115 116 public IndentPrinter( Writer writer, OutputFormat format) 117 { 118 super( writer, format ); 119 _line = new StringBuffer ( 80 ); 121 _text = new StringBuffer ( 20 ); 122 _spaces = 0; 123 _thisIndent = _nextIndent = 0; 124 } 125 126 127 135 public void enterDTD() 136 { 137 if ( _dtdWriter == null ) { 140 _line.append( _text ); 141 _text = new StringBuffer ( 20 ); 142 flushLine( false ); 143 _dtdWriter = new StringWriter (); 144 _docWriter = _writer; 145 _writer = _dtdWriter; 146 } 147 } 148 149 150 155 public String leaveDTD() 156 { 157 if ( _writer == _dtdWriter ) { 159 _line.append( _text ); 160 _text = new StringBuffer ( 20 ); 161 flushLine( false ); 162 _writer = _docWriter; 163 return _dtdWriter.toString(); 164 } else 165 return null; 166 } 167 168 169 178 public void printText( String text ) 179 { 180 _text.append( text ); 181 } 182 183 184 public void printText( StringBuffer text ) 185 { 186 _text.append( text.toString() ); 187 } 188 189 190 public void printText( char ch ) 191 { 192 _text.append( ch ); 193 } 194 195 196 public void printText( char[] chars, int start, int length ) 197 { 198 _text.append( chars, start, length ); 199 } 200 201 202 210 public void printSpace() 211 { 212 223 if ( _text.length() > 0 ) { 227 if ( _format.getLineWidth() > 0 && 232 _thisIndent + _line.length() + _spaces + _text.length() > _format.getLineWidth() ) { 233 flushLine( false ); 234 try { 235 _writer.write( _format.getLineSeparator() ); 237 } catch ( IOException except ) { 238 if ( _exception == null ) 241 _exception = except; 242 } 243 } 244 245 while ( _spaces > 0 ) { 248 _line.append( ' ' ); 249 --_spaces; 250 } 251 _line.append( _text ); 252 _text = new StringBuffer ( 20 ); 253 } 254 ++_spaces; 257 } 258 259 260 267 public void breakLine() 268 { 269 breakLine( false ); 270 } 271 272 273 public void breakLine( boolean preserveSpace ) 274 { 275 if ( _text.length() > 0 ) { 277 while ( _spaces > 0 ) { 278 _line.append( ' ' ); 279 --_spaces; 280 } 281 _line.append( _text ); 282 _text = new StringBuffer ( 20 ); 283 } 284 flushLine( preserveSpace ); 285 try { 286 _writer.write( _format.getLineSeparator() ); 288 } catch ( IOException except ) { 289 if ( _exception == null ) 292 _exception = except; 293 } 294 } 295 296 297 304 public void flushLine( boolean preserveSpace ) 305 { 306 int indent; 307 308 if ( _line.length() > 0 ) { 309 try { 310 311 if ( _format.getIndenting() && ! preserveSpace ) { 312 indent = _thisIndent; 314 if ( ( 2 * indent ) > _format.getLineWidth() && _format.getLineWidth() > 0 ) 315 indent = _format.getLineWidth() / 2; 316 while ( indent > 0 ) { 319 _writer.write( ' ' ); 320 --indent; 321 } 322 } 323 _thisIndent = _nextIndent; 324 325 _spaces = 0; 329 _writer.write( _line.toString() ); 330 331 _line = new StringBuffer ( 40 ); 332 } catch ( IOException except ) { 333 if ( _exception == null ) 336 _exception = except; 337 } 338 } 339 } 340 341 342 346 public void flush() 347 { 348 if ( _line.length() > 0 || _text.length() > 0 ) 349 breakLine(); 350 try { 351 _writer.flush(); 352 } catch ( IOException except ) { 353 if ( _exception == null ) 356 _exception = except; 357 } 358 } 359 360 361 364 public void indent() 365 { 366 _nextIndent += _format.getIndent(); 367 } 368 369 370 373 public void unindent() 374 { 375 _nextIndent -= _format.getIndent(); 376 if ( _nextIndent < 0 ) 377 _nextIndent = 0; 378 if ( ( _line.length() + _spaces + _text.length() ) == 0 ) 381 _thisIndent = _nextIndent; 382 } 383 384 385 public int getNextIndent() 386 { 387 return _nextIndent; 388 } 389 390 391 public void setNextIndent( int indent ) 392 { 393 _nextIndent = indent; 394 } 395 396 397 public void setThisIndent( int indent ) 398 { 399 _thisIndent = indent; 400 } 401 402 403 } 404 | Popular Tags |