ExportDataService.java
9.69 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
/*
* 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;
}
}
}