NIHVIVO-564 Describe directive to display methods declared on a template model object that are accessible to the template.

This commit is contained in:
rjy7 2010-08-09 16:31:22 +00:00
parent f8176ed041
commit 698d0877c3
9 changed files with 164 additions and 25 deletions

View file

@ -282,6 +282,7 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
// Add any Java directives the templates should have access to
private void addDirectives(Map<String, Object> map) {
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());

View file

@ -2,16 +2,13 @@
package edu.cornell.mannlib.vitro.webapp.web.directives;
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.dump.DumpDirective;
import freemarker.template.Configuration;
import freemarker.template.TemplateDirectiveModel;

View file

@ -0,0 +1,147 @@
/* $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.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
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.Configuration;
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);
@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 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 tm = null;
try {
tm = 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(tm);
} catch (TemplateModelException e) {
log.error("Cannot unwrap template model " + varName + ".");
}
if (! (unwrappedModel instanceof BaseTemplateModel) ) {
throw new TemplateModelException(
varName + " is not a template model.");
}
List<Method> methods = getPublicMethods(unwrappedModel.getClass());
List<String> methodDescriptions = new ArrayList<String>(methods.size());
for (Method m : methods) {
methodDescriptions.add(getMethodDescription(m));
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("var", varName);
map.put("methods", methodDescriptions);
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("describe.ftl", map, varName);
}
public String help(Configuration config) {
Map<String, Object> map = new HashMap<String, Object>();
String name = getDirectiveName();
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 mergeToTemplate(map, config);
}
private List<Method> getPublicMethods(Class<?> cls) {
List<Method> methods = getDeclaredPublicMethods(cls);
// Don't get methods for classes higher in the class hierarchy than BaseTemplateModel.
if (! cls.getName().equals("edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel")) {
methods.addAll(getPublicMethods(cls.getSuperclass()));
}
return methods;
}
private List<Method> getDeclaredPublicMethods(Class<?> cls) {
List<Method> methods = new ArrayList<Method>();
Method[] declaredMethods = cls.getDeclaredMethods();
for (Method m : declaredMethods) {
int mod = m.getModifiers();
if (Modifier.isPublic(mod) && !Modifier.isStatic(mod)) {
methods.add(m);
}
}
return methods;
}
private String getMethodDescription(Method method) {
return method.toString();
}
}

View file

@ -3,7 +3,6 @@
package edu.cornell.mannlib.vitro.webapp.web.directives.dump;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -13,13 +12,10 @@ 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 freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModel;

View file

@ -3,7 +3,6 @@
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;
@ -12,23 +11,15 @@ 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 DumpDirective extends BaseTemplateDirectiveModel {

View file

@ -59,7 +59,7 @@ public class DumpHelper {
}
// This case must precede the TemplateScalarModel case, because
// tm is an instance of StringModel.
// tm is an instance of StringModel and thus a TemplateScalarModel.
if (unwrappedModel instanceof BaseTemplateModel) {
type = unwrappedModel.getClass().getName();
value = ((BaseTemplateModel)unwrappedModel).dump();

View file

@ -3,7 +3,6 @@
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;
@ -12,22 +11,15 @@ 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 {

View file

@ -0,0 +1,13 @@
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
<#-- Template for displaying directive describe -->
<h3>Methods available to variable <em>${var}</em></h3>
<div class="dump">
<#list methods as method>
${method}<br />
</#list>
</div>
${stylesheets.add("/css/dump.css")}

View file

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