NIHVIVO-1563 Add template method models to dumpAll directive

This commit is contained in:
rjy7 2011-02-22 18:06:02 +00:00
parent 4d413bc451
commit c4da8ffc07
17 changed files with 229 additions and 61 deletions

View file

@ -339,8 +339,8 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
public static Map<String, Object> getMethods() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("profileUrl", new edu.cornell.mannlib.vitro.webapp.web.functions.IndividualProfileUrlMethod());
map.put("localName", new edu.cornell.mannlib.vitro.webapp.web.functions.IndividualLocalNameMethod());
map.put("profileUrl", new edu.cornell.mannlib.vitro.webapp.web.methods.IndividualProfileUrlMethod());
map.put("localName", new edu.cornell.mannlib.vitro.webapp.web.methods.IndividualLocalNameMethod());
return map;
}

View file

@ -56,7 +56,7 @@ import edu.cornell.mannlib.vitro.webapp.reasoner.SimpleReasoner;
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper;
import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapperFactory;
import edu.cornell.mannlib.vitro.webapp.web.ContentType;
import edu.cornell.mannlib.vitro.webapp.web.functions.IndividualLocalNameMethod;
import edu.cornell.mannlib.vitro.webapp.web.methods.IndividualLocalNameMethod;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModel;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.ListedIndividualTemplateModel;
import freemarker.ext.beans.BeansWrapper;

View file

@ -14,27 +14,20 @@ import freemarker.core.Environment;
import freemarker.template.Template;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModelException;
import freemarker.template.utility.DeepUnwrap;
public abstract class BaseTemplateDirectiveModel implements TemplateDirectiveModel {
private static final Log log = LogFactory.getLog(BaseTemplateDirectiveModel.class);
public String help(Environment environment) {
public String help(String name, Environment env) {
Map<String, Object> map = new HashMap<String, Object>();
String name = getDirectiveName();
map.put("name", name);
return mergeToHelpTemplate(map, environment);
}
protected String getDirectiveName() {
String className = this.getClass().getName();
String[] nameParts = className.split("\\.");
String directiveName = nameParts[nameParts.length-1];
directiveName = directiveName.replaceAll("Directive$", "");
directiveName = directiveName.substring(0, 1).toLowerCase() + directiveName.substring(1);
return directiveName;
return mergeToHelpTemplate(map, env);
}
protected String mergeToHelpTemplate(Map<String, Object> map, Environment env) {

View file

@ -60,10 +60,9 @@ public class UrlDirective extends BaseTemplateDirectiveModel {
out.write(url);
}
public String help(Environment env) {
public String help(String name, Environment env) {
Map<String, Object> map = new HashMap<String, Object>();
String name = getDirectiveName();
map.put("name", name);
map.put("effect", "Generate a full url from a path. Use for generating src attribute of image tags.");

View file

@ -93,10 +93,9 @@ public class WidgetDirective extends BaseTemplateDirectiveModel {
}
public String help(Environment env) {
public String help(String name, Environment env) {
Map<String, Object> map = new HashMap<String, Object>();
String name = getDirectiveName();
map.put("name", name);
map.put("effect", "Add a reuseable block of markup and functionality to the template, with associated scripts and stylesheets injected into the page &lt;head&gt; element.");

View file

@ -4,7 +4,6 @@ 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.Collections;
import java.util.HashMap;
@ -14,7 +13,6 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.utils.StringUtils;
import edu.cornell.mannlib.vitro.webapp.web.directives.BaseTemplateDirectiveModel;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
import freemarker.core.Environment;
@ -93,11 +91,10 @@ public class DescribeDirective extends BaseTemplateDirectiveModel {
helper.writeDump("describe.ftl", map, varName);
}
public String help(Environment env) {
@Override
public String help(String name, Environment env) {
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.");

View file

@ -13,8 +13,8 @@ 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.methods.BaseTemplateMethodModel;
import freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateHashModel;
@ -52,12 +52,16 @@ public class DumpAllDirective extends BaseTemplateDirectiveModel {
DumpHelper helper = new DumpHelper(env);
List<String> models = new ArrayList<String>();
List<String> directives = new ArrayList<String>();
List<String> methods = new ArrayList<String>();
for (String var : varNames) {
Object value = dm.get(var);
if (value instanceof BaseTemplateDirectiveModel) {
String help = ((BaseTemplateDirectiveModel) value).help(env);
String help = ((BaseTemplateDirectiveModel) value).help(var, env);
directives.add(help);
} else if (value instanceof BaseTemplateMethodModel) {
String help = ((BaseTemplateMethodModel) value).help(var, env);
methods.add(help);
} else {
models.add(helper.getVariableDump(var));
}
@ -66,6 +70,7 @@ public class DumpAllDirective extends BaseTemplateDirectiveModel {
Map<String, Object> map = new HashMap<String, Object>();
map.put("models", models);
map.put("directives", directives);
map.put("methods", methods);
map.put("containingTemplate", env.getTemplate().getName());
try {
@ -78,11 +83,10 @@ public class DumpAllDirective extends BaseTemplateDirectiveModel {
}
@Override
public String help(Environment env) {
@Override
public String help(String name, Environment env) {
Map<String, Object> map = new HashMap<String, Object>();
String name = getDirectiveName();
map.put("name", name);
map.put("effect", "Dump the contents of the template data model.");

View file

@ -60,10 +60,10 @@ public class DumpDirective extends BaseTemplateDirectiveModel {
helper.writeDump("dump.ftl", map, var);
}
public String help(Environment env) {
@Override
public String help(String name, Environment env) {
Map<String, Object> map = new HashMap<String, Object>();
String name = getDirectiveName();
map.put("name", name);
map.put("effect", "Dump the contents of a template variable.");

View file

@ -4,11 +4,9 @@ package edu.cornell.mannlib.vitro.webapp.web.directives.dump;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -22,7 +20,9 @@ import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
import freemarker.core.Environment;
import freemarker.template.TemplateBooleanModel;
import freemarker.template.TemplateDateModel;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateNumberModel;
@ -52,6 +52,7 @@ public class DumpHelper {
tm = dataModel.get(varName);
} catch (TemplateModelException tme) {
log.error("Error getting value of template model " + varName + " from data model.");
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
@ -63,6 +64,7 @@ public class DumpHelper {
unwrappedModel = DeepUnwrap.permissiveUnwrap(tm);
} catch (TemplateModelException e) {
log.error("Cannot unwrap template model " + varName + ".");
return null;
}
// Just use toString() method for now. Handles nested collections. Could make more sophisticated later.
@ -148,7 +150,8 @@ public class DumpHelper {
for (Method m : declaredMethods) {
int mod = m.getModifiers();
if (Modifier.isPublic(mod) && !Modifier.isStatic(mod)) {
// if the method takes args, make sure the BeanWrapper used makes this method visible
// If the method takes args, make sure the BeanWrapper used makes this method visible.
// RY It may not be possible to determine this.
methods.add(m);
}
}
@ -192,11 +195,19 @@ public class DumpHelper {
methods.add(key);
} else {
try {
Object value = method.invoke(model);
if (value == null) {
Object result = method.invoke(model);
String value = null;
if (result == null) {
value = "null"; // distinguish a null from an empty string
// } else if (result instanceof TemplateDirectiveModel) {
// // value = string output of the help() method processed through template
// } else if (result instanceof TemplateMethodModel) {
// // value = string output of the help() method processed through template
} else {
value = result.toString();
}
properties.put(key, value.toString());
properties.put(key, value);
} catch (Exception e) {
log.error(e, e);
continue;

View file

@ -59,7 +59,7 @@ public class HelpDirective extends BaseTemplateDirectiveModel {
Configuration config = env.getConfiguration();
Map<String, Object> map = new HashMap<String, Object>();
String help = ((BaseTemplateDirectiveModel) value).help(env);
String help = ((BaseTemplateDirectiveModel) value).help(directiveName, env);
map.put("help", help);
try {
@ -74,20 +74,20 @@ public class HelpDirective extends BaseTemplateDirectiveModel {
}
public String help(Environment env) {
public String help(String name, Environment env) {
Map<String, Object> map = new HashMap<String, Object>();
String name = getDirectiveName();
map.put("name", name);
map.put("effect", "Output directive help.");
map.put("effect", "Output help for directive or method.");
Map<String, String> params = new HashMap<String, String>();
params.put("var", "name of directive");
params.put("var", "name of directive/method");
map.put("params", params);
List<String> examples = new ArrayList<String>();
examples.add("<@" + name + " directive=\"dump\" />");
examples.add("<@" + name + " method=\"profileUrl\" />");
map.put("examples", examples);
return mergeToHelpTemplate(map, env);

View file

@ -0,0 +1,56 @@
package edu.cornell.mannlib.vitro.webapp.web.methods;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import freemarker.core.Environment;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateMethodModel;
public abstract class BaseTemplateMethodModel implements TemplateMethodModel {
private static final Log log = LogFactory.getLog(BaseTemplateMethodModel.class);
public String help(String name, Environment env) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
return mergeToHelpTemplate(map, env);
}
protected String mergeToHelpTemplate(Map<String, Object> map, Environment env) {
return processTemplateToString("help-method.ftl", map, env);
}
public static String processTemplateToString(String templateName, Map<String, Object> map, Environment env) {
Template template = getTemplate(templateName, env);
StringWriter sw = new StringWriter();
try {
template.process(map, sw);
} catch (TemplateException e) {
log.error("Template Exception creating processing environment", e);
} catch (IOException e) {
log.error("IOException creating processing environment", e);
}
return sw.toString();
}
private static Template getTemplate(String templateName, Environment env) {
Template template = null;
try {
template = env.getConfiguration().getTemplate(templateName);
} catch (IOException e) {
// RY Should probably throw this error instead.
log.error("Cannot get template " + templateName, e);
}
return template;
}
}

View file

@ -1,22 +1,24 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.web.functions;
package edu.cornell.mannlib.vitro.webapp.web.methods;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import freemarker.core.Environment;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;
public class IndividualLocalNameMethod implements TemplateMethodModel {
public class IndividualLocalNameMethod extends BaseTemplateMethodModel {
@SuppressWarnings("rawtypes")
@Override
public String exec(List args) throws TemplateModelException {
if (args.size() != 1) {
@ -33,4 +35,23 @@ public class IndividualLocalNameMethod implements TemplateMethodModel {
return individual.getLocalName();
}
@Override
public String help(String name, Environment env) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
map.put("returnValue", "The local name of the individual");
List<String>params = new ArrayList<String>();
params.add("Uri of individual");
map.put("params", params);
List<String> examples = new ArrayList<String>();
examples.add(name + "(individual.uri)");
map.put("examples", examples);
return mergeToHelpTemplate(map, env);
}
}

View file

@ -1,8 +1,11 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.web.functions;
package edu.cornell.mannlib.vitro.webapp.web.methods;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
@ -12,7 +15,7 @@ import freemarker.core.Environment;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;
public class IndividualProfileUrlMethod implements TemplateMethodModel {
public class IndividualProfileUrlMethod extends BaseTemplateMethodModel {
@Override
public String exec(List args) throws TemplateModelException {
@ -29,4 +32,22 @@ public class IndividualProfileUrlMethod implements TemplateMethodModel {
return (url == null) ? "" : url; // don't return a null to the template
}
@Override
public String help(String name, Environment env) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
map.put("returnValue", "The profile url of the individual");
List<String>params = new ArrayList<String>();
params.add("Uri of individual");
map.put("params", params);
List<String> examples = new ArrayList<String>();
examples.add(name + "(individual.uri)");
map.put("examples", examples);
return mergeToHelpTemplate(map, env);
}
}

View file

@ -8,18 +8,36 @@
padding: 1em 0;
margin: 1em 0;
}
.dump li {
list-style-type: none;
}
.dump.directive h6 {
.dump .directive h6 {
margin-top: 1em;
}
.dump.datamodel .var,
.dump.datamodel .directive {
border-bottom: 1px solid #ccc;
padding: 1em 0;
margin: 1em 0;
}
.dump .var p {
margin-bottom: .3em;
}
.dump .directive ol,
.dump .directive ul {
margin-left: 1.5em;
}
.dump .directive ol li,
.dump .directive ul li {
margin-bottom: .5em;
}
.dump .directive ol li {
list-style: decimal inside none;
}

View file

@ -6,21 +6,30 @@
<h3>Data model dump for template <em>${containingTemplate}</em></h3>
<h4>VARIABLES</h4>
<h4>VARIABLES</h4>
<ul>
<#list models as model>
<li>${model}</li>
</#list>
</ul>
<h4>DIRECTIVES</h4>
<ul>
<#list directives as directive>
<li>${directive}</li>
</#list>
</ul>
<#if directives?has_content>
<h4>DIRECTIVES</h4>
<ul>
<#list directives as directive>
<li>${directive}</li>
</#list>
</ul>
</#if>
<#if methods?has_content>
<h4>METHODS</h4>
<ul>
<#list methods as method>
<li>${method}</li>
</#list>
</ul>
</#if>
</div>

View file

@ -13,7 +13,7 @@
</#if>
<p><strong>Parameters: </strong>
<#if params?? && params?keys?has_content>
<#if params??>
</p>
<ul>
<#list params?keys as param>

View file

@ -0,0 +1,40 @@
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
<#-- Template for displaying directive help -->
<div class="directive">
<p><strong>Method name:</strong> ${name}</p>
<#if returnValue??>
<p><strong>Return value:</strong> ${returnValue}</p>
<#if comments??>
<p><strong>Comments:</strong> ${comments}</p>
</#if>
<p><strong>Parameters:</strong>
<#if params??>
</p>
<ol>
<#list params as param>
<li>${param}</li>
</#list>
</ol>
<#else>
none</p>
</#if>
<br />
<p><strong>Examples:</strong></p>
<#if examples??>
<ul>
<#list examples as example>
<li>${example}</li>
</#list>
</ul>
</#if>
<#else>
<p>No help available for this method.</p>
</#if>
</div>