PDFCosDictionary.java 19.9 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
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.internal.io.stream.InputByteStream
 *  com.adobe.internal.pdftoolkit.core.cos.CosArray
 *  com.adobe.internal.pdftoolkit.core.cos.CosDictionary
 *  com.adobe.internal.pdftoolkit.core.cos.CosDocument
 *  com.adobe.internal.pdftoolkit.core.cos.CosName
 *  com.adobe.internal.pdftoolkit.core.cos.CosNull
 *  com.adobe.internal.pdftoolkit.core.cos.CosNumeric
 *  com.adobe.internal.pdftoolkit.core.cos.CosObject
 *  com.adobe.internal.pdftoolkit.core.cos.CosStream
 *  com.adobe.internal.pdftoolkit.core.cos.CosString
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException
 *  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.ASDate
 *  com.adobe.internal.pdftoolkit.core.types.ASName
 *  com.adobe.internal.pdftoolkit.core.types.ASString
 */
package com.adobe.internal.pdftoolkit.pdf.document;

import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosName;
import com.adobe.internal.pdftoolkit.core.cos.CosNull;
import com.adobe.internal.pdftoolkit.core.cos.CosNumeric;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
import com.adobe.internal.pdftoolkit.core.cos.CosString;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
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.ASDate;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.pdf.document.PDFASNameInterface;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFText;
import com.adobe.internal.pdftoolkit.pdf.interchange.metadata.PDFMetadata;
import java.util.List;

public abstract class PDFCosDictionary
extends PDFCosObject {
    protected PDFCosDictionary(CosObject cosObject) throws PDFInvalidDocumentException {
        super(cosObject);
        if (!(cosObject instanceof CosDictionary)) {
            this.removeFromCache(cosObject);
            throw new PDFInvalidDocumentException("CosDictionary expected, found" + cosObject.getClass() + ". Object number of invalid cosObject:" + cosObject.getObjNum());
        }
    }

    public CosDictionary getCosDictionary() {
        if (!(this.getCosObject() instanceof CosDictionary)) {
            return null;
        }
        return (CosDictionary)this.getCosObject();
    }

    public boolean dictionaryContains(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.getCosObject() == null) {
            return false;
        }
        CosObject b = ((CosDictionary)this.getCosObject()).get(key);
        return b != null && !(b instanceof CosNull);
    }

    public boolean isEmpty() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosDictionary dict = this.getCosDictionary();
        if (dict == null) {
            return true;
        }
        return dict.isEmpty();
    }

    protected boolean containsKey(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.getCosDictionary().containsKey((Object)key);
    }

    public boolean removeValue(ASName name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.getCosObject() != null && ((CosDictionary)this.getCosObject()).containsKey((Object)name)) {
            CosObject old = this.getCosDictionary().remove(name);
            return old != null;
        }
        return false;
    }

    protected String getDictionaryTextStringValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject value = this.getCosDictionary().get(key);
        if (value == null) {
            return null;
        }
        if (value.getType() != 4) {
            return null;
        }
        PDFText pdfText = PDFText.getInstance(value);
        return pdfText.stringValue();
    }

    public String getDictionaryTextStringOrStreamValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject value = this.getCosDictionary().get(key);
        if (value == null) {
            return null;
        }
        PDFText pdfText = PDFText.getInstance(value);
        return pdfText.stringValue();
    }

    protected ASDate getDictionaryDateValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject dateValue = this.getCosDictionary().get(key);
        if (dateValue == null) {
            return null;
        }
        if (dateValue instanceof CosString) {
            return ((CosString)dateValue).asDate();
        }
        throw new PDFInvalidDocumentException(this.getErrorMessage("CosString", dateValue, key));
    }

    private String getErrorMessage(String expectedType, CosObject invalidObject, ASName key) {
        if (invalidObject != null) {
            return expectedType + " expected, found " + invalidObject.getClass() + " , invalid obj number: " + invalidObject.getObjNum() + ", dict key: " + (Object)key;
        }
        return expectedType + " expected, found Null, dict key: " + (Object)key;
    }

    public ASName getDictionaryNameValue(PDFASNameInterface key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.getDictionaryNameValue(key.getName());
    }

    public ASName getDictionaryNameValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject nameValue = this.getCosDictionary().get(key);
        if (nameValue == null) {
            return null;
        }
        if (nameValue instanceof CosName) {
            return ((CosName)nameValue).nameValue();
        }
        throw new PDFInvalidDocumentException(this.getErrorMessage("CosName", nameValue, key));
    }

    public String getDictionaryNameValueAsString(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        ASName nameValue = this.getDictionaryNameValue(key);
        if (nameValue == null) {
            return null;
        }
        return nameValue.asString(true);
    }

    public Integer getDictionaryIntValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject value = this.getCosDictionary().get(key);
        if (value == null) {
            return null;
        }
        if (!(value instanceof CosNumeric)) {
            throw new PDFInvalidDocumentException(this.getErrorMessage("CosNumeric", value, key));
        }
        return value.intValue();
    }

    public byte[] getDictionaryByteArrayValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject stringVal = this.getCosDictionary().get(key);
        if (stringVal == null) {
            return null;
        }
        if (stringVal instanceof CosString) {
            return ((CosString)stringVal).byteArrayValue();
        }
        throw new PDFInvalidDocumentException(this.getErrorMessage("CosString", stringVal, key));
    }

    public ASString getDictionaryStringValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject stringVal = this.getCosDictionary().get(key);
        if (stringVal == null) {
            return null;
        }
        if (stringVal instanceof CosString) {
            return ((CosString)stringVal).stringValue();
        }
        throw new PDFInvalidDocumentException(this.getErrorMessage("CosString", stringVal, key));
    }

    public String getDictionaryIntValueAsString(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        Number value = this.getDictionaryNumericValue(key);
        if (value == null) {
            return null;
        }
        return Integer.toString(value.intValue());
    }

    public double getDictionaryDoubleValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        Number value = this.getDictionaryNumericValue(key);
        if (value == null) {
            throw new PDFInvalidDocumentException(this.getErrorMessage("CosNumeric", null, key));
        }
        return value.doubleValue();
    }

    public boolean getDictionaryBooleanValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject booleanVal = this.getCosDictionary().get(key);
        if (booleanVal == null) {
            throw new PDFInvalidDocumentException(this.getErrorMessage("CosBoolean", null, key));
        }
        return booleanVal.booleanValue();
    }

    public String getDictionaryDoubleValueAsString(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        Number value = this.getDictionaryNumericValue(key);
        if (value == null) {
            return null;
        }
        return Double.toString(value.doubleValue());
    }

    public CosDictionary getDictionaryDictionaryValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject dictObject = this.getCosDictionary().get(key);
        if (dictObject instanceof CosDictionary) {
            return (CosDictionary)dictObject;
        }
        return null;
    }

    public Number getDictionaryNumericValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject value = this.getCosDictionary().get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof CosNumeric) {
            return ((CosNumeric)value).numberValue();
        }
        throw new PDFInvalidDocumentException(this.getErrorMessage("CosNumeric", value, key));
    }

    public InputByteStream getDictionaryStreamValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject streamVal = this.getCosDictionary().get(key);
        if (streamVal == null) {
            return null;
        }
        if (streamVal instanceof CosStream) {
            return ((CosStream)streamVal).getStreamDecoded();
        }
        throw new PDFInvalidDocumentException(this.getErrorMessage("CosStream", streamVal, key));
    }

    public CosObject getDictionaryCosObjectValue(ASName name) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.getCosObject() == null) {
            return null;
        }
        return ((CosDictionary)this.getCosObject()).get(name);
    }

    public CosArray getDictionaryArrayValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject arrObj = this.getCosDictionary().get(key);
        if (arrObj == null) {
            return null;
        }
        if (arrObj instanceof CosArray) {
            return (CosArray)arrObj;
        }
        throw new PDFInvalidDocumentException(this.getErrorMessage("CosArray", arrObj, key));
    }

    public void setDictionaryDateValue(ASName key, ASDate date) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryStringValue(key, date == null ? null : date.asString());
    }

    public void setDictionaryStringValue(ASName key, String value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryValue(key, value == null ? null : PDFText.createString(this.getPDFDocument(), value).getCosObject());
    }

    public void setDictionaryByteArrayValue(ASName key, byte[] value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryValue(key, (CosObject)(value == null ? null : PDFCosObject.newCosString(this.getPDFDocument(), value)));
    }

    public void setDictionaryASStringValue(ASName key, ASString value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryValue(key, (CosObject)(value == null ? null : PDFCosObject.newCosString(this.getPDFDocument(), value.getBytes())));
    }

    public void setDictionaryStringOrStreamValue(ASName key, String value) throws PDFIOException, PDFInvalidDocumentException, PDFSecurityException {
        this.setDictionaryValue(key, value == null ? null : PDFText.newInstance(this.getPDFDocument(), value));
    }

    public void setDictionaryStringOrStreamValue(ASName key, String value, boolean utf16) throws PDFIOException, PDFInvalidDocumentException, PDFSecurityException {
        this.setDictionaryValue(key, value == null ? null : PDFText.newInstance(value, utf16, this.getPDFDocument()));
    }

    public void setDictionaryTextValue(ASName key, PDFText value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryValue(key, value);
    }

    public void setDictionaryNameValue(ASName key, PDFASNameInterface value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryNameValue(key, value == null ? null : value.getName());
    }

    public void setDictionaryNameValue(ASName key, ASName value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (value == null) {
            this.getCosDictionary().remove(key);
        } else {
            this.getCosDictionary().put(key, value);
        }
    }

    public void setDictionaryNameValue(ASName key, String value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryNameValue(key, ASName.create((String)value));
    }

    public void setDictionaryIntValue(ASName key, long value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.getCosDictionary().put(key, value);
    }

    public void setDictionaryArrayValue(ASName key, long[] value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (value == null) {
            this.getCosDictionary().remove(key);
        } else {
            this.getCosDictionary().put(key, value);
        }
    }

    public void setDictionaryIntValue(ASName key, String value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        try {
            this.putValueInDictionary(key, (CosObject)(value == null ? null : this.getCosDictionary().getDocument().createCosNumeric(Integer.parseInt(value))));
        }
        catch (NumberFormatException e) {
            throw new PDFInvalidParameterException((Throwable)e);
        }
    }

    public void setDictionaryDoubleValue(ASName key, double value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.getCosDictionary().put(key, value);
    }

    public void setDictionaryDoubleValue(ASName key, String value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        try {
            this.getCosDictionary().put(key, Double.parseDouble(value));
        }
        catch (NumberFormatException e) {
            throw new PDFInvalidParameterException((Throwable)e);
        }
    }

    public void setDictionaryBooleanValue(ASName key, Boolean value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (value == null) {
            this.getCosDictionary().remove(key);
        } else {
            this.getCosDictionary().put(key, value.booleanValue());
        }
    }

    public void setDictionaryBooleanValue(ASName key, String value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (value != null) {
            this.getCosDictionary().put(key, "true".equals(value));
        } else {
            this.getCosDictionary().remove(key);
        }
    }

    public void setDictionaryArrayValue(ASName key, List value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryArrayValue(key, value == null ? null : PDFCosDictionary.makeCosArray(this.getPDFDocument(), value));
    }

    protected void setDictionaryArrayValue(ASName key, int[] value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryArrayValue(key, value == null ? null : PDFCosDictionary.makeCosArray(this.getPDFDocument(), value, 0, value.length - 1));
    }

    public CosArray setDictionaryArrayValue(ASName key, double[] value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.setDictionaryArrayValue(key, 0, value);
    }

    protected CosArray setDictionaryArrayValue(ASName key, int offset, double[] value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosArray valueArray = null;
        this.setDictionaryArrayValue(key, value == null ? null : (valueArray = PDFCosDictionary.makeCosArray(this.getPDFDocument(), offset, value, 0, value.length - 1)));
        return valueArray;
    }

    private void putValueInDictionary(ASName key, CosObject value) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        if (value == null) {
            this.getCosDictionary().remove(key);
        } else {
            this.getCosDictionary().put(key, value);
        }
    }

    public void setDictionaryArrayValue(ASName key, CosArray value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.putValueInDictionary(key, (CosObject)value);
    }

    public void setDictionaryStreamValue(ASName key, CosStream value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.putValueInDictionary(key, (CosObject)value);
    }

    public void setDictionaryValue(ASName key, CosObject value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.putValueInDictionary(key, value);
    }

    public void setDictionaryValue(ASName key, PDFCosObject value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.putValueInDictionary(key, value == null ? null : value.getCosObject());
    }

    public CosObject getDictionaryValue(ASName key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosObject value = null;
        value = this.getCosDictionary().get(key);
        return value;
    }

    public PDFMetadata getMetadata() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return PDFMetadata.getInstance(this.getDictionaryCosObjectValue(ASName.k_Metadata));
    }

    public void setMetadata(PDFMetadata pdfMetadata) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.setDictionaryValue(ASName.k_Metadata, pdfMetadata);
    }

    boolean hasXMP() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.dictionaryContains(ASName.k_Metadata);
    }

    protected CosArray setDictionaryArrayValue(ASName key, int offset, ASName[] value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosArray valueArray = null;
        this.setDictionaryArrayValue(key, value == null ? null : (valueArray = PDFCosDictionary.makeCosArray(this.getPDFDocument(), offset, value, 0, value.length - 1)));
        return valueArray;
    }
}