1 16 17 18 package org.apache.xml.serialize; 19 20 21 import java.io.Writer ; 22 import java.io.StringWriter ; 23 import java.io.IOException ; 24 25 26 33 public class IndentPrinter 34 extends Printer 35 { 36 37 38 42 private StringBuffer _line; 43 44 45 50 private StringBuffer _text; 51 52 53 58 private int _spaces; 59 60 61 65 private int _thisIndent; 66 67 68 72 private int _nextIndent; 73 74 75 public IndentPrinter( Writer writer, OutputFormat format) 76 { 77 super( writer, format ); 78 _line = new StringBuffer ( 80 ); 80 _text = new StringBuffer ( 20 ); 81 _spaces = 0; 82 _thisIndent = _nextIndent = 0; 83 } 84 85 86 94 public void enterDTD() 95 { 96 if ( _dtdWriter == null ) { 99 _line.append( _text ); 100 _text = new StringBuffer ( 20 ); 101 flushLine( false ); 102 _dtdWriter = new StringWriter (); 103 _docWriter = _writer; 104 _writer = _dtdWriter; 105 } 106 } 107 108 109 114 public String leaveDTD() 115 { 116 if ( _writer == _dtdWriter ) { 118 _line.append( _text ); 119 _text = new StringBuffer ( 20 ); 120 flushLine( false ); 121 _writer = _docWriter; 122 return _dtdWriter.toString(); 123 } else 124 return null; 125 } 126 127 128 137 public void printText( String text ) 138 { 139 _text.append( text ); 140 } 141 142 143 public void printText( StringBuffer text ) 144 { 145 _text.append( text.toString() ); 146 } 147 148 149 public void printText( char ch ) 150 { 151 _text.append( ch ); 152 } 153 154 155 public void printText( char[] chars, int start, int length ) 156 { 157 _text.append( chars, start, length ); 158 } 159 160 161 169 public void printSpace() 170 { 171 182 if ( _text.length() > 0 ) { 186 if ( _format.getLineWidth() > 0 && 191 _thisIndent + _line.length() + _spaces + _text.length() > _format.getLineWidth() ) { 192 flushLine( false ); 193 try { 194 _writer.write( _format.getLineSeparator() ); 196 } catch ( IOException except ) { 197 if ( _exception == null ) 200 _exception = except; 201 } 202 } 203 204 while ( _spaces > 0 ) { 207 _line.append( ' ' ); 208 --_spaces; 209 } 210 _line.append( _text ); 211 _text = new StringBuffer ( 20 ); 212 } 213 ++_spaces; 216 } 217 218 219 226 public void breakLine() 227 { 228 breakLine( false ); 229 } 230 231 232 public void breakLine( boolean preserveSpace ) 233 { 234 if ( _text.length() > 0 ) { 236 while ( _spaces > 0 ) { 237 _line.append( ' ' ); 238 --_spaces; 239 } 240 _line.append( _text ); 241 _text = new StringBuffer ( 20 ); 242 } 243 flushLine( preserveSpace ); 244 try { 245 _writer.write( _format.getLineSeparator() ); 247 } catch ( IOException except ) { 248 if ( _exception == null ) 251 _exception = except; 252 } 253 } 254 255 256 263 public void flushLine( boolean preserveSpace ) 264 { 265 int indent; 266 267 if ( _line.length() > 0 ) { 268 try { 269 270 if ( _format.getIndenting() && ! preserveSpace ) { 271 indent = _thisIndent; 273 if ( ( 2 * indent ) > _format.getLineWidth() && _format.getLineWidth() > 0 ) 274 indent = _format.getLineWidth() / 2; 275 while ( indent > 0 ) { 278 _writer.write( ' ' ); 279 --indent; 280 } 281 } 282 _thisIndent = _nextIndent; 283 284 _spaces = 0; 288 _writer.write( _line.toString() ); 289 290 _line = new StringBuffer ( 40 ); 291 } catch ( IOException except ) { 292 if ( _exception == null ) 295 _exception = except; 296 } 297 } 298 } 299 300 301 305 public void flush() 306 { 307 if ( _line.length() > 0 || _text.length() > 0 ) 308 breakLine(); 309 try { 310 _writer.flush(); 311 } catch ( IOException except ) { 312 if ( _exception == null ) 315 _exception = except; 316 } 317 } 318 319 320 323 public void indent() 324 { 325 _nextIndent += _format.getIndent(); 326 } 327 328 329 332 public void unindent() 333 { 334 _nextIndent -= _format.getIndent(); 335 if ( _nextIndent < 0 ) 336 _nextIndent = 0; 337 if ( ( _line.length() + _spaces + _text.length() ) == 0 ) 340 _thisIndent = _nextIndent; 341 } 342 343 344 public int getNextIndent() 345 { 346 return _nextIndent; 347 } 348 349 350 public void setNextIndent( int indent ) 351 { 352 _nextIndent = indent; 353 } 354 355 356 public void setThisIndent( int indent ) 357 { 358 _thisIndent = indent; 359 } 360 361 362 } 363 | Popular Tags |