NIHVIVO-2378 Add HelpDirective to freemarker dump package. Remove DescribeDirective from vitro source.
This commit is contained in:
parent
79b7f7a3e4
commit
c1a7c7a9f5
9 changed files with 417 additions and 117 deletions
|
@ -313,7 +313,6 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
|||
*/
|
||||
public static Map<String, Object> getDirectives() {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("describe", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.DescribeDirective());
|
||||
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("help", new edu.cornell.mannlib.vitro.webapp.web.directives.dump.HelpDirective());
|
||||
|
|
|
@ -1,109 +0,0 @@
|
|||
/* $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.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
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.web.directives.BaseTemplateDirectiveModel;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
|
||||
import freemarker.core.Environment;
|
||||
import freemarker.template.SimpleScalar;
|
||||
import freemarker.template.TemplateDirectiveBody;
|
||||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateHashModel;
|
||||
import freemarker.template.TemplateModel;
|
||||
import freemarker.template.TemplateModelException;
|
||||
import freemarker.template.utility.DeepUnwrap;
|
||||
|
||||
public class DescribeDirective extends BaseTemplateDirectiveModel {
|
||||
|
||||
private static final Log log = LogFactory.getLog(DescribeDirective.class);
|
||||
|
||||
@Override
|
||||
public void execute(Environment env, Map params, TemplateModel[] loopVars,
|
||||
TemplateDirectiveBody body) throws TemplateException, IOException {
|
||||
|
||||
if (loopVars.length != 0) {
|
||||
throw new TemplateModelException(
|
||||
"The describe directive doesn't allow loop variables.");
|
||||
}
|
||||
if (body != null) {
|
||||
throw new TemplateModelException(
|
||||
"The describe directive doesn't allow nested content.");
|
||||
}
|
||||
|
||||
Object o = params.get("var");
|
||||
if ( !(o instanceof SimpleScalar)) {
|
||||
throw new TemplateModelException(
|
||||
"Value of parameter 'var' must be a string.");
|
||||
}
|
||||
|
||||
String varName = ((SimpleScalar)o).getAsString();
|
||||
|
||||
TemplateHashModel dataModel = env.getDataModel();
|
||||
|
||||
TemplateModel wrappedModel = null;
|
||||
try {
|
||||
wrappedModel = dataModel.get(varName);
|
||||
} catch (TemplateModelException tme) {
|
||||
log.error("Error getting value of template model " + varName + " from data model.");
|
||||
}
|
||||
|
||||
Object unwrappedModel = null;
|
||||
try {
|
||||
unwrappedModel = DeepUnwrap.permissiveUnwrap(wrappedModel);
|
||||
} catch (TemplateModelException e) {
|
||||
log.error("Cannot unwrap template model " + varName + ".");
|
||||
}
|
||||
|
||||
if (! (unwrappedModel instanceof BaseTemplateModel) ) {
|
||||
throw new TemplateModelException(
|
||||
varName + " is not a template model.");
|
||||
}
|
||||
|
||||
DumpHelper helper = new DumpHelper(env);
|
||||
List<Method> methods = helper.getMethodsAvailableToTemplate(wrappedModel, (BaseTemplateModel)unwrappedModel);
|
||||
List<String> methodDisplayNames = new ArrayList<String>(methods.size());
|
||||
for (Method m : methods) {
|
||||
methodDisplayNames.add(helper.getMethodDisplayName(m));
|
||||
}
|
||||
Collections.sort(methodDisplayNames);
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("var", varName);
|
||||
map.put("methods", methodDisplayNames);
|
||||
|
||||
helper.writeDump("describe.ftl", map, varName, dataModel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String help(String name, Environment env) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
map.put("name", name);
|
||||
|
||||
map.put("effect", "Describe the methods callable on a template variable.");
|
||||
|
||||
//map.put("comments", "");
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("var", "name of variable to describe");
|
||||
map.put("params", params);
|
||||
|
||||
List<String> examples = new ArrayList<String>();
|
||||
examples.add("<@" + name + " var=\"stylesheets\" />");
|
||||
map.put("examples", examples);
|
||||
|
||||
return mergeToHelpTemplate(map, env);
|
||||
}
|
||||
|
||||
}
|
|
@ -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>();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
89
webapp/src/freemarker/ext/dump/HelpDirective.java
Normal file
89
webapp/src/freemarker/ext/dump/HelpDirective.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -228,9 +228,7 @@ public class DumpDirectiveTest {
|
|||
|
||||
test(varName, dataModel, expectedDump);
|
||||
}
|
||||
|
||||
// RY test method and directive types with and without help methods
|
||||
|
||||
|
||||
@Test
|
||||
public void dumpHelplessMethod() {
|
||||
|
||||
|
|
281
webapp/test/freemarker/ext/dump/HelpDirectiveTest.java
Normal file
281
webapp/test/freemarker/ext/dump/HelpDirectiveTest.java
Normal file
|
@ -0,0 +1,281 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package freemarker.ext.dump;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import freemarker.core.Environment;
|
||||
import freemarker.ext.dump.BaseDumpDirective.Key;
|
||||
import freemarker.ext.dump.BaseDumpDirective.Type;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateDirectiveBody;
|
||||
import freemarker.template.TemplateDirectiveModel;
|
||||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateMethodModel;
|
||||
import freemarker.template.TemplateModel;
|
||||
import freemarker.template.TemplateModelException;
|
||||
|
||||
public class HelpDirectiveTest {
|
||||
|
||||
private Template template;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Configuration config = new Configuration();
|
||||
String templateStr = "";
|
||||
try {
|
||||
template = new Template("template", new StringReader(templateStr), config);
|
||||
} catch (Exception e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
// Turn off log messages to console
|
||||
Logger.getLogger(BaseDumpDirective.class).setLevel(Level.OFF);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dumpHelplessMethod() {
|
||||
|
||||
String varName = "square";
|
||||
Map<String, Object> dataModel = new HashMap<String, Object>();
|
||||
|
||||
TemplateMethodModel methodModel = new HelplessMethod();
|
||||
dataModel.put(varName, methodModel);
|
||||
|
||||
Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
|
||||
expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD);
|
||||
expectedDumpValue.put("help", null);
|
||||
|
||||
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
||||
expectedDump.put(varName, expectedDumpValue);
|
||||
|
||||
test(varName, dataModel, expectedDump);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dumpHelpfulMethod() {
|
||||
|
||||
String varName = "square";
|
||||
Map<String, Object> dataModel = new HashMap<String, Object>();
|
||||
|
||||
TemplateMethodModel methodModel = new HelpfulMethod();
|
||||
dataModel.put(varName, methodModel);
|
||||
|
||||
Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
|
||||
expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD);
|
||||
expectedDumpValue.put("help", getMethodHelp(varName));
|
||||
|
||||
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
||||
expectedDump.put(varName, expectedDumpValue);
|
||||
|
||||
test(varName, dataModel, expectedDump);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dumpMethodWithBadHelp() {
|
||||
|
||||
String varName = "square";
|
||||
Map<String, Object> dataModel = new HashMap<String, Object>();
|
||||
|
||||
TemplateMethodModel methodModel = new MethodWithBadHelp();
|
||||
dataModel.put(varName, methodModel);
|
||||
|
||||
Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
|
||||
expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD);
|
||||
expectedDumpValue.put("help", null);
|
||||
|
||||
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
||||
expectedDump.put(varName, expectedDumpValue);
|
||||
|
||||
test(varName, dataModel, expectedDump);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dumpHelplessDirective() {
|
||||
|
||||
String varName = "dump";
|
||||
Map<String, Object> dataModel = new HashMap<String, Object>();
|
||||
|
||||
TemplateDirectiveModel directiveModel = new HelplessDirective();
|
||||
dataModel.put(varName, directiveModel);
|
||||
|
||||
Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
|
||||
expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE);
|
||||
expectedDumpValue.put("help", null);
|
||||
|
||||
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
||||
expectedDump.put(varName, expectedDumpValue);
|
||||
|
||||
test(varName, dataModel, expectedDump);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dumpHelpfulDirective() {
|
||||
|
||||
String varName = "dump";
|
||||
Map<String, Object> dataModel = new HashMap<String, Object>();
|
||||
|
||||
TemplateDirectiveModel directiveModel = new HelpfulDirective();
|
||||
dataModel.put(varName, directiveModel);
|
||||
|
||||
Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
|
||||
expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE);
|
||||
expectedDumpValue.put("help", getDirectiveHelp(varName));
|
||||
|
||||
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
||||
expectedDump.put(varName, expectedDumpValue);
|
||||
|
||||
test(varName, dataModel, expectedDump);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dumpDirectiveWithBadHelp() {
|
||||
|
||||
String varName = "dump";
|
||||
Map<String, Object> dataModel = new HashMap<String, Object>();
|
||||
|
||||
TemplateDirectiveModel directiveModel = new DirectiveWithBadHelp();
|
||||
dataModel.put(varName, directiveModel);
|
||||
|
||||
Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
|
||||
expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE);
|
||||
expectedDumpValue.put("help", null);
|
||||
|
||||
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
||||
expectedDump.put(varName, expectedDumpValue);
|
||||
|
||||
test(varName, dataModel, expectedDump);
|
||||
}
|
||||
|
||||
/////////////////////////// Private test classes and helper methods ///////////////////////////
|
||||
|
||||
private class HelplessMethod implements TemplateMethodModel {
|
||||
|
||||
@Override
|
||||
public Object exec(List arg0) throws TemplateModelException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private class HelpfulMethod implements TemplateMethodModel {
|
||||
|
||||
@Override
|
||||
public Object exec(List arg0) throws TemplateModelException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<String, Object> help(String name) {
|
||||
return getMethodHelp(name);
|
||||
}
|
||||
}
|
||||
|
||||
private class MethodWithBadHelp implements TemplateMethodModel {
|
||||
|
||||
@Override
|
||||
public Object exec(List arg0) throws TemplateModelException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<String, Object> help() {
|
||||
return new HashMap<String, Object>();
|
||||
}
|
||||
}
|
||||
|
||||
private class HelplessDirective implements TemplateDirectiveModel {
|
||||
|
||||
@Override
|
||||
public void execute(Environment arg0, Map arg1, TemplateModel[] arg2,
|
||||
TemplateDirectiveBody arg3) throws TemplateException,
|
||||
IOException {
|
||||
}
|
||||
}
|
||||
|
||||
private class HelpfulDirective implements TemplateDirectiveModel {
|
||||
|
||||
@Override
|
||||
public void execute(Environment arg0, Map arg1, TemplateModel[] arg2,
|
||||
TemplateDirectiveBody arg3) throws TemplateException,
|
||||
IOException {
|
||||
}
|
||||
|
||||
public Map<String, Object> help(String name) {
|
||||
return getDirectiveHelp(name);
|
||||
}
|
||||
}
|
||||
|
||||
private class DirectiveWithBadHelp implements TemplateDirectiveModel {
|
||||
|
||||
@Override
|
||||
public void execute(Environment arg0, Map arg1, TemplateModel[] arg2,
|
||||
TemplateDirectiveBody arg3) throws TemplateException,
|
||||
IOException {
|
||||
}
|
||||
|
||||
public String help(String name) {
|
||||
return "help";
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> getDirectiveHelp(String name) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
map.put("effect", "Dump the contents of a template variable.");
|
||||
|
||||
map.put("comments", "Sequences (lists and arrays) are enclosed in square brackets. Hashes are enclosed in curly braces.");
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private Map<String, Object> getMethodHelp(String name) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
map.put("returns", "The square of the argument");
|
||||
|
||||
List<String>params = new ArrayList<String>();
|
||||
params.add("Integer to square");
|
||||
map.put("params", params);
|
||||
|
||||
List<String> examples = new ArrayList<String>();
|
||||
examples.add(name + "(4)");
|
||||
map.put("examples", examples);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
private Map<String, Object> getDump(String varName, Map<String, Object> dataModel) {
|
||||
try {
|
||||
Environment env = template.createProcessingEnvironment(dataModel, new StringWriter());
|
||||
return new DumpDirective().getTemplateVariableDump(varName, env);
|
||||
} catch (Exception e) {
|
||||
fail(e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void test(String varName, Map<String, Object> dataModel, Map<String, Object> expectedDump) {
|
||||
Map<String, Object> dump = getDump(varName, dataModel);
|
||||
assertEquals(expectedDump, dump);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue