SharedPdfDocumentContext.java 8.53 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemfd.docmanager.Document
 *  com.adobe.aemfd.docmanager.io.IOUtils
 *  com.adobe.aemfd.docmanager.source.DocumentSourceHandler
 *  com.adobe.aemfd.docmanager.source.FileBasedDocumentSourceHandler
 *  com.adobe.internal.io.ByteArrayByteReader
 *  com.adobe.internal.io.ByteReader
 *  com.adobe.internal.io.RandomAccessFileByteReader
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
 *  com.adobe.internal.pdftoolkit.core.permissionprovider.ObjectOperations
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFDocument
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFOpenOptions
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFSaveOptions
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFVersion
 *  com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm
 *  com.adobe.internal.pdftoolkit.services.permissions.PermissionsManager
 *  com.adobe.internal.pdftoolkit.services.xmp.DocumentMetadata
 *  com.adobe.internal.pdftoolkit.services.xmp.MetadataOptions
 *  com.adobe.internal.pdftoolkit.services.xmp.MetadataOptions$MetadataOptionsBuilder
 *  com.adobe.internal.pdftoolkit.services.xmp.XMPService
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemfd.pdfdocmanager;

import com.adobe.aemfd.docmanager.Document;
import com.adobe.aemfd.docmanager.io.IOUtils;
import com.adobe.aemfd.docmanager.source.DocumentSourceHandler;
import com.adobe.aemfd.docmanager.source.FileBasedDocumentSourceHandler;
import com.adobe.aemfd.pdfdocmanager.SharedPdfDocument;
import com.adobe.aemfd.pdfdocmanager.internal.PermissionsManagerNoXFAPermissionsPatch;
import com.adobe.aemfd.pdfdocmanager.internal.io.DocumentDisposingFilterByteReader;
import com.adobe.aemfd.pdfdocmanager.internal.io.ManagedRandomAccessFileByteReader;
import com.adobe.internal.io.ByteArrayByteReader;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.RandomAccessFileByteReader;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.permissionprovider.ObjectOperations;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFOpenOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFVersion;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm;
import com.adobe.internal.pdftoolkit.services.permissions.PermissionsManager;
import com.adobe.internal.pdftoolkit.services.xmp.DocumentMetadata;
import com.adobe.internal.pdftoolkit.services.xmp.MetadataOptions;
import com.adobe.internal.pdftoolkit.services.xmp.XMPService;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SharedPdfDocumentContext {
    private static final Logger logger = LoggerFactory.getLogger(SharedPdfDocumentContext.class);
    private static MetadataOptions xmpOptions = null;

    private SharedPdfDocumentContext() {
    }

    private static ByteReader newByteReader(Document doc) throws PDFIOException {
        try {
            byte[] inline = doc.getInlineData();
            if (inline != null) {
                return new ByteArrayByteReader(inline);
            }
            DocumentSourceHandler dsh = doc.getSourceHandler();
            if (dsh == null) {
                throw new IllegalStateException("Source handler for the given document is null even after passivation!");
            }
            if (dsh instanceof FileBasedDocumentSourceHandler) {
                FileBasedDocumentSourceHandler fdsh = (FileBasedDocumentSourceHandler)dsh;
                RandomAccessFile raf = new RandomAccessFile(fdsh.getSourceFile(), "r");
                return new RandomAccessFileByteReader(raf);
            }
            File docFile = IOUtils.createTempFile((String)".pdf");
            doc.copyToFile(docFile);
            RandomAccessFile raf = new RandomAccessFile(docFile, "r");
            return new ManagedRandomAccessFileByteReader(raf, docFile);
        }
        catch (IOException e) {
            throw new PDFIOException("Error creating ByteReader for given document", (Throwable)e);
        }
    }

    public static PDFDocument toPDFDocument(Document doc, PDFOpenOptions options) throws PDFException {
        return SharedPdfDocumentContext.toPDFDocument(doc, options, false);
    }

    public static PDFDocument toPDFDocument(Document doc, PDFOpenOptions options, boolean ownDoc) throws PDFException {
        Object br = SharedPdfDocumentContext.newByteReader(doc);
        if (ownDoc) {
            br = new DocumentDisposingFilterByteReader((ByteReader)br, doc);
        }
        PDFOpenOptions opts = options != null ? options : PDFOpenOptions.newInstance();
        return PDFDocument.newInstance((ByteReader)br, (PDFOpenOptions)opts);
    }

    public static SharedPdfDocument toDocument(PDFDocument pdfDoc, PDFSaveOptions options) {
        return new SharedPdfDocument(pdfDoc, options);
    }

    public static boolean doesVersionChangeForceFullSave(PDFDocument pdfDoc) throws PDFException {
        boolean result = false;
        PDFVersion t = pdfDoc.getToSaveVersion();
        String toSaveVersionString = null;
        if (t != null) {
            toSaveVersionString = t.getVersionValue();
        }
        t = pdfDoc.getOriginalVersion();
        String originalVersionString = null;
        originalVersionString = t == null ? PDFVersion.vLatest.getVersionValue() : t.getVersionValue();
        if (toSaveVersionString == null) {
            toSaveVersionString = originalVersionString;
        }
        if (!originalVersionString.equals(toSaveVersionString)) {
            String[] originalVersionElements = originalVersionString.split("\\.");
            int originalVersionMajorVal = Integer.parseInt(originalVersionElements[0]);
            int originalVersionMinorVal = 0;
            if (originalVersionElements.length > 1) {
                originalVersionMinorVal = Integer.parseInt(originalVersionElements[1]);
            }
            String[] toSaveVersionElements = toSaveVersionString.split("\\.");
            int toSaveVersionMajorVal = Integer.parseInt(toSaveVersionElements[0]);
            int toSaveVersionMinorVal = 0;
            if (toSaveVersionElements.length > 1) {
                toSaveVersionMinorVal = Integer.parseInt(toSaveVersionElements[1]);
            }
            if (toSaveVersionMajorVal == 1 && toSaveVersionMinorVal < 4) {
                result = true;
            }
            if (toSaveVersionMajorVal < originalVersionMajorVal) {
                result = true;
            }
            if (toSaveVersionMajorVal == originalVersionMajorVal && toSaveVersionMinorVal < originalVersionMinorVal) {
                result = true;
            }
        }
        return result;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private static MetadataOptions getMetadataOptions() {
        if (xmpOptions != null) {
            return xmpOptions;
        }
        MetadataOptions.MetadataOptionsBuilder optionsBuilder = MetadataOptions.MetadataOptionsBuilder.newInstance();
        optionsBuilder.setAutoUpdate(true);
        optionsBuilder.setDocumentIdGenerator(new Random());
        optionsBuilder.setUpdateOnlyIfDocumentChanged(false);
        Class<SharedPdfDocumentContext> class_ = SharedPdfDocumentContext.class;
        synchronized (SharedPdfDocumentContext.class) {
            if (xmpOptions == null) {
                xmpOptions = optionsBuilder.build();
            }
            // ** MonitorExit[var1_1] (shouldn't be in output)
            return xmpOptions;
        }
    }

    public static void updateMetadataTimestamps(PDFDocument doc) {
        try {
            PDFInteractiveForm iform = doc.getInteractiveForm();
            if (iform != null && iform.hasXFA()) {
                return;
            }
            PermissionsManager pm = PermissionsManagerNoXFAPermissionsPatch.newInstance(doc);
            if (pm.isPermitted(ObjectOperations.DOC_MODIFY)) {
                DocumentMetadata md = XMPService.getDocumentMetadata((PDFDocument)doc, (MetadataOptions)SharedPdfDocumentContext.getMetadataOptions());
            }
        }
        catch (PDFException e) {
            logger.warn("Error encountered while updating PDF metadata timestamps", (Throwable)e);
        }
    }
}