MetaTemplateParser.java
9.67 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.xfa.AppModel
* com.adobe.xfa.Attribute
* com.adobe.xfa.Document
* com.adobe.xfa.Element
* com.adobe.xfa.Node
* com.adobe.xfa.XFA
* com.adobe.xfa.content.ImageValue
* com.adobe.xfa.content.TextValue
* com.adobe.xfa.protocol.ProtocolUtils
* com.adobe.xfa.template.TemplateModel
* com.adobe.xfa.template.containers.Subform
* org.apache.commons.io.IOUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* 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.BrandingComponentUtils;
import com.adobe.aemds.guide.addon.dor.ConfigPropertiesUtils;
import com.adobe.aemds.guide.addon.dor.DoRThreadLocal;
import com.adobe.aemds.guide.addon.dor.MetaTemplateConventions;
import com.adobe.xfa.AppModel;
import com.adobe.xfa.Attribute;
import com.adobe.xfa.Document;
import com.adobe.xfa.Element;
import com.adobe.xfa.Node;
import com.adobe.xfa.XFA;
import com.adobe.xfa.content.ImageValue;
import com.adobe.xfa.content.TextValue;
import com.adobe.xfa.protocol.ProtocolUtils;
import com.adobe.xfa.template.TemplateModel;
import com.adobe.xfa.template.containers.Subform;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MetaTemplateParser {
private static final Logger log = LoggerFactory.getLogger(MetaTemplateParser.class);
private Document document;
private String metaTemplateRef;
public MetaTemplateParser(Document document, String metaTemplateRef) {
this.document = document;
this.metaTemplateRef = metaTemplateRef;
}
public MetaTemplateParser(Document document) {
this(document, null);
}
public Element getMasterPage() {
Element pageArea;
if (this.document == null) {
return null;
}
TemplateModel templateModel = TemplateModel.getTemplateModel((AppModel)this.document.getAppModel(), (boolean)true);
if (this.metaTemplateRef != null) {
this.resolveImageReferences(templateModel, this.metaTemplateRef);
}
if ((pageArea = (Element)templateModel.resolveNode("AF_METATEMPLATE.#pageSet[0].AF_MASTERPAGE")) == null) {
pageArea = (Element)templateModel.resolveNode("#subform[0].#pageSet[0].#pageArea[0]");
}
return pageArea;
}
public JSONObject getBrandingComponents() {
return BrandingComponentUtils.getBrandingComponentsOfPage(this.getMasterPage());
}
private void resolveImageReferences(TemplateModel templateModel, String metaTemplateRef) {
this.processNode((Node)templateModel, metaTemplateRef);
}
private void processNode(Node node, String metaTemplateRef) {
int index;
String base = null;
String absolutePath = null;
if (metaTemplateRef != null && !metaTemplateRef.isEmpty() && (index = metaTemplateRef.lastIndexOf(47)) != -1) {
base = metaTemplateRef.substring(0, index + 1);
}
try {
ImageValue child;
if (node instanceof ImageValue && (child = (ImageValue)node).isPropertySpecified(XFA.HREFTAG, true, 0) && !child.isPropertySpecified(XFA.TRANSFERENCODINGTAG, true, 0)) {
String path = child.getAttribute(XFA.HREFTAG).getAttrValue();
boolean isCRXPath = true;
if (path.startsWith("/content")) {
absolutePath = path;
} else if (path.startsWith(".") && base != null) {
absolutePath = base + path.replace("\\", "/");
} else {
try {
URI uri = new URI(path);
if (uri.getScheme() == null && base != null) {
absolutePath = base + path.replace("\\", "/");
} else {
isCRXPath = false;
}
}
catch (URISyntaxException e) {
log.error("Error during resolving image reference", (Throwable)e);
return;
}
}
InputStream inputStream = null;
if (isCRXPath) {
if (absolutePath != null) {
ResourceResolver resourceResolver = DoRThreadLocal.getResourceResolver();
Resource resource = resourceResolver.getResource(absolutePath + "/jcr:content/renditions/original/jcr:content");
if (resource != null) {
inputStream = (InputStream)resource.adaptTo(InputStream.class);
}
} else {
log.error("The path of base template is not provided");
}
} else {
inputStream = ProtocolUtils.openUrl((String)path);
}
if (inputStream == null) {
return;
}
byte[] imageBytes = IOUtils.toByteArray((InputStream)inputStream);
inputStream.close();
String contentType = child.getAttribute(XFA.CONTENTTYPETAG).getAttrValue();
child.setValue(imageBytes, contentType);
Element parent = child.getXFAParent().getXFAParent();
TemplateModel templateModel = TemplateModel.getTemplateModel((AppModel)this.document.getAppModel(), (boolean)true);
Element desc = templateModel.createElement("desc", null, parent);
TextValue textValue = new TextValue(desc, null);
textValue.setValue(path);
textValue.setName("embeddedHref");
}
for (Node child2 = node.getFirstXFAChild(); child2 != null; child2 = child2.getNextXFASibling()) {
this.processNode(child2, metaTemplateRef);
}
}
catch (IOException e) {
log.error("Failed to load image bytes for " + absolutePath, (Throwable)e);
}
catch (Exception e) {
log.error("Failed to load image bytes for " + absolutePath, (Throwable)e);
}
}
public Map<String, Element> getDefaultElements() {
HashMap<String, Element> defaultElements = new HashMap<String, Element>(0);
if (this.document == null) {
return defaultElements;
}
TemplateModel templateModel = TemplateModel.getTemplateModel((AppModel)this.document.getAppModel(), (boolean)true);
Subform subform = (Subform)templateModel.resolveNode("AF_METATEMPLATE.AF_FIELDSSUBFORM");
if (subform == null) {
return defaultElements;
}
for (String str2 : MetaTemplateConventions.ELEMENTS) {
Element element = (Element)subform.resolveNode(str2);
defaultElements.put(str2, element);
}
for (String str2 : MetaTemplateConventions.TABLE_ELEMENTS) {
String parentSubform = "AF_TABLE_XFO";
Element element = (Element)subform.resolveNode(parentSubform + "." + str2);
defaultElements.put(str2, element);
}
return defaultElements;
}
public Element getVariables() {
if (this.document == null) {
return null;
}
TemplateModel templateModel = TemplateModel.getTemplateModel((AppModel)this.document.getAppModel(), (boolean)true);
Element variables = (Element)templateModel.resolveNode("#subform[0].#variables[0]");
return variables;
}
public Element getHyphenationElement() {
if (this.document == null) {
return null;
}
TemplateModel templateModel = TemplateModel.getTemplateModel((AppModel)this.document.getAppModel(), (boolean)true);
Element hyphenation = (Element)templateModel.resolveNode("#subform[0].designer__defaultHyphenation");
return hyphenation;
}
public JSONObject getConfigProperties() {
if (this.document == null) {
return null;
}
TemplateModel templateModel = TemplateModel.getTemplateModel((AppModel)this.document.getAppModel(), (boolean)true);
Subform rootSubform = (Subform)templateModel.resolveNode("AF_METATEMPLATE");
return ConfigPropertiesUtils.getConfigProperties(rootSubform);
}
public Element getFillerElement() {
if (this.document == null) {
return null;
}
TemplateModel templateModel = TemplateModel.getTemplateModel((AppModel)this.document.getAppModel(), (boolean)true);
Element filler = (Element)templateModel.resolveNode("#subform[0].spaceFiller");
return filler;
}
public Element getFontFamily() {
if (this.document == null) {
return null;
}
TemplateModel templateModel = TemplateModel.getTemplateModel((AppModel)this.document.getAppModel(), (boolean)true);
Element font = (Element)templateModel.resolveNode("#subform[0].#font");
return font;
}
public Element getAccentColor() {
if (this.document == null) {
return null;
}
TemplateModel templateModel = TemplateModel.getTemplateModel((AppModel)this.document.getAppModel(), (boolean)true);
Element color = (Element)templateModel.resolveNode("#subform[0].#fill.#color");
return color;
}
}