ImportDataService.java
7.48 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemfd.docmanager.Document
* 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.Service
*/
package com.adobe.fd.forms.internal;
import com.adobe.aemfd.docmanager.Document;
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.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
@Component(label="Import Data Service", description="Import Data Service for internal use")
@Service(value={ImportDataService.class})
public class ImportDataService {
private static FormsServiceLogger logger = new FormsServiceLogger(ImportDataService.class);
public Document importData(Document doc, Document data) throws Exception {
PDFDocument oPdfDocument = Utils.getSharedPDFDocument(doc);
boolean requireClose = false;
if (oPdfDocument == null) {
oPdfDocument = Utils.createPDFDocument(doc);
requireClose = true;
}
if (oPdfDocument == null) {
throw new FormsServiceException("AEM_FRM_001_009");
}
if (oPdfDocument.getInteractiveForm() == null) {
throw new FormsServiceException("AEM_FRM_001_010");
}
if (!Utils.areDataChangesAllowed(oPdfDocument)) {
throw new FormsServiceException("AEM_FRM_001_011");
}
try {
byte[] oDataBytes = Utils.getBytes(data);
if (Utils.isAcroForm(oPdfDocument)) {
oPdfDocument = Utils.importDataToAcroform(oPdfDocument, oDataBytes, true);
} else if (Utils.isXFAForm(oPdfDocument)) {
this.importIntoXFAPDFForm(oPdfDocument, oDataBytes);
}
return Utils.toDocument(oPdfDocument);
}
catch (Exception e) {
if (oPdfDocument != null && requireClose) {
oPdfDocument.close();
}
logger.error("AEM_FRM_001_004", new String[]{e.getMessage()}, e);
throw e;
}
}
private void importIntoXFAPDFForm(PDFDocument pdfDocument, byte[] oDataBytes) throws Exception {
String sDataFlavor = Utils.getDataFlavor(oDataBytes);
if (!sDataFlavor.equalsIgnoreCase("XDP") && !sDataFlavor.equalsIgnoreCase("XML")) {
throw new FormsServiceException();
}
if (sDataFlavor.equalsIgnoreCase("XML")) {
oDataBytes = StringUtils.stripXMLDescriptor(oDataBytes);
byte[][] arrayOfByteArrays = new byte[][]{"<?xml version=\"1.0\" encoding=\"UTF-8\"?><xdp:xdp xmlns:xdp=\"http://ns.adobe.com/xdp/\"><xfa:datasets xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\"><xfa:data>".getBytes(), oDataBytes, "</xfa:data></xfa:datasets></xdp:xdp>".getBytes()};
oDataBytes = StringUtils.joinArrayOfByteArrays(arrayOfByteArrays);
oDataBytes = StringUtils.getXMLBlockBytes(oDataBytes, "xfa:datasets");
} else {
oDataBytes = ImportDataService.propagateNmspcsFrmXdpToXFA(oDataBytes);
}
oDataBytes = ImportDataService.mergeDataDescriptionFromTemplate(oDataBytes, pdfDocument);
Utils.importXFAData(pdfDocument, oDataBytes);
}
private static byte[] propagateNmspcsFrmXdpToXFA(byte[] oDataBytes) {
Map<String, String> xdpNameSpacePrefixMap = StringUtils.getNamespacesDeclaredInTag(oDataBytes, "xdp:xdp", 0);
xdpNameSpacePrefixMap.remove("xdp");
Map<String, String> xfaNameSpacePrefixMap = null;
if (xdpNameSpacePrefixMap.size() > 0) {
xfaNameSpacePrefixMap = StringUtils.getNamespacesDeclaredInTag(oDataBytes, "xfa:datasets", 0);
xdpNameSpacePrefixMap.keySet().removeAll(xfaNameSpacePrefixMap.keySet());
}
if (xdpNameSpacePrefixMap.size() > 0) {
String additionalNameSpaces = ImportDataService.createNameSpaceString(xdpNameSpacePrefixMap);
int startIndexOfXFA = StringUtils.getStartBeginTagIndex(oDataBytes, "xfa:datasets", 0);
String searchString = "</xfa:datasets";
int lenOfXFABodyAndEndTag = 0;
int endIndexOfXFA = StringUtils.findArraySegmentReverse(oDataBytes, searchString.getBytes());
if (endIndexOfXFA < 0) {
endIndexOfXFA = StringUtils.findArraySegment(oDataBytes, startIndexOfXFA, ">".getBytes());
lenOfXFABodyAndEndTag = endIndexOfXFA - startIndexOfXFA - "xfa:datasets".length() - 1;
} else {
lenOfXFABodyAndEndTag = endIndexOfXFA - startIndexOfXFA - "xfa:datasets".length() - 1 + searchString.length();
}
StringBuilder strBuilder = new StringBuilder();
strBuilder.append('<').append("xfa:datasets").append(" ").append(additionalNameSpaces);
byte[][] arrayOfByteArrays = new byte[][]{strBuilder.toString().getBytes(), StringUtils.extractArraySegment(oDataBytes, startIndexOfXFA + "xfa:datasets".length() + 1, lenOfXFABodyAndEndTag), ">".getBytes()};
oDataBytes = StringUtils.joinArrayOfByteArrays(arrayOfByteArrays);
} else {
oDataBytes = StringUtils.getXMLBlockBytes(oDataBytes, "xfa:datasets");
}
return oDataBytes;
}
private static String createNameSpaceString(Map<String, String> xdpNameSpacePrefixMap) {
Collection<String> valueCollection = xdpNameSpacePrefixMap.values();
if (valueCollection != null) {
StringBuilder strBuilder = new StringBuilder();
for (String nameSpaceStr : valueCollection) {
strBuilder.append(" ");
strBuilder.append(nameSpaceStr);
strBuilder.append(" ");
}
return strBuilder.toString();
}
return "";
}
private static byte[] mergeDataDescriptionFromTemplate(byte[] newData, PDFDocument pdfDocument) {
if (newData == null) {
return newData;
}
try {
byte[] pdfData = Utils.exportXFAData(pdfDocument);
byte[] dd = StringUtils.getXMLBlockBytes(newData, "dd:dataDescription");
if (dd == null) {
byte[] ddNew = null;
byte[] ddFind = "<dd:dataDescription".getBytes();
int nFound = StringUtils.findArraySegment(pdfData, 0, ddFind);
while (nFound >= 0) {
dd = StringUtils.getXMLBlockBytes(pdfData, "dd:dataDescription", nFound);
if (dd != null) {
ddNew = ddNew == null ? dd : StringUtils.joinArray(ddNew, dd);
}
nFound = StringUtils.findArraySegment(pdfData, nFound + ddFind.length, ddFind);
}
if (ddNew != null) {
newData = StringUtils.replaceArraySegment(newData, "</xfa:datasets".getBytes(), StringUtils.joinArray(ddNew, "</xfa:datasets".getBytes()));
}
}
}
catch (Exception ex) {
logger.warn("AEM_FRM_001_005", new String[]{ex.getMessage()}, ex);
}
return newData;
}
}