ExportDataService.java 9.69 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemfd.docmanager.Document
 *  com.adobe.forms.option.LCFormsOptions
 *  com.adobe.forms.service.LCFormsOsgiService
 *  com.adobe.internal.pdftoolkit.pdf.document.PDFDocument
 *  com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 */
package com.adobe.fd.forms.internal;

import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.forms.api.DataFormat;
import com.adobe.fd.forms.internal.exception.FormsServiceException;
import com.adobe.fd.forms.internal.logging.FormsServiceLogger;
import com.adobe.fd.forms.internal.utils.StringUtils;
import com.adobe.fd.forms.internal.utils.Utils;
import com.adobe.fd.forms.internal.utils.XMLUtils;
import com.adobe.forms.option.LCFormsOptions;
import com.adobe.forms.service.LCFormsOsgiService;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm;
import java.io.IOException;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

@Component(label="Export Data Service", description="Export Data Service for internal use")
@Service(value={ExportDataService.class})
public class ExportDataService {
    private static FormsServiceLogger logger = new FormsServiceLogger(ExportDataService.class);
    @Reference
    private LCFormsOsgiService lcFormsOsgiService;

    private byte[] XDPToXML(byte[] dataXML, String dataNodePath) throws ParserConfigurationException, SAXException, IOException, TransformerException {
        org.w3c.dom.Document xmlDataDoc = XMLUtils.loadXMLbytes(dataXML, false);
        Element oResult = null;
        Element oFirstChild = null;
        Node xfaDataNode = XMLUtils.selectNode(xmlDataDoc.getDocumentElement(), dataNodePath, 0);
        if (xfaDataNode != null) {
            NodeList oNodeList = xfaDataNode.getChildNodes();
            for (int i = 0; i < oNodeList.getLength(); ++i) {
                Node oChild = oNodeList.item(i);
                if (oChild.getNodeType() != 1) continue;
                if (oFirstChild == null) {
                    oFirstChild = (Element)oChild;
                }
                if (oResult != null || oChild.getNodeName().startsWith("FS") && oChild.getNodeName().endsWith("_")) continue;
                oResult = (Element)oChild;
                break;
            }
            if (oResult == null) {
                oResult = oFirstChild;
            }
            if (oResult != null) {
                DOMSource ds = new DOMSource(oResult);
                dataXML = XMLUtils.toXML(ds);
            } else {
                dataXML = new byte[]{};
            }
        }
        return dataXML;
    }

    private byte[][] exportDataFromPDFInternal(PDFDocument oPdfDocument) throws Exception {
        if (oPdfDocument == null) {
            throw new FormsServiceException("AEM_FRM_001_009");
        }
        byte[][] returnVal = new byte[2][];
        byte[] oExportedDataBytes = new byte[]{};
        String sContentType = null;
        if (oPdfDocument.getInteractiveForm() == null) {
            oExportedDataBytes = Utils.exportDataFromXFAResourceDictionary(oPdfDocument);
            if (oExportedDataBytes == null || oExportedDataBytes.length == 0) {
                throw new FormsServiceException("AEM_FRM_001_010");
            }
            sContentType = "text/xml";
        } else if (Utils.isAcroForm(oPdfDocument)) {
            oExportedDataBytes = Utils.exportXFDFData(oPdfDocument);
            byte[] oExportedAnnotsBytes = Utils.exportXFDFAnnots(oPdfDocument);
            if (oExportedAnnotsBytes != null && oExportedAnnotsBytes.length > 0) {
                oExportedDataBytes = Utils.mergeDataWithAnnots(oExportedDataBytes, oExportedAnnotsBytes);
            }
            sContentType = "application/vnd.adobe.xfdf";
        } else if (Utils.isXFAForm(oPdfDocument)) {
            oExportedDataBytes = Utils.exportXFAData(oPdfDocument);
            if (oExportedDataBytes == null || oExportedDataBytes.length == 0) {
                oExportedDataBytes = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xfa:datasets xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\"><xfa:data></xfa:data></xfa:datasets>".getBytes("UTF-8");
            }
            sContentType = "text/xml";
        }
        returnVal[0] = oExportedDataBytes;
        returnVal[1] = sContentType.getBytes();
        return returnVal;
    }

    private Document exportDataFromPDF(PDFDocument pdfDoc, DataFormat dataFormat) throws Exception {
        byte[][] exportedData = this.exportDataFromPDFInternal(pdfDoc);
        byte[] dataXML = exportedData[0];
        String sContentType = new String(exportedData[1]);
        if (Utils.isXFAForm(pdfDoc)) {
            if (dataXML == null || dataXML.length == 0) {
                dataXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xfa:datasets xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\"><xfa:data></xfa:data></xfa:datasets>".getBytes("UTF-8");
            }
            if (dataFormat != null && dataFormat.equals((Object)DataFormat.XmlData)) {
                dataXML = this.XDPToXML(dataXML, "xfa:data");
            } else {
                byte[] dataBytes = dataXML;
                dataBytes = StringUtils.stripXMLDescriptor(dataBytes);
                byte[][] arrayOfByteArrays = new byte[][]{"<?xml version=\"1.0\" encoding=\"UTF-8\"?><xdp:xdp xmlns:xdp=\"http://ns.adobe.com/xdp/\">".getBytes(), dataBytes, "</xdp:xdp>".getBytes()};
                dataXML = dataBytes = StringUtils.joinArrayOfByteArrays(arrayOfByteArrays);
            }
        }
        Document dataXMLDoc = new Document(dataXML);
        dataXMLDoc.setContentType(sContentType);
        return dataXMLDoc;
    }

    private Document exportDataFromXDP(Document xdp, DataFormat dataFormat) throws FormsServiceException {
        try {
            LCFormsOptions options = new LCFormsOptions();
            options.setScriptsToRun("server");
            options.setTemplateBytes(new String(Utils.getBytes(xdp), "utf-8"));
            String ret = (String)this.lcFormsOsgiService.exportXFA(options).get("mergedformdom");
            byte[] dataBytes = ret.getBytes("utf-8");
            if (dataBytes == null || dataBytes.length == 0) {
                dataBytes = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xfa:datasets xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\"><xfa:data></xfa:data></xfa:datasets>".getBytes("UTF-8");
            }
            if (dataFormat != null && dataFormat == DataFormat.XmlData) {
                dataBytes = this.XDPToXML(dataBytes, "xfa:datasets/xfa:data");
            }
            return new Document(dataBytes);
        }
        catch (Exception e) {
            throw new FormsServiceException("AEM_FRM_001_004", new Object[]{e.getMessage()}, e);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Loose catch block
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Lifted jumps to return sites
     */
    public Document exportData(Document pdfOrXdp, DataFormat dataFormat) throws FormsServiceException {
        PDFDocument pdfDoc = null;
        boolean closeRequired = false;
        Document dataDocument = null;
        pdfDoc = Utils.getSharedPDFDocument(pdfOrXdp);
        if (pdfDoc == null) {
            pdfDoc = Utils.createPDFDocument(pdfOrXdp);
            closeRequired = true;
        }
        dataDocument = this.exportDataFromPDF(pdfDoc, dataFormat);
        if (pdfDoc == null) return dataDocument;
        if (!closeRequired) return dataDocument;
        try {
            pdfDoc.close();
            return dataDocument;
        }
        catch (Exception e) {
            throw new FormsServiceException("AEM_FRM_001_004", new Object[]{e.getMessage()}, e);
        }
        catch (Exception e) {
            try {
                if (pdfDoc != null) {
                    throw new FormsServiceException("AEM_FRM_001_004", new Object[]{e.getMessage()}, e);
                }
                dataDocument = this.exportDataFromXDP(pdfOrXdp, dataFormat);
                if (pdfDoc == null) return dataDocument;
                if (!closeRequired) return dataDocument;
            }
            catch (Throwable var7_9) {
                if (pdfDoc == null) throw var7_9;
                if (!closeRequired) throw var7_9;
                try {
                    pdfDoc.close();
                    throw var7_9;
                }
                catch (Exception e) {
                    throw new FormsServiceException("AEM_FRM_001_004", new Object[]{e.getMessage()}, e);
                }
            }
            try {
                pdfDoc.close();
                return dataDocument;
            }
            catch (Exception e) {
                throw new FormsServiceException("AEM_FRM_001_004", new Object[]{e.getMessage()}, e);
            }
        }
    }

    protected void bindLcFormsOsgiService(LCFormsOsgiService lCFormsOsgiService) {
        this.lcFormsOsgiService = lCFormsOsgiService;
    }

    protected void unbindLcFormsOsgiService(LCFormsOsgiService lCFormsOsgiService) {
        if (this.lcFormsOsgiService == lCFormsOsgiService) {
            this.lcFormsOsgiService = null;
        }
    }
}