BrandingComponentUtils.java
24.2 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.xfa.Attribute
* com.adobe.xfa.Element
* com.adobe.xfa.Node
* com.adobe.xfa.Proto
* com.adobe.xfa.XFA
* com.adobe.xfa.content.Content
* com.adobe.xfa.content.TextValue
* com.adobe.xfa.template.Items
* com.adobe.xfa.template.Value
* com.adobe.xfa.template.containers.ContentArea
* com.adobe.xfa.template.containers.Draw
* com.adobe.xfa.template.containers.ExclGroup
* com.adobe.xfa.template.containers.Field
* com.adobe.xfa.template.ui.UI
* org.apache.commons.lang3.StringUtils
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.aemds.guide.addon.dor;
import com.adobe.aemds.guide.addon.dor.AFDoRMapper;
import com.adobe.aemds.guide.addon.dor.DoRUtils;
import com.adobe.aemds.guide.addon.dor.MetaTemplateConventions;
import com.adobe.xfa.Attribute;
import com.adobe.xfa.Element;
import com.adobe.xfa.Node;
import com.adobe.xfa.Proto;
import com.adobe.xfa.XFA;
import com.adobe.xfa.content.Content;
import com.adobe.xfa.content.TextValue;
import com.adobe.xfa.template.Items;
import com.adobe.xfa.template.Value;
import com.adobe.xfa.template.containers.ContentArea;
import com.adobe.xfa.template.containers.Draw;
import com.adobe.xfa.template.containers.ExclGroup;
import com.adobe.xfa.template.containers.Field;
import com.adobe.xfa.template.ui.UI;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BrandingComponentUtils {
private static final Logger logger = LoggerFactory.getLogger(BrandingComponentUtils.class);
public static JSONObject getBrandingComponentsOfPage(Element masterPage) {
if (masterPage == null) {
return null;
}
JSONObject branding = new JSONObject();
JSONObject brandingComponents = new JSONObject();
JSONObject brandingItems = new JSONObject();
try {
brandingComponents.put("sling:resourceType", (Object)"granite/ui/components/coral/foundation/container");
brandingComponents.put("name", (Object)"branding");
brandingComponents.put("jcr:title", (Object)"Container for branding components");
brandingComponents.put("items", (Object)brandingItems);
branding.put("branding", (Object)brandingComponents);
String namePrefix = "./branding/items";
for (Node child = masterPage.getFirstXFAChild(); child != null; child = child.getNextXFASibling()) {
if (!BrandingComponentUtils.isBrandingComponent(child)) continue;
String name = child.getName();
JSONObject component = BrandingComponentUtils.getBrandingComponent(child, namePrefix);
if (component == null) continue;
BrandingComponentUtils.addSelectionComponentIfRequired(brandingItems, child, component);
brandingItems.put(name, (Object)component);
if (!component.has("resourceType")) continue;
brandingItems.put("resourceType", (Object)BrandingComponentUtils.getResourceTypeComponent(component));
}
}
catch (JSONException e) {
logger.error("Error while extracting branding components from meta-template", (Throwable)e);
}
return brandingItems.length() != 0 ? branding : null;
}
/*
* Enabled force condition propagation
* Lifted jumps to return sites
*/
public static boolean isBrandingComponent(Node node) {
boolean status = false;
if (node == null) {
return false;
}
if (node instanceof Draw || node instanceof Field) {
Element element = (Element)node;
ArrayList pis = new ArrayList();
element.getPI("templateDesigner", "floatingFieldPlaceholder", pis, true);
if (!pis.isEmpty()) {
return false;
}
Value valueNode = (Value)node.resolveNode("#value", true, false, true);
if (valueNode == null) return true;
Node node2 = valueNode.getOneOfChild();
Node content = node2;
if (content == null) {
return true;
}
switch (content.getClassName()) {
case "line":
case "arc":
case "rectangle": {
return false;
}
case "exData": {
String value = DoRUtils.getValue((Content)content);
if (!StringUtils.contains((CharSequence)value, (CharSequence)"xfa:embed")) return status;
return false;
}
default: {
return true;
}
}
}
if (!node.isContainer()) return status;
if (node instanceof ContentArea) return status;
if (node instanceof Proto) return status;
return true;
}
private static JSONObject getBrandingComponent(Node brandingNode, String namePrefix) {
JSONObject properties = new JSONObject();
try {
if (brandingNode instanceof Field) {
int uiElement = ((UI)((Field)brandingNode).getNode(XFA.UITAG, 0)).getOneOfChild().getClassTag();
if (uiElement == XFA.CHECKBUTTONTAG) {
properties = BrandingComponentUtils.getBrandingComponentForCheckBox(brandingNode, namePrefix);
} else if (uiElement == XFA.CHOICELISTTAG) {
properties = BrandingComponentUtils.getBrandingComponentForChoiceList(brandingNode, namePrefix);
} else if (uiElement == XFA.IMAGEEDITTAG) {
properties = BrandingComponentUtils.getBrandingComponentForImage(brandingNode, namePrefix);
} else {
properties.put("name", (Object)(namePrefix + "/" + brandingNode.getName() + "/value"));
String fieldLabel = BrandingComponentUtils.getFieldLabel(brandingNode);
properties.put("fieldLabel", (Object)fieldLabel);
String xfaElement = XFA.getString((int)uiElement);
String slingResourceType = AFDoRMapper.getSlingResourceType(xfaElement);
properties.put("sling:resourceType", (Object)slingResourceType);
}
if (uiElement != XFA.IMAGEEDITTAG) {
Value value = (Value)brandingNode.resolveNode("#value", true, false, true);
properties.put("value", (Object)BrandingComponentUtils.getValue(value));
}
} else if (brandingNode instanceof Draw) {
int uiElement = ((UI)((Draw)brandingNode).getNode(XFA.UITAG, 0)).getOneOfChild().getClassTag();
if (uiElement == XFA.TEXTEDITTAG) {
properties = BrandingComponentUtils.getBrandingComponentForStaticText(brandingNode, namePrefix);
} else if (uiElement == XFA.IMAGEEDITTAG) {
properties = BrandingComponentUtils.getBrandingComponentForImage(brandingNode, namePrefix);
}
} else if (brandingNode instanceof ExclGroup) {
properties = BrandingComponentUtils.getBrandingComponentForExclGroup(brandingNode, namePrefix);
} else if (brandingNode.isContainer() && BrandingComponentUtils.isBrandingComponent(brandingNode)) {
properties = BrandingComponentUtils.getBrandingComponentForContainer(brandingNode, namePrefix);
}
}
catch (JSONException e) {
logger.error("Error while extracting branding components from meta-template", (Throwable)e);
}
return properties;
}
private static JSONObject getBrandingComponentForContainer(Node brandingNode, String namePrefix) throws JSONException {
JSONObject properties = new JSONObject();
JSONObject items = new JSONObject();
String containerName = brandingNode.getName();
properties.put("name", (Object)containerName);
properties.put("sling:resourceType", (Object)"granite/ui/components/coral/foundation/container");
properties.put("items", (Object)items);
for (Node child = brandingNode.getFirstXFAChild(); child != null; child = child.getNextXFASibling()) {
if (!BrandingComponentUtils.isBrandingComponent(child)) continue;
String name = child.getName();
JSONObject component = BrandingComponentUtils.getBrandingComponent(child, namePrefix + "/" + containerName + "/items");
if (component == null) continue;
BrandingComponentUtils.addSelectionComponentIfRequired(items, child, component);
items.put(name, (Object)component);
if (!component.has("resourceType")) continue;
items.put(name + "_" + "resourceType", (Object)BrandingComponentUtils.getResourceTypeComponent(component));
}
return items.length() != 0 ? properties : null;
}
private static JSONObject getBrandingComponentForExclGroup(Node brandingNode, String namePrefix) throws JSONException {
String fieldLabel = BrandingComponentUtils.getFieldLabel(brandingNode);
JSONObject properties = new JSONObject();
JSONObject items = new JSONObject();
properties.put("name", (Object)(namePrefix + "/" + brandingNode.getName() + "/value"));
properties.put("fieldLabel", (Object)fieldLabel);
properties.put("sling:resourceType", (Object)"granite/ui/components/coral/foundation/form/radiogroup");
properties.put("vertical", true);
for (Node child = brandingNode.getFirstXFAChild(); child != null; child = child.getNextXFASibling()) {
String itemValue;
String _value;
if (!BrandingComponentUtils.isBrandingComponent(child)) continue;
JSONObject radioButton = new JSONObject();
radioButton.put("sling:resourceType", (Object)"granite/ui/components/coral/foundation/form/radio");
Value caption = (Value)child.resolveNode("#caption.#value", true, false, true);
Value value = (Value)child.resolveNode("#value", true, false, true);
Items radioButtonItem = (Items)child.resolveNode("#items", true, false, true);
if (caption != null) {
radioButton.put("text", (Object)BrandingComponentUtils.getValue(caption));
}
if (radioButtonItem != null && StringUtils.isNotBlank((CharSequence)(itemValue = DoRUtils.getValue((Content)radioButtonItem.getFirstXFAChild())))) {
radioButton.put("value", (Object)itemValue);
}
if (value != null && StringUtils.isNotBlank((CharSequence)(_value = BrandingComponentUtils.getValue(value)))) {
radioButton.put("checked", true);
}
items.put(child.getName(), (Object)radioButton);
}
properties.put("items", (Object)items);
return properties;
}
private static JSONObject getBrandingComponentForImage(Node brandingNode, String namePrefix) throws JSONException {
Value valueNode = (Value)brandingNode.resolveNode("#value", true, false, true);
Node value = null;
if (valueNode != null) {
value = valueNode.getOneOfChild(true, false);
}
String fieldLabel = BrandingComponentUtils.getFieldLabel(brandingNode);
String width = null;
String height = null;
if (((Element)brandingNode).isPropertySpecified(XFA.WTAG, true, 0)) {
width = ((Element)brandingNode).getAttribute(XFA.WTAG).getAttrValue();
} else if (((Element)brandingNode).isPropertySpecified(XFA.MAXWTAG, true, 0)) {
width = ((Element)brandingNode).getAttribute(XFA.MAXWTAG).getAttrValue();
}
if (((Element)brandingNode).isPropertySpecified(XFA.HTAG, true, 0)) {
height = ((Element)brandingNode).getAttribute(XFA.HTAG).getAttrValue();
} else if (((Element)brandingNode).isPropertySpecified(XFA.MAXHTAG, true, 0)) {
height = ((Element)brandingNode).getAttribute(XFA.MAXHTAG).getAttrValue();
}
if (width != null || height != null) {
fieldLabel = fieldLabel + " (";
fieldLabel = fieldLabel + (width != null ? new StringBuilder().append("width: ").append(width).toString() : "");
fieldLabel = fieldLabel + (width != null && height != null ? ", " : "");
fieldLabel = fieldLabel + (height != null ? new StringBuilder().append("height: ").append(height).toString() : "");
fieldLabel = fieldLabel + ") ";
}
JSONObject properties = new JSONObject();
properties.put("namehint", (Object)(namePrefix + "/" + brandingNode.getName() + "/value"));
properties.put("assetButtonText", (Object)"Browse Assets");
properties.put("async", true);
properties.put("autoStart", false);
properties.put("class", (Object)"cq-droptarget");
properties.put("fieldLabel", (Object)fieldLabel);
properties.put("fileNameParameter", (Object)"./fileName");
properties.put("fileReferenceParameter", (Object)"fileReference");
properties.put("granite:id", (Object)"imageUploadId");
properties.put("mimeTypes", (Object)"image");
properties.put("multiple", false);
properties.put("name", (Object)"./file");
properties.put("predicate", (Object)"nosystem");
properties.put("rootPath", (Object)"/content/dam");
properties.put("sizeLimit", 100000000);
properties.put("sling:resourceType", (Object)"fd/af/authoring/components/granite/form/imageupload");
properties.put("text", (Object)"Upload");
properties.put("title", (Object)"Upload Image");
properties.put("uploadUrl", (Object)"${requestPathInfo.resourcePath}");
properties.put("useHTML5", true);
if (value == null) {
return properties;
}
if (value.isPropertySpecified(XFA.HREFTAG, true, 0)) {
String href = BrandingComponentUtils.getValue(valueNode);
href = href.replaceAll("\\\\", "/");
properties.put("fileReference", (Object)href);
properties.put("fileName", (Object)StringUtils.substringAfterLast((String)href, (String)"/"));
} else {
String fileName;
String mimeType = ((Attribute)value.getProperty(XFA.CONTENTTYPETAG, 0)).getAttrValue();
String jcrPrimaryType = "nt:resource";
String jcrData = BrandingComponentUtils.getValue(valueNode);
JSONObject jcrContent = new JSONObject();
jcrContent.put("jcr:mimeType", (Object)mimeType);
jcrContent.put("jcr:primaryType", (Object)jcrPrimaryType);
jcrContent.put("jcr:data", (Object)jcrData);
JSONObject file = new JSONObject();
file.put("jcr:primaryType", (Object)"nt:file");
file.put("jcr:content", (Object)jcrContent);
properties.put("file", (Object)file);
TextValue descText = (TextValue)brandingNode.resolveNode("desc.embeddedHref", true, false, true);
if (descText != null && (fileName = descText.getValue()) != null) {
fileName = fileName.replaceAll("\\\\", "/");
fileName = StringUtils.substringAfterLast((String)fileName, (String)"/");
properties.put("fileName", (Object)fileName);
}
}
return properties;
}
private static JSONObject getBrandingComponentForStaticText(Node brandingNode, String namePrefix) throws JSONException {
Value valueNode = (Value)brandingNode.resolveNode("#value", true, false, true);
String fieldLabel = BrandingComponentUtils.getFieldLabel(brandingNode);
JSONObject properties = new JSONObject();
properties.put("name", (Object)(namePrefix + "/" + brandingNode.getName() + "/value"));
properties.put("fieldLabel", (Object)fieldLabel);
properties.put("sling:resourceType", (Object)"granite/ui/components/coral/foundation/form/textarea");
properties.put("value", (Object)BrandingComponentUtils.getValue(valueNode));
properties.put("resourceType", (Object)"granite/ui/components/coral/foundation/form/textarea");
return properties;
}
private static JSONObject getBrandingComponentForCheckBox(Node brandingNode, String namePrefix) throws JSONException {
String fieldLabel = BrandingComponentUtils.getFieldLabel(brandingNode);
String slingResourceType = AFDoRMapper.getSlingResourceType(XFA.getString((int)XFA.CHECKBUTTONTAG));
JSONObject properties = new JSONObject();
String onValue = ((Field)brandingNode).getOnValue();
String offValue = ((Field)brandingNode).getOffValue();
if (StringUtils.equals((CharSequence)onValue, (CharSequence)((Field)brandingNode).getRawValue())) {
properties.put("checked", true);
}
properties.put("uncheckedValue", (Object)offValue);
properties.put("text", (Object)fieldLabel);
properties.put("sling:resourceSuperType", (Object)slingResourceType);
properties.put("name", (Object)(namePrefix + "/" + brandingNode.getName() + "/value"));
return properties;
}
private static JSONObject getBrandingComponentForChoiceList(Node brandingNode, String namePrefix) throws JSONException {
String fieldLabel = BrandingComponentUtils.getFieldLabel(brandingNode);
String slingResourceType = AFDoRMapper.getSlingResourceType(XFA.getString((int)XFA.CHOICELISTTAG));
JSONObject slingItems = DoRUtils.getItems((Field)brandingNode);
JSONObject properties = new JSONObject();
properties.put("items", (Object)slingItems);
properties.put("fieldLabel", (Object)fieldLabel);
properties.put("sling:resourceSuperType", (Object)slingResourceType);
properties.put("name", (Object)(namePrefix + "/" + brandingNode.getName() + "/value"));
return properties;
}
private static JSONObject getResourceTypeComponent(JSONObject component) {
JSONObject resourceType = new JSONObject();
try {
String name = StringUtils.substringBeforeLast((String)component.getString("name"), (String)"/value");
resourceType.put("name", (Object)(name + "/" + "resourceType"));
resourceType.put("sling:resourceType", (Object)"granite/ui/components/coral/foundation/form/hidden");
resourceType.put("value", (Object)component.getString("sling:resourceType"));
}
catch (JSONException e) {
logger.error("Error while creating resource type for branding components from meta-template", (Throwable)e);
}
return resourceType;
}
private static JSONObject getSelectionComponent(JSONObject component, String mapping, String type) throws JSONException {
JSONObject datasource = new JSONObject();
datasource.put("sling:resourceType", (Object)"fd/af/dor/datasource/branding/mapping");
datasource.put("value", (Object)mapping);
datasource.put("type", (Object)type);
String name = "";
String selectionComponentName = "";
switch (type) {
case "StaticText": {
name = component.getString("name");
break;
}
case "Image": {
name = component.getString("namehint");
}
}
selectionComponentName = name + "From";
JSONObject graniteData = new JSONObject();
graniteData.put("af.listeners.onload", (Object)("guidelib.touchlib.editLayer.dialogUtils.showOrHideBrandingComponent('" + selectionComponentName + "');"));
graniteData.put("af.listeners.change", (Object)("guidelib.touchlib.editLayer.dialogUtils.showOrHideBrandingComponent('" + selectionComponentName + "');"));
graniteData.put("branding-component", (Object)name);
graniteData.put("type", (Object)type);
JSONObject selectionComponent = new JSONObject();
selectionComponent.put("name", (Object)selectionComponentName);
selectionComponent.put("fieldLabel", (Object)component.getString("fieldLabel"));
selectionComponent.put("deleteHint", true);
selectionComponent.put("value", (Object)mapping);
selectionComponent.put("emptyText", (Object)"Enter Custom");
selectionComponent.put("sling:resourceType", (Object)"granite/ui/components/coral/foundation/form/select");
selectionComponent.put("granite:data", (Object)graniteData);
selectionComponent.put("datasource", (Object)datasource);
return selectionComponent;
}
private static void addSelectionComponentIfRequired(JSONObject items, Node brandingNode, JSONObject brandingComponent) {
try {
if (brandingNode instanceof Draw) {
String ui;
boolean addSelectionComponent = false;
String mapping = "";
String type = "";
String name = brandingNode.getName();
switch (ui = ((UI)((Draw)brandingNode).getNode(XFA.UITAG, 0)).getOneOfChild().getClassName()) {
case "textEdit": {
type = "StaticText";
addSelectionComponent = true;
break;
}
case "imageEdit": {
type = "Image";
addSelectionComponent = true;
}
}
for (int i = 0; i < MetaTemplateConventions.BRANDING_COM_CONVENTION_NAMES.length; ++i) {
String conventionName = MetaTemplateConventions.BRANDING_COM_CONVENTION_NAMES[i];
if (!StringUtils.equals((CharSequence)name, (CharSequence)conventionName) || !StringUtils.equals((CharSequence)ui, (CharSequence)MetaTemplateConventions.BRANDING_COM_CONVENTION_TYPES[i])) continue;
mapping = MetaTemplateConventions.BRANDING_COM_CONVENTION_VALUE_FROM[i];
break;
}
if (addSelectionComponent) {
JSONObject selectionComponent = BrandingComponentUtils.getSelectionComponent(brandingComponent, mapping, type);
items.put(brandingNode.getName() + UUID.randomUUID(), (Object)selectionComponent);
JSONObject hiddenInput = new JSONObject();
hiddenInput.put("sling:resourceType", (Object)"/libs/granite/ui/components/coral/foundation/form/hidden");
hiddenInput.put("name", (Object)(selectionComponent.getString("name") + "@IgnoreBlanks"));
hiddenInput.put("value", true);
items.put(brandingNode.getName() + UUID.randomUUID(), (Object)hiddenInput);
brandingComponent.remove("fieldLabel");
}
}
}
catch (JSONException e) {
logger.error("Error while adding selection component from meta template", (Throwable)e);
}
}
private static String getFieldLabel(Node brandingNode) {
String fieldLabel = "";
Value caption = (Value)brandingNode.resolveNode("#caption.#value", true, false, true);
fieldLabel = BrandingComponentUtils.getValue(caption);
if (StringUtils.isEmpty((CharSequence)fieldLabel)) {
Content content = (Content)brandingNode.resolveNode("#desc.fieldLabel", true, false, true);
fieldLabel = DoRUtils.getValue(content);
}
if (StringUtils.isBlank((CharSequence)fieldLabel)) {
String name = brandingNode.getName();
for (int i = 0; i < MetaTemplateConventions.BRANDING_COM_CONVENTION_NAMES.length; ++i) {
if (!StringUtils.equals((CharSequence)name, (CharSequence)MetaTemplateConventions.BRANDING_COM_CONVENTION_NAMES[i])) continue;
name = MetaTemplateConventions.BRANDING_COM_CONVENTION_FIELD_LABELS[i];
break;
}
fieldLabel = name;
}
return fieldLabel;
}
private static String getValue(Value valueNode) {
if (valueNode == null) {
return null;
}
Content value = (Content)valueNode.getOneOfChild(true, false);
return DoRUtils.getValue(value);
}
}