DoRUtils.java
16.9 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.guide.utils.GuideUtils
* com.adobe.xfa.AppModel
* com.adobe.xfa.Attribute
* com.adobe.xfa.Element
* com.adobe.xfa.Model
* com.adobe.xfa.Node
* com.adobe.xfa.StringAttr
* com.adobe.xfa.XFA
* com.adobe.xfa.content.BooleanValue
* com.adobe.xfa.content.Content
* com.adobe.xfa.content.DateTimeValue
* com.adobe.xfa.content.DateValue
* com.adobe.xfa.content.DecimalValue
* com.adobe.xfa.content.ExDataValue
* com.adobe.xfa.content.FloatValue
* com.adobe.xfa.content.ImageValue
* com.adobe.xfa.content.IntegerValue
* com.adobe.xfa.content.TextValue
* com.adobe.xfa.content.TimeValue
* com.adobe.xfa.template.Items
* com.adobe.xfa.template.TemplateModel
* com.adobe.xfa.template.Value
* com.adobe.xfa.template.containers.AreaContainer
* com.adobe.xfa.template.containers.Draw
* com.adobe.xfa.template.containers.ExclGroup
* com.adobe.xfa.template.containers.Field
* com.adobe.xfa.template.containers.Field$ItemPair
* com.adobe.xfa.template.containers.Subform
* com.adobe.xfa.template.containers.SubformSet
* com.day.cq.i18n.I18n
* com.day.cq.wcm.foundation.Image
* javax.jcr.Binary
* javax.jcr.Property
* javax.jcr.RepositoryException
* org.apache.commons.codec.binary.Base64
* org.apache.commons.io.IOUtils
* org.apache.commons.lang3.StringUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* 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.DoRThreadLocal;
import com.adobe.aemds.guide.addon.dor.GuideType;
import com.adobe.aemds.guide.addon.dor.MetaTemplate;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.adobe.xfa.AppModel;
import com.adobe.xfa.Attribute;
import com.adobe.xfa.Element;
import com.adobe.xfa.Model;
import com.adobe.xfa.Node;
import com.adobe.xfa.StringAttr;
import com.adobe.xfa.XFA;
import com.adobe.xfa.content.BooleanValue;
import com.adobe.xfa.content.Content;
import com.adobe.xfa.content.DateTimeValue;
import com.adobe.xfa.content.DateValue;
import com.adobe.xfa.content.DecimalValue;
import com.adobe.xfa.content.ExDataValue;
import com.adobe.xfa.content.FloatValue;
import com.adobe.xfa.content.ImageValue;
import com.adobe.xfa.content.IntegerValue;
import com.adobe.xfa.content.TextValue;
import com.adobe.xfa.content.TimeValue;
import com.adobe.xfa.template.Items;
import com.adobe.xfa.template.TemplateModel;
import com.adobe.xfa.template.Value;
import com.adobe.xfa.template.containers.AreaContainer;
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.containers.Subform;
import com.adobe.xfa.template.containers.SubformSet;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.foundation.Image;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.Binary;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DoRUtils {
private static final Logger logger = LoggerFactory.getLogger(DoRUtils.class);
public static MetaTemplate getMetaTemplate(String metaTemplateReference) throws IOException {
Resource resource;
ResourceResolver resourceResolver;
byte[] metaTemplateBytes = null;
if (StringUtils.isNotEmpty((CharSequence)metaTemplateReference) && (resource = (resourceResolver = DoRThreadLocal.getResourceResolver()).getResource(metaTemplateReference + "/jcr:content/renditions/original/jcr:content")) != null) {
InputStream inputStream = (InputStream)resource.adaptTo(InputStream.class);
metaTemplateBytes = IOUtils.toByteArray((InputStream)inputStream);
inputStream.close();
}
MetaTemplate metaTemplate = new MetaTemplate(metaTemplateBytes, metaTemplateReference);
return metaTemplate;
}
public static String convertToPlainText(String htmlString) {
String result = "";
if (StringUtils.isNotEmpty((CharSequence)htmlString)) {
result = htmlString.replaceAll("<[^>]+>", "").replaceAll(" ", " ");
}
return result;
}
public static String convertToXfaCompliantHTML(String htmlString) {
String result = "";
if (StringUtils.isNotEmpty((CharSequence)htmlString)) {
result = htmlString.replaceAll("<br>", "<br> </br>");
result = result.replaceAll(" ", " ");
}
return result;
}
public static Properties resolveImage(ResourceResolver resourceResolver, String jcrPath) throws RepositoryException, IOException {
Properties properties = new Properties();
if (StringUtils.isNotEmpty((CharSequence)jcrPath) && resourceResolver != null) {
Resource resource = resourceResolver.resolve(jcrPath);
Image image = new Image(resource);
Property property = image.getData();
if (property == null) {
return properties;
}
InputStream inputStream = property.getBinary().getStream();
byte[] imageBytes = IOUtils.toByteArray((InputStream)inputStream);
inputStream.close();
String imageData = Base64.encodeBase64String((byte[])imageBytes);
properties.put("imageData", imageData);
String mimeType = image.getMimeType();
properties.setProperty("jcr:mimeType", mimeType);
properties.setProperty("imageHref", "");
}
return properties;
}
public static String getValue(Content value) {
String string = "";
if (value instanceof BooleanValue) {
string = ((BooleanValue)value).getStrValue();
} else if (value instanceof DateValue) {
string = ((DateValue)value).getValue();
} else if (value instanceof DateTimeValue) {
string = ((DateTimeValue)value).getValue();
} else if (value instanceof DecimalValue) {
string = ((DecimalValue)value).getFormattedValue();
} else if (value instanceof ExDataValue) {
string = ((ExDataValue)value).getValue(true, true, true);
} else if (value instanceof FloatValue) {
string = ((FloatValue)value).getStrValue();
} else if (value instanceof ImageValue) {
string = ((ImageValue)value).getValue();
} else if (value instanceof IntegerValue) {
string = ((IntegerValue)value).getStrValue();
} else if (value instanceof TextValue) {
string = ((TextValue)value).getValue();
} else if (value instanceof TimeValue) {
string = ((TimeValue)value).getValue();
}
return string;
}
public static void setValue(Content content, String text, String contentType) throws NumberFormatException {
if (StringUtils.equals((CharSequence)"text/html", (CharSequence)contentType)) {
Value value = (Value)content.getXFAParent();
DoRUtils.enableRichText(value);
content = (Content)value.getOneOfChild();
}
if (content instanceof BooleanValue) {
((BooleanValue)content).setValue(Boolean.parseBoolean(text));
} else if (content instanceof DateValue) {
((DateValue)content).setValue(text);
} else if (content instanceof DateTimeValue) {
((DateTimeValue)content).setValue(text);
} else if (content instanceof DecimalValue) {
((DecimalValue)content).setValue(Double.parseDouble(text), true, false, false);
} else if (content instanceof ExDataValue) {
if ("text/html".equals(contentType)) {
try {
ExDataValue exDataValue = (ExDataValue)content;
TemplateModel templateModel = TemplateModel.getTemplateModel((AppModel)content.getAppModel(), (boolean)true);
Element body = templateModel.createElement("body");
body.setModel(exDataValue.getModel());
body.setNS("http://www.w3.org/1999/xhtml");
body.setAttribute("", "xmlns:xfa", "xfa", "http://www.xfa.org/schema/xfa-data/1.0/");
String richText = "<body xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xmlns=\"http://www.w3.org/1999/xhtml\">" + text + "</" + "body" + ">";
ByteArrayInputStream is = new ByteArrayInputStream(richText.getBytes("UTF-8"));
body.loadXML((InputStream)is, true, true);
exDataValue.setContent((Node)body, false);
is.close();
}
catch (Exception e) {
logger.error("Error in setting text/html content", (Throwable)e);
}
} else {
((ExDataValue)content).setValue(text);
}
} else if (content instanceof FloatValue) {
((FloatValue)content).setValue((double)Float.parseFloat(text), false, true);
} else if (content instanceof ImageValue) {
((ImageValue)content).setValue(text, contentType);
} else if (content instanceof IntegerValue) {
((IntegerValue)content).setValue(Integer.parseInt(text));
} else if (content instanceof TextValue) {
((TextValue)content).setValue(text);
} else if (content instanceof TimeValue) {
((TimeValue)content).setValue(text);
}
}
public static JSONObject getItems(Field field) throws JSONException {
JSONObject slingItems = new JSONObject();
Field.ItemPair items = new Field.ItemPair();
field.getItemLists(true, items, false);
Items displayItems = items.mDisplayItems;
Items saveItems = items.mSaveItems;
Node displayOption = displayItems.getFirstXFAChild();
Node saveOption = saveItems.getFirstXFAChild();
while (displayOption != null && saveItems != null) {
JSONObject slingItem = new JSONObject();
String saveValue = "";
String displayValue = "";
if (displayOption instanceof TextValue) {
saveValue = ((TextValue)saveOption).getStrValue();
displayValue = ((TextValue)displayOption).getStrValue();
} else if (displayOption instanceof IntegerValue) {
saveValue = ((IntegerValue)saveOption).getStrValue();
displayValue = ((IntegerValue)displayOption).getStrValue();
}
slingItem.put("text", (Object)displayValue);
slingItem.put("value", (Object)saveValue);
slingItems.put(saveValue, (Object)slingItem);
displayOption = displayOption.getNextXFASibling();
saveOption = saveOption.getNextXFASibling();
}
return slingItems;
}
public static void translate(Node element, I18n i18n) {
for (Node child = element.getFirstXFAChild(); child != null; child = child.getNextXMLSibling()) {
if (child instanceof Field) {
String translatedText;
Content captionTextNode = (Content)((Value)child.resolveNode("caption.value")).getOneOfChild(false, true);
String captionText = DoRUtils.getValue(captionTextNode);
if (!StringUtils.equals((CharSequence)captionText, (CharSequence)(translatedText = GuideUtils.translateOrReturnOriginal((String)captionText, (I18n)i18n)))) {
DoRUtils.setValue(captionTextNode, translatedText, null);
}
Field.ItemPair items = new Field.ItemPair();
((Field)child).getItemLists(true, items, true);
Items displayItems = items.mDisplayItems;
if (displayItems == null) continue;
for (Node displayOption = displayItems.getFirstXFAChild(); displayOption != null; displayOption = displayOption.getNextXFASibling()) {
Content displayTextNode = (Content)displayOption;
String displayText = DoRUtils.getValue(displayTextNode);
if (StringUtils.equals((CharSequence)displayText, (CharSequence)(translatedText = GuideUtils.translateOrReturnOriginal((String)displayText, (I18n)i18n)))) continue;
DoRUtils.setValue(displayTextNode, translatedText, null);
}
continue;
}
if (child instanceof Draw) {
String translatedText;
String staticText;
Content staticTextNode;
Value value = (Value)child.resolveNode("value");
Node valueNode = value.getOneOfChild(false, true);
if (!(valueNode instanceof TextValue) && !(valueNode instanceof ExDataValue) || StringUtils.equals((CharSequence)(staticText = DoRUtils.getValue(staticTextNode = (Content)valueNode)), (CharSequence)(translatedText = GuideUtils.translateOrReturnOriginal((String)staticText, (I18n)i18n)))) continue;
DoRUtils.setValue(staticTextNode, translatedText, null);
continue;
}
if (child instanceof ExclGroup) {
DoRUtils.translate(child, i18n);
continue;
}
if (child instanceof Subform) {
DoRUtils.translate(child, i18n);
continue;
}
if (child instanceof SubformSet) {
DoRUtils.translate(child, i18n);
continue;
}
if (!(child instanceof AreaContainer)) continue;
DoRUtils.translate(child, i18n);
}
}
public static void enableRichText(Value value) {
if (value == null) {
return;
}
Node child = value.getOneOfChild(false, true);
if (child instanceof TextValue) {
child.remove();
ExDataValue exDataValue = new ExDataValue((Element)value, null);
exDataValue.setAttribute((Attribute)new StringAttr("contentType", "text/html"), XFA.CONTENTTYPETAG);
value.appendChild((Node)exDataValue);
}
}
public static void enablePlainText(Value value) {
if (value == null) {
return;
}
Node child = value.getOneOfChild(false, true);
if (child instanceof ExDataValue) {
child.remove();
TextValue textValue = new TextValue((Element)value, null);
value.appendChild((Node)textValue);
}
}
public static String convertColorToRGB(String color) {
String rgbColor = "";
if (StringUtils.startsWith((CharSequence)color, (CharSequence)"#")) {
rgbColor = "" + Integer.parseInt(color.substring(1, 3), 16) + "," + Integer.parseInt(color.substring(3, 5), 16) + "," + Integer.parseInt(color.substring(5, 7), 16);
} else if (StringUtils.startsWith((CharSequence)color, (CharSequence)"rgba")) {
rgbColor = StringUtils.substringBeforeLast((String)color.substring(5), (String)",");
} else if (StringUtils.startsWith((CharSequence)color, (CharSequence)"rgb")) {
rgbColor = color.substring(4, color.length() - 1);
}
return rgbColor;
}
public static String convertColorToHEX(String color) {
String[] hexComponents;
String hexColor = "#";
for (String hexComponent : hexComponents = StringUtils.split((String)color, (String)", ")) {
String hexValue = Integer.toHexString(Integer.parseInt(hexComponent));
if (StringUtils.length((CharSequence)hexValue) == 1) {
hexValue = "0" + hexValue;
}
hexColor = hexColor + hexValue;
}
return hexColor;
}
public static Boolean isSchemaBasedForm(GuideType guideType) {
return GuideType.XSD_BASED.equals((Object)guideType) || GuideType.JSON_BASED.equals((Object)guideType);
}
public static String getValueMatch(String pattern, String input) {
Pattern widthpattern = Pattern.compile(pattern);
Matcher matchWidthValue = widthpattern.matcher(input);
if (matchWidthValue.find()) {
return matchWidthValue.group();
}
return null;
}
public static Element getSpaceFillerElement(Element parent) {
Draw space = new Draw(parent, null);
space.setAttribute("name", "name", "name", "dorSpacer");
space.setAttribute("usehref", "usehref", "usehref", ".#som($template.#subform.spaceFiller)");
return space;
}
}