| 1 |
/* Formatter.java |
| 2 |
* |
| 3 |
* This is a modified version of Jocelyn Paine's Formatter package: |
| 4 |
* http://www.j-paine.org/Formatter |
| 5 |
* |
| 6 |
* Modifications are flagged with "kgs" in the comments. |
| 7 |
*/ |
| 8 |
|
| 9 |
package org.j_paine.formatter; |
| 10 |
|
| 11 |
import java.io.BufferedReader; |
| 12 |
import java.io.IOException; |
| 13 |
import java.io.PrintStream; |
| 14 |
import java.io.StringReader; |
| 15 |
import java.util.Hashtable; |
| 16 |
import java.util.Vector; |
| 17 |
|
| 18 |
|
| 19 |
/* This class holds a Format, and has methods for reading and |
| 20 |
writing data against it. |
| 21 |
*/ |
| 22 |
public class Formatter |
| 23 |
{ |
| 24 |
private Format format = null; |
| 25 |
private FormatMap format_map = null; |
| 26 |
|
| 27 |
|
| 28 |
public Formatter( String format ) throws InvalidFormatException |
| 29 |
{ |
| 30 |
this( new Format(format) ); |
| 31 |
} |
| 32 |
|
| 33 |
public Formatter( Format format ) |
| 34 |
{ |
| 35 |
this.format = format; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
public void setFormatMap( FormatMap format_map ) |
| 40 |
{ |
| 41 |
this.format_map = format_map; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
public void write( Vector v, PrintStream out ) |
| 46 |
throws OutputFormatException |
| 47 |
{ |
| 48 |
FormatX dummy_el = new FormatX(); |
| 49 |
FormatOutputList vp = new VectorAndPointer( v ); |
| 50 |
|
| 51 |
/* Loop back around and reuse the format spec if |
| 52 |
* there are still elements in the vector. Keep |
| 53 |
* going until all elements in the vector have |
| 54 |
* been printed. --kgs |
| 55 |
*/ |
| 56 |
while(true) { |
| 57 |
try { |
| 58 |
this.format.write( vp, out ); |
| 59 |
vp.checkCurrentElementForWrite(dummy_el); |
| 60 |
out.println(); |
| 61 |
}catch(EndOfVectorOnWriteException e) { |
| 62 |
break; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public void write( int i, PrintStream out ) |
| 68 |
throws OutputFormatException |
| 69 |
{ |
| 70 |
write( new Integer(i), out ); |
| 71 |
} |
| 72 |
|
| 73 |
public void write( long l, PrintStream out ) |
| 74 |
throws OutputFormatException |
| 75 |
{ |
| 76 |
write( new Long(l), out ); |
| 77 |
} |
| 78 |
|
| 79 |
public void write( float f, PrintStream out ) |
| 80 |
throws OutputFormatException |
| 81 |
{ |
| 82 |
write( new Float(f), out ); |
| 83 |
} |
| 84 |
|
| 85 |
public void write( double d, PrintStream out ) |
| 86 |
throws OutputFormatException |
| 87 |
{ |
| 88 |
write( new Double(d), out ); |
| 89 |
} |
| 90 |
|
| 91 |
public void write( Object o, PrintStream out ) |
| 92 |
throws OutputFormatException |
| 93 |
{ |
| 94 |
Vector v = new Vector(); |
| 95 |
v.addElement( o ); |
| 96 |
write( v, out ); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
public void read( Vector v, BufferedReader in ) |
| 101 |
throws InputFormatException |
| 102 |
{ |
| 103 |
FormatInputList vp = new VectorAndPointer( v ); |
| 104 |
InputStreamAndBuffer inb = new InputStreamAndBuffer(in); |
| 105 |
this.format.read( vp, inb, this.format_map ); |
| 106 |
} |
| 107 |
|
| 108 |
public void read( Vector v, Hashtable ht, BufferedReader in ) |
| 109 |
throws InputFormatException |
| 110 |
{ |
| 111 |
FormatInputList vp = new StringsHashtableAndPointer( v, ht ); |
| 112 |
InputStreamAndBuffer inb = new InputStreamAndBuffer(in); |
| 113 |
this.format.read( vp, inb, this.format_map ); |
| 114 |
} |
| 115 |
|
| 116 |
public void read( String[] s, Hashtable ht, BufferedReader in ) |
| 117 |
throws InputFormatException |
| 118 |
{ |
| 119 |
Vector v = new Vector(); |
| 120 |
for ( int i = 0; i<s.length; i++ ) |
| 121 |
v.addElement( s[i] ); |
| 122 |
read( v, ht, in ); |
| 123 |
} |
| 124 |
|
| 125 |
public Object read( BufferedReader in ) |
| 126 |
throws InputFormatException |
| 127 |
{ |
| 128 |
Vector v = new Vector(); |
| 129 |
read( v, in ); |
| 130 |
return v.elementAt(0); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
public boolean eof( BufferedReader in ) |
| 135 |
throws IOException |
| 136 |
{ |
| 137 |
return ( in.ready() ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
public String toString() |
| 142 |
{ |
| 143 |
return "[Formatter " + this.format.toString() + "]"; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
/* Below, we define various classes for holding complete formats, |
| 149 |
format elements, and so on. The class FormatUniv is a superclass |
| 150 |
of them all. This makes it a convenient "universal type" to |
| 151 |
use to hold any piece of, or a complete, format. |
| 152 |
*/ |
| 153 |
abstract class FormatUniv |
| 154 |
{ |
| 155 |
abstract void write( FormatOutputList vp, PrintStream out ) |
| 156 |
throws OutputFormatException; |
| 157 |
|
| 158 |
abstract void read( FormatInputList vp, |
| 159 |
InputStreamAndBuffer in, |
| 160 |
FormatMap format_map |
| 161 |
) |
| 162 |
throws InputFormatException; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
/* This class represents a complete format, i.e. a sequence of |
| 167 |
elements such as F12.5 and so on. Some of the elements may |
| 168 |
themselves be formats. |
| 169 |
We implement it as a vector of elements. |
| 170 |
*/ |
| 171 |
class Format extends FormatUniv |
| 172 |
{ |
| 173 |
private Vector elements = new Vector(); |
| 174 |
|
| 175 |
public Format( String s ) throws InvalidFormatException |
| 176 |
{ |
| 177 |
FormatParser fp = |
| 178 |
Parsers.theParsers().format_parser; |
| 179 |
// fp.ReInit( new StringBufferInputStream(s) ); |
| 180 |
fp.ReInit( new StringReader(s) ); |
| 181 |
try { |
| 182 |
Format f = fp.Format(); |
| 183 |
this.elements = f.elements; |
| 184 |
} |
| 185 |
catch ( ParseException e ) { |
| 186 |
throw new InvalidFormatException( e.getMessage() ); |
| 187 |
} |
| 188 |
catch ( TokenMgrError e ) { |
| 189 |
throw new InvalidFormatException( e.getMessage() ); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
// We call this one from inside the parser, which needs a Format |
| 194 |
// with its vector initialised. |
| 195 |
Format() |
| 196 |
{ |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
public void addElement( FormatUniv fu ) |
| 201 |
{ |
| 202 |
this.elements.addElement( fu ); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
public void write( FormatOutputList vp, PrintStream out ) |
| 207 |
throws OutputFormatException |
| 208 |
{ |
| 209 |
for ( int i=0; i<this.elements.size(); i++ ) { |
| 210 |
FormatUniv fu = (FormatUniv)this.elements.elementAt(i); |
| 211 |
fu.write( vp, out ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
public void read( FormatInputList vp, |
| 217 |
InputStreamAndBuffer in, |
| 218 |
FormatMap format_map |
| 219 |
) |
| 220 |
throws InputFormatException |
| 221 |
{ |
| 222 |
for ( int i=0; i<this.elements.size(); i++ ) { |
| 223 |
FormatUniv fu = (FormatUniv)this.elements.elementAt(i); |
| 224 |
fu.read( vp, in, format_map ); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
public String toString() |
| 230 |
{ |
| 231 |
String s = ""; |
| 232 |
for ( int i=0; i<this.elements.size(); i++ ) { |
| 233 |
if ( i!=0 ) |
| 234 |
s = s + ", "; |
| 235 |
s = s + this.elements.elementAt(i).toString(); |
| 236 |
} |
| 237 |
return s; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
/* This class represents a repeated item, e.g. 3F12.5 or 3X. |
| 243 |
The integer r gives the repetition factor. |
| 244 |
The item may be either a format element, or an entire format. |
| 245 |
To cater for either, we hold it in a FormatUniv object (this is |
| 246 |
why we introduced the class FormatUniv). |
| 247 |
*/ |
| 248 |
class FormatRepeatedItem extends FormatUniv |
| 249 |
{ |
| 250 |
private int r=1; |
| 251 |
private FormatUniv format_univ = null; |
| 252 |
|
| 253 |
|
| 254 |
public FormatRepeatedItem( FormatUniv format_univ ) |
| 255 |
{ |
| 256 |
this( 1, format_univ ); |
| 257 |
} |
| 258 |
|
| 259 |
public FormatRepeatedItem( int r, FormatUniv format_univ ) |
| 260 |
{ |
| 261 |
this.r = r; |
| 262 |
this.format_univ = format_univ; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
public void write( FormatOutputList vp, PrintStream out ) |
| 267 |
throws OutputFormatException |
| 268 |
{ |
| 269 |
for ( int i=1; i<=this.r; i++ ) |
| 270 |
this.format_univ.write( vp, out ); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
public void read( FormatInputList vp, |
| 275 |
InputStreamAndBuffer in, |
| 276 |
FormatMap format_map |
| 277 |
) |
| 278 |
throws InputFormatException |
| 279 |
{ |
| 280 |
for ( int i=1; i<=this.r; i++ ) |
| 281 |
this.format_univ.read( vp, in, format_map ); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
public String toString() |
| 286 |
{ |
| 287 |
if (r==1) |
| 288 |
return this.format_univ.toString(); |
| 289 |
else |
| 290 |
return this.r+"("+this.format_univ.toString()+")"; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
/* This class represents a single format element such as |
| 296 |
F12.5, I2, or X. |
| 297 |
*/ |
| 298 |
abstract class FormatElement extends FormatUniv |
| 299 |
{ |
| 300 |
/* This method will be defined differently by each subclass. |
| 301 |
*/ |
| 302 |
public abstract void write( FormatOutputList vp, PrintStream out ) |
| 303 |
throws OutputFormatException; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
/* This class represents a format element that reads or writes |
| 308 |
data. So F12.5 or I3, but not X. |
| 309 |
We assume that all format elements are fixed width. |
| 310 |
*/ |
| 311 |
abstract class FormatIOElement extends FormatElement |
| 312 |
{ |
| 313 |
private int width; |
| 314 |
|
| 315 |
void setWidth( int width ) |
| 316 |
{ |
| 317 |
this.width = width; |
| 318 |
} |
| 319 |
|
| 320 |
int getWidth() |
| 321 |
{ |
| 322 |
return this.width; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
public void write( FormatOutputList vp, PrintStream out ) |
| 327 |
throws OutputFormatException |
| 328 |
{ |
| 329 |
vp.checkCurrentElementForWrite( this ); |
| 330 |
Object o = vp.getCurrentElementAndAdvance(); |
| 331 |
out.print( convertToString(o,vp.getPtr()-1) ); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
/* This method is called by write, above. It will be |
| 336 |
defined differently for each subclass of FormatIOElement. |
| 337 |
The idea is that getting the next element to write from |
| 338 |
the output list, and printing its string representation, |
| 339 |
are the same for all FormatIOElements. However, the |
| 340 |
conversion to string is different for each one. |
| 341 |
*/ |
| 342 |
abstract String convertToString( Object o, int vecptr ) |
| 343 |
throws OutputFormatException; |
| 344 |
|
| 345 |
|
| 346 |
public void read( FormatInputList vp, |
| 347 |
InputStreamAndBuffer in, |
| 348 |
FormatMap format_map |
| 349 |
) |
| 350 |
throws InputFormatException |
| 351 |
{ |
| 352 |
/* Get next width characters. */ |
| 353 |
String s = in.getSlice( this.width, vp.getPtr(), this ); |
| 354 |
|
| 355 |
/* Try translating if there's a format map. */ |
| 356 |
if ( format_map != null ) { |
| 357 |
String repl = format_map.getMapping( s ); |
| 358 |
if ( repl != null ) |
| 359 |
s = repl; |
| 360 |
} |
| 361 |
|
| 362 |
/* Parse the string to check it's a valid number, and put into |
| 363 |
the vector if so. |
| 364 |
Also, advance the stream input pointer. |
| 365 |
*/ |
| 366 |
Object o = convertFromString( s, vp, in ); |
| 367 |
vp.checkCurrentElementForRead( this, in ); |
| 368 |
vp.putElementAndAdvance( o, this, in ); |
| 369 |
in.advance( this.width ); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
/* This method is called by read, above. It will be |
| 374 |
defined differently for each subclass of FormatIOElement. |
| 375 |
The idea is that getting the next element to read from |
| 376 |
the input stream, and putting it into the input list, |
| 377 |
are the same for all FormatIOElements. However, the |
| 378 |
conversion from string is different for each one. |
| 379 |
vp and in are used only in generating error messages. |
| 380 |
*/ |
| 381 |
abstract Object convertFromString( String s, |
| 382 |
FormatInputList vp, |
| 383 |
InputStreamAndBuffer in |
| 384 |
) |
| 385 |
throws InputFormatException; |
| 386 |
} |
| 387 |
|
| 388 |
/* This class represents a P format element, but the scaling |
| 389 |
* is not implemented yet. |
| 390 |
*/ |
| 391 |
class FormatP extends FormatElement |
| 392 |
{ |
| 393 |
FormatRepeatedItem ritem = null; |
| 394 |
|
| 395 |
public FormatRepeatedItem getRepeatedItem() { |
| 396 |
return ritem; |
| 397 |
} |
| 398 |
|
| 399 |
public FormatP(int r, FormatUniv format_univ) { |
| 400 |
if(format_univ != null) |
| 401 |
ritem = new FormatRepeatedItem(r, format_univ); |
| 402 |
} |
| 403 |
|
| 404 |
public void write( FormatOutputList vp, PrintStream out ) |
| 405 |
{ |
| 406 |
/* the P element itself produces no output. it's a scale factor |
| 407 |
* for other elements, but that isn't being handled yet. |
| 408 |
*/ |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
public void read( FormatInputList vp, |
| 413 |
InputStreamAndBuffer in, |
| 414 |
FormatMap format_map |
| 415 |
) |
| 416 |
{ |
| 417 |
/* the P element doesn't consume input. --kgs */ |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
public String toString() |
| 422 |
{ |
| 423 |
return "P"; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
/* This class represents an X format element. |
| 429 |
*/ |
| 430 |
class FormatX extends FormatElement |
| 431 |
{ |
| 432 |
public void write( FormatOutputList vp, PrintStream out ) |
| 433 |
{ |
| 434 |
out.print( " " ); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
public void read( FormatInputList vp, |
| 439 |
InputStreamAndBuffer in, |
| 440 |
FormatMap format_map |
| 441 |
) |
| 442 |
{ |
| 443 |
in.advance( 1 ); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
public String toString() |
| 448 |
{ |
| 449 |
return "X"; |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
/* This class represents an Aw format element. |
| 455 |
*/ |
| 456 |
class FormatA extends FormatIOElement |
| 457 |
{ |
| 458 |
public FormatA( int w ) |
| 459 |
{ |
| 460 |
setWidth( w ); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
String convertToString( Object o, int vecptr ) |
| 465 |
throws IllegalObjectOnWriteException, |
| 466 |
StringTooWideOnWriteException |
| 467 |
{ |
| 468 |
String s; |
| 469 |
|
| 470 |
if ( o instanceof String ) { |
| 471 |
/* pad or truncate strings as necessary. --kgs */ |
| 472 |
s = (String)o; |
| 473 |
if ( (getWidth() != -1) && (s.length() > getWidth()) ) |
| 474 |
return s.substring(0, getWidth()); |
| 475 |
else { |
| 476 |
if(getWidth() > s.length()) { |
| 477 |
char [] pad = new char[getWidth() - s.length()]; |
| 478 |
|
| 479 |
for(int i=0;i<pad.length;i++) |
| 480 |
pad[i] = ' '; |
| 481 |
|
| 482 |
return new String(pad) + s; |
| 483 |
} |
| 484 |
else |
| 485 |
return s; |
| 486 |
} |
| 487 |
} |
| 488 |
else { |
| 489 |
char [] blah = new char[getWidth()]; |
| 490 |
|
| 491 |
/* if this is a non-string argument with an A edit descriptor, |
| 492 |
* just print some nonsense. --kgs |
| 493 |
*/ |
| 494 |
for(int i=0;i<blah.length;i++) |
| 495 |
blah[i] = '#'; |
| 496 |
|
| 497 |
return new String(blah); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
/* vp and in are used only in generating error messages. |
| 503 |
*/ |
| 504 |
Object convertFromString( String s, |
| 505 |
FormatInputList vp, |
| 506 |
InputStreamAndBuffer in |
| 507 |
) |
| 508 |
throws InvalidNumberOnReadException |
| 509 |
{ |
| 510 |
int len; |
| 511 |
|
| 512 |
len = getWidth() - s.length(); |
| 513 |
|
| 514 |
/* if the spec width is wider than the string, |
| 515 |
* return a padded string. --kgs |
| 516 |
*/ |
| 517 |
if(len > 0) { |
| 518 |
char [] pad = new char[len]; |
| 519 |
for(int i=0;i<len;i++) |
| 520 |
pad[i] = ' '; |
| 521 |
String padstr = new String(pad); |
| 522 |
|
| 523 |
return s.concat(padstr); |
| 524 |
} |
| 525 |
|
| 526 |
/* We just return the slice read, as a string. */ |
| 527 |
return s; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
public String toString() |
| 532 |
{ |
| 533 |
return "A"+getWidth(); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
/* This class represents an Iw format element. |
| 539 |
*/ |
| 540 |
class FormatI extends FormatIOElement |
| 541 |
{ |
| 542 |
public FormatI( int w ) |
| 543 |
{ |
| 544 |
setWidth( w ); |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
String convertToString( Object o, int vecptr ) |
| 549 |
throws IllegalObjectOnWriteException, |
| 550 |
NumberTooWideOnWriteException |
| 551 |
{ |
| 552 |
String s; |
| 553 |
|
| 554 |
/* Convert the number to a string. */ |
| 555 |
if ( o instanceof Integer || o instanceof Long ) { |
| 556 |
CJFormat cjf = new CJFormat(); |
| 557 |
cjf.setWidth( getWidth() ); |
| 558 |
cjf.setPre( "" ); |
| 559 |
cjf.setPost( "" ); |
| 560 |
cjf.setLeadingZeroes( false ); |
| 561 |
cjf.setShowPlus( false ); |
| 562 |
cjf.setAlternate( false ); |
| 563 |
cjf.setShowSpace( false ); |
| 564 |
cjf.setLeftAlign( false ); |
| 565 |
cjf.setFmt( 'i' ); |
| 566 |
s = cjf.form( ((Number)o).longValue() ); |
| 567 |
|
| 568 |
/* Throw an exception if the string won't fit. */ |
| 569 |
if ( s.length() > getWidth() ) |
| 570 |
throw new NumberTooWideOnWriteException( (Number)o, |
| 571 |
vecptr, |
| 572 |
this.toString() |
| 573 |
); |
| 574 |
else |
| 575 |
return s; |
| 576 |
} |
| 577 |
else if(o instanceof String) { |
| 578 |
/* String passed to I edit descriptor. try converting the |
| 579 |
* first character to an integer. --kgs |
| 580 |
*/ |
| 581 |
return convertToString(new Integer((int) (((String)o).charAt(0))), vecptr); |
| 582 |
} |
| 583 |
else |
| 584 |
throw new IllegalObjectOnWriteException( o, |
| 585 |
vecptr, |
| 586 |
this.toString() |
| 587 |
); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
/* vp and in are used only in generating error messages. |
| 592 |
*/ |
| 593 |
Object convertFromString( String s, |
| 594 |
FormatInputList vp, |
| 595 |
InputStreamAndBuffer in |
| 596 |
) |
| 597 |
throws InvalidNumberOnReadException |
| 598 |
{ |
| 599 |
/* Parse the string to check it's a valid number, |
| 600 |
and convert if so. |
| 601 |
*/ |
| 602 |
NumberParser np = |
| 603 |
Parsers.theParsers().number_parser; |
| 604 |
// np.ReInit( new StringBufferInputStream(s) ); |
| 605 |
np.ReInit( new StringReader(s) ); |
| 606 |
try { |
| 607 |
int start = np.Integer(); |
| 608 |
Long l = new Long( s.substring(start) ); |
| 609 |
return l; |
| 610 |
} |
| 611 |
catch ( ParseException e ) { |
| 612 |
throw new InvalidNumberOnReadException( s, |
| 613 |
vp.getPtr(), |
| 614 |
this.toString(), |
| 615 |
in.getLineErrorReport(), |
| 616 |
e.getMessage() |
| 617 |
); |
| 618 |
} |
| 619 |
catch ( TokenMgrError e ) { |
| 620 |
throw new InvalidNumberOnReadException( s, |
| 621 |
vp.getPtr(), |
| 622 |
this.toString(), |
| 623 |
in.getLineErrorReport(), |
| 624 |
e.getMessage() |
| 625 |
); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
public String toString() |
| 631 |
{ |
| 632 |
return "I"+getWidth(); |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
/* |
| 637 |
* Handles logical (boolean) edit descriptors. |
| 638 |
*/ |
| 639 |
|
| 640 |
class FormatL extends FormatIOElement |
| 641 |
{ |
| 642 |
public FormatL( int w ) |
| 643 |
{ |
| 644 |
setWidth( w ); |
| 645 |
} |
| 646 |
|
| 647 |
String convertToString( Object o, int vecptr ) |
| 648 |
throws IllegalObjectOnWriteException, |
| 649 |
NumberTooWideOnWriteException |
| 650 |
{ |
| 651 |
String s; |
| 652 |
|
| 653 |
/* Convert the number to a string. */ |
| 654 |
if ( o instanceof Boolean ) { |
| 655 |
char [] b = new char[getWidth()]; |
| 656 |
int i; |
| 657 |
|
| 658 |
for(i=0;i<b.length-1;i++) |
| 659 |
b[i] = ' '; |
| 660 |
|
| 661 |
b[i] = (((Boolean)o).booleanValue() == true) ? 'T' : 'F'; |
| 662 |
|
| 663 |
s = new String(b); |
| 664 |
|
| 665 |
/* Throw an exception if the string won't fit. */ |
| 666 |
if ( s.length() > getWidth() ) |
| 667 |
throw new NumberTooWideOnWriteException( (Number)o, |
| 668 |
vecptr, |
| 669 |
this.toString() |
| 670 |
); |
| 671 |
else |
| 672 |
return s; |
| 673 |
} |
| 674 |
else |
| 675 |
throw new IllegalObjectOnWriteException( o, |
| 676 |
vecptr, |
| 677 |
this.toString() |
| 678 |
); |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
/* vp and in are used only in generating error messages. |
| 683 |
*/ |
| 684 |
Object convertFromString( String s, |
| 685 |
FormatInputList vp, |
| 686 |
InputStreamAndBuffer in |
| 687 |
) |
| 688 |
throws InvalidNumberOnReadException |
| 689 |
{ |
| 690 |
/* Parse the string to check it's a valid number, |
| 691 |
and convert if so. |
| 692 |
*/ |
| 693 |
NumberParser np = |
| 694 |
Parsers.theParsers().number_parser; |
| 695 |
// np.ReInit( new StringBufferInputStream(s) ); |
| 696 |
np.ReInit( new StringReader(s) ); |
| 697 |
try { |
| 698 |
int start = np.Boolean(); |
| 699 |
char brep = s.substring(start).charAt(0); |
| 700 |
Boolean b; |
| 701 |
|
| 702 |
if(brep == 't' || brep == 'T') |
| 703 |
b = new Boolean(true); |
| 704 |
else if(brep == 'f' || brep == 'F') |
| 705 |
b = new Boolean(false); |
| 706 |
else |
| 707 |
throw new ParseException("bad logical value"); |
| 708 |
return b; |
| 709 |
} |
| 710 |
catch ( ParseException e ) { |
| 711 |
throw new InvalidNumberOnReadException( s, |
| 712 |
vp.getPtr(), |
| 713 |
this.toString(), |
| 714 |
in.getLineErrorReport(), |
| 715 |
e.getMessage() |
| 716 |
); |
| 717 |
} |
| 718 |
catch ( TokenMgrError e ) { |
| 719 |
throw new InvalidNumberOnReadException( s, |
| 720 |
vp.getPtr(), |
| 721 |
this.toString(), |
| 722 |
in.getLineErrorReport(), |
| 723 |
e.getMessage() |
| 724 |
); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
public String toString() |
| 729 |
{ |
| 730 |
return "L"+getWidth(); |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
/* This class represents an Fw.d format element. |
| 735 |
Numbers should be output with d decimal places. |
| 736 |
*/ |
| 737 |
class FormatF extends FormatIOElement |
| 738 |
{ |
| 739 |
private int d; |
| 740 |
|
| 741 |
|
| 742 |
public FormatF( int w, int d ) |
| 743 |
{ |
| 744 |
setWidth( w ); |
| 745 |
this.d = d; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
String convertToString( Object o, int vecptr ) |
| 750 |
throws IllegalObjectOnWriteException, |
| 751 |
NumberTooWideOnWriteException |
| 752 |
{ |
| 753 |
String s; |
| 754 |
|
| 755 |
/* Convert the number to a string. */ |
| 756 |
if ( o instanceof Integer || o instanceof Long || |
| 757 |
o instanceof Float || o instanceof Double ) { |
| 758 |
CJFormat cjf = new CJFormat(); |
| 759 |
cjf.setWidth( getWidth() ); |
| 760 |
cjf.setPrecision( this.d ); |
| 761 |
cjf.setPre( "" ); |
| 762 |
cjf.setPost( "" ); |
| 763 |
cjf.setLeadingZeroes( false ); |
| 764 |
cjf.setShowPlus( false ); |
| 765 |
cjf.setAlternate( false ); |
| 766 |
cjf.setShowSpace( false ); |
| 767 |
cjf.setLeftAlign( false ); |
| 768 |
cjf.setFmt( 'f' ); |
| 769 |
s = cjf.form( ((Number)o).doubleValue() ); |
| 770 |
|
| 771 |
/* Throw an exception if the string won't fit. */ |
| 772 |
if ( s.length() > getWidth() ) |
| 773 |
throw new NumberTooWideOnWriteException( (Number)o, |
| 774 |
vecptr, |
| 775 |
this.toString() |
| 776 |
); |
| 777 |
else |
| 778 |
return s; |
| 779 |
} |
| 780 |
else |
| 781 |
throw new IllegalObjectOnWriteException( o, |
| 782 |
vecptr, |
| 783 |
this.toString() |
| 784 |
); |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
/* vp and in are used only in generating error messages. |
| 789 |
*/ |
| 790 |
Object convertFromString( String s, |
| 791 |
FormatInputList vp, |
| 792 |
InputStreamAndBuffer in |
| 793 |
) |
| 794 |
throws InvalidNumberOnReadException |
| 795 |
{ |
| 796 |
/* Parse the string to check it's a valid number, |
| 797 |
and convert if so. |
| 798 |
*/ |
| 799 |
NumberParser np = |
| 800 |
Parsers.theParsers().number_parser; |
| 801 |
// np.ReInit( new StringBufferInputStream(s) ); |
| 802 |
np.ReInit( new StringReader(s) ); |
| 803 |
try { |
| 804 |
int start = np.Float(); |
| 805 |
Double d = new Double( s.substring(start) ); |
| 806 |
return d; |
| 807 |
} |
| 808 |
catch ( ParseException e ) { |
| 809 |
throw new InvalidNumberOnReadException( s, |
| 810 |
vp.getPtr(), |
| 811 |
this.toString(), |
| 812 |
in.getLineErrorReport(), |
| 813 |
e.getMessage() |
| 814 |
); |
| 815 |
} |
| 816 |
catch ( TokenMgrError e ) { |
| 817 |
throw new InvalidNumberOnReadException( s, |
| 818 |
vp.getPtr(), |
| 819 |
this.toString(), |
| 820 |
in.getLineErrorReport(), |
| 821 |
e.getMessage() |
| 822 |
); |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
public String toString() |
| 828 |
{ |
| 829 |
return "F"+getWidth()+"."+this.d; |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
/* This class represents an Ew.d format element. |
| 835 |
Numbers should be output as |
| 836 |
s0.dd...ddEsdd |
| 837 |
where s is a sign. |
| 838 |
*/ |
| 839 |
class FormatE extends FormatIOElement |
| 840 |
{ int d; |
| 841 |
|
| 842 |
|
| 843 |
public FormatE( int w, int d ) |
| 844 |
{ |
| 845 |
setWidth( w ); |
| 846 |
this.d = d; |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
String convertToString( Object o, int vecptr ) |
| 851 |
throws IllegalObjectOnWriteException, |
| 852 |
NumberTooWideOnWriteException |
| 853 |
{ |
| 854 |
String s; |
| 855 |
|
| 856 |
/* Convert the number to a string. */ |
| 857 |
if ( o instanceof Integer || o instanceof Long || |
| 858 |
o instanceof Float || o instanceof Double ) { |
| 859 |
CJFormat cjf = new CJFormat(); |
| 860 |
cjf.setWidth( getWidth() ); |
| 861 |
cjf.setPrecision( this.d ); |
| 862 |
cjf.setPre( "" ); |
| 863 |
cjf.setPost( "" ); |
| 864 |
cjf.setLeadingZeroes( false ); |
| 865 |
cjf.setShowPlus( false ); |
| 866 |
cjf.setAlternate( false ); |
| 867 |
cjf.setShowSpace( false ); |
| 868 |
cjf.setLeftAlign( false ); |
| 869 |
cjf.setFmt( 'E' ); |
| 870 |
s = cjf.form( ((Number)o).doubleValue() ); |
| 871 |
|
| 872 |
/* Throw an exception if the string won't fit. */ |
| 873 |
if ( s.length() > getWidth() ) |
| 874 |
throw new NumberTooWideOnWriteException( (Number)o, |
| 875 |
vecptr, |
| 876 |
this.toString() |
| 877 |
); |
| 878 |
else |
| 879 |
return s; |
| 880 |
} |
| 881 |
else |
| 882 |
throw new IllegalObjectOnWriteException( o, |
| 883 |
vecptr, |
| 884 |
this.toString() |
| 885 |
); |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
/* vp and in are used only in generating error messages. |
| 890 |
*/ |
| 891 |
Object convertFromString( String s, |
| 892 |
FormatInputList vp, |
| 893 |
InputStreamAndBuffer in |
| 894 |
) |
| 895 |
throws InvalidNumberOnReadException |
| 896 |
{ |
| 897 |
/* Parse the string to check it's a valid number, |
| 898 |
and convert if so. |
| 899 |
*/ |
| 900 |
NumberParser np = |
| 901 |
Parsers.theParsers().number_parser; |
| 902 |
// np.ReInit( new StringBufferInputStream(s) ); |
| 903 |
np.ReInit( new StringReader(s) ); |
| 904 |
try { |
| 905 |
int start = np.Float(); |
| 906 |
Double d = new Double( s.substring(start) ); |
| 907 |
return d; |
| 908 |
} |
| 909 |
catch ( ParseException e ) { |
| 910 |
throw new InvalidNumberOnReadException( s, |
| 911 |
vp.getPtr(), |
| 912 |
this.toString(), |
| 913 |
in.getLineErrorReport(), |
| 914 |
e.getMessage() |
| 915 |
); |
| 916 |
} |
| 917 |
catch ( TokenMgrError e ) { |
| 918 |
throw new InvalidNumberOnReadException( s, |
| 919 |
vp.getPtr(), |
| 920 |
this.toString(), |
| 921 |
in.getLineErrorReport(), |
| 922 |
e.getMessage() |
| 923 |
); |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
public String toString() |
| 929 |
{ |
| 930 |
return "E"+getWidth()+"."+this.d; |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
/* This class represents an / item. |
| 936 |
*/ |
| 937 |
class FormatSlash extends FormatElement |
| 938 |
{ |
| 939 |
public void write( FormatOutputList vp, PrintStream out ) |
| 940 |
{ |
| 941 |
out.println(); |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
public void read( FormatInputList vp, |
| 946 |
InputStreamAndBuffer in, |
| 947 |
FormatMap format_map |
| 948 |
) |
| 949 |
throws InputFormatException |
| 950 |
{ |
| 951 |
in.readLine( vp.getPtr(), this ); |
| 952 |
} |
| 953 |
|
| 954 |
|
| 955 |
public String toString() |
| 956 |
{ |
| 957 |
return "/"; |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
|
| 962 |
/* This class represents an embedded literal, e.g. 'Title'. |
| 963 |
toString() does not yet handle embedded quotes. |
| 964 |
*/ |
| 965 |
class FormatString extends FormatElement |
| 966 |
{ |
| 967 |
private String s; |
| 968 |
|
| 969 |
|
| 970 |
public FormatString( String s ) |
| 971 |
{ |
| 972 |
this.s = s; |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
public void write( FormatOutputList vp, PrintStream out ) |
| 977 |
{ |
| 978 |
out.print(this.s); |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
public void read( FormatInputList vp, |
| 983 |
InputStreamAndBuffer in, |
| 984 |
FormatMap format_map |
| 985 |
) |
| 986 |
throws InputFormatException |
| 987 |
{ |
| 988 |
String s = in.getSlice( this.s.length(), vp.getPtr(), this ); |
| 989 |
if ( !( this.s.equals(s) ) ) |
| 990 |
throw new UnmatchedStringOnReadException( s, |
| 991 |
vp.getPtr(), |
| 992 |
this.toString(), |
| 993 |
in.getLineErrorReport() |
| 994 |
); |
| 995 |
in.advance( this.s.length() ); |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
public String toString() |
| 1000 |
{ |
| 1001 |
return "'" + this.s + "'"; |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
/* This class represents a mapping from input data. We use it to specify, |
| 1007 |
for example, that on input, an "X" should be replaced by a "0" before |
| 1008 |
being interpreted by the formatted input routines. |
| 1009 |
The user must provide an instance of this class, with getMapping |
| 1010 |
defined. getMapping should return either null, if the input string |
| 1011 |
is to be left as it is, or a replacement string. |
| 1012 |
*/ |
| 1013 |
abstract class FormatMap |
| 1014 |
{ |
| 1015 |
public abstract String getMapping( String in ); |
| 1016 |
} |
| 1017 |
|
| 1018 |
|
| 1019 |
interface FormatOutputList |
| 1020 |
{ |
| 1021 |
boolean hasCurrentElement(); |
| 1022 |
|
| 1023 |
void checkCurrentElementForWrite( FormatElement format_element ) |
| 1024 |
throws EndOfVectorOnWriteException; |
| 1025 |
|
| 1026 |
Object getCurrentElement(); |
| 1027 |
|
| 1028 |
Object getCurrentElementAndAdvance(); |
| 1029 |
|
| 1030 |
/* Returns the current pointer. |
| 1031 |
Used only in generating error messages. |
| 1032 |
*/ |
| 1033 |
int getPtr(); |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
interface FormatInputList |
| 1038 |
{ |
| 1039 |
/* format_element and in are only for generating error messages. |
| 1040 |
*/ |
| 1041 |
void checkCurrentElementForRead( FormatElement format_element, |
| 1042 |
InputStreamAndBuffer in |
| 1043 |
) |
| 1044 |
throws InputFormatException; |
| 1045 |
// If the list is a VectorAndPointer, it won't throw an exception. |
| 1046 |
// If it is a StringsHashtableAndPointer, it will throw a |
| 1047 |
// EndOfKeyVectorOnReadException. |
| 1048 |
|
| 1049 |
/* Puts o into the input list and advances its pointer. |
| 1050 |
Must be defined for each subclass. |
| 1051 |
format_element and in are only for generating error messages. |
| 1052 |
*/ |
| 1053 |
void putElementAndAdvance( Object o, |
| 1054 |
FormatElement format_element, |
| 1055 |
InputStreamAndBuffer in |
| 1056 |
) |
| 1057 |
throws InputFormatException; |
| 1058 |
|
| 1059 |
/* Returns the current pointer. |
| 1060 |
Used only in generating error messages. |
| 1061 |
*/ |
| 1062 |
int getPtr(); |
| 1063 |
} |
| 1064 |
|
| 1065 |
|
| 1066 |
/* This class represents a Vector and a current-element pointer. |
| 1067 |
We use it when outputting or inputting a Vector against a format: |
| 1068 |
the pointer keeps track of the current element being output, and |
| 1069 |
can be incremented by the format write and read methods. |
| 1070 |
*/ |
| 1071 |
class VectorAndPointer implements FormatInputList, FormatOutputList |
| 1072 |
{ |
| 1073 |
private Vector v = null; |
| 1074 |
private int vecptr = 0; |
| 1075 |
// On output, vecptr points at the next element to be used. |
| 1076 |
// On input, it points at the next free slot to be filled. |
| 1077 |
|
| 1078 |
|
| 1079 |
public VectorAndPointer( Vector v ) |
| 1080 |
{ |
| 1081 |
this.v = v; |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
public VectorAndPointer() |
| 1086 |
{ |
| 1087 |
this.v = new Vector(); |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
public boolean hasCurrentElement() |
| 1092 |
{ |
| 1093 |
return ( this.vecptr < this.v.size() ); |
| 1094 |
} |
| 1095 |
|
| 1096 |
|
| 1097 |
public void checkCurrentElementForWrite( FormatElement format_element ) |
| 1098 |
throws EndOfVectorOnWriteException |
| 1099 |
{ |
| 1100 |
if ( !hasCurrentElement() ) |
| 1101 |
throw new EndOfVectorOnWriteException( this.vecptr, |
| 1102 |
format_element.toString() |
| 1103 |
); |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
/* Checks that the current element in the input list is OK and |
| 1108 |
throws an exception if not. For this implementation of |
| 1109 |
FormatInputList, there are no error conditions - we |
| 1110 |
introduced the method for the StringHashtableAndPointer class, |
| 1111 |
and need it here for compatibility. |
| 1112 |
format_element and in are only for generating error messages. |
| 1113 |
*/ |
| 1114 |
public void checkCurrentElementForRead( FormatElement format_element, |
| 1115 |
InputStreamAndBuffer in |
| 1116 |
) |
| 1117 |
{ |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
public Object getCurrentElement() |
| 1122 |
{ |
| 1123 |
return this.v.elementAt( this.vecptr ); |
| 1124 |
} |
| 1125 |
|
| 1126 |
public Object getCurrentElementAndAdvance() |
| 1127 |
{ |
| 1128 |
this.vecptr = this.vecptr+1; |
| 1129 |
return this.v.elementAt( this.vecptr-1 ); |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
/* Puts o into the input list and advances its pointer. |
| 1134 |
format_element and in are only for generating error messages, |
| 1135 |
and not used in this implementation, since no error conditions |
| 1136 |
can arise. |
| 1137 |
*/ |
| 1138 |
public void putElementAndAdvance( Object o, |
| 1139 |
FormatElement format_element, |
| 1140 |
InputStreamAndBuffer in |
| 1141 |
) |
| 1142 |
{ |
| 1143 |
this.v.addElement(o); |
| 1144 |
this.vecptr = this.vecptr + 1; |
| 1145 |
} |
| 1146 |
|
| 1147 |
|
| 1148 |
public void advance() |
| 1149 |
{ |
| 1150 |
this.vecptr = this.vecptr + 1; |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
/* Returns the current pointer. |
| 1155 |
Used only in generating error messages. |
| 1156 |
*/ |
| 1157 |
public int getPtr() |
| 1158 |
{ |
| 1159 |
return this.vecptr; |
| 1160 |
} |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
/* This class represents a Vector of Strings and a current-element pointer. |
| 1165 |
We use it when inputting data against a format. |
| 1166 |
*/ |
| 1167 |
class StringsHashtableAndPointer implements FormatInputList |
| 1168 |
{ |
| 1169 |
private VectorAndPointer vp; |
| 1170 |
private Hashtable ht; |
| 1171 |
|
| 1172 |
|
| 1173 |
public StringsHashtableAndPointer( Vector strings, Hashtable ht ) |
| 1174 |
{ |
| 1175 |
this.vp = new VectorAndPointer( strings ); |
| 1176 |
this.ht = ht; |
| 1177 |
} |
| 1178 |
|
| 1179 |
|
| 1180 |
/* Checks that there is a current element in the key vector, and |
| 1181 |
throws an exception if not. |
| 1182 |
format_element and in are only for generating error messages. |
| 1183 |
*/ |
| 1184 |
public void checkCurrentElementForRead( FormatElement format_element, |
| 1185 |
InputStreamAndBuffer in |
| 1186 |
) |
| 1187 |
throws EndOfKeyVectorOnReadException |
| 1188 |
{ |
| 1189 |
if ( !(this.vp.hasCurrentElement() ) ) |
| 1190 |
throw new EndOfKeyVectorOnReadException( this.vp.getPtr(), |
| 1191 |
format_element.toString(), |
| 1192 |
in.getLineErrorReport() |
| 1193 |
); |
| 1194 |
} |
| 1195 |
|
| 1196 |
|
| 1197 |
/* Puts o into the input list and advances its pointer. |
| 1198 |
In this implementation, that means getting the current key, |
| 1199 |
putting o into an appropriate hashtable slot, and advancing |
| 1200 |
the pointer in the vector of keys. |
| 1201 |
format_element and in are only for generating error messages. |
| 1202 |
*/ |
| 1203 |
public void putElementAndAdvance( Object o, |
| 1204 |
FormatElement format_element, |
| 1205 |
InputStreamAndBuffer in |
| 1206 |
) |
| 1207 |
throws KeyNotStringOnReadException |
| 1208 |
{ |
| 1209 |
Object current_key = this.vp.getCurrentElement(); |
| 1210 |
if ( current_key instanceof String ) { |
| 1211 |
this.ht.put( (String)current_key, o ); |
| 1212 |
this.vp.advance(); |
| 1213 |
} |
| 1214 |
else |
| 1215 |
throw new KeyNotStringOnReadException( current_key, |
| 1216 |
this.vp.getPtr(), |
| 1217 |
format_element.toString(), |
| 1218 |
in.getLineErrorReport() |
| 1219 |
); |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
/* Returns the current pointer. |
| 1224 |
Used only in generating error messages. |
| 1225 |
*/ |
| 1226 |
public int getPtr() |
| 1227 |
{ |
| 1228 |
return this.vp.getPtr(); |
| 1229 |
} |
| 1230 |
} |
| 1231 |
|
| 1232 |
|
| 1233 |
/* This class holds an input stream and a line buffer. |
| 1234 |
*/ |
| 1235 |
class InputStreamAndBuffer |
| 1236 |
{ |
| 1237 |
private BufferedReader in; |
| 1238 |
// The stream we read from. |
| 1239 |
|
| 1240 |
private String line; |
| 1241 |
// The line just read. |
| 1242 |
|
| 1243 |
private int ptr; |
| 1244 |
// Initialised to 0 after reading a line. Index of the next |
| 1245 |
// character to use in line. |
| 1246 |
|
| 1247 |
private int line_number; |
| 1248 |
// Initially 0. Is incremented each time a line is read, so |
| 1249 |
// the first line read is number 1. |
| 1250 |
|
| 1251 |
private boolean nothing_read; |
| 1252 |
// Initially true. Is set false after reading a line. We |
| 1253 |
// use this so that the first call of getSlice |
| 1254 |
// knows to read a line. |
| 1255 |
|
| 1256 |
|
| 1257 |
public InputStreamAndBuffer( BufferedReader in ) |
| 1258 |
{ |
| 1259 |
this.in = in; |
| 1260 |
this.ptr = 0; |
| 1261 |
this.line = ""; |
| 1262 |
this.line_number = 0; |
| 1263 |
this.nothing_read = true; |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
/* Reads the next line into the line buffer. |
| 1268 |
vecptr and format are used only in generating error messages. |
| 1269 |
*/ |
| 1270 |
public void readLine( int vecptr, FormatElement format ) |
| 1271 |
throws EndOfFileWhenStartingReadException, |
| 1272 |
LineMissingOnReadException, |
| 1273 |
IOExceptionOnReadException |
| 1274 |
{ |
| 1275 |
try { |
| 1276 |
String line = this.in.readLine(); |
| 1277 |
|
| 1278 |
if ( line == null ) { |
| 1279 |
if ( this.nothing_read ) |
| 1280 |
throw new EndOfFileWhenStartingReadException( vecptr, |
| 1281 |
format.toString(), |
| 1282 |
this.line, |
| 1283 |
this.line_number |
| 1284 |
); |
| 1285 |
else |
| 1286 |
throw new LineMissingOnReadException( vecptr, |
| 1287 |
format.toString(), |
| 1288 |
this.line, |
| 1289 |
this.line_number |
| 1290 |
); |
| 1291 |
} |
| 1292 |
else { |
| 1293 |
this.ptr = 0; |
| 1294 |
this.nothing_read = false; |
| 1295 |
this.line_number = this.line_number + 1; |
| 1296 |
this.line = line; |
| 1297 |
// Don't do the assignment until we've checked for a null |
| 1298 |
// line, because then we can then use this.line as the |
| 1299 |
// previous value for error messages. |
| 1300 |
} |
| 1301 |
} |
| 1302 |
catch ( IOException e ) { |
| 1303 |
throw new IOExceptionOnReadException( this.line, this.line_number, |
| 1304 |
e.getMessage() |
| 1305 |
); |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
|
| 1310 |
/* Returns a string consisting of the next width characters, |
| 1311 |
and throws an exception if the line is not long enough. |
| 1312 |
The 'vecptr' and 'format' parameters are used only in generating error |
| 1313 |
messages. |
| 1314 |
*/ |
| 1315 |
public String getSlice( int width, int vecptr, FormatElement format ) |
| 1316 |
throws DataMissingOnReadException, |
| 1317 |
LineMissingOnReadException, |
| 1318 |
EndOfFileWhenStartingReadException, |
| 1319 |
IOExceptionOnReadException |
| 1320 |
{ |
| 1321 |
if ( this.nothing_read ) |
| 1322 |
readLine( vecptr, format ); |
| 1323 |
if ( this.ptr+width > this.line.length() ) { |
| 1324 |
/* if there aren't 'width' characters left, just return the |
| 1325 |
* remainder of the line. --kgs |
| 1326 |
*/ |
| 1327 |
return this.line.substring( this.ptr ); |
| 1328 |
} |
| 1329 |
else { |
| 1330 |
return this.line.substring( this.ptr, this.ptr+width ); |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
|
| 1335 |
/* Advances the pointer by width. |
| 1336 |
*/ |
| 1337 |
public void advance( int width ) |
| 1338 |
{ |
| 1339 |
this.ptr = this.ptr + width; |
| 1340 |
} |
| 1341 |
|
| 1342 |
|
| 1343 |
/* Generates an error report showing the line, character pointer |
| 1344 |
ptr and line number. |
| 1345 |
*/ |
| 1346 |
public String getLineErrorReport() |
| 1347 |
{ |
| 1348 |
StringBuffer s = new StringBuffer(); |
| 1349 |
|
| 1350 |
/* Report the line number. */ |
| 1351 |
s.append( " Line number = " + this.line_number + ":\n" ); |
| 1352 |
|
| 1353 |
/* Show the line. */ |
| 1354 |
s.append( this.line + "\n" ); |
| 1355 |
|
| 1356 |
/* Show an arrow under ptr. */ |
| 1357 |
for ( int i=0; i<this.ptr; i++ ) |
| 1358 |
s.append( " " ); |
| 1359 |
s.append( "^" ); |
| 1360 |
|
| 1361 |
return s.toString(); |
| 1362 |
} |
| 1363 |
} |
| 1364 |
|
| 1365 |
|
| 1366 |
/* This exception is a generic one, a superclass of all those |
| 1367 |
thrown to report an error while doing formatted output. |
| 1368 |
*/ |
| 1369 |
abstract class OutputFormatException extends Exception |
| 1370 |
{ |
| 1371 |
public OutputFormatException( String s ) |
| 1372 |
{ |
| 1373 |
super( s ); |
| 1374 |
} |
| 1375 |
|
| 1376 |
public OutputFormatException() |
| 1377 |
{ |
| 1378 |
super(); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
|
| 1383 |
/* This exception is thrown if formatted output runs off the |
| 1384 |
end of the vector being output before it has completed the |
| 1385 |
format. |
| 1386 |
*/ |
| 1387 |
class EndOfVectorOnWriteException extends OutputFormatException |
| 1388 |
{ |
| 1389 |
public EndOfVectorOnWriteException( int vecptr, |
| 1390 |
String format |
| 1391 |
) |
| 1392 |
{ |
| 1393 |
this( "End of vector while writing formatted data:\n" + |
| 1394 |
" Index = " + vecptr + "\n" + |
| 1395 |
" Format = " + format + " ." |
| 1396 |
); |
| 1397 |
} |
| 1398 |
|
| 1399 |
public EndOfVectorOnWriteException( String s ) |
| 1400 |
{ |
| 1401 |
super( s ); |
| 1402 |
} |
| 1403 |
|
| 1404 |
public EndOfVectorOnWriteException( ) |
| 1405 |
{ |
| 1406 |
super( ); |
| 1407 |
} |
| 1408 |
} |
| 1409 |
|
| 1410 |
|
| 1411 |
/* This exception is thrown if formatted output detects an object |
| 1412 |
that's the wrong type for a format element, e.g. a real |
| 1413 |
when outputting against an Iw element. |
| 1414 |
*/ |
| 1415 |
class IllegalObjectOnWriteException extends OutputFormatException |
| 1416 |
{ |
| 1417 |
public IllegalObjectOnWriteException( Object o, |
| 1418 |
int vecptr, |
| 1419 |
String format |
| 1420 |
) |
| 1421 |
{ |
| 1422 |
this( "Illegal object while writing formatted data:\n" + |
| 1423 |
" Object = \"" + o + "\"\n" + |
| 1424 |
" Index = " + vecptr + "\n" + |
| 1425 |
" Format = " + format + " ." |
| 1426 |
); |
| 1427 |
} |
| 1428 |
|
| 1429 |
public IllegalObjectOnWriteException( String s ) |
| 1430 |
{ |
| 1431 |
super( s ); |
| 1432 |
} |
| 1433 |
|
| 1434 |
public IllegalObjectOnWriteException( ) |
| 1435 |
{ |
| 1436 |
super( ); |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
|
| 1441 |
/* This exception is thrown if formatted output detects a string |
| 1442 |
that won't fit in its format, e.g. trying to output abcde |
| 1443 |
against an A4 element. |
| 1444 |
*/ |
| 1445 |
class StringTooWideOnWriteException extends OutputFormatException |
| 1446 |
{ |
| 1447 |
public StringTooWideOnWriteException( String s, |
| 1448 |
int vecptr, |
| 1449 |
String format |
| 1450 |
) |
| 1451 |
{ |
| 1452 |
this( "String too wide while writing formatted data:\n" + |
| 1453 |
" String = \"" + s + "\"\n" + |
| 1454 |
" Index = " + vecptr + "\n" + |
| 1455 |
" Format = " + format + " ." |
| 1456 |
); |
| 1457 |
} |
| 1458 |
|
| 1459 |
public StringTooWideOnWriteException( String s ) |
| 1460 |
{ |
| 1461 |
super( s ); |
| 1462 |
} |
| 1463 |
|
| 1464 |
public StringTooWideOnWriteException( ) |
| 1465 |
{ |
| 1466 |
super( ); |
| 1467 |
} |
| 1468 |
} |
| 1469 |
|
| 1470 |
|
| 1471 |
/* This exception is thrown if formatted output detects a number |
| 1472 |
that won't fit in its format, e.g. trying to output 1234 |
| 1473 |
against an I3 element. |
| 1474 |
*/ |
| 1475 |
class NumberTooWideOnWriteException extends OutputFormatException |
| 1476 |
{ |
| 1477 |
public NumberTooWideOnWriteException( Number n, |
| 1478 |
int vecptr, |
| 1479 |
String format |
| 1480 |
) |
| 1481 |
{ |
| 1482 |
this( "Number too wide while writing formatted data:\n" + |
| 1483 |
" Number = \"" + n + "\"\n" + |
| 1484 |
" Index = " + vecptr + "\n" + |
| 1485 |
" Format = " + format + " ." |
| 1486 |
); |
| 1487 |
} |
| 1488 |
|
| 1489 |
public NumberTooWideOnWriteException( String s ) |
| 1490 |
{ |
| 1491 |
super( s ); |
| 1492 |
} |
| 1493 |
|
| 1494 |
public NumberTooWideOnWriteException( ) |
| 1495 |
{ |
| 1496 |
super( ); |
| 1497 |
} |
| 1498 |
} |
| 1499 |
|
| 1500 |
|
| 1501 |
/* This exception is a generic one, a superclass of all those |
| 1502 |
thrown to report an error while doing formatted input. |
| 1503 |
*/ |
| 1504 |
abstract class InputFormatException extends Exception |
| 1505 |
{ |
| 1506 |
public InputFormatException( String s ) |
| 1507 |
{ |
| 1508 |
super( s ); |
| 1509 |
} |
| 1510 |
|
| 1511 |
public InputFormatException() |
| 1512 |
{ |
| 1513 |
super(); |
| 1514 |
} |
| 1515 |
|
| 1516 |
|
| 1517 |
} |
| 1518 |
|
| 1519 |
|
| 1520 |
class LineMissingOnReadException extends InputFormatException |
| 1521 |
{ |
| 1522 |
public LineMissingOnReadException( int vecptr, |
| 1523 |
String format, |
| 1524 |
String line, |
| 1525 |
int line_number |
| 1526 |
) |
| 1527 |
{ |
| 1528 |
this( "End of file while reading formatted data:\n" + |
| 1529 |
" Index = " + vecptr + "\n" + |
| 1530 |
" Format = " + format + "\n" + |
| 1531 |
"Last line was number " + line_number + ":\n" + |
| 1532 |
line |
| 1533 |
); |
| 1534 |
} |
| 1535 |
|
| 1536 |
public LineMissingOnReadException( String s ) |
| 1537 |
{ |
| 1538 |
super( s ); |
| 1539 |
} |
| 1540 |
|
| 1541 |
public LineMissingOnReadException( ) |
| 1542 |
{ |
| 1543 |
super( ); |
| 1544 |
} |
| 1545 |
} |
| 1546 |
|
| 1547 |
|
| 1548 |
class DataMissingOnReadException extends InputFormatException |
| 1549 |
{ |
| 1550 |
public DataMissingOnReadException( int vecptr, |
| 1551 |
String format, |
| 1552 |
String line_error_report |
| 1553 |
) |
| 1554 |
{ |
| 1555 |
this("Warning: EOL reading formatted data: idx=" + |
| 1556 |
vecptr + " fmt=" + format); |
| 1557 |
} |
| 1558 |
|
| 1559 |
public DataMissingOnReadException( String s ) |
| 1560 |
{ |
| 1561 |
super( s ); |
| 1562 |
} |
| 1563 |
|
| 1564 |
public DataMissingOnReadException( ) |
| 1565 |
{ |
| 1566 |
super( ); |
| 1567 |
} |
| 1568 |
} |
| 1569 |
|
| 1570 |
|
| 1571 |
class InvalidNumberOnReadException extends InputFormatException |
| 1572 |
{ |
| 1573 |
public InvalidNumberOnReadException( String number, |
| 1574 |
int vecptr, |
| 1575 |
String format, |
| 1576 |
String line_error_report, |
| 1577 |
String parser_message |
| 1578 |
) |
| 1579 |
{ |
| 1580 |
this( "Invalid number while reading formatted data:\n" + |
| 1581 |
" Number = \"" + number + "\"\n" + |
| 1582 |
" Index = " + vecptr + "\n" + |
| 1583 |
" Format = " + format + "\n" + |
| 1584 |
line_error_report + "\n" + |
| 1585 |
parser_message |
| 1586 |
); |
| 1587 |
} |
| 1588 |
|
| 1589 |
public InvalidNumberOnReadException( String s ) |
| 1590 |
{ |
| 1591 |
super( s ); |
| 1592 |
} |
| 1593 |
|
| 1594 |
public InvalidNumberOnReadException( ) |
| 1595 |
{ |
| 1596 |
super( ); |
| 1597 |
} |
| 1598 |
} |
| 1599 |
|
| 1600 |
|
| 1601 |
class UnmatchedStringOnReadException extends InputFormatException |
| 1602 |
{ |
| 1603 |
public UnmatchedStringOnReadException( String string, |
| 1604 |
int vecptr, |
| 1605 |
String format, |
| 1606 |
String line_error_report |
| 1607 |
) |
| 1608 |
{ |
| 1609 |
this( "Unmatched string while reading formatted data:\n" + |
| 1610 |
" String = \"" + string + "\"\n" + |
| 1611 |
" Index = " + vecptr + "\n" + |
| 1612 |
" Format = " + format + "\n" + |
| 1613 |
line_error_report + "\n" |
| 1614 |
); |
| 1615 |
} |
| 1616 |
|
| 1617 |
public UnmatchedStringOnReadException( String s ) |
| 1618 |
{ |
| 1619 |
super( s ); |
| 1620 |
} |
| 1621 |
|
| 1622 |
public UnmatchedStringOnReadException( ) |
| 1623 |
{ |
| 1624 |
super( ); |
| 1625 |
} |
| 1626 |
} |
| 1627 |
|
| 1628 |
|
| 1629 |
class EndOfKeyVectorOnReadException extends InputFormatException |
| 1630 |
{ |
| 1631 |
public EndOfKeyVectorOnReadException( int vecptr, |
| 1632 |
String format, |
| 1633 |
String line_error_report |
| 1634 |
) |
| 1635 |
{ |
| 1636 |
this( "End of key vector while reading formatted data:\n" + |
| 1637 |
" Index = " + vecptr + "\n" + |
| 1638 |
" Format = " + format + "\n" + |
| 1639 |
line_error_report + "\n" |
| 1640 |
); |
| 1641 |
} |
| 1642 |
|
| 1643 |
public EndOfKeyVectorOnReadException( String s ) |
| 1644 |
{ |
| 1645 |
super( s ); |
| 1646 |
} |
| 1647 |
|
| 1648 |
public EndOfKeyVectorOnReadException( ) |
| 1649 |
{ |
| 1650 |
super( ); |
| 1651 |
} |
| 1652 |
} |
| 1653 |
|
| 1654 |
|
| 1655 |
class KeyNotStringOnReadException extends InputFormatException |
| 1656 |
{ |
| 1657 |
public KeyNotStringOnReadException( Object key, |
| 1658 |
int vecptr, |
| 1659 |
String format, |
| 1660 |
String line_error_report |
| 1661 |
) |
| 1662 |
{ |
| 1663 |
this( "Key not string while reading formatted data:\n" + |
| 1664 |
" Key = \"" + vecptr + "\"\n" + |
| 1665 |
" Index = " + vecptr + "\n" + |
| 1666 |
" Format = " + format + "\n" + |
| 1667 |
line_error_report + "\n" |
| 1668 |
); |
| 1669 |
} |
| 1670 |
|
| 1671 |
public KeyNotStringOnReadException( String s ) |
| 1672 |
{ |
| 1673 |
super( s ); |
| 1674 |
} |
| 1675 |
|
| 1676 |
public KeyNotStringOnReadException( ) |
| 1677 |
{ |
| 1678 |
super( ); |
| 1679 |
} |
| 1680 |
} |
| 1681 |
|
| 1682 |
|
| 1683 |
class IOExceptionOnReadException extends InputFormatException |
| 1684 |
{ |
| 1685 |
public IOExceptionOnReadException( String line, |
| 1686 |
int line_number, |
| 1687 |
String IOMessage |
| 1688 |
) |
| 1689 |
{ |
| 1690 |
this( "IOException while reading formatted data:\n" + |
| 1691 |
"Last line was number " + line_number + ":\n" + |
| 1692 |
line + "\n" + |
| 1693 |
IOMessage |
| 1694 |
); |
| 1695 |
} |
| 1696 |
|
| 1697 |
public IOExceptionOnReadException( String s ) |
| 1698 |
{ |
| 1699 |
super( s ); |
| 1700 |
} |
| 1701 |
|
| 1702 |
public IOExceptionOnReadException( ) |
| 1703 |
{ |
| 1704 |
super( ); |
| 1705 |
} |
| 1706 |
} |
| 1707 |
|
| 1708 |
|
| 1709 |
/* This exception is thrown when a syntax error is detected while |
| 1710 |
parsing a format string. |
| 1711 |
*/ |
| 1712 |
class InvalidFormatException extends Exception |
| 1713 |
{ |
| 1714 |
public InvalidFormatException( String parser_message ) |
| 1715 |
{ |
| 1716 |
super( parser_message ); |
| 1717 |
} |
| 1718 |
|
| 1719 |
public InvalidFormatException( ) |
| 1720 |
{ |
| 1721 |
super( ); |
| 1722 |
} |
| 1723 |
} |
| 1724 |
|
| 1725 |
|
| 1726 |
/* This class is used to hold the parsers for formats and numbers. |
| 1727 |
We generate them static (see JavaCC documentation) because it |
| 1728 |
makes them more efficient. However, that then means that we need |
| 1729 |
somewhere to put an instance of each. That's what we use the result |
| 1730 |
of Parsers.theParsers() for. |
| 1731 |
*/ |
| 1732 |
class Parsers |
| 1733 |
{ |
| 1734 |
static boolean already_created = false; |
| 1735 |
static Parsers parsers = null; |
| 1736 |
|
| 1737 |
FormatParser format_parser = null; |
| 1738 |
NumberParser number_parser = null; |
| 1739 |
|
| 1740 |
|
| 1741 |
static Parsers theParsers() |
| 1742 |
{ |
| 1743 |
if ( !(already_created) ) { |
| 1744 |
parsers = new Parsers(); |
| 1745 |
already_created = true; |
| 1746 |
} |
| 1747 |
return parsers; |
| 1748 |
} |
| 1749 |
|
| 1750 |
|
| 1751 |
private Parsers() |
| 1752 |
{ |
| 1753 |
// this.format_parser = new FormatParser( new StringBufferInputStream("") ); |
| 1754 |
// this.number_parser = new NumberParser( new StringBufferInputStream("") ); |
| 1755 |
this.format_parser = new FormatParser( new StringReader("") ); |
| 1756 |
this.number_parser = new NumberParser( new StringReader("") ); |
| 1757 |
} |
| 1758 |
} |