XFAJSONTransformer.java
14.8 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.guide.xfa;
import com.adobe.aemds.guide.service.GuideException;
import com.adobe.aemds.guide.utils.CustomJSONWriter;
import com.adobe.aemds.guide.xfa.XFAJSONTransformerUtil;
import com.adobe.aemds.guide.xfa.XFAJSONWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class XFAJSONTransformer {
private Logger logger = LoggerFactory.getLogger(XFAJSONTransformer.class);
private static List<String> panelCandidates = Arrays.asList("area", "subform", "subformSet");
private static List<String> leafCandidates = Arrays.asList("draw", "exclGroup", "field");
private static List<String> unsupportedUiTypes = Arrays.asList("barcode", "exObject", "signature");
private static List<String> unsupportedValueTypes = Arrays.asList("arc", "line", "rectangle");
private JSONObject xfaJson;
private XFAJSONWriter jsonWriter;
private StringWriter stringWriter;
private Hashtable<String, String> xfaObjects;
private XFAJSONTransformerUtil transformUtil;
private Map<String, Integer> currentContainerChildren = new HashMap<String, Integer>();
private ResourceResolver resourceResolver = null;
public XFAJSONTransformer(JSONObject xfaJson) {
this(xfaJson, null, "");
}
public XFAJSONTransformer(JSONObject xfaJson, ResourceResolver resourceResolver, String xfaPath) {
this.xfaJson = xfaJson;
this.stringWriter = new StringWriter();
this.jsonWriter = new XFAJSONWriter(this.stringWriter);
this.transformUtil = new XFAJSONTransformerUtil(resourceResolver, xfaPath);
}
public XFAJSONTransformer(JSONObject xfaJson, boolean saveObjects, ResourceResolver resourceResolver, String xfaPath) {
this(xfaJson, resourceResolver, xfaPath);
this.xfaObjects = new Hashtable();
}
public boolean acceptObject(JSONObject jsonObject) throws Exception {
String childType = jsonObject.getString("_class");
if (!panelCandidates.contains(childType) && !leafCandidates.contains(childType)) {
return false;
}
if (leafCandidates.contains(childType) && !this.acceptLeaf(jsonObject)) {
return false;
}
return true;
}
public String transform() {
try {
JSONObject rootSubformJson = (JSONObject)this.transformUtil.getOrElse(this.xfaJson, "form.subform", null, false);
String rootSubformName = this.transformUtil.getOrGenerateName(rootSubformJson);
this.jsonWriter.startObject(null).startObject(rootSubformName);
this.transformContainer(rootSubformJson);
this.jsonWriter.completeObject(rootSubformName).completeObject(null);
return this.stringWriter.toString();
}
catch (Exception e) {
this.logger.error("Error in transforming xfa json:" + e.getMessage(), (Throwable)e);
throw new GuideException(e);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void transformChildren(JSONObject containerJson, boolean isChildOfTable) throws Exception {
if (containerJson.has("children") && containerJson.get("children") instanceof JSONArray) {
Map<String, Integer> oldChildMap = this.currentContainerChildren;
this.currentContainerChildren = new HashMap<String, Integer>();
try {
JSONObject lastInstanceManager = null;
this.jsonWriter.startObject("items");
JSONArray containerChildren = containerJson.getJSONArray("children");
for (int i = 0; i < containerChildren.length(); ++i) {
JSONObject childJson = containerChildren.getJSONObject(i);
String childType = childJson.getString("_class");
if (!"subform".equals(childType)) {
lastInstanceManager = null;
}
if ("instanceManager".equals(childType)) {
lastInstanceManager = childJson;
continue;
}
if (!this.acceptObject(childJson)) continue;
String childName = this.transformUtil.getOrGenerateName(childJson);
int childIndex = -1;
if (this.currentContainerChildren.containsKey(childName)) {
childIndex = this.currentContainerChildren.get(childName);
}
this.currentContainerChildren.put(childName, ++childIndex);
childName = childName + (childIndex == 0 ? "" : Integer.valueOf(childIndex));
this.jsonWriter.startObject(childName);
if (panelCandidates.contains(childType)) {
if (isChildOfTable && childJson.has("layout") && "row".equals((String)childJson.get("layout"))) {
this.transformTableChild(childJson);
} else {
this.transformContainer(childJson);
}
if (lastInstanceManager != null) {
if (lastInstanceManager.has("min")) {
this.jsonWriter.key("minOccur").value(lastInstanceManager.getInt("min"));
}
if (lastInstanceManager.has("max")) {
this.jsonWriter.key("maxOccur").value(lastInstanceManager.getInt("max"));
}
}
} else if ("exclGroup".equals(childType)) {
this.transformExclGroup(childJson, isChildOfTable);
} else if ("draw".equals(childType)) {
this.transformDraw(childJson, isChildOfTable);
} else {
this.transformField(childJson, isChildOfTable);
}
this.jsonWriter.completeObject(childName);
}
this.jsonWriter.completeObject("items");
}
finally {
this.currentContainerChildren = oldChildMap;
}
}
}
private void transformContainer(JSONObject containerJson, boolean isJsonTable) throws Exception {
if (isJsonTable) {
this.transformTable(containerJson);
} else {
this.transformPanel(containerJson);
}
this.jsonWriter.startObject("layout");
Hashtable<String, Object> hash = this.transformUtil.getLayoutProperties(null, isJsonTable);
this.jsonWriter.writeProperties(hash);
this.jsonWriter.completeObject("layout");
this.transformChildren(containerJson, isJsonTable);
}
public boolean checkIfSupportedTable(JSONObject containerJson) throws Exception {
return this.checkIfSupportedTable(containerJson, 0);
}
private boolean checkIfSupportedTable(JSONObject containerJson, int tableHeaderCount) throws Exception {
if (containerJson.has("children") && containerJson.get("children") instanceof JSONArray) {
JSONArray containerChildren = containerJson.getJSONArray("children");
for (int i = 0; i < containerChildren.length(); ++i) {
boolean isValidTable;
JSONObject childJson = containerChildren.getJSONObject(i);
String childType = childJson.getString("_class");
if (!this.acceptObject(childJson) || !panelCandidates.contains(childType)) continue;
if (childJson.has("layout") && "table".equals((String)childJson.get("layout"))) {
return false;
}
if (tableHeaderCount == 0) {
if (this.checkIfContainerIsTableHeader(childJson)) {
if (isValidTable = this.checkIfSupportedTable(childJson, tableHeaderCount++)) continue;
return false;
}
return false;
}
if (this.checkIfContainerIsTableHeader(childJson)) {
return false;
}
isValidTable = this.checkIfSupportedTable(childJson, tableHeaderCount);
if (isValidTable) continue;
return false;
}
}
return true;
}
private void transformContainer(JSONObject containerJson) throws Exception {
String layout;
boolean bIsJsonTable = false;
if (containerJson.has("layout") && containerJson.get("layout") instanceof String && (layout = (String)containerJson.get("layout")) != null && layout.length() > 0 && "table".equals(layout)) {
bIsJsonTable = this.checkIfSupportedTable(containerJson, 0);
}
this.transformContainer(containerJson, bIsJsonTable);
}
private boolean checkIfContainerIsTableHeader(JSONObject containerJson) throws Exception {
boolean isHeader = false;
JSONArray containerChildren = containerJson.getJSONArray("children");
for (int i = 0; i < containerChildren.length(); ++i) {
JSONObject childJson = containerChildren.getJSONObject(i);
String childType = childJson.getString("_class");
if (!"assist".equals(childType)) continue;
isHeader = "TH".equals(childJson.getString("role"));
break;
}
return isHeader;
}
private void transformTableChild(JSONObject containerJson) throws Exception {
Hashtable<String, Object> hash = this.transformUtil.getCommonProperties(null, containerJson);
boolean bIsHeader = this.checkIfContainerIsTableHeader(containerJson);
if (bIsHeader) {
hash.put("sling:resourceType", "fd/af/components/tableHeader");
} else {
hash.put("sling:resourceType", "fd/af/components/tableRow");
}
hash.put("guideNodeClass", "guideTableRow");
if (hash.containsKey("bindRef")) {
this.transformObjectFromHash(hash);
} else {
this.logger.warn("XFA Element for: " + hash.get("jcr:title") + " does not contain bindRef.");
}
hash.clear();
this.jsonWriter.startObject("layout");
String layoutResourceType = null;
layoutResourceType = bIsHeader ? "fd/af/layouts/table/headerLayout" : "fd/af/layouts/table/rowLayout";
hash.put("jcr:primaryType", "nt:unstructured");
hash.put("sling:resourceType", layoutResourceType);
this.jsonWriter.writeProperties(hash);
this.jsonWriter.completeObject("layout");
this.transformChildren(containerJson, true);
}
private String getJsonStringFromHash(Hashtable<String, Object> hash) {
StringWriter writer = new StringWriter();
XFAJSONWriter jsonWriter = new XFAJSONWriter(writer);
jsonWriter.startObject(null);
jsonWriter.writeProperties(hash);
jsonWriter.completeObject(null);
return writer.toString();
}
public void transformTable(JSONObject tableJson) throws Exception {
Hashtable<String, Object> hash = this.transformUtil.getCommonProperties(null, tableJson);
if ((hash = this.transformUtil.getTableProperties(hash)).containsKey("bindRef")) {
this.transformObjectFromHash(hash);
} else {
this.logger.warn("XFA Element for: " + hash.get("jcr:title") + " does not contain bindRef.");
}
}
private void transformPanel(JSONObject panelJson) throws Exception {
Hashtable<String, Object> hash = this.transformUtil.getCommonProperties(null, panelJson);
if ((hash = this.transformUtil.getPanelProperties(hash)).containsKey("bindRef")) {
this.transformObjectFromHash(hash);
} else {
this.logger.warn("XFA Element for: " + hash.get("jcr:title") + " does not contain bindRef.");
}
}
private void transformObjectFromHash(Hashtable<String, Object> hash) {
String jsonString = this.getJsonStringFromHash(hash);
if (this.xfaObjects != null) {
this.xfaObjects.put((String)hash.get("bindRef"), jsonString);
}
hash.put("xfajson", jsonString);
this.jsonWriter.writeProperties(hash);
}
private void transformField(JSONObject fieldJson, boolean isChildOfTable) throws Exception {
Hashtable<String, Object> input = this.transformUtil.getFieldProperties(null, fieldJson);
if (isChildOfTable && fieldJson.has("colSpan")) {
input.put("colspan", fieldJson.get("colSpan"));
}
this.transformObjectFromHash(input);
}
private void transformDraw(JSONObject drawJson, boolean isChildOfTable) throws Exception {
Hashtable<String, Object> input = this.transformUtil.getDrawProperties(null, drawJson);
if (isChildOfTable && drawJson.has("colSpan")) {
input.put("colspan", drawJson.get("colSpan"));
}
this.transformObjectFromHash(input);
}
private void transformExclGroup(JSONObject exclGroupJson, boolean isChildOfTable) throws Exception {
Hashtable<String, Object> input = this.transformUtil.getExclGroupProperties(null, exclGroupJson);
if (isChildOfTable && exclGroupJson.has("colSpan")) {
input.put("colspan", exclGroupJson.get("colSpan"));
}
this.transformObjectFromHash(input);
}
public final Hashtable<String, String> getXFAObjects() {
return this.xfaObjects;
}
public boolean acceptLeaf(JSONObject leafJson) throws Exception {
String childType = leafJson.getString("_class");
if ("draw".equals(childType) || "field".equals(childType)) {
String valueType;
String fieldUiType = (String)this.transformUtil.getOrElse(leafJson, "ui.oneOfChild._class", null, true);
if (unsupportedUiTypes.contains(fieldUiType)) {
return false;
}
if ("defaultUi".equals(fieldUiType) && unsupportedValueTypes.contains(valueType = (String)this.transformUtil.getOrElse(leafJson, "value.oneOfChild._class", null, true))) {
return false;
}
return true;
}
if ("exclGroup".equals(childType)) {
return true;
}
return false;
}
}