NIHVIVO-564 Help directive for directive help output.

This commit is contained in:
rjy7 2010-08-05 19:54:24 +00:00
parent 91468db876
commit 216d7a7905
6 changed files with 119 additions and 2 deletions

View file

@ -283,7 +283,8 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
// Add any Java directives the templates should have access to // Add any Java directives the templates should have access to
private void addDirectives(Map<String, Object> map) { private void addDirectives(Map<String, Object> map) {
map.put("dump", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DumpDirective()); map.put("dump", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DumpDirective());
map.put("dumpAll", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DumpAllDirective()); map.put("dumpAll", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DumpAllDirective());
map.put("help", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.HelpDirective());
} }
// Add variables that should be available only to the page's root map, not to the body. // Add variables that should be available only to the page's root map, not to the body.

View file

@ -38,7 +38,7 @@ public abstract class BaseTemplateDirectiveModel implements TemplateDirectiveMod
} }
protected String mergeToTemplate(Map<String, Object> map, Configuration config) { protected String mergeToTemplate(Map<String, Object> map, Configuration config) {
return new FreemarkerHelper(config).mergeMapToTemplate("directive-help.ftl", map); return new FreemarkerHelper(config).mergeMapToTemplate("help-directive.ftl", map);
} }
} }

View file

@ -0,0 +1,104 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.web.directives.dump;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHelper;
import edu.cornell.mannlib.vitro.webapp.web.directives.BaseTemplateDirectiveModel;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
import freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateBooleanModel;
import freemarker.template.TemplateDateModel;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateNumberModel;
import freemarker.template.TemplateScalarModel;
import freemarker.template.TemplateSequenceModel;
import freemarker.template.utility.DeepUnwrap;
public class HelpDirective extends BaseTemplateDirectiveModel {
private static final Log log = LogFactory.getLog(HelpDirective.class);
@SuppressWarnings("unchecked")
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body) throws TemplateException, IOException {
if (loopVars.length != 0) {
throw new TemplateModelException(
"The help directive doesn't allow loop variables.");
}
if (body != null) {
throw new TemplateModelException(
"The help directive doesn't allow nested content.");
}
Object o = params.get("directive");
if ( !(o instanceof SimpleScalar)) {
throw new TemplateModelException(
"Value of parameter 'directive' must be a string.");
}
String directiveName = ((SimpleScalar)o).getAsString();
TemplateHashModel dataModel = env.getDataModel();
Map<String, Object> dm = (Map<String, Object>) DeepUnwrap.permissiveUnwrap(dataModel);
Object value = dm.get(directiveName);
if (! (value instanceof BaseTemplateDirectiveModel) ) {
throw new TemplateModelException(
directiveName + " must be the name of a directive.");
}
Configuration config = env.getConfiguration();
Map<String, Object> map = new HashMap<String, Object>();
String help = ((BaseTemplateDirectiveModel) value).help(config);
map.put("help", help);
try {
map.put("stylesheets", dataModel.get("stylesheets"));
} catch (TemplateModelException e) {
log.error("Error getting value of stylesheets variable from data model.");
}
DumpHelper helper = new DumpHelper(env);
helper.writeDump("help.ftl", map, directiveName);
}
public String help(Configuration config) {
Map<String, Object> map = new HashMap<String, Object>();
String name = getDirectiveName();
map.put("name", name);
map.put("effect", "Output directive help.");
Map<String, String> params = new HashMap<String, String>();
params.put("var", "name of directive");
map.put("params", params);
List<String> examples = new ArrayList<String>();
examples.add("<@" + name + " directive=\"dump\" />");
map.put("examples", examples);
return mergeToTemplate(map, config);
}
}

View file

@ -0,0 +1,10 @@
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
<#-- Template for displaying directive help -->
<h3>Template directive help</h3>
${help}
${stylesheets.add("/css/dump.css")}

View file

@ -61,3 +61,5 @@ ${scripts.add("/js/script1.js", "/js/script2.js", "/js/script3.js")}
<@dumpAll /> <@dumpAll />
<@help directive="dump" />