1 57 58 59 64 65 package com.sun.org.apache.xml.internal.serialize; 66 67 68 import java.io.Writer ; 69 import java.io.StringWriter ; 70 import java.io.IOException ; 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 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 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 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 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 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 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 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 312 { 313 try { 314 if ( _pos == BufferSize ) { 315 _writer.write( _buffer ); 316 _pos = 0; 317 } 318 String line = _format.getLineSeparator(); 319 char [] chL = line.toCharArray(); 320 for(int i =0; i<chL.length;i++){ 321 _buffer[ _pos++] = chL[i]; 322 } 323 } catch ( IOException except ) { 326 if ( _exception == null ) 329 _exception = except; 330 throw except; 331 } 332 } 333 334 335 public void breakLine( boolean preserveSpace ) 336 throws IOException 337 { 338 breakLine(); 339 } 340 341 342 public void flushLine( boolean preserveSpace ) 343 throws IOException 344 { 345 try { 347 _writer.write( _buffer, 0, _pos ); 348 } catch ( IOException except ) { 349 if ( _exception == null ) 352 _exception = except; 353 } 354 _pos = 0; 355 } 356 357 358 362 public void flush() 363 throws IOException 364 { 365 try { 366 _writer.write( _buffer, 0, _pos ); 367 _writer.flush(); 368 } catch ( IOException except ) { 369 if ( _exception == null ) 372 _exception = except; 373 throw except; 374 } 375 _pos = 0; 376 } 377 378 379 public void indent() 380 { 381 } 383 384 385 public void unindent() 386 { 387 } 389 390 391 public int getNextIndent() 392 { 393 return 0; 394 } 395 396 397 public void setNextIndent( int indent ) 398 { 399 } 400 401 402 public void setThisIndent( int indent ) 403 { 404 } 405 406 407 } 408 | Popular Tags |