1 57 58 59 64 65 package org.enhydra.apache.xml.serialize; 66 67 68 import java.io.IOException ; 69 import java.io.StringWriter ; 70 import java.io.Writer ; 71 72 73 82 public class Printer 83 { 84 85 86 92 protected final OutputFormat _format; 93 94 95 98 protected Writer _writer; 99 100 101 107 protected StringWriter _dtdWriter; 108 109 110 114 protected Writer _docWriter; 115 116 117 121 protected IOException _exception; 122 123 124 127 private static final int BufferSize = 4096; 128 129 130 133 private final char[] _buffer = new char[ BufferSize ]; 134 135 136 139 private int _pos = 0; 140 141 142 public Printer( Writer writer, OutputFormat format) 143 { 144 _writer = writer; 145 _format = format; 146 _exception = null; 147 _dtdWriter = null; 148 _docWriter = null; 149 _pos = 0; 150 } 151 152 153 public IOException getException() 154 { 155 return _exception; 156 } 157 158 159 167 public void enterDTD() 168 throws IOException ![JavaDoc](../../../../../../cmn/javadoc.gif) 169 { 170 if ( _dtdWriter == null ) { 173 flushLine( false ); 174 175 _dtdWriter = new StringWriter (); 176 _docWriter = _writer; 177 _writer = _dtdWriter; 178 } 179 } 180 181 182 187 public String leaveDTD() 188 throws IOException ![JavaDoc](../../../../../../cmn/javadoc.gif) 189 { 190 if ( _writer == _dtdWriter ) { 192 flushLine( false ); 193 194 _writer = _docWriter; 195 return _dtdWriter.toString(); 196 } else 197 return null; 198 } 199 200 201 public void printText( String text ) 202 throws IOException ![JavaDoc](../../../../../../cmn/javadoc.gif) 203 { 204 try { 205 int length = text.length(); 206 for ( int i = 0 ; i < length ; ++i ) { 207 if ( _pos == BufferSize ) { 208 _writer.write( _buffer ); 209 _pos = 0; 210 } 211 _buffer[ _pos ] = text.charAt( i ); 212 ++_pos; 213 } 214 } catch ( IOException except ) { 215 if ( _exception == null ) 218 _exception = except; 219 throw except; 220 } 221 } 222 223 224 public void printText( StringBuffer text ) 225 throws IOException ![JavaDoc](../../../../../../cmn/javadoc.gif) 226 { 227 try { 228 int length = text.length(); 229 for ( int i = 0 ; i < length ; ++i ) { 230 if ( _pos == BufferSize ) { 231 _writer.write( _buffer ); 232 _pos = 0; 233 } 234 _buffer[ _pos ] = text.charAt( i ); 235 ++_pos; 236 } 237 } catch ( IOException except ) { 238 if ( _exception == null ) 241 _exception = except; 242 throw except; 243 } 244 } 245 246 247 public void printText( char[] chars, int start, int length ) 248 throws IOException ![JavaDoc](../../../../../../cmn/javadoc.gif) 249 { 250 try { 251 while ( length-- > 0 ) { 252 if ( _pos == BufferSize ) { 253 _writer.write( _buffer ); 254 _pos = 0; 255 } 256 _buffer[ _pos ] = chars[ start ]; 257 ++start; 258 ++_pos; 259 } 260 } catch ( IOException except ) { 261 if ( _exception == null ) 264 _exception = except; 265 throw except; 266 } 267 } 268 269 270 public void printText( char ch ) 271 throws IOException ![JavaDoc](../../../../../../cmn/javadoc.gif) 272 { 273 try { 274 if ( _pos == BufferSize ) { 275 _writer.write( _buffer ); 276 _pos = 0; 277 } 278 _buffer[ _pos ] = ch; 279 ++_pos; 280 } catch ( IOException except ) { 281 if ( _exception == null ) 284 _exception = except; 285 throw except; 286 } 287 } 288 289 290 public void printSpace() 291 throws IOException ![JavaDoc](../../../../../../cmn/javadoc.gif) 292 { 293 try { 294 if ( _pos == BufferSize ) { 295 _writer.write( _buffer ); 296 _pos = 0; 297 } 298 _buffer[ _pos ] = ' '; 299 ++_pos; 300 } catch ( IOException except ) { 301 if ( _exception == null ) 304 _exception = except; 305 throw except; 306 } 307 } 308 309 310 public void breakLine() 311 throws IOException ![JavaDoc](../../../../../../cmn/javadoc.gif) 312 { 313 try { 314 if ( _pos == BufferSize ) { 315 _writer.write( _buffer ); 316 _pos = 0; 317 } 318 _buffer[ _pos ] = '\n'; 319 ++_pos; 320 } catch ( IOException except ) { 321 if ( _exception == null ) 324 _exception = except; 325 throw except; 326 } 327 } 328 329 330 public void breakLine( boolean preserveSpace ) 331 throws IOException ![JavaDoc](../../../../../../cmn/javadoc.gif) 332 { 333 breakLine(); 334 } 335 336 337 public void flushLine( boolean preserveSpace ) 338 throws IOException ![JavaDoc](../../../../../../cmn/javadoc.gif) 339 { 340 try { 342 _writer.write( _buffer, 0, _pos ); 343 } catch ( IOException except ) { 344 if ( _exception == null ) 347 _exception = except; 348 } 349 _pos = 0; 350 } 351 352 353 357 public void flush() 358 throws IOException ![JavaDoc](../../../../../../cmn/javadoc.gif) 359 { 360 try { 361 _writer.write( _buffer, 0, _pos ); 362 _writer.flush(); 363 } catch ( IOException except ) { 364 if ( _exception == null ) 367 _exception = except; 368 throw except; 369 } 370 _pos = 0; 371 } 372 373 374 public void indent() 375 { 376 } 378 379 380 public void unindent() 381 { 382 } 384 385 386 public int getNextIndent() 387 { 388 return 0; 389 } 390 391 392 public void setNextIndent( int indent ) 393 { 394 } 395 396 397 public void setThisIndent( int indent ) 398 { 399 } 400 401 402 } 403 | Popular Tags |