RenderPDFFormService.java
6.73 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
/*
* 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
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.fd.forms.internal;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.forms.api.PDFFormRenderOptions;
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 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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
@Component(label="Render PDF Form Service", description="Render PDF Form Service for internal use")
@Service(value={RenderPDFFormService.class})
public class RenderPDFFormService {
private static Logger logger = LoggerFactory.getLogger(RenderPDFFormService.class);
@Reference
LCFormsOsgiService lcFormsOsgiService;
public Document renderPDFForm(String urlOrfilename, Document data, PDFFormRenderOptions pdfFormRenderOptions) throws Exception {
LCFormsOptions lcOptions = Utils.create(pdfFormRenderOptions);
lcOptions.setTemplate(urlOrfilename);
byte[] dataBytes = Utils.getBytes(data);
byte[] taggedDataBytes = this.addFSTags(dataBytes, urlOrfilename, pdfFormRenderOptions.getContentRoot());
lcOptions.setData(new String(taggedDataBytes, "UTF-8"));
Map result = this.lcFormsOsgiService.render(lcOptions);
byte[] outputContent = (byte[])result.get("outputContent");
String contentType = (String)result.get("contentType");
Document doc = new Document(outputContent);
doc.setContentType(contentType);
return doc;
}
protected void insertFSTags(org.w3c.dom.Document xdpDocument, String template, String contentRoot) throws TransformerException {
Node dataNode = this.getOrCreateDataNode(xdpDocument);
if (template != null && !template.isEmpty()) {
Node fsFormsQueryNode = this.getOrCreateNode(xdpDocument, dataNode, "FSFORMQUERY_");
XMLUtils.removeChildNodes((Element)fsFormsQueryNode);
fsFormsQueryNode.appendChild(xdpDocument.createTextNode(template));
}
if (contentRoot != null && !contentRoot.isEmpty()) {
Node fsContentRootNode = this.getOrCreateNode(xdpDocument, dataNode, "FSCRURI_");
XMLUtils.removeChildNodes((Element)fsContentRootNode);
fsContentRootNode.appendChild(xdpDocument.createTextNode(contentRoot));
}
}
protected byte[] addFSTags(byte[] XMLData, String template, String contentRoot) throws Exception {
boolean canonicalize = false;
org.w3c.dom.Document xdpdatasets = this.createXDPDatasetsPacket(XMLData);
this.insertFSTags(xdpdatasets, template, contentRoot);
return XMLUtils.toXML(new DOMSource(xdpdatasets), canonicalize);
}
protected org.w3c.dom.Document createXDPDatasetsPacket(byte[] XMLData) throws ParserConfigurationException, SAXException, IOException {
String XDPPacket = "<?xfa generator=\"XFA2_4\" APIVersion=\"3.6.14099.0\"?><xdp:xdp xmlns:xdp=\"http://ns.adobe.com/xdp/\" ><xfa:datasets xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" ><xfa:data></xfa:data></xfa:datasets></xdp:xdp>";
org.w3c.dom.Document XDPDataDom = null;
org.w3c.dom.Document XMLDataDom = null;
Node rootXMLNode = null;
if (XMLData != null && XMLData.length > 0 && (XMLDataDom = XMLUtils.loadXMLbytes(XMLData, false)) != null) {
rootXMLNode = XMLDataDom.getDocumentElement();
}
if (rootXMLNode == null) {
XDPDataDom = XMLUtils.loadXMLbytes(XDPPacket.getBytes(), false);
} else if ("xdp:xdp".equals(rootXMLNode.getNodeName())) {
XDPDataDom = XMLDataDom;
} else if ("xfa:data".equals(rootXMLNode.getNodeName())) {
XDPDataDom = XMLUtils.loadXMLbytes(XDPPacket.getBytes(), false);
Node oNode = XMLUtils.selectNode(XDPDataDom.getDocumentElement(), "xfa:datasets/xfa:data", 0);
Node newNode = XDPDataDom.importNode(rootXMLNode, true);
oNode.getParentNode().replaceChild(newNode, oNode);
} else {
XDPDataDom = XMLUtils.loadXMLbytes(XDPPacket.getBytes(), false);
Node oNode = XMLUtils.selectNode(XDPDataDom.getDocumentElement(), "xfa:datasets/xfa:data", 0);
Node newNode = XDPDataDom.importNode(rootXMLNode, true);
oNode.appendChild(newNode);
}
return XDPDataDom;
}
protected Node getOrCreateDataNode(org.w3c.dom.Document xdpDocument) {
Node dataNode = XMLUtils.selectNode(xdpDocument.getDocumentElement(), "xfa:datasets/xfa:data", 0);
logger.debug("getOrCreateDataNode: dataNode " + dataNode);
if (dataNode == null) {
Node datasetsNode = XMLUtils.selectNode(xdpDocument.getDocumentElement(), "xfa:datasets", 0);
logger.debug("getOrCreateDataNode: datasetsNode " + datasetsNode);
if (datasetsNode == null) {
logger.debug("getOrCreateDataNode: creating datasets Node");
Element xdpNode = xdpDocument.getDocumentElement();
datasetsNode = xdpNode.appendChild(xdpDocument.createElementNS("http://www.xfa.org/schema/xfa-data/1.0/", "xfa:datasets"));
}
logger.debug("getOrCreateDataNode: creating data Node");
dataNode = datasetsNode.appendChild(xdpDocument.createElement("xfa:data"));
}
return dataNode;
}
protected Node getOrCreateNode(org.w3c.dom.Document xdpDocument, Node parentNode, String name) {
Node node = XMLUtils.selectNode((Element)parentNode, name, 0);
if (node == null) {
node = parentNode.appendChild(xdpDocument.createElement(name));
}
return node;
}
protected void bindLcFormsOsgiService(LCFormsOsgiService lCFormsOsgiService) {
this.lcFormsOsgiService = lCFormsOsgiService;
}
protected void unbindLcFormsOsgiService(LCFormsOsgiService lCFormsOsgiService) {
if (this.lcFormsOsgiService == lCFormsOsgiService) {
this.lcFormsOsgiService = null;
}
}
}