IncludeExtension.java 3.26 KB
/*
 * 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;
    }
}