XsdSubmitDataCollector.java
6.5 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang.StringUtils
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
*/
package com.adobe.aemds.guide.utils;
import com.adobe.aemds.guide.service.GuideException;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.adobe.aemds.guide.utils.SubmitDataCollector;
import com.adobe.aemds.guide.utils.XMLUtils;
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 org.apache.commons.lang.StringUtils;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class XsdSubmitDataCollector
extends SubmitDataCollector {
protected final String xsdRootElement;
public XsdSubmitDataCollector(String mergeJsonString, JSONObject templateJson, String guidePrefillXml, String submissionInfo) {
super(mergeJsonString, templateJson, guidePrefillXml, null, submissionInfo, false);
this.xsdRootElement = XMLUtils.extractXsdRootElement(templateJson);
}
public XsdSubmitDataCollector(String mergeJsonString, JSONObject templateJson, String guidePrefillXml, String submissionInfo, boolean isLazyXml) {
super(mergeJsonString, templateJson, guidePrefillXml, null, submissionInfo, isLazyXml);
this.xsdRootElement = XMLUtils.extractXsdRootElement(templateJson);
}
public XsdSubmitDataCollector(String mergeJsonString, JSONObject templateJson, String guidePrefillXml) {
this(mergeJsonString, templateJson, guidePrefillXml, null);
}
protected Node getInitialRepeatNode() throws Exception {
if (this.startsWithAfData) {
return (Node)this.xPath.evaluate("afBoundData/data", this.dataDoc.getDocumentElement(), XPathConstants.NODE);
}
return this.dataDoc.getDocumentElement();
}
protected void updatePrefillXML() throws Exception {
DocumentBuilder db = this.dbf.newDocumentBuilder();
Element afBoundData = null;
if (this.startsWithAfData && this.xPath.evaluate("afBoundData", this.dataDoc.getDocumentElement(), XPathConstants.NODE) == null) {
afBoundData = this.createBoundTag(this.dataDoc.getDocumentElement());
this.dataDoc.getDocumentElement().appendChild(afBoundData);
} else if (!this.isLazyXml) {
this.generateFullXml(db);
}
}
protected void addBoundPartToDataXml() {
try {
Node afBoundData = (Node)this.xPath.evaluate("afBoundData/data", this.dataDoc.getDocumentElement(), XPathConstants.NODE);
Node dataInsideAfBounddata = (Node)this.xPath.evaluate("*", afBoundData, XPathConstants.NODE);
if (dataInsideAfBounddata != null) {
afBoundData.removeChild(dataInsideAfBounddata);
}
}
catch (Exception e) {
this.logger.error("Error in adding bound part to xml" + e.getMessage(), (Throwable)e);
throw new GuideException(e);
}
}
protected Node getInitialRepeatUnboundNode() throws Exception {
if (this.startsWithAfData) {
return (Node)this.xPath.evaluate("afUnboundData/data", this.dataDoc.getDocumentElement(), XPathConstants.NODE);
}
return null;
}
protected String getRelativeRepeatPath(JSONObject templateJsonObject) throws JSONException {
String bindRef = "";
if (templateJsonObject.has("bindRef")) {
bindRef = XMLUtils.getXSDRootBindRef(templateJsonObject, this.xsdRootElement);
if (this.currentRepeatFullPath.length() > 0 && bindRef.startsWith(this.currentRepeatFullPath)) {
bindRef = StringUtils.substringAfter((String)bindRef, (String)this.currentRepeatFullPath);
}
bindRef = GuideUtils.removePrefix(bindRef, "/");
}
return bindRef;
}
protected String getFullRepeatPath(JSONObject templateJson) throws JSONException {
return XMLUtils.getXSDRootBindRef(templateJson, this.xsdRootElement);
}
protected void updateBoundFile(JSONObject jsonObject, JSONObject templateJsonObject) throws JSONException, Exception {
if (jsonObject.has("files")) {
String bindRef = XMLUtils.getXSDRootBindRef(templateJsonObject, this.xsdRootElement);
String relativeBindRef = this.getRelativeRepeatPath(templateJsonObject);
Node bindNode = null;
bindNode = StringUtils.equals((String)bindRef, (String)relativeBindRef) ? this.getInitialRepeatNode() : this.currentRepeatNode;
Node bindedFileAttachmentNode = XMLUtils.createNode(this.dataDoc, this.xPath, bindNode, relativeBindRef);
XMLUtils.createAttachmentTag(this.dataDoc, bindedFileAttachmentNode, jsonObject);
}
}
protected void updateBoundField(JSONObject jsonObject, JSONObject templateJsonObject) throws JSONException, Exception {
if (jsonObject.has("_value")) {
String bindRef = XMLUtils.getXSDRootBindRef(templateJsonObject, this.xsdRootElement);
String relativeBindRef = this.getRelativeRepeatPath(templateJsonObject);
Node bindNode = null;
bindNode = StringUtils.equals((String)bindRef, (String)relativeBindRef) ? this.getInitialRepeatNode() : this.currentRepeatNode;
boolean setDataGroup = false;
String dataNodePath = "";
String attributePathPrefix = "";
if (relativeBindRef.contains("/@")) {
attributePathPrefix = "/@";
dataNodePath = relativeBindRef.split(attributePathPrefix)[0];
setDataGroup = (Node)this.xPath.evaluate(dataNodePath, bindNode, XPathConstants.NODE) == null;
} else if (relativeBindRef.startsWith("@")) {
attributePathPrefix = "@";
setDataGroup = true;
}
if (setDataGroup) {
XMLUtils.addToDocument(this.dataDoc, this.xPath, bindNode, dataNodePath + attributePathPrefix + "xfa:dataNode", "dataGroup");
}
XMLUtils.addToDocument(this.dataDoc, this.xPath, bindNode, relativeBindRef, jsonObject.getString("_value"));
}
}
protected boolean needRepeatHandling(JSONObject jsonObject) {
return true;
}
protected void updateCurrentRepeatUnboundNode() {
}
}