PDFTree.java 19 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
/*
 * 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.cos.CosScalar
 *  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.types.ASName
 */
package com.adobe.internal.pdftoolkit.pdf.document;

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.cos.CosScalar;
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.types.ASName;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFTreeLeaf;
import com.adobe.internal.pdftoolkit.pdf.document.PDFTreeList;
import com.adobe.internal.pdftoolkit.pdf.document.PDFTreeNode;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ListIterator;
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 abstract class PDFTree<K, V>
extends PDFTreeNode<K, V> {
    protected PDFTree(CosObject cosObject, ASName nameDictionaryKey) throws PDFInvalidDocumentException {
        super(cosObject, nameDictionaryKey);
    }

    private void addFirstEntry(CosScalar name, CosObject value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        PDFDocument pdfDocument = this.getPDFDocument();
        ASName keyName = this.getNameDictionaryKey();
        PDFTreeLeaf leaf = PDFTreeLeaf.newInstance(pdfDocument, null, keyName, name, value);
        PDFTreeList kids = PDFTreeList.newInstance(pdfDocument, null, keyName, leaf);
        this.getCosDictionary().put(ASName.k_Kids, kids.getCosObject());
    }

    private void setChildren(PDFTreeList<K, V> kids) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.getCosDictionary().put(ASName.k_Kids, kids.getCosObject());
    }

    private PDFTreeLeaf<K, V> getLeaf() throws PDFInvalidDocumentException {
        return PDFTreeLeaf.getInstance(this.getCosObject(), null, this.getNameDictionaryKey());
    }

    @Override
    protected void resetLimits() {
    }

    @Override
    protected void setLimitLower(CosScalar lower) {
    }

    @Override
    protected void setLimitUpper(CosScalar upper) {
    }

    @Override
    protected int inRange(CosScalar name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.hasChildren()) {
            return PDFTreeNode.super.inRange(name);
        }
        PDFTreeLeaf<K, V> leaf = this.getLeaf();
        if (leaf == null) {
            return -1;
        }
        return leaf.inRange(name);
    }

    @Override
    protected CosObject keyValue(CosScalar name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.hasChildren()) {
            return PDFTreeNode.super.keyValue(name);
        }
        PDFTreeLeaf<K, V> leaf = this.getLeaf();
        if (leaf == null) {
            return null;
        }
        return leaf.keyValue(name);
    }

    @Override
    protected CosScalar previousKey(CosScalar name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.hasChildren()) {
            return PDFTreeNode.super.previousKey(name);
        }
        PDFTreeLeaf<K, V> leaf = this.getLeaf();
        if (leaf == null) {
            return null;
        }
        return leaf.previousKey(name);
    }

    @Override
    public boolean isEmpty() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.hasChildren()) {
            return PDFTreeNode.super.isEmpty();
        }
        PDFTreeLeaf<K, V> leaf = this.getLeaf();
        if (leaf == null) {
            return true;
        }
        return leaf.isEmpty();
    }

    @Override
    protected boolean deleteValue(CosScalar name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.hasChildren()) {
            boolean deleted = PDFTreeNode.super.deleteValue(name);
            this.removeRootLimits();
            return deleted;
        }
        PDFTreeLeaf<K, V> leaf = this.getLeaf();
        if (leaf == null) {
            return false;
        }
        boolean deleted = leaf.deleteValue(name);
        this.removeRootLimits();
        return deleted;
    }

    private void removeRootLimits() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosDictionary cosDict = this.getCosDictionary();
        if (cosDict.containsKey((Object)ASName.k_Limits)) {
            cosDict.remove(ASName.k_Limits);
        }
    }

    @Override
    protected PDFTreeNode<K, V> putValue(CosScalar name, CosObject value, boolean replace) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        if (this.hasChildren()) {
            PDFTreeList orgKids = this.getChildren();
            PDFTreeList splitKids = orgKids.putValue(name, value, replace);
            if (splitKids != null) {
                PDFTreeNode kid_a = PDFTreeNode.newInstance(this.getPDFDocument(), this.getNameDictionaryKey(), orgKids);
                PDFTreeNode kid_b = PDFTreeNode.newInstance(this.getPDFDocument(), this.getNameDictionaryKey(), splitKids);
                PDFTreeList newKids = PDFTreeList.newInstance(this.getPDFDocument(), this, this.getNameDictionaryKey());
                newKids.add(kid_a);
                newKids.add(kid_b);
                this.setChildren(newKids);
            }
            return null;
        }
        PDFTreeLeaf<K, V> leaf = this.getLeaf();
        if (leaf == null) {
            this.addFirstEntry(name, value);
            return null;
        }
        PDFTreeNode<K, V> kid_a = leaf.putValue(name, value, replace);
        if (kid_a != null) {
            PDFTreeList newKids = PDFTreeList.newInstance(this.getPDFDocument(), this, this.getNameDictionaryKey());
            PDFTreeLeaf kid_b = PDFTreeLeaf.newInstance(this.getPDFDocument(), this, this.getNameDictionaryKey(), this.getDictionaryArrayValue(this.getNameDictionaryKey()));
            newKids.add(kid_b);
            newKids.add(kid_a);
            this.setChildren(newKids);
            this.getCosDictionary().remove(this.getNameDictionaryKey());
        }
        this.removeRootLimits();
        return null;
    }

    private boolean addValue(CosScalar name, V value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        try {
            this.putValue(name, this.internalGetCosObjectFromValue(value), false);
            return true;
        }
        catch (PDFInvalidParameterException exception) {
            return false;
        }
    }

    private boolean replaceValue(CosScalar name, V value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        try {
            this.putValue(name, this.internalGetCosObjectFromValue(value), true);
            return true;
        }
        catch (PDFInvalidParameterException exception) {
            return false;
        }
    }

    public Iterator<K> keyIterator() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return new KeyIterator(this);
    }

    public Iterator<PDFTree<K, V>> iterator() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.listIterator();
    }

    public Set<V> values() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        HashSet value = new HashSet();
        ListIterator<PDFTree<K, V>> iter = this.listIterator();
        Entry next = null;
        while (iter.hasNext()) {
            next = (Entry)((Object)iter.next());
            value.add(next.getValue());
        }
        return value;
    }

    public ListIterator<PDFTree<K, V>> listIterator() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return new EntryIterator(this.treeNodeIterator());
    }

    ListIterator<PDFTreeNode<K, V>> treeNodeIterator() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFTreeList kids = this.getChildren();
        if (kids != null) {
            return PDFTreeNode.super.treeNodeListIterator();
        }
        PDFTreeLeaf<K, V> leaf = this.getLeaf();
        return leaf.treeNodeListIterator();
    }

    protected abstract V makeValueType(CosObject var1) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException;

    protected abstract CosScalar makeInternalKeyType(K var1) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException;

    protected abstract K makeExternalKeyType(CosScalar var1) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException;

    protected abstract CosObject getCosObjectFromValue(V var1) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException;

    private CosObject internalGetCosObjectFromValue(V value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return value != null ? this.getCosObjectFromValue(value) : PDFCosObject.newCosNull(this.getPDFDocument());
    }

    public V getEntry(K key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosScalar internalKey = this.makeInternalKeyType(key);
        CosObject cosObject = this.keyValue(internalKey);
        if (PDFCosObject.checkNullCosObject(cosObject) == null) {
            return null;
        }
        return this.makeValueType(cosObject);
    }

    public boolean hasEntry(K key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosScalar internalKey = this.makeInternalKeyType(key);
        CosObject cosObject = this.keyValue(internalKey);
        if (PDFCosObject.checkNullCosObject(cosObject) == null) {
            return false;
        }
        return true;
    }

    public boolean addEntry(K key, V value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        CosScalar internalKey = this.makeInternalKeyType(key);
        return this.addValue(internalKey, value);
    }

    public boolean removeEntry(K key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosScalar internalKey = this.makeInternalKeyType(key);
        return this.deleteValue(internalKey);
    }

    public boolean replaceEntry(K key, V value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        CosScalar internalKey = this.makeInternalKeyType(key);
        return this.replaceValue(internalKey, value);
    }

    public K previousKey(K key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        CosScalar internalKey = this.makeInternalKeyType(key);
        CosScalar previousKey = this.previousKey(internalKey);
        if (previousKey == null) {
            return null;
        }
        return this.makeExternalKeyType(previousKey);
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    class KeyIterator
    implements Iterator<K> {
        private final Iterator<PDFTree<K, V>> keyIterator;
        private Set<Integer> visitedSet;
        private Integer lastAccessedkey;
        final /* synthetic */ PDFTree this$0;

        public KeyIterator(PDFTree pDFTree) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            this.this$0 = pDFTree;
            this.keyIterator = pDFTree.listIterator();
            this.visitedSet = new HashSet<Integer>();
        }

        public KeyIterator(PDFTree pDFTree, Iterator<PDFTree<K, V>> iterator) {
            this.this$0 = pDFTree;
            this.keyIterator = iterator;
            this.visitedSet = new HashSet<Integer>();
        }

        @Override
        public void remove() {
            this.keyIterator.remove();
            this.visitedSet.remove(this.lastAccessedkey);
        }

        @Override
        public boolean hasNext() {
            return this.keyIterator.hasNext();
        }

        @Override
        public K next() {
            K key;
            block3 : {
                Entry entry = (Entry)((Object)this.keyIterator.next());
                key = null;
                try {
                    key = entry.getKey();
                    if (!(key instanceof CosObject)) break block3;
                    CosObject cosObject = (CosObject)key;
                    this.lastAccessedkey = cosObject.getObjNum();
                    if (this.lastAccessedkey == 0) break block3;
                    if (!this.visitedSet.contains(this.lastAccessedkey)) {
                        this.visitedSet.add(this.lastAccessedkey);
                        break block3;
                    }
                    throw new RuntimeException("Cycle has been detected in PDF Tree");
                }
                catch (PDFException e) {
                    NoSuchElementException newException = new NoSuchElementException("Error generating next PDF object.");
                    newException.initCause((Throwable)e);
                    throw newException;
                }
            }
            return key;
        }
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    private class EntryIterator
    implements ListIterator<PDFTree<K, V>> {
        private final ListIterator<PDFTreeNode<K, V>> nodeIterator;
        private Set<Integer> visitedSet;
        private Integer lastAccessedEntry;

        public EntryIterator(ListIterator<PDFTreeNode<K, V>> nodeIterator) {
            this.nodeIterator = nodeIterator;
            this.visitedSet = new HashSet<Integer>();
        }

        @Override
        public boolean hasNext() {
            return this.nodeIterator.hasNext();
        }

        @Override
        public PDFTree<K, V> next() {
            PDFTreeNode.InternalEntry entry = (PDFTreeNode.InternalEntry)((Object)this.nodeIterator.next());
            CosScalar key = entry.getKey();
            this.lastAccessedEntry = key.getObjNum();
            if (this.lastAccessedEntry != 0) {
                if (this.visitedSet.contains(this.lastAccessedEntry)) {
                    throw new RuntimeException("Cycle has been detected in PDF Tree");
                }
                this.visitedSet.add(this.lastAccessedEntry);
            }
            Entry next = null;
            try {
                next = new Entry(key, PDFTree.this.makeValueType(entry.getValue()));
            }
            catch (PDFException e) {
                NoSuchElementException newException = new NoSuchElementException("Error generating next PDF object.");
                newException.initCause((Throwable)e);
                throw newException;
            }
            return next;
        }

        @Override
        public void remove() {
            this.nodeIterator.remove();
            this.visitedSet.remove(this.lastAccessedEntry);
        }

        @Override
        public void add(PDFTree<K, V> o) {
            try {
                this.nodeIterator.add((PDFTreeNode<K, V>)((Object)new PDFTreeNode.InternalEntry(PDFTree.this, o.key, PDFTree.this.internalGetCosObjectFromValue(o.value))));
            }
            catch (PDFException e) {
                IllegalArgumentException newException = new IllegalArgumentException("Error generating next PDF object.");
                newException.initCause((Throwable)e);
                throw newException;
            }
        }

        @Override
        public boolean hasPrevious() {
            return this.nodeIterator.hasPrevious();
        }

        @Override
        public int nextIndex() {
            return this.nodeIterator.nextIndex();
        }

        @Override
        public PDFTree<K, V> previous() {
            PDFTreeNode.InternalEntry entry = (PDFTreeNode.InternalEntry)((Object)this.nodeIterator.previous());
            Entry previous = null;
            try {
                previous = new Entry(entry.getKey(), PDFTree.this.makeValueType(entry.getValue()));
            }
            catch (PDFException e) {
                NoSuchElementException newException = new NoSuchElementException("Error generating next PDF object.");
                newException.initCause((Throwable)e);
                throw newException;
            }
            return previous;
        }

        @Override
        public int previousIndex() {
            return this.nodeIterator.previousIndex();
        }

        @Override
        public void set(PDFTree<K, V> o) {
            try {
                this.nodeIterator.set((PDFTreeNode<K, V>)((Object)new PDFTreeNode.InternalEntry(PDFTree.this, o.key, PDFTree.this.internalGetCosObjectFromValue(o.value))));
            }
            catch (PDFException e) {
                IllegalArgumentException newException = new IllegalArgumentException("Error generating next PDF object.");
                newException.initCause((Throwable)e);
                throw newException;
            }
        }
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    public class Entry {
        private final CosScalar key;
        private final V value;

        protected Entry(CosScalar key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            return PDFTree.this.makeExternalKeyType(this.key);
        }

        public V getValue() {
            return this.value;
        }

        public String toString() {
            try {
                StringBuilder builder = new StringBuilder();
                builder.append("[");
                builder.append(this.getKey());
                builder.append(", ");
                builder.append(this.getValue());
                builder.append("]");
                return builder.toString();
            }
            catch (Exception e) {
                return null;
            }
        }
    }

}