KeyValueDataMerger.java
5.97 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
/*
* 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.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.service.GuideModuleImporter;
import com.adobe.aemds.guide.utils.CustomJSONWriter;
import com.adobe.aemds.guide.utils.GuideUtils;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;
import org.apache.commons.lang.StringUtils;
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 KeyValueDataMerger {
protected Logger logger = LoggerFactory.getLogger(KeyValueDataMerger.class);
protected JSONObject guideJSON;
protected CustomJSONWriter jsonWriter;
protected StringWriter stringWriter;
protected GuideModuleImporter guideModuleImporter;
protected Map<String, Object> moduleParameter;
public KeyValueDataMerger(JSONObject guideJson, Map<String, Object> params) {
this.guideJSON = guideJson;
this.stringWriter = new StringWriter();
this.jsonWriter = new CustomJSONWriter(this.stringWriter);
this.moduleParameter = params;
if (params != null) {
this.guideModuleImporter = (GuideModuleImporter)params.get(GuideModuleImporter.class.getName());
}
}
public JSONObject merge() throws GuideException {
try {
this.jsonWriter.object();
this.mergeJSONObject(this.guideJSON);
this.jsonWriter.endObject();
return new JSONObject(this.stringWriter.toString());
}
catch (XPathExpressionException e) {
if (e.getCause() instanceof TransformerException && "Empty expression!".equals(e.getCause().getMessage())) {
throw new GuideException("It might be that Unbound repeatable panels are present in AF.This is not supported for adaptive forms using the XFA form template or XSD.", e);
}
throw new GuideException("Invalid X Path", e);
}
catch (Exception e) {
throw new GuideException(e);
}
}
public void updateMergedJson(JSONObject jsonObject) throws Exception {
}
protected void moduleMergeJson(JSONObject jsonObject, String guideNodeClass) throws Exception {
if (("guideAdModule".equals(guideNodeClass) || "guideAdModuleGroup".equals(guideNodeClass)) && this.moduleParameter != null && this.guideModuleImporter != null) {
String resolvedHTML = null;
if ("guideAdModule".equals(guideNodeClass)) {
if (jsonObject.has("assetRef")) {
resolvedHTML = this.guideModuleImporter.getModuleResolution(jsonObject.getString("assetRef"), this.moduleParameter);
}
} else if ("guideAdModuleGroup".equals(guideNodeClass)) {
String assetRef = null;
if (jsonObject.has("assetRef")) {
assetRef = jsonObject.getString("assetRef");
}
if (StringUtils.isEmpty((String)assetRef)) {
JSONObject itemsJSONObject = null;
if (jsonObject.has("items")) {
itemsJSONObject = (JSONObject)jsonObject.get("items");
Iterator itemKeys = itemsJSONObject.keys();
ArrayList<String> moduleAssetRefs = new ArrayList<String>();
while (itemKeys.hasNext()) {
JSONObject moduleJSONObject = (JSONObject)itemsJSONObject.get((String)itemKeys.next());
if (!moduleJSONObject.has("assetRef")) continue;
moduleAssetRefs.add(moduleJSONObject.getString("assetRef"));
}
this.moduleParameter.put("moduleList", moduleAssetRefs);
resolvedHTML = this.guideModuleImporter.getModuleGroupResolution(null, this.moduleParameter);
}
} else {
resolvedHTML = this.guideModuleImporter.getModuleGroupResolution(assetRef, this.moduleParameter);
}
}
if (resolvedHTML != null) {
this.jsonWriter.key("_value").value(resolvedHTML);
}
}
}
protected void mergeJSONObject(JSONObject jsonObject) throws Exception {
String name = "";
String guideNodeClass = null;
if (jsonObject.has("guideNodeClass")) {
guideNodeClass = jsonObject.getString("guideNodeClass");
this.jsonWriter.key("guideNodeClass").value(guideNodeClass);
}
if (jsonObject.has("name")) {
name = jsonObject.getString("name");
this.jsonWriter.key("name").value(name);
if (GuideUtils.isGuideFieldModel(guideNodeClass)) {
this.updateMergedJson(jsonObject);
}
}
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
String key = (String)keys.next();
Object value = jsonObject.get(key);
if (!"guideAdModuleGroup".equals(guideNodeClass) && value instanceof JSONObject) {
this.jsonWriter.key(key).object();
this.mergeJSONObject((JSONObject)value);
this.jsonWriter.endObject();
continue;
}
if (!(value instanceof JSONArray)) continue;
this.logger.trace("found an json array in guide node structure. Ignoring this. Key:" + key);
}
this.moduleMergeJson(jsonObject, guideNodeClass);
}
}