PDFPageTreeNode.java 36.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.internal.pdftoolkit.core.cos.CosArray
 *  com.adobe.internal.pdftoolkit.core.cos.CosDictionary
 *  com.adobe.internal.pdftoolkit.core.cos.CosObject
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFUnsupportedFeatureException
 *  com.adobe.internal.pdftoolkit.core.types.ASName
 *  com.adobe.internal.util.ArrayListStack
 */
package com.adobe.internal.pdftoolkit.pdf.page;

import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFUnsupportedFeatureException;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosArrayList;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosDictionary;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObjectContainer;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosUtils;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFResources;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFRectangle;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFRotation;
import com.adobe.internal.pdftoolkit.pdf.interactive.action.PDFAdditionalActions;
import com.adobe.internal.pdftoolkit.pdf.interactive.action.PDFAdditionalActionsPage;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotation;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationIterator;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationList;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPage;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPageTreeList;
import com.adobe.internal.pdftoolkit.pdf.utils.PDFUtil;
import com.adobe.internal.util.ArrayListStack;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class PDFPageTreeNode
extends PDFCosDictionary {
    static final int MAX_NODES = 5;

    protected PDFPageTreeNode(CosObject cosObject) throws PDFInvalidDocumentException {
        super(cosObject);
    }

    PDFPageTreeNode next() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFPageTreeNode parent = this.getParent();
        if (parent == null) {
            return null;
        }
        PDFPageTreeList kids = parent.getKids();
        if (kids == null) {
            return null;
        }
        int thisInd = kids.indexOf(this) + 1;
        return thisInd < kids.size() ? (PDFPageTreeNode)kids.get(thisInd) : null;
    }

    public static PDFPageTreeNode getInstance(CosObject cosObject) throws PDFInvalidDocumentException {
        if (PDFCosObject.checkNullCosObject(cosObject) == null) {
            return null;
        }
        PDFPageTreeNode pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFPageTreeNode.class);
        if (pdfObject == null) {
            pdfObject = new PDFPageTreeNode(cosObject);
        }
        return pdfObject;
    }

    static PDFPageTreeNode newInstance(PDFDocument pdfDocument, PDFPageTreeNode parent) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosDictionary nodeDict = PDFCosObject.newCosDictionary(pdfDocument);
        PDFPageTreeNode node = PDFPageTreeNode.getInstance((CosObject)nodeDict);
        node.setDictionaryNameValue(ASName.k_Type, ASName.k_Pages);
        node.setCount(0);
        PDFPageTreeList kids = PDFPageTreeList.newInstance(pdfDocument);
        node.setKids(kids);
        if (parent != null) {
            node.setParent(parent);
        }
        return node;
    }

    public Iterator<PDFPage> iterator() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return new PageIterator(this);
    }

    public PDFPageTreePagesIterator oldIterator() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return new PDFPageTreePagesIterator(this, null);
    }

    public Iterator<PDFPage> iterator(int index) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PageIterator iter = new PageIterator(this);
        while (iter.hasNext() && index-- > 0) {
            iter.next();
        }
        return iter;
    }

    public Iterator<PDFPage> iterator(PDFPageTreeNode startingNode) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PageIterator iter = new PageIterator(this);
        if (startingNode != null) {
            while (iter.hasNext() && !PDFUtil.isPDFCosObjectRefEqual(startingNode, (PDFCosObject)iter.next())) {
            }
        }
        return iter;
    }

    public PDFPageTreePagesIterator pagesIterator(PDFPageTreeNode startingNode) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFPageTreeNode parentNode = startingNode == null ? this : startingNode.getParent();
        return new PDFPageTreePagesIterator(parentNode, startingNode);
    }

    public PDFAnnotationIterator getAnnotationsIterator() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return new PDFPagesAnnotationIterator(this);
    }

    public int getCount() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.getDictionaryIntValue(ASName.k_Count);
    }

    void setCount(int count) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryIntValue(ASName.k_Count, count);
    }

    void incrementCount() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        int count = this.getCount();
        this.setCount(count + 1);
        PDFPageTreeNode parentNode = this.getParent();
        if (parentNode != null) {
            parentNode.incrementCount();
        }
    }

    void decrementCount() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        int count = this.getCount();
        this.setCount(count - 1);
        PDFPageTreeNode parentNode = this.getParent();
        if (parentNode != null) {
            parentNode.decrementCount();
        }
    }

    public int getPageIndex() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        int pageCount = 0;
        PDFPageTreeNode parent = this.getParent();
        if (parent != null && parent.getKids() != null) {
            PDFPageTreeNode nextNode;
            Iterator parentIter = parent.getKids().iterator();
            while (parentIter.hasNext() && !PDFUtil.isPDFCosObjectRefEqual(this, nextNode = (PDFPageTreeNode)parentIter.next())) {
                if (nextNode instanceof PDFPage) {
                    ++pageCount;
                    continue;
                }
                pageCount += nextNode.getCount();
            }
            pageCount += parent.getPageIndex();
        }
        return pageCount;
    }

    PDFPageTreeNode getParent() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return PDFPageTreeNode.getInstance(this.getDictionaryCosObjectValue(ASName.k_Parent));
    }

    void setParent(PDFPageTreeNode parent) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryValue(ASName.k_Parent, parent);
    }

    public boolean appendKid(PDFPage page) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFPageTreeList kids = this.getKids();
        kids.add(page);
        page.setDictionaryValue(ASName.k_Parent, this);
        this.incrementCount();
        return true;
    }

    PDFPageTreeList getKids() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return PDFPageTreeList.getInstance(this.getDictionaryCosObjectValue(ASName.k_Kids));
    }

    void setKids(PDFPageTreeList kids) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryArrayValue(ASName.k_Kids, kids.getCosArray());
    }

    public void removeKids() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.removeValue(ASName.k_Kids);
    }

    public void removeKid(PDFPageTreeNode kid) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFPageTreeList kids = this.getKids();
        Iterator kidsIter = kids.iterator();
        while (kidsIter.hasNext()) {
            PDFPageTreeNode currentKid = (PDFPageTreeNode)kidsIter.next();
            if (!PDFUtil.isPDFCosObjectRefEqual(currentKid, kid)) continue;
            kidsIter.remove();
            if (kid instanceof PDFPage) {
                this.decrementCount();
            } else {
                this.setCount(this.getCount() - kid.getCount());
            }
            return;
        }
    }

    public void treeRemoveKid(PDFPageTreeNode kid) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.removeKid(kid);
        if (this.getCount() == 0) {
            this.getParent().treeRemoveKid(this);
        }
    }

    public void balancePages() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFPageTreeNode newRoot;
        int pageCount = this.getCount();
        if (pageCount < 10) {
            return;
        }
        PDFPageTreeNode curNode = newRoot = PDFPageTreeNode.newInstance(this.getPDFDocument(), null);
        while (pageCount > 0) {
            PDFPageTreeNode childNode = PDFPageTreeNode.newInstance(this.getPDFDocument(), curNode);
            curNode.getKids().add(childNode);
            curNode = childNode;
            if ((pageCount /= 5) >= 10) continue;
            break;
        }
        Iterator<PDFPage> pagesIterator = this.iterator();
        while (pagesIterator.hasNext()) {
            PDFPage curPage = pagesIterator.next();
            PDFResources curPageResources = curPage.getResources();
            if (curPageResources == null) {
                curPageResources = PDFResources.newInstance(this.getPDFDocument());
            }
            curPage.setResources(curPageResources);
            curNode = this.addBalancedNode(curPage, curNode, true);
        }
        this.optimizeResources(curNode);
        PDFPageTreeList kids = newRoot.getKids();
        Iterator kidsIterator = kids.iterator();
        while (kidsIterator.hasNext()) {
            ((PDFPageTreeNode)kidsIterator.next()).setParent(this);
        }
        this.setKids(newRoot.getKids());
        this.optimizeResources(this);
    }

    private PDFPageTreeNode addBalancedNode(PDFPageTreeNode node, PDFPageTreeNode parent, boolean counting) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFPageTreeNode parentsParent = parent.getParent();
        PDFPageTreeList kids = parent.getKids();
        if (kids.size() < 5 || parentsParent == null) {
            kids.add(node);
            node.setParent(parent);
            if (counting) {
                parent.incrementCount();
            }
            return parent;
        }
        this.optimizeResources(parent);
        PDFPageTreeNode newParent = PDFPageTreeNode.newInstance(this.getPDFDocument(), parentsParent);
        this.addBalancedNode(newParent, parentsParent, false);
        this.addBalancedNode(node, newParent, counting);
        return newParent;
    }

    private void optimizeResources(PDFPageTreeNode parent) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFPageTreeList kids = parent.getKids();
        if (kids == null) {
            return;
        }
        HashMap<PDFResources, int[]> resources = new HashMap<PDFResources, int[]>(5, 1.0f);
        for (PDFPageTreeNode curNode2 : kids) {
            PDFResources curResources = curNode2.getResources();
            if (curResources == null) continue;
            if (resources.containsKey(curResources)) {
                int[] arrn = (int[])resources.get(curResources);
                arrn[0] = arrn[0] + 1;
                continue;
            }
            int[] count = new int[]{1};
            resources.put(curResources, count);
        }
        PDFResources parentResources = null;
        int maxCount = 0;
        for (Map.Entry curEntry : resources.entrySet()) {
            int curCount = ((int[])curEntry.getValue())[0];
            if (curCount <= maxCount) continue;
            parentResources = (PDFResources)curEntry.getKey();
            maxCount = curCount;
        }
        if (maxCount > 2) {
            parent.setResources(parentResources);
            for (PDFPageTreeNode curNode2 : kids) {
                if (parentResources == null || !parentResources.equals(curNode2.getResources())) continue;
                curNode2.setResources(null);
            }
        }
    }

    public PDFResources getResources() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.containsKey(ASName.k_Resources)) {
            return PDFResources.getInstance(this.getDictionaryCosObjectValue(ASName.k_Resources));
        }
        PDFPageTreeNode parent = this.getParent();
        if (parent != null) {
            return parent.getResources();
        }
        return null;
    }

    public void setResources(PDFResources resources) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryValue(ASName.k_Resources, resources);
    }

    public PDFRectangle getMediaBox() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.containsKey(ASName.k_MediaBox)) {
            return PDFRectangle.getInstance(this.getDictionaryCosObjectValue(ASName.k_MediaBox));
        }
        PDFPageTreeNode parent = this.getParent();
        if (parent != null) {
            return parent.getMediaBox();
        }
        return null;
    }

    public PDFRectangle getCropBox() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.containsKey(ASName.k_CropBox)) {
            return PDFRectangle.getInstance(this.getDictionaryCosObjectValue(ASName.k_CropBox));
        }
        PDFPageTreeNode parent = this.getParent();
        if (parent != null) {
            return parent.getCropBox();
        }
        return null;
    }

    public PDFAdditionalActionsPage getAdditionalActions() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return PDFAdditionalActionsPage.getInstance(this.getDictionaryCosObjectValue(ASName.k_AA));
    }

    public PDFAdditionalActions procureAdditionalActions() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (PDFAdditionalActionsPage.getInstance(PDFCosUtils.getInheritableValue(ASName.k_AA, ASName.k_Parent, this)) != null) {
            return PDFAdditionalActionsPage.getInstance(PDFCosUtils.getInheritableValue(ASName.k_AA, ASName.k_Parent, this));
        }
        PDFAdditionalActionsPage page = PDFAdditionalActionsPage.newInstance(this.getPDFDocument());
        this.setAdditionalActions(page);
        return page;
    }

    public void setAdditionalActions(PDFAdditionalActionsPage additionalActions) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryValue(ASName.k_AA, additionalActions);
    }

    public PDFRotation getRotation() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.containsKey(ASName.k_Rotate)) {
            try {
                return PDFRotation.getInstance(this.getDictionaryIntValue(ASName.k_Rotate));
            }
            catch (PDFInvalidParameterException e) {
                throw new PDFInvalidDocumentException("Invalid page rotation", (Throwable)e);
            }
        }
        PDFPageTreeNode parent = this.getParent();
        if (parent != null) {
            return parent.getRotation();
        }
        return null;
    }

    public void setRotation(PDFRotation angle) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryIntValue(ASName.k_Rotate, angle.getValue());
    }

    private boolean containsKey(ASName key, boolean inheritable) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.containsKey(key)) {
            return true;
        }
        if (inheritable) {
            PDFPageTreeNode parent = this.getParent();
            if (parent != null) {
                return parent.containsKey(key, inheritable);
            }
            return false;
        }
        return false;
    }

    protected boolean containsResources() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.containsKey(ASName.k_Resources, true);
    }

    protected boolean containsMediaBox() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.containsKey(ASName.k_MediaBox, true);
    }

    protected boolean containsCropBox() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.containsKey(ASName.k_CropBox, true);
    }

    protected boolean containsRotation() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.containsKey(ASName.k_Rotate, true);
    }

    private void debugPrint(PrintWriter o) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.getKids() == null) {
            this.debugPrintLeaf(o);
        } else {
            this.debugPrintTree(o);
        }
    }

    private void debugPrintLeaf(PrintWriter o) {
        PrintStream out = System.out;
        out.println("--Begin PDFPageTreeNode(Leaf)--:" + this.hashCode());
        out.println("mCosObject:" + this.getCosObject().toString());
        out.println("--End PDFPageTreeNode(Leaf)--" + this.hashCode());
    }

    private void debugPrintTree(PrintWriter o) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PrintStream out = System.out;
        int kSize = this.getKids().size();
        out.println("--Begin PDFPageTreeNode(Tree)--" + this.hashCode() + " has " + kSize + " kids");
        out.println("mCosObject:" + this.getCosObject().toString());
        Iterator iter = this.getKids().iterator();
        int i = 0;
        while (iter.hasNext()) {
            PDFPageTreeNode node = (PDFPageTreeNode)iter.next();
            out.println("Kid:" + node.hashCode() + "(" + ++i + "of" + kSize + ") Of Obj:" + this.hashCode());
            node.debugPrint(o);
        }
        out.println("--End PDFPageTreeNode(Tree)--" + this.hashCode());
    }

    int indexOfParent(PDFPageTreeNode node) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFPageTreeList kids = this.getKids();
        int i = 0;
        Iterator iter = kids.iterator();
        while (iter.hasNext() && !PDFUtil.isPDFCosObjectRefEqual(node, (PDFCosObject)iter.next())) {
            ++i;
        }
        return i;
    }

    static class PDFPagesAnnotationIterator
    implements PDFAnnotationIterator {
        Iterator<PDFPage> mPagesIterator = null;
        PDFAnnotationIterator mAnnotsIterator = null;

        PDFPagesAnnotationIterator(PDFPageTreeNode root) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            this.mPagesIterator = root.iterator();
            if (this.mPagesIterator == null && root instanceof PDFPage) {
                PDFPage nextPage = (PDFPage)root;
                PDFAnnotationList annots = nextPage.getAnnotationList();
                this.mAnnotsIterator = annots == null ? null : annots.iterator();
            }
        }

        public void remove() throws PDFUnsupportedFeatureException {
            this.mAnnotsIterator.remove();
        }

        public boolean hasNext() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            if (this.mAnnotsIterator != null && this.mAnnotsIterator.hasNext()) {
                return true;
            }
            if (this.mPagesIterator == null) {
                return false;
            }
            while (this.mAnnotsIterator == null || !this.mAnnotsIterator.hasNext()) {
                this.mAnnotsIterator = this.getNextAnnotsIterator();
                if (this.mAnnotsIterator != null) continue;
                return false;
            }
            return true;
        }

        public PDFAnnotation next() {
            return this.mAnnotsIterator.next();
        }

        private PDFAnnotationIterator getNextAnnotsIterator() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            PDFAnnotationIterator nextAnnotsIterator = null;
            while (nextAnnotsIterator == null && this.mPagesIterator.hasNext()) {
                PDFPage nextPage = this.mPagesIterator.next();
                PDFAnnotationList annots = nextPage.getAnnotationList();
                if (annots == null) continue;
                nextAnnotsIterator = annots.iterator();
            }
            return nextAnnotsIterator;
        }
    }

    public class PDFPageTreePagesIterator {
        PDFPageTreeNode mParentNode;
        Iterator<PDFPageTreeNode> mParentKidsIter;
        Iterator<PDFPage> mNodeIterator;
        private Set<Integer> visitedSet;
        private Integer lastVisitedPage;

        private PDFPageTreePagesIterator(PDFPageTreeNode parentNode, PDFPageTreeNode startingNode) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            PDFPageTreeNode curParent = parentNode;
            PDFPageTreeList parentKids = null;
            this.mParentKidsIter = null;
            int kidsInd = 0;
            this.mParentNode = curParent != null ? curParent.getParent() : null;
            this.visitedSet = new HashSet<Integer>();
            if (this.mParentNode != null) {
                parentKids = this.mParentNode.getKids();
            }
            if (parentKids != null) {
                kidsInd = parentKids.indexOf(curParent) + 1;
                this.mParentKidsIter = parentKids.iterator(kidsInd);
            }
            if (parentNode != null) {
                if (startingNode != null) {
                    this.mNodeIterator = parentNode.iterator(startingNode);
                    this.lastVisitedPage = startingNode.getCosObject().getObjNum();
                    this.visitedSet.add(this.lastVisitedPage);
                } else {
                    this.mNodeIterator = parentNode.iterator();
                }
            }
        }

        public boolean hasNext() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            PDFPageTreeNode curNode;
            if (this.mNodeIterator != null && this.mNodeIterator.hasNext()) {
                if (!this.visitedSet.contains(this.mNodeIterator.next().getCosObject().getObjNum())) {
                    return true;
                }
                throw new RuntimeException("Cycle has been detected in page tree");
            }
            if (this.mParentKidsIter != null && this.mParentKidsIter.hasNext()) {
                curNode = this.mParentKidsIter.next();
            } else {
                curNode = this.mParentNode;
                this.mParentNode = this.mParentNode.getParent();
                if (this.mParentNode == null) {
                    return false;
                }
                PDFPageTreeList parentKids = this.mParentNode.getKids();
                if (parentKids == null) {
                    this.mParentKidsIter = null;
                } else {
                    int kidsInd = parentKids.indexOf(curNode) + 1;
                    this.mParentKidsIter = parentKids.iterator(kidsInd);
                }
                curNode = this.mParentKidsIter != null && this.mParentKidsIter.hasNext() ? this.mParentKidsIter.next() : null;
            }
            this.mNodeIterator = curNode != null ? curNode.iterator() : null;
            return this.hasNext();
        }

        public PDFPage next() {
            if (this.mNodeIterator != null) {
                PDFPage page = this.mNodeIterator.next();
                if (page != null) {
                    this.lastVisitedPage = page.getCosObject().getObjNum();
                }
                this.visitedSet.add(this.lastVisitedPage);
                return page;
            }
            return null;
        }

        public void remove() {
            if (this.mNodeIterator != null) {
                this.mNodeIterator.remove();
                this.visitedSet.remove(this.lastVisitedPage);
            }
        }
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    protected static class PageIterator
    implements Iterator<PDFPage> {
        private final ArrayListStack nodeStack = new ArrayListStack();
        private PDFPage currentPage;
        private PDFPage nextPage;
        private Set<Integer> vistedSet;

        protected PageIterator(PDFPageTreeNode rootNode) {
            this.nodeStack.push((Object)rootNode);
            this.vistedSet = new HashSet<Integer>();
        }

        @Override
        public boolean hasNext() {
            boolean retValue = false;
            if (this.nextPage == null) {
                try {
                    this.nextPage = this.getNext();
                }
                catch (PDFException e) {
                    // empty catch block
                }
            }
            if (this.nextPage != null) {
                if (!this.vistedSet.contains(this.nextPage.getCosObject().getObjNum())) {
                    retValue = true;
                } else {
                    throw new RuntimeException("Cycle has been detected in page tree");
                }
            }
            return retValue;
        }

        @Override
        public PDFPage next() {
            if (this.nextPage == null) {
                try {
                    this.currentPage = this.getNext();
                }
                catch (PDFException e) {
                    return null;
                }
            } else {
                this.currentPage = this.nextPage;
                Integer value = this.currentPage.getCosObject().getObjNum();
                if (value != 0) {
                    this.vistedSet.add(value);
                }
                this.nextPage = null;
            }
            return this.currentPage;
        }

        @Override
        public void remove() {
            if (this.currentPage == null) {
                throw new IllegalStateException("Can't remove page.");
            }
            try {
                PDFPageTreeNode parent = this.currentPage.getParent();
                this.vistedSet.remove(this.currentPage.getCosObject().getObjNum());
                parent.removeKid(this.currentPage);
                this.currentPage = null;
            }
            catch (PDFException e) {
                IllegalStateException ise = new IllegalStateException("Can't remove page.");
                ise.initCause((Throwable)e);
                throw ise;
            }
        }

        private PDFPage getNext() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            PDFPageTreeNode top = null;
            PDFPage next = null;
            while (!this.nodeStack.empty()) {
                top = (PDFPageTreeNode)this.nodeStack.pop();
                if (top instanceof PDFPage) {
                    next = (PDFPage)top;
                    break;
                }
                PDFPageTreeList nodeList = top.getKids();
                for (int i = nodeList.size() - 1; i >= 0; --i) {
                    this.nodeStack.push(nodeList.get(i));
                }
            }
            return next;
        }
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    class PDFPageTreeNodeIterator
    implements Iterator<PDFPage> {
        private PDFPageTreeNode mCurNode;
        private PDFPageTreeNodeIterator mCurNodeSubtreeIterator;
        private PDFPageTreeNodeListIterator mRootsChildrenIterator;
        private PDFPageTreeNode mRoot;
        private Set<Integer> visitedSet;

        private void initialize(PDFPageTreeNode root) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            this.mRoot = root;
            this.mCurNode = null;
            this.mCurNodeSubtreeIterator = null;
            this.mRootsChildrenIterator = null;
            this.visitedSet = new HashSet<Integer>();
            this.visitedSet.add(root.getCosObject().getObjNum());
            PDFPageTreeList kids = root.getKids();
            if (kids != null) {
                this.mRootsChildrenIterator = new PDFPageTreeNodeListIterator(kids, this.visitedSet);
            }
        }

        PDFPageTreeNodeIterator(PDFPageTreeNode root) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            this.initialize(root);
        }

        PDFPageTreeNodeIterator(PDFPageTreeNode parent, int index) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            this(parent);
            while (this.hasNext() && index > 0) {
                this.next();
                --index;
            }
        }

        @Override
        public boolean hasNext() {
            if (this.mRootsChildrenIterator == null) {
                return false;
            }
            if (this.mCurNode == null) {
                return this.mRootsChildrenIterator.hasNext();
            }
            if (this.mCurNode instanceof PDFPage) {
                return this.mRootsChildrenIterator.hasNext();
            }
            if (this.mCurNodeSubtreeIterator.hasNext()) {
                return true;
            }
            return this.mRootsChildrenIterator.hasNext();
        }

        @Override
        public PDFPage next() {
            try {
                if (this.mRootsChildrenIterator == null) {
                    return null;
                }
                if (this.mCurNode == null) {
                    this.mCurNode = (PDFPageTreeNode)this.mRootsChildrenIterator.next();
                } else if (this.mCurNode instanceof PDFPage) {
                    this.mCurNode = (PDFPageTreeNode)this.mRootsChildrenIterator.next();
                }
                if (this.mCurNode == null) {
                    return null;
                }
                if (this.mCurNode instanceof PDFPage) {
                    this.mCurNodeSubtreeIterator = null;
                    this.visitedSet.add(((PDFPage)this.mCurNode).getCosObject().getObjNum());
                    return (PDFPage)this.mCurNode;
                }
                if (this.mCurNodeSubtreeIterator == null) {
                    this.mCurNodeSubtreeIterator = (PDFPageTreeNodeIterator)this.mCurNode.iterator();
                }
                PDFPage ret_obj = null;
                if (this.mCurNodeSubtreeIterator.hasNext()) {
                    ret_obj = this.mCurNodeSubtreeIterator.next();
                    if (ret_obj == null) {
                        throw new RuntimeException("Assumption Violation in PDFPageTreeNode : TreeNode has no leaf pages");
                    }
                    return ret_obj;
                }
                this.initCurNode();
                return this.next();
            }
            catch (PDFException e) {
                NoSuchElementException newException = new NoSuchElementException("Error during PDFPageTreeNodeIterator.next().");
                newException.initCause((Throwable)e);
                throw newException;
            }
        }

        @Override
        public void remove() {
            try {
                if (this.mCurNode instanceof PDFPage) {
                    this.mRoot.decrementCount();
                    this.mRootsChildrenIterator.remove();
                    this.visitedSet.remove(((PDFPage)this.mCurNode).getCosObject().getObjNum());
                    if (this.mRoot.getCount() == 0) {
                        this.initCurNode();
                    }
                } else if (this.mCurNodeSubtreeIterator.subremove()) {
                    this.mRootsChildrenIterator.remove();
                    if (this.mRoot.getCount() == 0) {
                        this.initCurNode();
                    }
                }
            }
            catch (PDFException e) {
                IllegalStateException newException = new IllegalStateException("Error during PDFPageTreeNodeIterator.remove().");
                newException.initCause((Throwable)e);
                throw newException;
            }
        }

        private boolean subremove() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            if (this.mCurNode instanceof PDFPage) {
                this.mRoot.decrementCount();
                this.mRootsChildrenIterator.remove();
                if (this.mRoot.getCount() == 0) {
                    this.mRoot.removeKids();
                    return true;
                }
                return false;
            }
            if (this.mCurNodeSubtreeIterator.subremove()) {
                this.mRootsChildrenIterator.remove();
                if (this.mRoot.getCount() == 0) {
                    return true;
                }
                return false;
            }
            return false;
        }

        private void initCurNode() {
            this.mCurNode = null;
            this.mCurNodeSubtreeIterator = null;
        }
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    class PDFPageTreeNodeListIterator
    extends PDFCosArrayList<PDFPageTreeNode> {
        private Set<Integer> visitedSet;

        PDFPageTreeNodeListIterator(PDFPageTreeList kids, Set<Integer> visitedSet) {
            PDFPageTreeList pDFPageTreeList = kids;
            pDFPageTreeList.getClass();
            super((PDFCosArrayList)pDFPageTreeList, kids.getCosArray());
            this.visitedSet = visitedSet;
        }

        public boolean hasNext() {
            if (!PDFCosArrayList.PDFCosArrayListIterator.super.hasNext()) {
                return false;
            }
            PDFPageTreeNode curNode = (PDFPageTreeNode)PDFCosArrayList.PDFCosArrayListIterator.super.next();
            if (!(curNode instanceof PDFPage)) {
                PDFPageTreeList kids = null;
                try {
                    kids = curNode.getKids();
                }
                catch (PDFException e) {
                    throw new RuntimeException("PDF error in Page Tree iterator", (Throwable)e);
                }
                if (kids != null && kids.isEmpty()) {
                    return this.hasNext();
                }
            }
            PDFCosArrayList.PDFCosArrayListIterator.super.previous();
            if (!this.visitedSet.contains(curNode.getCosObject().getObjNum())) {
                throw new RuntimeException("Cycle has been detected in page tree");
            }
            return true;
        }

        public boolean hasPrevious() {
            if (!PDFCosArrayList.PDFCosArrayListIterator.super.hasPrevious()) {
                return false;
            }
            Object curNode = PDFCosArrayList.PDFCosArrayListIterator.super.previous();
            if (!(curNode instanceof PDFPage)) {
                PDFPageTreeList kids = null;
                try {
                    kids = ((PDFPageTreeNode)curNode).getKids();
                }
                catch (PDFException e) {
                    throw new RuntimeException("PDF error in Page Tree iterator", (Throwable)e);
                }
                if (kids != null && kids.isEmpty()) {
                    return this.hasPrevious();
                }
            }
            PDFCosArrayList.PDFCosArrayListIterator.super.next();
            return true;
        }
    }

}