1 16 17 18 23 24 package org.apache.xml.serialize; 25 26 27 import java.io.Writer ; 28 import java.io.StringWriter ; 29 import java.io.IOException ; 30 31 32 41 public class Printer 42 { 43 44 45 51 protected final OutputFormat _format; 52 53 54 57 protected Writer _writer; 58 59 60 66 protected StringWriter _dtdWriter; 67 68 69 73 protected Writer _docWriter; 74 75 76 80 protected IOException _exception; 81 82 83 86 private static final int BufferSize = 4096; 87 88 89 92 private final char[] _buffer = new char[ BufferSize ]; 93 94 95 98 private int _pos = 0; 99 100 101 public Printer( Writer writer, OutputFormat format) 102 { 103 _writer = writer; 104 _format = format; 105 _exception = null; 106 _dtdWriter = null; 107 _docWriter = null; 108 _pos = 0; 109 } 110 111 112 public IOException getException() 113 { 114 return _exception; 115 } 116 117 118 126 public void enterDTD() 127 throws IOException 128 { 129 if ( _dtdWriter == null ) { 132 flushLine( false ); 133 134 _dtdWriter = new StringWriter (); 135 _docWriter = _writer; 136 _writer = _dtdWriter; 137 } 138 } 139 140 141 146 public String leaveDTD() 147 throws IOException 148 { 149 if ( _writer == _dtdWriter ) { 151 flushLine( false ); 152 153 _writer = _docWriter; 154 return _dtdWriter.toString(); 155 } else 156 return null; 157 } 158 159 160 public void printText( String text ) 161 throws IOException 162 { 163 try { 164 int length = text.length(); 165 for ( int i = 0 ; i < length ; ++i ) { 166 if ( _pos == BufferSize ) { 167 _writer.write( _buffer ); 168 _pos = 0; 169 } 170 _buffer[ _pos ] = text.charAt( i ); 171 ++_pos; 172 } 173 } catch ( IOException except ) { 174 if ( _exception == null ) 177 _exception = except; 178 throw except; 179 } 180 } 181 182 183 public void printText( StringBuffer text ) 184 throws IOException 185 { 186 try { 187 int length = text.length(); 188 for ( int i = 0 ; i < length ; ++i ) { 189 if ( _pos == BufferSize ) { 190 _writer.write( _buffer ); 191 _pos = 0; 192 } 193 _buffer[ _pos ] = text.charAt( i ); 194 ++_pos; 195 } 196 } catch ( IOException except ) { 197 if ( _exception == null ) 200 _exception = except; 201 throw except; 202 } 203 } 204 205 206 public void printText( char[] chars, int start, int length ) 207 throws IOException 208 { 209 try { 210 while ( length-- > 0 ) { 211 if ( _pos == BufferSize ) { 212 _writer.write( _buffer ); 213 _pos = 0; 214 } 215 _buffer[ _pos ] = chars[ start ]; 216 ++start; 217 ++_pos; 218 } 219 } catch ( IOException except ) { 220 if ( _exception == null ) 223 _exception = except; 224 throw except; 225 } 226 } 227 228 229 public void printText( char ch ) 230 throws IOException 231 { 232 try { 233 if ( _pos == BufferSize ) { 234 _writer.write( _buffer ); 235 _pos = 0; 236 } 237 _buffer[ _pos ] = ch; 238 ++_pos; 239 } catch ( IOException except ) { 240 if ( _exception == null ) 243 _exception = except; 244 throw except; 245 } 246 } 247 248 249 public void printSpace() 250 throws IOException 251 { 252 try { 253 if ( _pos == BufferSize ) { 254 _writer.write( _buffer ); 255 _pos = 0; 256 } 257 _buffer[ _pos ] = ' '; 258 ++_pos; 259 } catch ( IOException except ) { 260 if ( _exception == null ) 263 _exception = except; 264 throw except; 265 } 266 } 267 268 269 public void breakLine() 270 throws IOException 271 { 272 try { 273 if ( _pos == BufferSize ) { 274 _writer.write( _buffer ); 275 _pos = 0; 276 } 277 _buffer[ _pos ] = '\n'; 278 ++_pos; 279 } catch ( IOException except ) { 280 if ( _exception == null ) 283 _exception = except; 284 throw except; 285 } 286 } 287 288 289 public void breakLine( boolean preserveSpace ) 290 throws IOException 291 { 292 breakLine(); 293 } 294 295 296 public void flushLine( boolean preserveSpace ) 297 throws IOException 298 { 299 try { 301 _writer.write( _buffer, 0, _pos ); 302 } catch ( IOException except ) { 303 if ( _exception == null ) 306 _exception = except; 307 } 308 _pos = 0; 309 } 310 311 312 316 public void flush() 317 throws IOException 318 { 319 try { 320 _writer.write( _buffer, 0, _pos ); 321 _writer.flush(); 322 } catch ( IOException except ) { 323 if ( _exception == null ) 326 _exception = except; 327 throw except; 328 } 329 _pos = 0; 330 } 331 332 333 public void indent() 334 { 335 } 337 338 339 public void unindent() 340 { 341 } 343 344 345 public int getNextIndent() 346 { 347 return 0; 348 } 349 350 351 public void setNextIndent( int indent ) 352 { 353 } 354 355 356 public void setThisIndent( int indent ) 357 { 358 } 359 360 361 } 362 | Popular Tags |