LetterInstanceParser.java
18.2 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.icc.dbforms.obj.DataModule
* com.adobe.icc.dbforms.obj.ImageModule
* com.adobe.icc.dbforms.obj.LDMAssignment
* com.adobe.icc.dbforms.obj.ListDataModule
* com.adobe.icc.dbforms.obj.ListDataModule$Style
* com.adobe.icc.dbforms.obj.ListDataModule$Type
* com.adobe.icc.dbforms.obj.ModuleAssignment
* com.adobe.icc.dbforms.obj.TargetAreaAssignment
* com.adobe.icc.dbforms.obj.TextModule
* com.adobe.icc.dbforms.obj.Variable
* com.adobe.icc.services.api.DataModuleService
* org.apache.commons.lang3.StringUtils
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.fd.adaddon.internal.utils;
import com.adobe.fd.adaddon.internal.utils.DataUtils;
import com.adobe.icc.dbforms.obj.DataModule;
import com.adobe.icc.dbforms.obj.ImageModule;
import com.adobe.icc.dbforms.obj.LDMAssignment;
import com.adobe.icc.dbforms.obj.ListDataModule;
import com.adobe.icc.dbforms.obj.ModuleAssignment;
import com.adobe.icc.dbforms.obj.TargetAreaAssignment;
import com.adobe.icc.dbforms.obj.TextModule;
import com.adobe.icc.dbforms.obj.Variable;
import com.adobe.icc.services.api.DataModuleService;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class LetterInstanceParser {
private static Logger logger = LoggerFactory.getLogger(LetterInstanceParser.class);
private static String ICC_NAMESPACE = "http://ns.adobe.com/icc/1.0/";
private static String TEMPLATE_SOM_PREFIX = "xfa[0].template[0]";
private static String MODULE_CONTAINER_SOM_SUFFIX = "ModuleContainer[0]";
private XPath xPath;
private Document letterInstanceData;
private Map<String, String> variablesValueMap;
private Map<String, String> fieldsValueMap;
private String letterRef;
private String iccPrefix = "";
private boolean isNamespaced;
Node ddiXML;
private List<TargetAreaAssignment> taaList = new ArrayList<TargetAreaAssignment>();
private Map<String, TextModule> moduleCache = new HashMap<String, TextModule>();
private DataModuleService dataModuleService;
public LetterInstanceParser(Document letterInstanceData, DataModuleService dataModuleService, boolean nameSpaceAware) {
this.letterInstanceData = letterInstanceData;
this.dataModuleService = dataModuleService;
this.xPath = XPathFactory.newInstance().newXPath();
this.isNamespaced = nameSpaceAware;
if (nameSpaceAware) {
this.xPath.setNamespaceContext(new ICCNamespaceContext());
this.iccPrefix = "icc:";
}
}
private String namespaced(String pattern) {
if (!this.isNamespaced) {
return pattern.replaceAll("icc:", "");
}
return pattern;
}
private Object eval(String xpath, Object context, QName type) throws XPathExpressionException {
return this.xPath.evaluate(this.namespaced(xpath), context, type);
}
private boolean compareNodeName(Node node, String localName) {
if (this.isNamespaced) {
return localName.equals(node.getLocalName()) && ICC_NAMESPACE.equals(node.getNamespaceURI());
}
return ("icc:" + localName).equals(node.getNodeName());
}
private void parseICCNode(Node iccNode) throws XPathExpressionException {
NodeList nodeList = iccNode.getChildNodes();
HashMap<String, Node> refNodeMap = new HashMap<String, Node>();
for (int i = 0; i < nodeList.getLength(); ++i) {
Node childNode = nodeList.item(i);
if (childNode.getNodeType() != 1) continue;
if (this.compareNodeName(childNode, "variables")) {
this.parseVariablesNode(childNode);
continue;
}
if (this.compareNodeName(childNode, "letter")) {
this.parseLetterNode(childNode);
continue;
}
String refAttr = (String)this.eval("string(@icc:ref)", childNode, XPathConstants.STRING);
if (refAttr == null || refAttr.length() <= 0 || !refAttr.endsWith(MODULE_CONTAINER_SOM_SUFFIX)) continue;
this.parseTargetAreaData(TEMPLATE_SOM_PREFIX + "." + refAttr.substring(0, refAttr.length() - MODULE_CONTAINER_SOM_SUFFIX.length() - 1), childNode);
refNodeMap.put(refAttr.substring(0, refAttr.length() - MODULE_CONTAINER_SOM_SUFFIX.length() - 1), childNode);
}
}
private String getAttribute(Node node, String attributeName) throws XPathExpressionException {
return (String)this.eval("string(@" + attributeName + ")", node, XPathConstants.STRING);
}
private String wrapInContentPacket(String html) {
return "<content><guid/><xfaXhtml><body xfa:APIVersion=\"2.7.0.0\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\">" + html + "</body></xfaXhtml></content>";
}
private TextModule parseTextModuleNode(Node moduleNode) throws XPathExpressionException {
TextModule tm = new TextModule();
String id = this.getAttribute(moduleNode, "icc:ref");
tm.setId(id);
if (id != null && id.startsWith("extraContentNL")) {
String emptyLineContent = (String)this.eval("icc:content/text()", moduleNode, XPathConstants.STRING);
tm.setContentXML(this.wrapInContentPacket(emptyLineContent));
return tm;
}
String textContent = (String)this.eval("icc:raw/icc:content/text()", moduleNode, XPathConstants.STRING);
if (StringUtils.isNotEmpty((CharSequence)textContent)) {
tm.setContentXML(this.wrapInContentPacket(URLDecoder.decode(textContent)));
} else {
if (this.moduleCache.get(id) == null) {
this.moduleCache.put(id, (TextModule)this.dataModuleService.getDataModule(id));
}
tm = this.moduleCache.get(id);
}
NodeList vars = (NodeList)this.eval("icc:raw/icc:variables/icc:var", moduleNode, XPathConstants.NODESET);
HashSet<Variable> variables = new HashSet<Variable>();
for (int j = 0; vars != null && j < vars.getLength(); ++j) {
Node var = vars.item(j);
String varName = this.getAttribute(var, "icc:name");
Variable variable = new Variable();
variable.setName(varName);
variables.add(variable);
}
tm.setVariableList(variables);
return tm;
}
private ImageModule parseImageModuleNode(Node moduleNode) throws XPathExpressionException {
ImageModule im = new ImageModule();
String id = this.getAttribute(moduleNode, "icc:ref");
String imageData = (String)this.eval("icc:content/text()", moduleNode, XPathConstants.STRING);
String dataURI = "data:image;base64," + imageData;
im.setId(dataURI);
return im;
}
private ModuleAssignment getModuleAssignment(String id, String ref, List<ModuleAssignment> moduleAssignments) {
for (ModuleAssignment ma : moduleAssignments) {
DataModule dm = ma.getDataModule();
String dmId = dm.getId();
if (!dmId.equals(id + ":" + ref)) continue;
return ma;
}
return null;
}
private LDMAssignment getLDMAssignment(String id, String ref, List<LDMAssignment> ldmAssignments) {
for (LDMAssignment ma : ldmAssignments) {
if (ma == null || ma.getTarget() == null || ma.getTarget().getId() == null || !ma.getTarget().getId().equals(id + ":" + ref)) continue;
return ma;
}
return null;
}
private void parseModuleNode(Node moduleNode, ListDataModule ldm, int depth) throws XPathExpressionException {
List listModuleAssignments = ldm.getAssignmentList();
NodeList parents = (NodeList)this.eval("icc:origin/icc:parent", moduleNode, XPathConstants.NODESET);
NodeList bullets = (NodeList)this.eval("icc:format/icc:bullet", moduleNode, XPathConstants.NODESET);
if (parents != null && parents.getLength() > depth) {
String parentRef;
Node lastParent = parents.item(depth);
String parentId = this.getAttribute(lastParent, "icc:id");
LDMAssignment ma = this.getLDMAssignment(parentId, parentRef = this.getAttribute(lastParent, "icc:ref"), listModuleAssignments);
if (ma != null) {
ListDataModule dm = (ListDataModule)ma.getTarget();
this.parseModuleNode(moduleNode, dm, depth + 1);
} else {
String bulletType;
Node bullet;
ma = new LDMAssignment();
ListDataModule listModule = new ListDataModule();
if (bullets != null && bullets.getLength() > 0 && (bulletType = this.getAttribute(bullet = bullets.item(0), "icc:type")) != null && bulletType.length() > 0) {
ListDataModule.Type type = ListDataModule.Type.valueOf((String)bulletType);
listModule.setStyle(this.getListStyle(bulletType));
listModule.setType(type);
}
listModule.setId(parentId + ":" + parentRef);
ArrayList ldma = new ArrayList();
listModule.setAssignmentList(ldma);
ma.setTarget((DataModule)listModule);
listModuleAssignments.add(ma);
this.parseModuleNode(moduleNode, listModule, depth + 1);
}
} else {
String moduleType = this.getAttribute(moduleNode, "icc:type");
LDMAssignment ma = new LDMAssignment();
listModuleAssignments.add(ma);
if ("text".equals(moduleType)) {
TextModule tm = this.parseTextModuleNode(moduleNode);
ma.setTarget((DataModule)tm);
String indent = (String)this.eval("icc:format/icc:indent/@icc:level", moduleNode, XPathConstants.STRING);
ma.setIndentationLevel(Double.valueOf(Double.parseDouble(indent)));
} else if ("image".equals(moduleType)) {
ma.setTarget((DataModule)this.parseImageModuleNode(moduleNode));
String indent = (String)this.eval("icc:format/icc:indent/@icc:level", moduleNode, XPathConstants.STRING);
ma.setIndentationLevel(Double.valueOf(Double.parseDouble(indent)));
}
}
}
private ListDataModule.Style getListStyle(String bulletType) {
if (bulletType != null && bulletType.length() > 0) {
if (bulletType.startsWith("TYPE_NUMBER")) {
return ListDataModule.Style.STYLE_NUMBERED;
}
if (bulletType.startsWith("TYPE_LETTER")) {
return ListDataModule.Style.STYLE_LETTERED;
}
if (bulletType.startsWith("TYPE_BULLET")) {
return ListDataModule.Style.STYLE_BULLETED;
}
return ListDataModule.Style.STYLE_PLAIN;
}
return ListDataModule.Style.STYLE_PLAIN;
}
private ModuleAssignment parseModuleNode(Node moduleNode, TargetAreaAssignment taa) throws XPathExpressionException {
List moduleAssignments = taa.getModuleAssignmentList();
NodeList parents = (NodeList)this.eval("icc:origin/icc:parent", moduleNode, XPathConstants.NODESET);
NodeList bullets = (NodeList)this.eval("icc:format/icc:bullet", moduleNode, XPathConstants.NODESET);
if (parents != null && parents.getLength() > 0) {
String parentRef;
Node lastParent = parents.item(0);
String parentId = this.getAttribute(lastParent, "icc:id");
ModuleAssignment ma = this.getModuleAssignment(parentId, parentRef = this.getAttribute(lastParent, "icc:ref"), moduleAssignments);
if (ma != null) {
ListDataModule dm = (ListDataModule)ma.getDataModule();
List ldma = dm.getAssignmentList();
this.parseModuleNode(moduleNode, dm, 1);
} else {
String bulletType;
Node bullet;
ma = new ModuleAssignment();
ma.setPreSelected(true);
ListDataModule listModule = new ListDataModule();
listModule.setId(parentId + ":" + parentRef);
ArrayList ldma = new ArrayList();
if (bullets != null && bullets.getLength() > 0 && (bulletType = this.getAttribute(bullet = bullets.item(0), "icc:type")) != null && bulletType.length() > 0) {
ListDataModule.Type type = ListDataModule.Type.valueOf((String)bulletType);
listModule.setStyle(this.getListStyle(bulletType));
listModule.setType(type);
}
listModule.setAssignmentList(ldma);
ma.setDataModule((DataModule)listModule);
moduleAssignments.add(ma);
this.parseModuleNode(moduleNode, listModule, 1);
}
} else {
String moduleType = this.getAttribute(moduleNode, "icc:type");
ModuleAssignment ma = new ModuleAssignment();
ma.setPreSelected(true);
moduleAssignments.add(ma);
String indent = (String)this.eval("icc:format/icc:indent/@icc:level", moduleNode, XPathConstants.STRING);
if ("text".equals(moduleType)) {
ma.setDataModule((DataModule)this.parseTextModuleNode(moduleNode));
ma.setIndentationLevel(Double.valueOf(Double.parseDouble(indent)));
} else if ("image".equals(moduleType)) {
ma.setDataModule((DataModule)this.parseImageModuleNode(moduleNode));
ma.setIndentationLevel(Double.valueOf(Double.parseDouble(indent)));
} else {
logger.error("Unexpected module type : " + moduleType);
}
}
return null;
}
private void parseTargetAreaData(String refAttr, Node childNode) throws XPathExpressionException {
NodeList moduleNodes = (NodeList)this.eval("./icc:module", childNode, XPathConstants.NODESET);
TargetAreaAssignment taa = new TargetAreaAssignment();
this.taaList.add(taa);
taa.setTargetAreaPath(refAttr);
ArrayList moduleAssignments = new ArrayList();
taa.setModuleAssignmentList(moduleAssignments);
for (int i = 0; i < moduleNodes.getLength(); ++i) {
Node moduleNode = moduleNodes.item(i);
this.parseModuleNode(moduleNode, taa);
}
}
public void parse() throws XPathExpressionException {
Element de = this.letterInstanceData.getDocumentElement();
Node iccNode = (Node)this.eval("/*/icc:icc", de, XPathConstants.NODE);
if (iccNode != null) {
this.parseICCNode(iccNode);
}
}
private void parseVariablesNode(Node variablesNode) throws XPathExpressionException {
NodeList vars;
this.variablesValueMap = new HashMap<String, String>();
if (variablesNode != null && (vars = (NodeList)this.eval("./icc:var", variablesNode, XPathConstants.NODESET)) != null) {
for (int i = 0; i < vars.getLength(); ++i) {
Node varNode = vars.item(i);
String varName = (String)this.eval("string(./@icc:name)", varNode, XPathConstants.STRING);
String varVal = (String)this.eval("string(./text())", varNode, XPathConstants.STRING);
this.variablesValueMap.put(varName, varVal);
}
}
}
private void parseLetterNode(Node letterNode) throws XPathExpressionException {
this.ddiXML = (Node)this.eval("./icc:dataDictionary/icc:instance/*", letterNode, XPathConstants.NODE);
this.letterRef = (String)this.eval("string(./@icc:ref)", letterNode, XPathConstants.STRING);
Node fieldsNode = (Node)this.eval("./icc:layout/icc:fields", letterNode, XPathConstants.NODE);
this.parseFieldsNode(fieldsNode);
}
private void parseFieldsNode(Node fieldsNode) throws XPathExpressionException {
NodeList fields;
this.fieldsValueMap = new HashMap<String, String>();
Element dataRoot = this.letterInstanceData.getDocumentElement();
if (fieldsNode != null && (fields = (NodeList)this.eval("./icc:field", fieldsNode, XPathConstants.NODESET)) != null) {
for (int i = 0; i < fields.getLength(); ++i) {
Node fieldNode = fields.item(i);
String iccRef = TEMPLATE_SOM_PREFIX + "." + this.eval("string(./@icc:ref)", fieldNode, XPathConstants.STRING);
String dataSOM = (String)this.eval("string(./@icc:dataSOM)", fieldNode, XPathConstants.STRING);
Node valueNode = DataUtils.resolveDataSom(dataRoot, dataSOM, false);
this.fieldsValueMap.put(iccRef, null);
if (valueNode == null) continue;
this.fieldsValueMap.put("somExpr:" + iccRef, valueNode.getTextContent());
}
}
}
public List<TargetAreaAssignment> getTargetAreaAssignmentList() {
return this.taaList;
}
public Map<String, String> getVariablesValueMap() {
return this.variablesValueMap;
}
public Map<String, String> getFieldsValueMap() {
return this.fieldsValueMap;
}
public String getLetterRef() {
return this.letterRef;
}
public Node getDdiXML() {
return this.ddiXML;
}
static class ICCNamespaceContext
implements NamespaceContext {
ICCNamespaceContext() {
}
@Override
public String getNamespaceURI(String prefix) {
if ("icc".equals(prefix)) {
return ICC_NAMESPACE;
}
return null;
}
@Override
public String getPrefix(String namespaceURI) {
return null;
}
@Override
public Iterator getPrefixes(String namespaceURI) {
return null;
}
}
}