freemarker.ext.dump.DumpDirective Maven / Gradle / Ivy
The newest version!
/* $This file is distributed under the terms of the license in LICENSE$ */
package freemarker.ext.dump;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import freemarker.core.Environment;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
public class DumpDirective extends BaseDumpDirective {
@SuppressWarnings("unused")
private static final Log log = LogFactory.getLog(DumpDirective.class);
@SuppressWarnings("rawtypes")
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
if (loopVars.length != 0) {
throw new TemplateModelException(
"The dump directive doesn't allow loop variables.");
}
if (body != null) {
throw new TemplateModelException(
"The dump directive doesn't allow nested content.");
}
Object o = params.get("var");
if ( o == null) {
throw new TemplateModelException(
"Must specify 'var' argument.");
}
if ( !(o instanceof SimpleScalar)) {
throw new TemplateModelException(
"Value of parameter 'var' must be a string.");
}
String varName = o.toString(); //((SimpleScalar)o).getAsString();
Map map = getTemplateVariableDump(varName, env);
String title = "Template variable dump";
dump(map, env, title);
}
@Override
public Map help(String name) {
Map map = new LinkedHashMap();
map.put("effect", "Dumps the contents of a template variable.");
Map params = new HashMap();
params.put("var", "name of variable to dump");
map.put("parameters", params);
List examples = new ArrayList();
examples.add("<@" + name + " var=\"urls\" />");
map.put("examples", examples);
return map;
}
}