1 package com.etymon.pj; 2 3 import java.io.*; 4 import java.util.*; 5 import com.etymon.pj.exception.*; 6 import com.etymon.pj.object.*; 7 import com.etymon.pj.object.pagemark.*; 8 9 public class StreamParser { 10 11 public StreamParser() { 12 } 13 14 public Vector parse(PjStream stream) throws PdfFormatException { 15 _buffer = stream.getBuffer(); 16 _stack = new Stack(); 17 int b; 18 _counter = 0; 19 _image = false; 20 while (_counter < _buffer.length) { 21 getToken(); 22 processToken(); 23 } 24 Vector v = new Vector(); 25 while ( ! _stack.empty() ) { 26 v.insertElementAt(_stack.pop(), 0); 27 } 28 return v; 29 } 30 31 private void getImageData() { 33 boolean done = false; 34 int start = _counter; 35 do { 36 if ( ( (_buffer[_counter] == '\n') || 37 (_buffer[_counter] == '\r') ) && 38 ((_counter - start) >= 3) ) { 39 if ( (_buffer[_counter - 1] == 'I') && 40 (_buffer[_counter - 2] == 'E') && 41 ( (_buffer[_counter - 3] == '\n') || 42 (_buffer[_counter - 3] == '\r') ) ) { 43 _imageData = new byte[_counter - start - 2]; 44 System.arraycopy(_buffer, start, _imageData, 0, _imageData.length); 45 done = true; 46 } 47 } 48 _counter++; 49 } while (done == false); 50 _token = ""; 51 } 52 53 private void getToken() { 54 if (_image == true) { 56 getImageData(); 57 return; 58 } 59 skipWhitespace(); 61 StringBuffer token = new StringBuffer (); 62 int stringMode = 0; 63 char ch = '\0'; 64 char lastch = '\0'; 65 boolean done = false; 66 int c; 67 while ( (!done) && ((c = _counter) < _buffer.length) ) { 68 int b = _buffer[c]; 69 char oldlastch = lastch; 70 lastch = ch; 71 ch = (char)b; 72 if (stringMode == 0) { 73 switch (ch) { 74 case '(': 75 if (token.length() == 0) { 76 stringMode = 1; 77 token.append('('); 78 } else { 79 _counter--; 80 done = true; 81 } 82 break; 83 case '[': 84 case ']': 85 if (token.length() == 0) { 86 token.append(ch); 87 } else { 88 _counter--; 89 } 90 done = true; 91 break; 92 case '/': 93 if (token.length() == 0) { 94 token.append(ch); 95 } else { 96 _counter--; 97 done = true; 98 } 99 break; 100 case '<': 101 case '>': 102 int x = token.length(); 103 if (x == 0) { 104 token.append(ch); 105 } else { 106 if ( (x == 1) && (token.charAt(0) == ch) ) { 107 token.append(ch); 108 done = true; 109 } else { 110 _counter--; 111 done = true; 112 } 113 } 114 break; 115 default: 116 if (isWhitespace(ch)) { 117 done = true; 118 } else { 119 token.append(ch); 120 } 121 } 122 } else { 123 switch (ch) { 125 case '(': 126 token.append('('); 127 if ( (lastch != '\\') || (oldlastch == '\\') ) { 128 stringMode++; 129 } 130 break; 131 case ')': 132 token.append(')'); 133 if ( (lastch != '\\') || (oldlastch == '\\') ) { 134 stringMode--; 135 if (stringMode == 0) { 136 done = true; 137 } 138 } 139 break; 140 default: 141 token.append(ch); 142 } 143 } 144 _counter++; 145 } 146 _token = token.toString(); 147 } 148 149 private void skipWhitespace() { 150 int c; 151 while ( ((c = _counter) < _buffer.length) && (isWhitespace((char)_buffer[c])) ) { 152 _counter++; 153 } 154 } 155 156 private static boolean isWhitespace(char c) { 157 return ( (c == ' ') || 158 (c == '\t') || 159 (c == '\r') || 160 (c == '\n') ); 161 } 162 163 private void processToken() throws PdfFormatException { 164 if ( (_token.length() == 0) && (_image == false) ) { 165 return; 166 } 167 if (_image == true) { 168 _image = false; 169 _o1 = _stack.pop(); 171 if ( ! (_o1 instanceof PjDictionary) ) { 172 throw new PdfFormatException("Dictionary expected before ID."); 173 } 174 _stack.push(new XEI((PjDictionary)_o1, _imageData)); 175 } 176 else if (_token.equals("BI")) { 177 _stack.push("<<"); 178 } 179 else if ( (_token.equals(">>")) || (_token.equals("ID")) ) { 180 _b1 = false; 181 _h1 = new Hashtable(); 182 while ( ! _b1 ) { 183 _o2 = _stack.pop(); 184 if ( (_o2 instanceof String ) && 185 (((String )_o2).equals("<<")) ) { 186 _b1 = true; 187 } else { 188 if ( ! (_o2 instanceof PjObject) ) { 189 throw new PdfFormatException("PDF object expected within dictionary."); 190 } 191 _o1 = _stack.pop(); 192 if ( ! (_o1 instanceof PjName) ) { 193 throw new PdfFormatException("Name (key) expected within dictionary."); 194 } 195 _h1.put(_o1, _o2); 196 } 197 } 198 _stack.push(new PjDictionary(_h1)); 199 if (_token.equals("ID")) { 200 _image = true; 201 } 202 } 203 else if (_token.equals("BT")) { 204 _stack.push(new XBT()); 205 } 206 else if (_token.equals("ET")) { 207 _stack.push(new XET()); 208 } 209 else if (_token.equals("Td")) { 210 _o1 = _stack.pop(); 211 if (_o1 instanceof PjNumber) { 212 _n2 = (PjNumber)(_o1); 213 } else { 214 throw new PdfFormatException("Number (y offset) expected before Td."); 215 } 216 _o1 = _stack.pop(); 217 if (_o1 instanceof PjNumber) { 218 _n1 = (PjNumber)(_o1); 219 } else { 220 throw new PdfFormatException("Number (x offset) expected before Td."); 221 } 222 _stack.push(new XTd(_n1, _n2)); 223 } 224 else if (_token.equals("TD")) { 225 _o1 = _stack.pop(); 226 if (_o1 instanceof PjNumber) { 227 _n2 = (PjNumber)(_o1); 228 } else { 229 throw new PdfFormatException("Number (y offset) expected before TD."); 230 } 231 _o1 = _stack.pop(); 232 if (_o1 instanceof PjNumber) { 233 _n1 = (PjNumber)(_o1); 234 } else { 235 throw new PdfFormatException("Number (x offset) expected before TD."); 236 } 237 _stack.push(new XXTD(_n1, _n2)); 238 } 239 else if (_token.equals("m")) { 240 _o1 = _stack.pop(); 241 if (_o1 instanceof PjNumber) { 242 _n2 = (PjNumber)(_o1); 243 } else { 244 throw new PdfFormatException("Number (y) expected before m."); 245 } 246 _o1 = _stack.pop(); 247 if (_o1 instanceof PjNumber) { 248 _n1 = (PjNumber)(_o1); 249 } else { 250 throw new PdfFormatException("Number (x) expected before m."); 251 } 252 _stack.push(new Xm(_n1, _n2)); 253 } 254 else if (_token.equals("l")) { 255 _o1 = _stack.pop(); 256 if (_o1 instanceof PjNumber) { 257 _n2 = (PjNumber)(_o1); 258 } else { 259 throw new PdfFormatException("Number (y) expected before l."); 260 } 261 _o1 = _stack.pop(); 262 if (_o1 instanceof PjNumber) { 263 _n1 = (PjNumber)(_o1); 264 } else { 265 throw new PdfFormatException("Number (x) expected before l."); 266 } 267 _stack.push(new Xl(_n1, _n2)); 268 } 269 else if (_token.equals("d0")) { 270 _o1 = _stack.pop(); 271 if (_o1 instanceof PjNumber) { 272 _n2 = (PjNumber)(_o1); 273 } else { 274 throw new PdfFormatException("Number (w[y]) expected before d0."); 275 } 276 _o1 = _stack.pop(); 277 if (_o1 instanceof PjNumber) { 278 _n1 = (PjNumber)(_o1); 279 } else { 280 throw new PdfFormatException("Number (w[x]) expected before d0."); 281 } 282 _stack.push(new Xd0(_n1, _n2)); 283 } 284 else if (_token.equals("i")) { 285 _o1 = _stack.pop(); 286 if (_o1 instanceof PjNumber) { 287 _n1 = (PjNumber)(_o1); 288 } else { 289 throw new PdfFormatException("Number (flatness) expected before i."); 290 } 291 _stack.push(new Xi(_n1)); 292 } 293 else if (_token.equals("g")) { 294 _o1 = _stack.pop(); 295 if (_o1 instanceof PjNumber) { 296 _n1 = (PjNumber)(_o1); 297 } else { 298 throw new PdfFormatException("Number (gray) expected before g."); 299 } 300 _stack.push(new Xg(_n1)); 301 } 302 else if (_token.equals("G")) { 303 _o1 = _stack.pop(); 304 if (_o1 instanceof PjNumber) { 305 _n1 = (PjNumber)(_o1); 306 } else { 307 throw new PdfFormatException("Number (gray) expected before G."); 308 } 309 _stack.push(new XXG(_n1)); 310 } 311 else if (_token.equals("w")) { 312 _o1 = _stack.pop(); 313 if (_o1 instanceof PjNumber) { 314 _n1 = (PjNumber)(_o1); 315 } else { 316 throw new PdfFormatException("Number (line width) expected before w."); 317 } 318 _stack.push(new Xw(_n1)); 319 } 320 else if (_token.equals("Ts")) { 321 _o1 = _stack.pop(); 322 if (_o1 instanceof PjNumber) { 323 _n1 = (PjNumber)(_o1); 324 } else { 325 throw new PdfFormatException("Number (rise) expected before Ts."); 326 } 327 _stack.push(new XTs(_n1)); 328 } 329 else if (_token.equals("TL")) { 330 _o1 = _stack.pop(); 331 if (_o1 instanceof PjNumber) { 332 _n1 = (PjNumber)(_o1); 333 } else { 334 throw new PdfFormatException("Number (leading) expected before TL."); 335 } 336 _stack.push(new XTL(_n1)); 337 } 338 else if (_token.equals("Tz")) { 339 _o1 = _stack.pop(); 340 if (_o1 instanceof PjNumber) { 341 _n1 = (PjNumber)(_o1); 342 } else { 343 throw new PdfFormatException("Number (scale) expected before Tz."); 344 } 345 _stack.push(new XTz(_n1)); 346 } 347 else if (_token.equals("Tw")) { 348 _o1 = _stack.pop(); 349 if (_o1 instanceof PjNumber) { 350 _n1 = (PjNumber)(_o1); 351 } else { 352 throw new PdfFormatException("Number (word space) expected before Tw."); 353 } 354 _stack.push(new XTw(_n1)); 355 } 356 else if (_token.equals("Tc")) { 357 _o1 = _stack.pop(); 358 if (_o1 instanceof PjNumber) { 359 _n1 = (PjNumber)(_o1); 360 } else { 361 throw new PdfFormatException("Number (char space) expected before Tc."); 362 } 363 _stack.push(new XTc(_n1)); 364 } 365 else if (_token.equals("Tr")) { 366 _o1 = _stack.pop(); 367 if (_o1 instanceof PjNumber) { 368 _n1 = (PjNumber)(_o1); 369 } else { 370 throw new PdfFormatException("Number (render) expected before Tr."); 371 } 372 _stack.push(new XTr(_n1)); 373 } 374 else if (_token.equals("j")) { 375 _o1 = _stack.pop(); 376 if (_o1 instanceof PjNumber) { 377 _n1 = (PjNumber)(_o1); 378 } else { 379 throw new PdfFormatException("Number (line join) expected before j."); 380 } 381 _stack.push(new Xj(_n1)); 382 } 383 else if (_token.equals("J")) { 384 _o1 = _stack.pop(); 385 if (_o1 instanceof PjNumber) { 386 _n1 = (PjNumber)(_o1); 387 } else { 388 throw new PdfFormatException("Number (line cap) expected before J."); 389 } 390 _stack.push(new XXJ(_n1)); 391 } 392 else if (_token.equals("M")) { 393 _o1 = _stack.pop(); 394 if (_o1 instanceof PjNumber) { 395 _n1 = (PjNumber)(_o1); 396 } else { 397 throw new PdfFormatException("Number (miter limit) expected before M."); 398 } 399 _stack.push(new XXM(_n1)); 400 } 401 else if (_token.equals("cm")) { 402 _o6 = _stack.pop(); 403 _o5 = _stack.pop(); 404 _o4 = _stack.pop(); 405 _o3 = _stack.pop(); 406 _o2 = _stack.pop(); 407 _o1 = _stack.pop(); 408 if ( (_o1 instanceof PjNumber) && 409 (_o2 instanceof PjNumber) && 410 (_o3 instanceof PjNumber) && 411 (_o4 instanceof PjNumber) && 412 (_o5 instanceof PjNumber) && 413 (_o6 instanceof PjNumber) ) { 414 _n1 = (PjNumber)_o1; 415 _n2 = (PjNumber)_o2; 416 _n3 = (PjNumber)_o3; 417 _n4 = (PjNumber)_o4; 418 _n5 = (PjNumber)_o5; 419 _n6 = (PjNumber)_o6; 420 } else { 421 throw new PdfFormatException("Number expected before cm."); 422 } 423 _stack.push(new Xcm(_n1, _n2, _n3, _n4, _n5, _n6)); 424 } 425 else if (_token.equals("d1")) { 426 _o6 = _stack.pop(); 427 _o5 = _stack.pop(); 428 _o4 = _stack.pop(); 429 _o3 = _stack.pop(); 430 _o2 = _stack.pop(); 431 _o1 = _stack.pop(); 432 if ( (_o1 instanceof PjNumber) && 433 (_o2 instanceof PjNumber) && 434 (_o3 instanceof PjNumber) && 435 (_o4 instanceof PjNumber) && 436 (_o5 instanceof PjNumber) && 437 (_o6 instanceof PjNumber) ) { 438 _n1 = (PjNumber)_o1; 439 _n2 = (PjNumber)_o2; 440 _n3 = (PjNumber)_o3; 441 _n4 = (PjNumber)_o4; 442 _n5 = (PjNumber)_o5; 443 _n6 = (PjNumber)_o6; 444 } else { 445 throw new PdfFormatException("Number expected before d1."); 446 } 447 _stack.push(new Xd1(_n1, _n2, _n3, _n4, _n5, _n6)); 448 } 449 else if (_token.equals("c")) { 450 _o6 = _stack.pop(); 451 _o5 = _stack.pop(); 452 _o4 = _stack.pop(); 453 _o3 = _stack.pop(); 454 _o2 = _stack.pop(); 455 _o1 = _stack.pop(); 456 if ( (_o1 instanceof PjNumber) && 457 (_o2 instanceof PjNumber) && 458 (_o3 instanceof PjNumber) && 459 (_o4 instanceof PjNumber) && 460 (_o5 instanceof PjNumber) && 461 (_o6 instanceof PjNumber) ) { 462 _n1 = (PjNumber)_o1; 463 _n2 = (PjNumber)_o2; 464 _n3 = (PjNumber)_o3; 465 _n4 = (PjNumber)_o4; 466 _n5 = (PjNumber)_o5; 467 _n6 = (PjNumber)_o6; 468 } else { 469 throw new PdfFormatException("Number expected before c."); 470 } 471 _stack.push(new Xc(_n1, _n2, _n3, _n4, _n5, _n6)); 472 } 473 else if (_token.equals("v")) { 474 _o4 = _stack.pop(); 475 _o3 = _stack.pop(); 476 _o2 = _stack.pop(); 477 _o1 = _stack.pop(); 478 if ( (_o1 instanceof PjNumber) && 479 (_o2 instanceof PjNumber) && 480 (_o3 instanceof PjNumber) && 481 (_o4 instanceof PjNumber) ) { 482 _n1 = (PjNumber)_o1; 483 _n2 = (PjNumber)_o2; 484 _n3 = (PjNumber)_o3; 485 _n4 = (PjNumber)_o4; 486 } else { 487 throw new PdfFormatException("Number expected before v."); 488 } 489 _stack.push(new Xv(_n1, _n2, _n3, _n4)); 490 } 491 else if (_token.equals("y")) { 492 _o4 = _stack.pop(); 493 _o3 = _stack.pop(); 494 _o2 = _stack.pop(); 495 _o1 = _stack.pop(); 496 if ( (_o1 instanceof PjNumber) && 497 (_o2 instanceof PjNumber) && 498 (_o3 instanceof PjNumber) && 499 (_o4 instanceof PjNumber) ) { 500 _n1 = (PjNumber)_o1; 501 _n2 = (PjNumber)_o2; 502 _n3 = (PjNumber)_o3; 503 _n4 = (PjNumber)_o4; 504 } else { 505 throw new PdfFormatException("Number expected before y."); 506 } 507 _stack.push(new Xy(_n1, _n2, _n3, _n4)); 508 } 509 else if (_token.equals("Tm")) { 510 _o6 = _stack.pop(); 511 _o5 = _stack.pop(); 512 _o4 = _stack.pop(); 513 _o3 = _stack.pop(); 514 _o2 = _stack.pop(); 515 _o1 = _stack.pop(); 516 if ( (_o1 instanceof PjNumber) && 517 (_o2 instanceof PjNumber) && 518 (_o3 instanceof PjNumber) && 519 (_o4 instanceof PjNumber) && 520 (_o5 instanceof PjNumber) && 521 (_o6 instanceof PjNumber) ) { 522 _n1 = (PjNumber)_o1; 523 _n2 = (PjNumber)_o2; 524 _n3 = (PjNumber)_o3; 525 _n4 = (PjNumber)_o4; 526 _n5 = (PjNumber)_o5; 527 _n6 = (PjNumber)_o6; 528 } else { 529 throw new PdfFormatException("Number expected before Tm."); 530 } 531 _stack.push(new XTm(_n1, _n2, _n3, _n4, _n5, _n6)); 532 } 533 else if (_token.equals("k")) { 534 _o4 = _stack.pop(); 535 _o3 = _stack.pop(); 536 _o2 = _stack.pop(); 537 _o1 = _stack.pop(); 538 if ( (_o1 instanceof PjNumber) && 539 (_o2 instanceof PjNumber) && 540 (_o3 instanceof PjNumber) && 541 (_o4 instanceof PjNumber) ) { 542 _n1 = (PjNumber)_o1; 543 _n2 = (PjNumber)_o2; 544 _n3 = (PjNumber)_o3; 545 _n4 = (PjNumber)_o4; 546 } else { 547 throw new PdfFormatException("Number expected before k."); 548 } 549 _stack.push(new Xk(_n1, _n2, _n3, _n4)); 550 } 551 else if (_token.equals("K")) { 552 _o4 = _stack.pop(); 553 _o3 = _stack.pop(); 554 _o2 = _stack.pop(); 555 _o1 = _stack.pop(); 556 if ( (_o1 instanceof PjNumber) && 557 (_o2 instanceof PjNumber) && 558 (_o3 instanceof PjNumber) && 559 (_o4 instanceof PjNumber) ) { 560 _n1 = (PjNumber)_o1; 561 _n2 = (PjNumber)_o2; 562 _n3 = (PjNumber)_o3; 563 _n4 = (PjNumber)_o4; 564 } else { 565 throw new PdfFormatException("Number expected before K."); 566 } 567 _stack.push(new XXK(_n1, _n2, _n3, _n4)); 568 } 569 else if (_token.equals("sc")) { 570 _o4 = _stack.pop(); 571 _o3 = _stack.pop(); 572 _o2 = _stack.pop(); 573 if (_stack.peek() instanceof PjNumber) { 574 _o1 = _stack.pop(); 575 } else { 576 _o1 = null; 577 } 578 if ( ( (_o1 == null) || (_o1 instanceof PjNumber) ) && 579 (_o2 instanceof PjNumber) && 580 (_o3 instanceof PjNumber) && 581 (_o4 instanceof PjNumber) ) { 582 if (_o1 == null) { 583 _n1 = (PjNumber)_o2; 584 _n2 = (PjNumber)_o3; 585 _n3 = (PjNumber)_o4; 586 _stack.push(new Xsc(_n1, _n2, _n3)); 587 } else { 588 _n1 = (PjNumber)_o1; 589 _n2 = (PjNumber)_o2; 590 _n3 = (PjNumber)_o3; 591 _n4 = (PjNumber)_o4; 592 _stack.push(new Xsc(_n1, _n2, _n3, _n4)); 593 } 594 } else { 595 throw new PdfFormatException("Number expected before sc."); 596 } 597 } 598 else if (_token.equals("SC")) { 599 _o4 = _stack.pop(); 600 _o3 = _stack.pop(); 601 _o2 = _stack.pop(); 602 if (_stack.peek() instanceof PjNumber) { 603 _o1 = _stack.pop(); 604 } else { 605 _o1 = null; 606 } 607 if ( ( (_o1 == null) || (_o1 instanceof PjNumber) ) && 608 (_o2 instanceof PjNumber) && 609 (_o3 instanceof PjNumber) && 610 (_o4 instanceof PjNumber) ) { 611 if (_o1 == null) { 612 _n1 = (PjNumber)_o2; 613 _n2 = (PjNumber)_o3; 614 _n3 = (PjNumber)_o4; 615 _stack.push(new XXSC(_n1, _n2, _n3)); 616 } else { 617 _n1 = (PjNumber)_o1; 618 _n2 = (PjNumber)_o2; 619 _n3 = (PjNumber)_o3; 620 _n4 = (PjNumber)_o4; 621 _stack.push(new XXSC(_n1, _n2, _n3, _n4)); 622 } 623 } else { 624 throw new PdfFormatException("Number expected before SC."); 625 } 626 } 627 else if (_token.equals("scn")) { 628 if (_stack.peek() instanceof PjName) { 630 _stack.pop(); 631 } 632 while (_stack.peek() instanceof PjNumber) { 633 _stack.pop(); 634 } 635 } 636 else if (_token.equals("rg")) { 637 _o3 = _stack.pop(); 638 _o2 = _stack.pop(); 639 _o1 = _stack.pop(); 640 if ( (_o1 instanceof PjNumber) && 641 (_o2 instanceof PjNumber) && 642 (_o3 instanceof PjNumber) ) { 643 _n1 = (PjNumber)_o1; 644 _n2 = (PjNumber)_o2; 645 _n3 = (PjNumber)_o3; 646 } else { 647 throw new PdfFormatException("Number expected before rg."); 648 } 649 _stack.push(new Xrg(_n1, _n2, _n3)); 650 } 651 else if (_token.equals("RG")) { 652 _o3 = _stack.pop(); 653 _o2 = _stack.pop(); 654 _o1 = _stack.pop(); 655 if ( (_o1 instanceof PjNumber) && 656 (_o2 instanceof PjNumber) && 657 (_o3 instanceof PjNumber) ) { 658 _n1 = (PjNumber)_o1; 659 _n2 = (PjNumber)_o2; 660 _n3 = (PjNumber)_o3; 661 } else { 662 throw new PdfFormatException("Number expected before RG."); 663 } 664 _stack.push(new XXRG(_n1, _n2, _n3)); 665 } 666 else if (_token.equals("re")) { 667 _o4 = _stack.pop(); 668 _o3 = _stack.pop(); 669 _o2 = _stack.pop(); 670 _o1 = _stack.pop(); 671 if ( (_o1 instanceof PjNumber) && 672 (_o2 instanceof PjNumber) && 673 (_o3 instanceof PjNumber) && 674 (_o4 instanceof PjNumber) ) { 675 _n1 = (PjNumber)_o1; 676 _n2 = (PjNumber)_o2; 677 _n3 = (PjNumber)_o3; 678 _n4 = (PjNumber)_o4; 679 } else { 680 throw new PdfFormatException("Number expected before re."); 681 } 682 _stack.push(new Xre(_n1, _n2, _n3, _n4)); 683 } 684 else if (_token.equals("Tf")) { 685 _o1 = _stack.pop(); 686 if (_o1 instanceof PjNumber) { 687 _n1 = (PjNumber)(_o1); 688 } else { 689 throw new PdfFormatException("Number (font size) expected before Tf."); 690 } 691 _o1 = _stack.pop(); 692 if (_o1 instanceof PjName) { 693 _m1 = (PjName)(_o1); 694 } else { 695 throw new PdfFormatException("Name (font name) expected before Tf."); 696 } 697 _stack.push(new XTf(_m1, _n1)); 698 } 699 else if (_token.equals("\"")) { 700 _o1 = _stack.pop(); 701 if (_o1 instanceof PjString) { 702 _s1 = (PjString)(_o1); 703 } else { 704 throw new PdfFormatException("String (text) expected before \"."); 705 } 706 _o1 = _stack.pop(); 707 if (_o1 instanceof PjNumber) { 708 _n2 = (PjNumber)(_o1); 709 } else { 710 throw new PdfFormatException("Number (a[c]) expected before \"."); 711 } 712 _o1 = _stack.pop(); 713 if (_o1 instanceof PjNumber) { 714 _n1 = (PjNumber)(_o1); 715 } else { 716 throw new PdfFormatException("Number (a[w]) expected before \"."); 717 } 718 _stack.push(new Xquot(_n1, _n2, _s1)); 719 } 720 else if (_token.equals("BMC")) { 721 _o1 = _stack.pop(); 722 if (_o1 instanceof PjName) { 723 _m1 = (PjName)(_o1); 724 } else { 725 throw new PdfFormatException("Name (tag) expected before BMC."); 726 } 727 _stack.push(new XBMC(_m1)); 728 } 729 else if (_token.equals("MP")) { 730 _o1 = _stack.pop(); 731 if (_o1 instanceof PjName) { 732 _m1 = (PjName)(_o1); 733 } else { 734 throw new PdfFormatException("Name (tag) expected before MP."); 735 } 736 _stack.push(new XMP(_m1)); 737 } 738 else if (_token.equals("gs")) { 739 _o1 = _stack.pop(); 740 if (_o1 instanceof PjName) { 741 _m1 = (PjName)(_o1); 742 } else { 743 throw new PdfFormatException("Name (name) expected before gs."); 744 } 745 _stack.push(new Xgs(_m1)); 746 } 747 else if (_token.equals("Do")) { 748 _o1 = _stack.pop(); 749 if (_o1 instanceof PjName) { 750 _m1 = (PjName)(_o1); 751 } else { 752 throw new PdfFormatException("Name (XObject) expected before Do."); 753 } 754 _stack.push(new XDo(_m1)); 755 } 756 else if (_token.equals("cs")) { 757 _o1 = _stack.pop(); 758 if (_o1 instanceof PjName) { 759 _m1 = (PjName)(_o1); 760 } else { 761 throw new PdfFormatException("Name (color space) expected before cs."); 762 } 763 _stack.push(new Xcs(_m1)); 764 } 765 else if (_token.equals("CS")) { 766 _o1 = _stack.pop(); 767 if (_o1 instanceof PjName) { 768 _m1 = (PjName)(_o1); 769 } else { 770 throw new PdfFormatException("Name (color space) expected before CS."); 771 } 772 _stack.push(new XXCS(_m1)); 773 } 774 else if (_token.equals("d")) { 775 _o1 = _stack.pop(); 776 if (_o1 instanceof PjNumber) { 777 _n1 = (PjNumber)(_o1); 778 } else { 779 throw new PdfFormatException("Number (phase) expected before d."); 780 } 781 _o1 = _stack.pop(); 782 if (_o1 instanceof PjArray) { 783 _a1 = (PjArray)(_o1); 784 } else { 785 throw new PdfFormatException("Array (dash pattern) expected before d."); 786 } 787 _stack.push(new Xd(_a1, _n1)); 788 } 789 else if (_token.equals("TJ")) { 790 _o1 = _stack.pop(); 791 if (_o1 instanceof PjArray) { 792 _a1 = (PjArray)(_o1); 793 } else { 794 throw new PdfFormatException("Array expected before TJ."); 795 } 796 _stack.push(new XXTJ(_a1)); 797 } 798 else if (_token.equals("BDC")) { 799 _o1 = _stack.pop(); 800 if ( ( ! (_o1 instanceof PjName) ) && 801 ( ! (_o1 instanceof PjDictionary) ) ) { 802 throw new PdfFormatException("Name or dictionary (property list) expected before BDC."); 803 } 804 _o2 = _stack.pop(); 805 if (_o2 instanceof PjName) { 806 _m1 = (PjName)(_o2); 807 } else { 808 throw new PdfFormatException("Name (tag) expected before BDC."); 809 } 810 if (_o1 instanceof PjName) { 811 _stack.push(new XBDC(_m1, (PjName)(_o1))); 812 } else { 813 _stack.push(new XBDC(_m1, (PjDictionary)(_o1))); 814 } 815 } 816 else if (_token.equals("DP")) { 817 _o1 = _stack.pop(); 818 if ( ( ! (_o1 instanceof PjName) ) && 819 ( ! (_o1 instanceof PjDictionary) ) ) { 820 throw new PdfFormatException("Name or dictionary (property list) expected before DP."); 821 } 822 _o2 = _stack.pop(); 823 if (_o2 instanceof PjName) { 824 _m1 = (PjName)(_o2); 825 } else { 826 throw new PdfFormatException("Name (tag) expected before DP."); 827 } 828 if (_o1 instanceof PjName) { 829 _stack.push(new XDP(_m1, (PjName)(_o1))); 830 } else { 831 _stack.push(new XDP(_m1, (PjDictionary)(_o1))); 832 } 833 } 834 else if (_token.equals("Tj")) { 835 _o1 = _stack.pop(); 836 if (_o1 instanceof PjString) { 837 _s1 = (PjString)(_o1); 838 } else { 839 throw new PdfFormatException("String (text) expected before Tj."); 840 } 841 _stack.push(new XTj(_s1)); 842 } 843 else if (_token.equals("'")) { 844 _o1 = _stack.pop(); 845 if (_o1 instanceof PjString) { 846 _s1 = (PjString)(_o1); 847 } else { 848 throw new PdfFormatException("String (text) expected before '."); 849 } 850 _stack.push(new Xapost(_s1)); 851 } 852 else if (_token.equals("n")) { 853 _stack.push(new Xn()); 854 } 855 else if (_token.equals("s")) { 856 _stack.push(new Xs()); 857 } 858 else if (_token.equals("S")) { 859 _stack.push(new XXS()); 860 } 861 else if (_token.equals("T*")) { 862 _stack.push(new XTstar()); 863 } 864 else if ( (_token.equals("f")) || (_token.equals("F")) ) { 865 _stack.push(new Xf()); 866 } 867 else if (_token.equals("f*")) { 868 _stack.push(new Xfstar()); 869 } 870 else if (_token.equals("b")) { 871 _stack.push(new Xb()); 872 } 873 else if (_token.equals("W")) { 874 _stack.push(new XXW()); 875 } 876 else if (_token.equals("W*")) { 877 _stack.push(new XWstar()); 878 } 879 else if (_token.equals("b*")) { 880 _stack.push(new Xbstar()); 881 } 882 else if (_token.equals("B")) { 883 _stack.push(new XXB()); 884 } 885 else if (_token.equals("B*")) { 886 _stack.push(new XXBstar()); 887 } 888 else if (_token.equals("h")) { 889 _stack.push(new Xh()); 890 } 891 else if (_token.equals("q")) { 892 _stack.push(new Xq()); 893 } 894 else if (_token.equals("Q")) { 895 _stack.push(new XXQ()); 896 } 897 else if (_token.equals("BX")) { 898 _stack.push(new XBX()); 899 } 900 else if (_token.equals("EX")) { 901 _stack.push(new XEX()); 902 } 903 else if (_token.equals("EMC")) { 904 _stack.push(new XEMC()); 905 } 906 else if (_token.charAt(0) == '/') { 907 _stack.push(new PjName(_token.substring(1))); 908 } 909 else if ( 910 (Character.isDigit(_token.charAt(0))) 911 || (_token.charAt(0) == '-') 912 || (_token.charAt(0) == '.') ) { 913 _stack.push(new PjNumber(new Float (_token).floatValue())); 914 } 915 else if (_token.charAt(0) == '(') { 916 _stack.push(new PjString(PjString.decodePdf(_token))); 917 } 918 else if (_token.equals("<<")) { 919 _stack.push("<<"); 920 } 921 else if (_token.equals("[")) { 922 _stack.push("["); 923 } 924 else if (_token.equals("]")) { 925 _b1 = false; 926 _v1 = new Vector(); 927 while ( ! _b1 ) { 928 _o1 = _stack.pop(); 929 if ( (_o1 instanceof String ) && 930 (((String )_o1).equals("[")) ) { 931 _b1 = true; 932 } else { 933 if ( ! (_o1 instanceof PjObject) ) { 934 throw new PdfFormatException("PDF object expected within array."); 935 } 936 _v1.insertElementAt(_o1, 0); 937 } 938 } 939 _stack.push(new PjArray(_v1)); 940 } 941 else if (_token.equals("true")) { 942 _stack.push(new PjBoolean(true)); 943 } 944 else if (_token.equals("false")) { 945 _stack.push(new PjBoolean(false)); 946 } 947 else if ( (_token.equals("scn")) || 948 (_token.equals("SCN")) || 949 (_token.equals("sh")) || 950 (_token.equals("ri")) ) { 951 _stack.pop(); 953 } 954 else if (_token.startsWith("%%")) { 955 } 957 else { 958 throw new PdfFormatException("Token \"" + _token + "\" not recognized."); 959 } 960 } 961 962 private String _token; 963 private byte[] _imageData; 964 private Stack _stack; 965 private int _counter; 966 private byte[] _buffer; 967 private boolean _image; 968 969 private PjNumber _n1, _n2, _n3, _n4, _n5, _n6; 970 private Stack _k1; 971 private boolean _b1; 972 private int _x1, _x2; 973 private Vector _v1; 974 private Object _o1, _o2, _o3, _o4, _o5, _o6; 975 private PjName _m1; 976 private PjString _s1; 977 private PjArray _a1; 978 private Hashtable _h1; 979 980 } 981 | Popular Tags |