1 17 18 22 23 package org.apache.geronimo.system.configuration; 24 25 import java.io.Writer ; 26 import java.io.StringWriter ; 27 import java.io.IOException ; 28 29 30 39 public class Printer 40 { 41 42 43 49 protected final OutputFormat format; 50 51 52 55 protected Writer writer; 56 57 58 64 protected StringWriter dtdWriter; 65 66 67 71 protected Writer docWriter; 72 73 74 78 protected IOException exception; 79 80 81 84 private static final int BufferSize = 4096; 85 86 87 90 private final char[] buffer = new char[ BufferSize ]; 91 92 93 96 private int pos = 0; 97 98 99 public Printer( Writer writer, OutputFormat format) 100 { 101 this.writer = writer; 102 this.format = format; 103 exception = null; 104 dtdWriter = null; 105 docWriter = null; 106 pos = 0; 107 } 108 109 110 public IOException getException() 111 { 112 return exception; 113 } 114 115 116 124 public void enterDTD() 125 throws IOException 126 { 127 if ( dtdWriter == null ) { 130 flushLine( false ); 131 132 dtdWriter = new StringWriter (); 133 docWriter = writer; 134 writer = dtdWriter; 135 } 136 } 137 138 139 144 public String leaveDTD() 145 throws IOException 146 { 147 if ( writer == dtdWriter ) { 149 flushLine( false ); 150 151 writer = docWriter; 152 return dtdWriter.toString(); 153 } else 154 return null; 155 } 156 157 158 public void printText( String text ) 159 throws IOException 160 { 161 try { 162 int length = text.length(); 163 for ( int i = 0 ; i < length ; ++i ) { 164 if ( pos == BufferSize ) { 165 writer.write( buffer ); 166 pos = 0; 167 } 168 buffer[ pos ] = text.charAt( i ); 169 ++pos; 170 } 171 } catch ( IOException except ) { 172 if ( exception == null ) 175 exception = except; 176 throw except; 177 } 178 } 179 180 181 public void printText( StringBuffer text ) 182 throws IOException 183 { 184 try { 185 int length = text.length(); 186 for ( int i = 0 ; i < length ; ++i ) { 187 if ( pos == BufferSize ) { 188 writer.write( buffer ); 189 pos = 0; 190 } 191 buffer[ pos ] = text.charAt( i ); 192 ++pos; 193 } 194 } catch ( IOException except ) { 195 if ( exception == null ) 198 exception = except; 199 throw except; 200 } 201 } 202 203 204 public void printText( char[] chars, int start, int length ) 205 throws IOException 206 { 207 try { 208 while ( length-- > 0 ) { 209 if ( pos == BufferSize ) { 210 writer.write( buffer ); 211 pos = 0; 212 } 213 buffer[ pos ] = chars[ start ]; 214 ++start; 215 ++pos; 216 } 217 } catch ( IOException except ) { 218 if ( exception == null ) 221 exception = except; 222 throw except; 223 } 224 } 225 226 227 public void printText( char ch ) 228 throws IOException 229 { 230 try { 231 if ( pos == BufferSize ) { 232 writer.write( buffer ); 233 pos = 0; 234 } 235 buffer[ pos ] = ch; 236 ++pos; 237 } catch ( IOException except ) { 238 if ( exception == null ) 241 exception = except; 242 throw except; 243 } 244 } 245 246 247 public void printSpace() 248 throws IOException 249 { 250 try { 251 if ( pos == BufferSize ) { 252 writer.write( buffer ); 253 pos = 0; 254 } 255 buffer[ pos ] = ' '; 256 ++pos; 257 } catch ( IOException except ) { 258 if ( exception == null ) 261 exception = except; 262 throw except; 263 } 264 } 265 266 267 public void breakLine() 268 throws IOException 269 { 270 try { 271 if ( pos == BufferSize ) { 272 writer.write( buffer ); 273 pos = 0; 274 } 275 buffer[ pos ] = '\n'; 276 ++pos; 277 } catch ( IOException except ) { 278 if ( exception == null ) 281 exception = except; 282 throw except; 283 } 284 } 285 286 287 public void breakLine( boolean preserveSpace ) 288 throws IOException 289 { 290 breakLine(); 291 } 292 293 294 public void flushLine( boolean preserveSpace ) 295 throws IOException 296 { 297 try { 299 writer.write( buffer, 0, pos ); 300 } catch ( IOException except ) { 301 if ( exception == null ) 304 exception = except; 305 } 306 pos = 0; 307 } 308 309 310 314 public void flush() 315 throws IOException 316 { 317 try { 318 writer.write( buffer, 0, pos ); 319 writer.flush(); 320 } catch ( IOException except ) { 321 if ( exception == null ) 324 exception = except; 325 throw except; 326 } 327 pos = 0; 328 } 329 330 331 public void indent() 332 { 333 } 335 336 337 public void unindent() 338 { 339 } 341 342 343 public int getNextIndent() 344 { 345 return 0; 346 } 347 348 349 public void setNextIndent( int indent ) 350 { 351 } 352 353 354 public void setThisIndent( int indent ) 355 { 356 } 357 358 359 } 360 | Popular Tags |