SubmitDataCollector.java
24.4 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.StringUtils
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.guide.utils;
import com.adobe.aemds.guide.service.GuideException;
import com.adobe.aemds.guide.utils.CustomJSONWriter;
import com.adobe.aemds.guide.utils.GuideModelUtils;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.adobe.aemds.guide.utils.XMLUtils;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class SubmitDataCollector {
protected Logger logger = LoggerFactory.getLogger(SubmitDataCollector.class);
private StringWriter stringWriter;
protected CustomJSONWriter jsonWriter;
protected JSONObject mergeJson;
protected JSONObject templateJson;
protected JSONArray fileAttachments;
protected JSONObject submissionInfo;
protected DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
protected Document dataDoc;
protected Node currentRepeatNode;
protected Node currentRepeatUnboundNode;
protected String currentRepeatRelativePath = "";
protected String currentRepeatFullPath = "";
protected boolean startsWithAfData;
private String xdpValue;
protected boolean isLazyXml;
XPath xPath = XPathFactory.newInstance().newXPath();
public SubmitDataCollector(String mergeJsonString, JSONObject templateJson, String guidePrefillXml, String xdpValue, String submissionInfo, boolean isLazyXml) {
try {
this.mergeJson = new JSONObject(mergeJsonString);
this.templateJson = templateJson;
this.stringWriter = new StringWriter();
this.jsonWriter = new CustomJSONWriter(this.stringWriter);
this.fileAttachments = new JSONArray();
this.xdpValue = xdpValue;
this.submissionInfo = submissionInfo != null ? new JSONObject(submissionInfo) : null;
this.isLazyXml = isLazyXml;
DocumentBuilder db = this.dbf.newDocumentBuilder();
if (guidePrefillXml == null) {
this.generateFullXml(db);
} else {
this.dataDoc = db.parse(new ByteArrayInputStream(guidePrefillXml.getBytes("UTF-8")));
this.startsWithAfData = this.dataDoc.getDocumentElement().getTagName().equals("afData");
this.updatePrefillXML();
}
this.currentRepeatNode = this.getInitialRepeatNode();
this.currentRepeatUnboundNode = this.getInitialRepeatUnboundNode();
this.addSubmissionInfoXml();
}
catch (Exception e) {
this.logger.error(e.getMessage(), (Throwable)e);
throw new GuideException(e);
}
}
public SubmitDataCollector(String mergeJsonString, JSONObject templateJson, String guidePrefillXml, String xdpValue) {
this(mergeJsonString, templateJson, guidePrefillXml, xdpValue, null, false);
}
public JSONObject collectSubmitData() {
JSONObject guideResult;
try {
String prefillXmlOriginal;
String changedOrOrignialPrefillXml = prefillXmlOriginal = XMLUtils.getXMLfromXsdDom(this.dataDoc.getDocumentElement());
this.jsonWriter.object();
this.jsonWriter.key("guideValue").object();
this.getKeyValueFromGuideDom(this.mergeJson, this.templateJson);
this.jsonWriter.endObject();
this.jsonWriter.key("fileAttachmentsList").value((Object)this.fileAttachments);
if (this.dataDoc != null) {
DocumentBuilder db = this.dbf.newDocumentBuilder();
DOMImplementation domImpl = db.getDOMImplementation();
Document dorDoc = domImpl.createDocument(null, null, null);
if (!this.isLazyXml) {
changedOrOrignialPrefillXml = XMLUtils.getXMLfromXsdDom(this.dataDoc.getDocumentElement());
}
this.jsonWriter.key("dataXml").value(changedOrOrignialPrefillXml);
this.jsonWriter.key("dorDataXml").value(GuideModelUtils.getDataForDORMerge(dorDoc, this.dataDoc, this.templateJson));
}
this.jsonWriter.endObject();
guideResult = new JSONObject(this.stringWriter.toString());
}
catch (Exception e) {
this.logger.error("Unable to create JSON for Submit", (Throwable)e);
throw new GuideException(e);
}
return guideResult;
}
private void createBoundAndUnboundWrapper(DocumentBuilder db) {
DOMImplementation domImpl = db.getDOMImplementation();
this.dataDoc = domImpl.createDocument(null, null, null);
String dataDocumentRoot = "afData";
Element root = this.dataDoc.createElement(dataDocumentRoot);
this.dataDoc.appendChild(root);
Element afUnboundData = this.createUnboundTag(root);
this.createDataTag(afUnboundData);
Element afBoundData = this.createBoundTag(root);
Element dataInBoundData = this.createDataTag(afBoundData);
if (StringUtils.isEmpty((String)this.xdpValue)) {
XMLUtils.setXfaNameSpace(dataInBoundData);
}
}
protected void addBoundPartToDataXml() {
try {
DocumentBuilder db;
Document xdpDoc;
Node afBoundData = (Node)this.xPath.evaluate("afBoundData", this.dataDoc.getDocumentElement(), XPathConstants.NODE);
Node dataInsideAfBounddata = (Node)this.xPath.evaluate("*", afBoundData, XPathConstants.NODE);
if (this.xdpValue != null && this.xdpValue.length() > 0 && (xdpDoc = (db = this.dbf.newDocumentBuilder()).parse(new ByteArrayInputStream(this.xdpValue.getBytes("UTF-8")))) != null) {
Node elemDoc = this.dataDoc.importNode(xdpDoc.getDocumentElement(), true);
if (dataInsideAfBounddata != null) {
afBoundData.replaceChild(elemDoc, dataInsideAfBounddata);
} else {
afBoundData.appendChild(elemDoc);
}
}
}
catch (Exception e) {
this.logger.error("Error in adding bound part to xml" + e.getMessage(), (Throwable)e);
throw new GuideException(e);
}
}
private void addSubmissionInfoXml() {
try {
Element root;
Element submissionInfoRootTag;
if (this.startsWithAfData && (submissionInfoRootTag = this.createSubmissionInfoTag(root = this.dataDoc.getDocumentElement())) != null && this.submissionInfo != null) {
this.addSubmissionInfoJSON(this.submissionInfo, submissionInfoRootTag);
}
}
catch (Exception e) {
this.logger.error("Error in adding submission info to xml" + e.getMessage(), (Throwable)e);
}
}
private Element createSubmissionInfoTag(Element root) {
Element afSubmissionInfo = this.dataDoc.createElement("afSubmissionInfo");
root.appendChild(afSubmissionInfo);
return afSubmissionInfo;
}
private void addSubmissionInfoJSON(JSONObject jsonData, Element root) {
try {
Iterator keys = jsonData.keys();
while (keys.hasNext()) {
Element childTag;
String key = (String)keys.next();
Object value = jsonData.get(key);
if (value instanceof JSONArray) {
JSONArray childJSONArr = (JSONArray)value;
childTag = this.dataDoc.createElement(key);
StringBuilder str = new StringBuilder();
if (childJSONArr != null) {
for (int i = 0; i < childJSONArr.length(); ++i) {
if (!(childJSONArr.get(i) instanceof String)) continue;
str.append(childJSONArr.getString(i));
if (i == childJSONArr.length() - 1) continue;
str.append(",");
}
}
childTag.setTextContent(str.toString());
root.appendChild(childTag);
continue;
}
if (value instanceof JSONObject) {
JSONObject childJSONObj = (JSONObject)value;
childTag = this.dataDoc.createElement(key);
root.appendChild(childTag);
this.addSubmissionInfoJSON(childJSONObj, childTag);
continue;
}
Element computedData = this.dataDoc.createElement(key);
computedData.setTextContent(value.toString());
root.appendChild(computedData);
}
}
catch (Exception e) {
this.logger.error("Error in adding submission info to xml" + e.getMessage(), (Throwable)e);
}
}
protected void generateFullXml(DocumentBuilder db) {
this.createBoundAndUnboundWrapper(db);
this.startsWithAfData = true;
this.addBoundPartToDataXml();
}
protected void updatePrefillXML() throws Exception {
DocumentBuilder db = this.dbf.newDocumentBuilder();
Document xdpDoc = null;
Element afBoundData = null;
if (this.xdpValue != null && this.xdpValue.length() > 0) {
xdpDoc = db.parse(new ByteArrayInputStream(this.xdpValue.getBytes("UTF-8")));
if (this.startsWithAfData) {
if (xdpDoc != null && this.xPath.evaluate("afBoundData", this.dataDoc.getDocumentElement(), XPathConstants.NODE) == null) {
afBoundData = this.createBoundTag(this.dataDoc.getDocumentElement());
Node elemDoc = this.dataDoc.importNode(xdpDoc.getDocumentElement(), true);
afBoundData.appendChild(elemDoc);
}
} else {
this.generateFullXml(db);
}
}
}
protected Node getInitialRepeatNode() throws Exception {
return this.getInitialRepeatUnboundNode();
}
protected Node getInitialRepeatUnboundNode() throws Exception {
if (this.startsWithAfData) {
return (Node)this.xPath.evaluate("afUnboundData/data", this.dataDoc.getDocumentElement(), XPathConstants.NODE);
}
if (this.xdpValue != null && this.xdpValue.length() > 0) {
return null;
}
return this.dataDoc.getDocumentElement();
}
protected Element createDataTag(Element root) {
Element data = this.dataDoc.createElement("data");
root.appendChild(data);
return data;
}
protected Element createBoundTag(Element root) {
Element afBoundData = this.dataDoc.createElement("afBoundData");
root.appendChild(afBoundData);
return afBoundData;
}
protected Element createUnboundTag(Element root) {
Element afUnboundData = this.dataDoc.createElement("afUnboundData");
root.appendChild(afUnboundData);
return afUnboundData;
}
protected boolean needRepeatHandling(JSONObject jsonObject) {
if (this.xdpValue != null && this.xdpValue.length() > 0) {
return false;
}
return true;
}
private void getKeyValueFromGuideDom(JSONObject mergeJson, JSONObject templateJson) throws Exception {
String guideNodeClass = null;
boolean isFileUpload = false;
boolean isButton = false;
if (mergeJson.has("guideNodeClass")) {
guideNodeClass = mergeJson.getString("guideNodeClass");
}
if (GuideUtils.isGuideFileUploadModel(guideNodeClass)) {
String attachmentId = null;
attachmentId = this.getGuideNodeId(mergeJson, attachmentId);
this.fileAttachments.put((Object)attachmentId);
isFileUpload = true;
}
if (GuideUtils.isGuideButtonModel(guideNodeClass)) {
isButton = true;
}
if (isFileUpload) {
if (this.addToBoundData(templateJson)) {
this.updateBoundFile(mergeJson, templateJson);
}
if (this.addToUnBoundData(templateJson)) {
this.updateUnBoundFile(mergeJson, templateJson);
}
} else if (GuideUtils.isGuideFieldModel(guideNodeClass) && !isButton && !isFileUpload) {
if (this.addToBoundData(templateJson)) {
this.updateBoundField(mergeJson, templateJson);
}
if (this.addToUnBoundData(templateJson)) {
this.updateUnBoundField(mergeJson, templateJson);
}
}
Iterator templateKeys = templateJson.keys();
while (templateKeys.hasNext()) {
String templateKey = (String)templateKeys.next();
Object templateValue = templateJson.get(templateKey);
if (templateValue instanceof JSONObject && !"layout".equals(templateKey)) {
JSONObject templateChild = (JSONObject)templateValue;
String templateChildName = null;
List mergeChildJsonList = null;
if (templateChild.has("guideNodeClass") && templateChild.has("name")) {
templateChildName = templateChild.getString("name");
boolean templateChildRepeatable = this.isRepeatable(templateChild);
mergeChildJsonList = this.getMergedJson(mergeJson, templateChildName, templateChildRepeatable);
} else if (mergeJson.has(templateKey)) {
mergeChildJsonList = new ArrayList();
JSONObject mergeChildValue = (JSONObject)mergeJson.get(templateKey);
mergeJson.remove(templateKey);
mergeChildJsonList.add(mergeChildValue);
} else {
this.logger.warn("templateKey not found in merge json:" + templateKey);
continue;
}
boolean repeatable = this.isRepeatable(templateChild);
if (repeatable && this.needRepeatHandling(templateChild)) {
this.handleRepeatJson(mergeChildJsonList, templateChild);
continue;
}
if (!mergeChildJsonList.isEmpty()) {
this.getKeyValueFromGuideDom((JSONObject)mergeChildJsonList.get(0), templateChild);
continue;
}
this.logger.warn("no object found in mergeJSON corresponding to templateJson. Key:" + templateKey + templateValue);
continue;
}
if (!(templateValue instanceof JSONArray)) continue;
this.logger.debug("found an json array in guide node structure that should not happen. Ignoring this. Key:" + templateKey);
}
}
protected void updateCurrentRepeatUnboundNode() {
this.currentRepeatUnboundNode = this.currentRepeatNode;
}
private String getCommonPath(String currentRepeatPath, String originalRepeatPath) {
String[] originalParts = StringUtils.split((String)originalRepeatPath, (String)"/");
String[] currentParts = StringUtils.split((String)currentRepeatPath, (String)"/");
String relativePath = null;
StringBuilder commonParts = new StringBuilder("");
for (int i = 0; i < Math.min(originalParts.length, currentParts.length) && originalParts[i].equals(currentParts[i]); ++i) {
commonParts.append(originalParts[i]);
commonParts.append("/");
}
relativePath = commonParts.toString();
if (relativePath.length() > 0) {
relativePath = relativePath.substring(0, relativePath.lastIndexOf("/"));
}
return relativePath;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void handleRepeatJson(List<JSONObject> mergeJsonObjects, JSONObject templateJson) throws Exception {
block15 : {
Node originalRepeatNode = this.currentRepeatNode;
String originalRepeatRelativePath = this.currentRepeatRelativePath;
String originalRepeatFullPath = this.currentRepeatFullPath;
NodeList currentRepeatNodeList = null;
try {
this.currentRepeatRelativePath = this.getRelativeRepeatPath(templateJson);
this.currentRepeatFullPath = this.getFullRepeatPath(templateJson);
if (!this.currentRepeatRelativePath.isEmpty()) {
Node parentDataNode = originalRepeatNode;
if (this.currentRepeatRelativePath.length() > 0 && originalRepeatRelativePath.length() > 0) {
String commonPath = this.getCommonPath(this.currentRepeatFullPath, originalRepeatFullPath);
if (!commonPath.equals(originalRepeatFullPath)) {
if (commonPath.length() == 0) {
parentDataNode = this.getInitialRepeatNode();
} else {
this.currentRepeatRelativePath = StringUtils.substringAfter((String)this.currentRepeatFullPath, (String)(commonPath + "/"));
parentDataNode = (Node)this.xPath.evaluate(commonPath, this.getInitialRepeatNode(), XPathConstants.NODE);
}
} else {
parentDataNode = originalRepeatNode;
}
}
currentRepeatNodeList = (NodeList)this.xPath.evaluate(this.currentRepeatRelativePath, parentDataNode, XPathConstants.NODESET);
int mergeJsonSize = mergeJsonObjects.size();
int currentRepeatNodeSize = currentRepeatNodeList.getLength();
if (currentRepeatNodeSize > mergeJsonSize) {
for (int j = mergeJsonSize; j < currentRepeatNodeSize; ++j) {
Node repeatNode = currentRepeatNodeList.item(j);
repeatNode.getParentNode().removeChild(repeatNode);
}
currentRepeatNodeSize = mergeJsonSize;
}
for (int i = 0; i < mergeJsonSize; ++i) {
JSONObject mergeJson = mergeJsonObjects.get(i);
if (currentRepeatNodeSize <= i) {
this.currentRepeatNode = XMLUtils.createNode(this.dataDoc, this.xPath, parentDataNode, this.currentRepeatRelativePath);
if (templateJson.has("bindRef")) {
((Element)this.currentRepeatNode).setAttribute("xfa:dataNode", "dataGroup");
}
} else {
this.currentRepeatNode = currentRepeatNodeList.item(i);
}
this.updateCurrentRepeatUnboundNode();
this.getKeyValueFromGuideDom(mergeJson, templateJson);
}
break block15;
}
this.logger.error("currentRepeatRelativePath is empty for templateJson" + templateJson.toString());
}
finally {
this.currentRepeatNode = originalRepeatNode;
this.currentRepeatRelativePath = originalRepeatRelativePath;
this.currentRepeatFullPath = originalRepeatFullPath;
this.updateCurrentRepeatUnboundNode();
}
}
}
protected boolean addToBoundData(JSONObject templateJsonObject) {
return templateJsonObject.has("bindRef");
}
protected boolean addToUnBoundData(JSONObject templateJsonObject) {
return !templateJsonObject.has("bindRef");
}
protected void updateUnBoundField(JSONObject jsonObject, JSONObject templateJson) throws JSONException {
if (jsonObject.has("_value")) {
String name = jsonObject.getString("name");
String _value = jsonObject.getString("_value");
this.jsonWriter.key(name).value(_value);
if (this.currentRepeatUnboundNode != null) {
XMLUtils.addToDocument(this.dataDoc, this.xPath, this.currentRepeatUnboundNode, name, _value);
}
}
}
protected void updateUnBoundFile(JSONObject jsonObject, JSONObject templateJson) throws JSONException {
if (jsonObject.has("files")) {
String name = jsonObject.getString("name");
if (this.currentRepeatUnboundNode != null) {
Element nonBindedFileAttachmentNode = this.dataDoc.createElement(name);
this.currentRepeatUnboundNode.appendChild(nonBindedFileAttachmentNode);
XMLUtils.createAttachmentTag(this.dataDoc, nonBindedFileAttachmentNode, jsonObject);
}
}
}
protected void updateBoundField(JSONObject jsonObject, JSONObject templateJson) throws JSONException, Exception {
}
protected void updateBoundFile(JSONObject jsonObject, JSONObject templateJsonObject) throws JSONException, Exception {
}
protected String getRelativeRepeatPath(JSONObject templateJsonObject) throws JSONException {
String bindPath = "";
if (templateJsonObject.has("name")) {
bindPath = templateJsonObject.getString("name");
}
return bindPath;
}
protected String getFullRepeatPath(JSONObject templateJsonObject) throws JSONException {
return "";
}
private String getGuideNodeId(JSONObject jsonObject, String attachmentId) throws JSONException {
if (jsonObject.has("id") && jsonObject.getString("id").length() > 0) {
attachmentId = jsonObject.getString("id");
} else if (jsonObject.has("templateId") && jsonObject.getString("templateId").length() > 0) {
attachmentId = jsonObject.getString("templateId");
} else {
this.logger.error("Guide Object does not have any associated id. Log and ignore it:" + jsonObject.toString());
}
return attachmentId;
}
private boolean isRepeatable(JSONObject jsonObject) throws JSONException {
int minOccur = 1;
int maxOccur = 1;
if (jsonObject.has("minOccur")) {
minOccur = jsonObject.getInt("minOccur");
}
if (jsonObject.has("maxOccur")) {
maxOccur = jsonObject.getInt("maxOccur");
}
if (minOccur != 1 || maxOccur != 1) {
return true;
}
return false;
}
private List<JSONObject> getMergedJson(JSONObject mergedParent, String name, boolean namedTemplateChildRepeatable) throws JSONException {
LinkedHashMap<String, JSONObject> mergeInstancesMap = new LinkedHashMap<String, JSONObject>();
Iterator keys = mergedParent.keys();
while (keys.hasNext()) {
JSONObject childJson;
String key = (String)keys.next();
Object value = mergedParent.get(key);
if (!(value instanceof JSONObject) || !(childJson = (JSONObject)value).has("name")) continue;
String childName = childJson.getString("name");
if (!StringUtils.equals((String)childName, (String)name)) break;
mergeInstancesMap.put(key, childJson);
boolean repeatable = namedTemplateChildRepeatable;
if (repeatable) continue;
}
ArrayList<JSONObject> mergeInstances = new ArrayList<JSONObject>();
for (Map.Entry entry : mergeInstancesMap.entrySet()) {
mergedParent.remove((String)entry.getKey());
mergeInstances.add((JSONObject)entry.getValue());
}
return mergeInstances;
}
}