diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DescribeDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DescribeDirective.java index 5851502b2..9b7ace9db 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DescribeDirective.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DescribeDirective.java @@ -18,7 +18,6 @@ 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; -import freemarker.template.Configuration; import freemarker.template.SimpleScalar; import freemarker.template.TemplateDirectiveBody; import freemarker.template.TemplateException; @@ -31,7 +30,6 @@ 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 { @@ -74,16 +72,17 @@ public class DescribeDirective extends BaseTemplateDirectiveModel { varName + " is not a template model."); } - List methods = getPublicMethods(unwrappedModel.getClass()); - List methodDescriptions = new ArrayList(methods.size()); + DumpHelper helper = new DumpHelper(env); + List methods = helper.getMethodsAvailableToTemplate(unwrappedModel.getClass()); + List methodDisplayNames = new ArrayList(methods.size()); for (Method m : methods) { - methodDescriptions.add(getMethodDescription(m)); + methodDisplayNames.add(helper.getMethodDisplayName(m)); } - Collections.sort(methodDescriptions); + Collections.sort(methodDisplayNames); Map map = new HashMap(); map.put("var", varName); - map.put("methods", methodDescriptions); + map.put("methods", methodDisplayNames); try { map.put("stylesheets", dataModel.get("stylesheets")); @@ -91,7 +90,6 @@ public class DescribeDirective extends BaseTemplateDirectiveModel { log.error("Error getting value of stylesheets variable from data model."); } - DumpHelper helper = new DumpHelper(env); helper.writeDump("describe.ftl", map, varName); } @@ -116,56 +114,5 @@ public class DescribeDirective extends BaseTemplateDirectiveModel { return mergeToHelpTemplate(map, env); } - - private List getPublicMethods(Class cls) { - List methods = new ArrayList(); - - // Go up the class hierarchy only as far as the immediate subclass of BaseTemplateModel - if (! cls.getName().equals("edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel")) { - methods = getDeclaredPublicMethods(cls); - methods.addAll(getPublicMethods(cls.getSuperclass())); - } - - return methods; - } - - private List getDeclaredPublicMethods(Class cls) { - - List methods = new ArrayList(); - 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) { - - String methodName = method.getName(); - methodName = methodName.replaceAll("^(get|is)", ""); - methodName = methodName.substring(0, 1).toLowerCase() + methodName.substring(1); - - Class[] paramTypes = method.getParameterTypes(); - String paramList = ""; - if (paramTypes.length > 0) { - List paramTypeList = new ArrayList(paramTypes.length); - for (Class cls : paramTypes) { - String name = cls.getName(); - String[] nameParts = name.split("\\."); - String typeName = nameParts[nameParts.length-1]; - typeName = typeName.replaceAll(";", "s"); - typeName = typeName.substring(0,1).toLowerCase() + typeName.substring(1); - paramTypeList.add(typeName); - } - paramList = "(" + StringUtils.join(paramTypeList) + ")"; - } - - return methodName + paramList; - - } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpHelper.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpHelper.java index 74e9e222d..39170a09c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpHelper.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/dump/DumpHelper.java @@ -4,13 +4,19 @@ 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; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import edu.cornell.mannlib.vitro.webapp.controller.freemarker.TemplateProcessingHelper; +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; @@ -73,7 +79,7 @@ public class DumpHelper { // view in the dump. Not sure if we should handle our application-specific, non-template // model objects in the same way. For now, these get assigned a shorthand type below. if (unwrappedModel instanceof BaseTemplateModel) { - value = ((BaseTemplateModel)unwrappedModel).dump(); + value = getTemplateModelDump((BaseTemplateModel)unwrappedModel); //((BaseTemplateModel)unwrappedModel).dump(); type = className; } // Can't use this, because tm of (at least some) POJOs are @@ -123,4 +129,85 @@ public class DumpHelper { } } + protected List getMethodsAvailableToTemplate(Class cls) { + List methods = new ArrayList(); + + // Go up the class hierarchy only as far as the immediate subclass of BaseTemplateModel + if (! cls.getName().equals("edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel")) { + methods = getDeclaredPublicMethods(cls); + methods.addAll(getMethodsAvailableToTemplate(cls.getSuperclass())); + } + + return methods; + } + + private List getDeclaredPublicMethods(Class cls) { + + List methods = new ArrayList(); + Method[] declaredMethods = cls.getDeclaredMethods(); + 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 + methods.add(m); + } + } + return methods; + } + + protected String getMethodDisplayName(Method method) { + String methodName = method.getName(); + methodName = methodName.replaceAll("^(get|is)", ""); + methodName = methodName.substring(0, 1).toLowerCase() + methodName.substring(1); + + Class[] paramTypes = method.getParameterTypes(); + String paramList = ""; + if (paramTypes.length > 0) { + List paramTypeList = new ArrayList(paramTypes.length); + for (Class cls : paramTypes) { + String name = cls.getName(); + String[] nameParts = name.split("\\."); + String typeName = nameParts[nameParts.length-1]; + typeName = typeName.replaceAll(";", "s"); + typeName = typeName.substring(0,1).toLowerCase() + typeName.substring(1); + paramTypeList.add(typeName); + } + paramList = "(" + StringUtils.join(paramTypeList) + ")"; + } + + return methodName + paramList; + } + + private String getTemplateModelDump(BaseTemplateModel model) { + + log.debug(model.getClass()); + Map map = new HashMap(); + List publicMethods = getMethodsAvailableToTemplate(model.getClass()); + Map properties = new HashMap(); + List methods = new ArrayList(); + for (Method method : publicMethods) { + String key = getMethodDisplayName(method); + + if (key.endsWith(")")) { + methods.add(key); + } else { + try { + Object value = method.invoke(model); + if (value == null) { + value = "null"; // distinguish a null from an empty string + } + properties.put(key, value.toString()); + } catch (Exception e) { + log.error(e, e); + continue; + } + } + } + + map.put("properties", properties); + map.put("methods", methods); + return BaseTemplateDirectiveModel.processTemplateToString("dump-tm.ftl", map, env); + + } + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/BaseTemplateModel.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/BaseTemplateModel.java index 5d8c617e2..e0decfe72 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/BaseTemplateModel.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/BaseTemplateModel.java @@ -43,8 +43,8 @@ public abstract class BaseTemplateModel { servletContext = context; } - public String dump() { - return toString(); // fallback when subclass doesn't define a class-specific dump() - } +// public String dump() { +// return toString(); // fallback when subclass doesn't define a class-specific dump() +// } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/files/Files.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/files/Files.java index 85dc413a8..e742ea9cc 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/files/Files.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/files/Files.java @@ -74,10 +74,6 @@ public abstract class Files extends BaseTemplateModel { } return tags; } - - public String dump() { - return list.toString(); - } protected abstract String getTag(String url); diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump-tm.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump-tm.ftl new file mode 100644 index 000000000..1a4c8b3de --- /dev/null +++ b/webapp/web/templates/freemarker/body/partials/dump/dump-tm.ftl @@ -0,0 +1,19 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Template for dumping a template model object --> + +<#if properties?has_content> +
    + <#list properties?keys as property> +
  • ${property}: ${properties[property]?html}
  • + +
+ + +<#if methods?has_content> +
    + <#list methods as method> +
  • ${method}
  • + +
+ \ No newline at end of file diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump-var.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump-var.ftl index 33c74add4..e23cae502 100644 --- a/webapp/web/templates/freemarker/body/partials/dump/dump-var.ftl +++ b/webapp/web/templates/freemarker/body/partials/dump/dump-var.ftl @@ -5,8 +5,8 @@

Variable name: ${var}

<#if value??> -

Value: ${value}

Type: ${type}

+
Value: ${value}
<#else>

Variable is undefined in the data model