CMDocumentDataMerger.java
6.61 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.guide.utils.DocumentDataMerger
* com.adobe.dct.transfer.DataDictionary
* com.adobe.dct.transfer.DataDictionaryInstance
* org.apache.commons.lang.StringUtils
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.fd.adaddon.internal.utils;
import com.adobe.aemds.guide.utils.DocumentDataMerger;
import com.adobe.dct.transfer.DataDictionary;
import com.adobe.dct.transfer.DataDictionaryInstance;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
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.slf4j.LoggerFactory;
import org.w3c.dom.Document;
public class CMDocumentDataMerger
extends DocumentDataMerger {
private static Logger logger = LoggerFactory.getLogger(CMDocumentDataMerger.class);
private DataDictionaryInstance ddi;
private static final String REF_NAME_SEPARATOR = "%";
public CMDocumentDataMerger(JSONObject guideJson, Document mergeReferenceDocument, Map<String, Object> params) {
super(guideJson, mergeReferenceDocument, params);
this.ddi = (DataDictionaryInstance)params.get("moduleDDI");
this.setCurrentContext((Object)null);
this.currentRepeatPath = "";
}
protected void setBindProperty() {
this.bindProperty = "bindRef";
}
protected String getComputedBindProperty(JSONObject jsonObject) throws JSONException {
if (jsonObject.has(this.bindProperty)) {
String bindProp = jsonObject.getString(this.bindProperty);
if (StringUtils.isNotEmpty((String)bindProp)) {
return (String)this.ddi.getDataDictionary().getReferenceNameToPathMap().get(bindProp) + "%" + bindProp;
}
return "";
}
return "";
}
protected boolean isInCurrentContext(String bindPath) {
if (StringUtils.isEmpty((String)bindPath)) {
return false;
}
String absPath = bindPath.split("%")[0];
String currentAbsPath = this.currentRepeatPath.split("%")[0];
return StringUtils.isNotEmpty((String)absPath) && StringUtils.isNotEmpty((String)currentAbsPath) && absPath.startsWith(currentAbsPath);
}
protected String getRelativePath(String bindPath) {
if (StringUtils.isEmpty((String)bindPath)) {
return "";
}
String[] parts = bindPath.split("%");
if (parts.length < 2) {
return "";
}
String absPath = parts[0];
String ref = parts[1];
bindPath = StringUtils.substringAfter((String)absPath, (String)this.currentRepeatPath.split("%")[0]);
if (bindPath.startsWith(".")) {
bindPath = bindPath.substring(1);
}
return bindPath + "%" + ref;
}
private Object resolveValue(String relativePath, boolean isRepeatChild) {
if (StringUtils.isEmpty((String)relativePath)) {
return null;
}
String[] parts = relativePath.split("%");
if (parts.length < 2) {
return null;
}
String path = parts[0];
String ref = parts[1];
Object val = null;
if (isRepeatChild && this.getCurrentContext() != null && StringUtils.isNotEmpty((String)path)) {
StringTokenizer st = new StringTokenizer(path);
val = this.deepSearchContext(st, (Map)this.getCurrentContext());
}
if (val == null) {
val = this.ddi.getValue2(ref);
}
return val;
}
protected String matchPrimitive(String relativePath, boolean isRepeatChild, boolean isRichText) throws Exception {
return this.matchPrimitive(relativePath, isRepeatChild);
}
protected String matchPrimitive(String relativePath, boolean isRepeatChild) throws Exception {
Object val = this.resolveValue(relativePath, isRepeatChild);
if (val != null) {
if (val instanceof Map || val instanceof List) {
logger.debug("matchPrimitive: Matched composite/collection for " + relativePath);
} else {
logger.debug("matchPrimitive: Matched value=" + val + " , " + relativePath);
}
return val + "";
}
return null;
}
private Object deepSearchContext(StringTokenizer pathElements, Map curContext) {
Object matchedValue = null;
while (pathElements.hasMoreElements()) {
String element = pathElements.nextToken();
if (curContext.get(element) == null) continue;
matchedValue = curContext.get(element);
if (matchedValue instanceof Map) {
curContext = matchedValue;
continue;
}
if (matchedValue instanceof List) {
return this.flattenList(pathElements, matchedValue);
}
if (!pathElements.hasMoreElements()) continue;
logger.debug("More elements to search on path but literal value found");
return null;
}
return matchedValue;
}
private List flattenList(StringTokenizer pathElements, List list) {
ArrayList<Object> matchedList = new ArrayList<Object>();
for (Object listItem : list) {
if (listItem instanceof Map) {
matchedList.add(this.deepSearchContext(pathElements, (Map)listItem));
continue;
}
matchedList.add(listItem);
}
return matchedList;
}
protected Object matchComposite(String relativePath, String fullPath) throws Exception {
Object val = this.resolveValue(relativePath, true);
if (val instanceof List) {
return val;
}
if (val instanceof Map) {
ArrayList<Object> list = new ArrayList<Object>();
list.add(val);
return list;
}
logger.debug("matchComposite: Matched primitive " + val + " for " + relativePath);
return null;
}
protected int getMatchCount(Object match) {
if (match != null) {
List nodeList = (List)match;
return nodeList.size();
}
return 0;
}
protected Object getMatchItem(Object match, int ith) {
if (match != null) {
List nodeList = (List)match;
return nodeList.get(ith);
}
return null;
}
protected void resetContext() {
this.setCurrentContext((Object)null);
}
}