NIHVIVO-2378 Add HelpDirective to freemarker dump package. Remove DescribeDirective from vitro source.

This commit is contained in:
ryounes 2011-04-20 18:56:05 +00:00
parent 79b7f7a3e4
commit c1a7c7a9f5
9 changed files with 417 additions and 117 deletions

View file

@ -465,4 +465,7 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
out.write(sw.toString());
}
protected Map<String, Object> help(String name) {
return new HashMap<String, Object>();
}
}

View file

@ -48,7 +48,7 @@ public class DumpAllDirective extends BaseDumpDirective {
}
SortedMap<String, Object> dataModelDump = getDataModelDump(env);
dump("dumpAll.ftl", dataModelDump, env);
dump("dump-all.ftl", dataModelDump, env);
}
SortedMap<String, Object> getDataModelDump(Environment env) throws TemplateModelException {
@ -66,5 +66,21 @@ public class DumpAllDirective extends BaseDumpDirective {
return dump;
}
@Override
protected Map<String, Object> help(String name) {
Map<String, Object> map = new HashMap<String, Object>();
//map.put("name", name);
map.put("effect", "Dump the contents of the template data model.");
//map.put("comments", "");
List<String> examples = new ArrayList<String>();
examples.add("<@" + name + " />");
map.put("examples", examples);
return map;
}
}

View file

@ -3,7 +3,9 @@
package freemarker.ext.dump;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
@ -42,9 +44,30 @@ public class DumpDirective extends BaseDumpDirective {
}
String varName = ((SimpleScalar)o).getAsString();
Map<String, Object> map = new HashMap<String, Object>();
map.put("var", getTemplateVariableDump(varName, env));
Map<String, Object> map = getTemplateVariableDump(varName, env);
dump("dumpvar.ftl", map, env);
}
@Override
protected Map<String, Object> help(String name) {
Map<String, Object> map = new HashMap<String, Object>();
//map.put("name", name);
map.put("effect", "Dump the contents of a template variable.");
//map.put("comments", "");
Map<String, String> params = new HashMap<String, String>();
params.put("var", "name of variable to dump");
map.put("params", params);
List<String> examples = new ArrayList<String>();
examples.add("<@" + name + " var=\"urls\" />");
map.put("examples", examples);
return map;
}
}

View file

@ -0,0 +1,89 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package freemarker.ext.dump;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.core.Environment;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.utility.DeepUnwrap;
public class HelpDirective extends BaseDumpDirective {
@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("for");
if ( o == null) {
throw new TemplateModelException(
"Must specify 'for' argument.");
}
if ( !(o instanceof SimpleScalar)) {
throw new TemplateModelException(
"Value of parameter 'for' must be a string.");
}
String varName = ((SimpleScalar)o).getAsString();
TemplateHashModel dataModel = env.getDataModel();
Object templateModel = dataModel.get(varName);
if (templateModel == null) {
throw new TemplateModelException(
"Value of parameter '" + varName + "' must be the name of a directive or method");
}
if (! (templateModel instanceof TemplateMethodModel || templateModel instanceof TemplateDirectiveModel)) {
throw new TemplateModelException(
"Value of parameter '" + varName + "' must be the name of a directive or method");
}
Map<String, Object> map = getTemplateVariableDump(varName, env);
dump("dumpvar.ftl", map, env);
}
@Override
protected Map<String, Object> help(String name) {
Map<String, Object> map = new HashMap<String, Object>();
//map.put("name", name);
map.put("effect", "Output help for a directive or method.");
//map.put("comments", "");
Map<String, String> params = new HashMap<String, String>();
params.put("for", "name of directive or method");
map.put("params", params);
List<String> examples = new ArrayList<String>();
examples.add("<@" + name + " for=\"dump\" />");
examples.add("<@" + name + " for=\"profileUrl\" />");
map.put("examples", examples);
return map;
}
}