IncludeExtension.java
3.26 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.scripting.sightly.SightlyException
* org.apache.sling.scripting.sightly.extension.RuntimeExtension
* org.apache.sling.scripting.sightly.render.RenderContext
*/
package com.adobe.cq.sightly.internal.extensions;
import com.adobe.cq.sightly.WCMScriptHelper;
import com.adobe.cq.sightly.internal.extensions.ExtensionUtils;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import javax.script.Bindings;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.scripting.sightly.SightlyException;
import org.apache.sling.scripting.sightly.extension.RuntimeExtension;
import org.apache.sling.scripting.sightly.render.RenderContext;
@Component
@Service(value={RuntimeExtension.class})
@Properties(value={@Property(name="org.apache.sling.scripting.sightly.extension.name", value={"include"}), @Property(name="service.ranking", intValue={1000})})
public class IncludeExtension
implements RuntimeExtension {
public static final String FUNCTION = "include";
private static final String OPTION_FILE = "file";
private static final String OPTION_PREPEND_PATH = "prependPath";
private static final String OPTION_APPEND_PATH = "appendPath";
private static final String OPTION_WCMMODE = "wcmmode";
private static final int ARG_COUNT = 2;
public /* varargs */ Object call(RenderContext renderContext, Object ... arguments) {
Map options;
if (arguments.length != 2) {
throw new SightlyException(String.format("Extension %s requires %d arguments.", "include", 2));
}
String originalPath = ExtensionUtils.objectToString(arguments[0]);
String path = this.buildPath(originalPath, options = (Map)arguments[1]);
if (path == null) {
throw new SightlyException("Path for include is empty.");
}
WCMScriptHelper helper = (WCMScriptHelper)renderContext.getBindings().get("slyWcmHelper");
String wcmMode = (String)options.get("wcmmode");
StringWriter output = new StringWriter();
helper.includeScript(path, wcmMode, new PrintWriter(output));
return output.toString();
}
private String buildPath(String path, Map options) {
if (StringUtils.isEmpty((CharSequence)path)) {
path = (String)options.get("file");
}
if (StringUtils.isEmpty((CharSequence)path)) {
return null;
}
String prependPath = (String)options.get("prependPath");
String appendPath = (String)options.get("appendPath");
if (StringUtils.isNotEmpty((CharSequence)prependPath)) {
path = prependPath + path;
}
if (StringUtils.isNotEmpty((CharSequence)appendPath)) {
path = path + appendPath;
}
return path;
}
}