NIHVIVO-2479 Continue work on dump templates for values of different types

This commit is contained in:
ryounes 2011-04-22 16:55:36 +00:00
parent 930ee45f52
commit 530c7a70f8
7 changed files with 74 additions and 58 deletions

View file

@ -57,6 +57,7 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
enum Key { enum Key {
DATE_TYPE("dateType"), DATE_TYPE("dateType"),
HELP("help"),
METHODS("methods"), METHODS("methods"),
PROPERTIES("properties"), PROPERTIES("properties"),
TYPE("type"), TYPE("type"),
@ -416,36 +417,39 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
private Map<String, Object> getTemplateModelDump(TemplateMethodModel model, String varName) throws TemplateModelException { private Map<String, Object> getTemplateModelDump(TemplateMethodModel model, String varName) throws TemplateModelException {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put(Key.TYPE.toString(), Type.METHOD); map.put(Key.TYPE.toString(), Type.METHOD);
map.put("help", getHelp(model, varName)); map.put(Key.HELP.toString(), getHelp(model, varName));
return map; return map;
} }
private Map<String, Object> getTemplateModelDump(TemplateDirectiveModel model, String varName) throws TemplateModelException { private Map<String, Object> getTemplateModelDump(TemplateDirectiveModel model, String varName) throws TemplateModelException {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put(Key.TYPE.toString(), Type.DIRECTIVE); map.put(Key.TYPE.toString(), Type.DIRECTIVE);
map.put("help", getHelp(model, varName)); map.put(Key.HELP.toString(), getHelp(model, varName));
return map; return map;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Map<String, Object> getHelp(TemplateModel model, String varName) { private Map<String, Object> getHelp(TemplateModel model, String varName) {
Map<String, Object> map = null;
if ( model instanceof TemplateMethodModel || model instanceof TemplateDirectiveModel ) { if ( model instanceof TemplateMethodModel || model instanceof TemplateDirectiveModel ) {
String modelClass = model instanceof TemplateMethodModel ? "TemplateMethodModel" : "TemplateDirectiveModel";
Class<?> cls = model.getClass(); Class<?> cls = model.getClass();
Method[] methods = cls.getMethods(); try {
for (Method method : methods) { Method help = cls.getMethod("help", String.class);
if ( method.getName().equals("help") ) { try {
try { return (Map<String, Object>) help.invoke(model, varName);
map = (Map<String, Object>) method.invoke(model, varName); } catch (Exception e) {
} catch (Exception e) { log.error("Error invoking method help() on " + modelClass + " of class " + cls.getName());
String modelClass = model instanceof TemplateMethodModel ? "TemplateMethodModel" : "TemplateDirectiveModel"; return null;
log.error("Error invoking method help() on " + modelClass + " of class " + cls.getName()); }
} } catch (NoSuchMethodException e) {
break; log.info("No help() method defined for " + modelClass + " of class " + cls.getName());
} return null;
} } catch (Exception e) {
log.error("Error getting method help() for " + modelClass + " " + cls.getName());
return null;
}
} }
return map; return null;
} }
private Map<String, Object> getTemplateModelDump(TemplateModel model) throws TemplateModelException { private Map<String, Object> getTemplateModelDump(TemplateModel model) throws TemplateModelException {
@ -483,7 +487,7 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
out.write(sw.toString()); out.write(sw.toString());
} }
protected Map<String, Object> help(String name) { public Map<String, Object> help(String name) {
return new HashMap<String, Object>(); return new HashMap<String, Object>();
} }
} }

View file

@ -68,12 +68,10 @@ public class DumpAllDirective extends BaseDumpDirective {
} }
@Override @Override
protected Map<String, Object> help(String name) { public Map<String, Object> help(String name) {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
//map.put("name", name); map.put("effect", "Dumps the contents of the template data model.");
map.put("effect", "Dump the contents of the template data model.");
//map.put("comments", ""); //map.put("comments", "");

View file

@ -56,18 +56,16 @@ public class DumpDirective extends BaseDumpDirective {
} }
@Override @Override
protected Map<String, Object> help(String name) { public Map<String, Object> help(String name) {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
//map.put("name", name); map.put("effect", "Dumps the contents of a template variable.");
map.put("effect", "Dump the contents of a template variable.");
//map.put("comments", ""); //map.put("comments", "");
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put("var", "name of variable to dump"); params.put("var", "name of variable to dump");
map.put("params", params); map.put("parameters", params);
List<String> examples = new ArrayList<String>(); List<String> examples = new ArrayList<String>();
examples.add("<@" + name + " var=\"urls\" />"); examples.add("<@" + name + " var=\"urls\" />");

View file

@ -58,23 +58,21 @@ public class HelpDirective extends BaseDumpDirective {
Map<String, Object> map = getTemplateVariableDump(varName, env); Map<String, Object> map = getTemplateVariableDump(varName, env);
String type = templateModel instanceof TemplateMethodModel ? "method" : "directive"; String type = templateModel instanceof TemplateMethodModel ? "method" : "directive";
String title = "Help for " + type; String title = "Template " + type + " help";
dump(map, env, title); dump(map, env, title);
} }
@Override @Override
protected Map<String, Object> help(String name) { public Map<String, Object> help(String name) {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
//map.put("name", name);
map.put("effect", "Output help for a directive or method."); map.put("effect", "Outputs help for a directive or method.");
//map.put("comments", ""); //map.put("comments", "");
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put("for", "name of directive or method"); params.put("for", "name of directive or method");
map.put("params", params); map.put("parameters", params);
List<String> examples = new ArrayList<String>(); List<String> examples = new ArrayList<String>();
examples.add("<@" + name + " for=\"dump\" />"); examples.add("<@" + name + " for=\"dump\" />");

View file

@ -255,7 +255,7 @@ public class DumpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD); expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD);
expectedDumpValue.put("help", null); expectedDumpValue.put(Key.HELP.toString(), null);
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -274,7 +274,7 @@ public class DumpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD); expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD);
expectedDumpValue.put("help", getMethodHelp(varName)); expectedDumpValue.put(Key.HELP.toString(), getMethodHelp(varName));
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -293,7 +293,7 @@ public class DumpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD); expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD);
expectedDumpValue.put("help", null); expectedDumpValue.put(Key.HELP.toString(), null);
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -312,7 +312,7 @@ public class DumpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE); expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE);
expectedDumpValue.put("help", null); expectedDumpValue.put(Key.HELP.toString(), null);
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -331,7 +331,7 @@ public class DumpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE); expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE);
expectedDumpValue.put("help", getDirectiveHelp(varName)); expectedDumpValue.put(Key.HELP.toString(), getDirectiveHelp(varName));
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -350,7 +350,7 @@ public class DumpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE); expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE);
expectedDumpValue.put("help", null); expectedDumpValue.put(Key.HELP.toString(), null);
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -862,7 +862,7 @@ public class DumpDirectiveTest {
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put("var", "name of variable to dump"); params.put("var", "name of variable to dump");
map.put("params", params); map.put("parameters", params);
List<String> examples = new ArrayList<String>(); List<String> examples = new ArrayList<String>();
examples.add("<@" + name + " var=\"urls\" />"); examples.add("<@" + name + " var=\"urls\" />");
@ -878,7 +878,7 @@ public class DumpDirectiveTest {
List<String>params = new ArrayList<String>(); List<String>params = new ArrayList<String>();
params.add("Integer to square"); params.add("Integer to square");
map.put("params", params); map.put("parameters", params);
List<String> examples = new ArrayList<String>(); List<String> examples = new ArrayList<String>();
examples.add(name + "(4)"); examples.add(name + "(4)");

View file

@ -58,7 +58,7 @@ public class HelpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD); expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD);
expectedDumpValue.put("help", null); expectedDumpValue.put(Key.HELP.toString(), null);
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -77,7 +77,7 @@ public class HelpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD); expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD);
expectedDumpValue.put("help", getMethodHelp(varName)); expectedDumpValue.put(Key.HELP.toString(), getMethodHelp(varName));
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -96,7 +96,7 @@ public class HelpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD); expectedDumpValue.put(Key.TYPE.toString(), Type.METHOD);
expectedDumpValue.put("help", null); expectedDumpValue.put(Key.HELP.toString(), null);
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -115,7 +115,7 @@ public class HelpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE); expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE);
expectedDumpValue.put("help", null); expectedDumpValue.put(Key.HELP.toString(), null);
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -134,7 +134,7 @@ public class HelpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE); expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE);
expectedDumpValue.put("help", getDirectiveHelp(varName)); expectedDumpValue.put(Key.HELP.toString(), getDirectiveHelp(varName));
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -153,7 +153,7 @@ public class HelpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE); expectedDumpValue.put(Key.TYPE.toString(), Type.DIRECTIVE);
expectedDumpValue.put("help", null); expectedDumpValue.put(Key.HELP.toString(), null);
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -237,9 +237,9 @@ public class HelpDirectiveTest {
map.put("comments", "Sequences (lists and arrays) are enclosed in square brackets. Hashes are enclosed in curly braces."); 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>(); Map<String, String> parameters = new HashMap<String, String>();
params.put("var", "name of variable to dump"); parameters.put("var", "name of variable to dump");
map.put("params", params); map.put("parameters", parameters);
List<String> examples = new ArrayList<String>(); List<String> examples = new ArrayList<String>();
examples.add("<@" + name + " var=\"urls\" />"); examples.add("<@" + name + " var=\"urls\" />");
@ -253,9 +253,9 @@ public class HelpDirectiveTest {
map.put("returns", "The square of the argument"); map.put("returns", "The square of the argument");
List<String>params = new ArrayList<String>(); List<String> params = new ArrayList<String>();
params.add("Integer to square"); params.add("Integer to square");
map.put("params", params); map.put("parameters", params);
List<String> examples = new ArrayList<String>(); List<String> examples = new ArrayList<String>();
examples.add(name + "(4)"); examples.add(name + "(4)");

View file

@ -88,7 +88,7 @@ div.dump {
<li class="item"> <li class="item">
<#if type == "Sequence"> <#if type == "Sequence">
Item ${item_index}: Item ${item_index}:
<@valueDiv item /> <@valueDiv><@doMap item /></@valueDiv>
<#else><@doMap item /> <#else><@doMap item />
</#if> </#if>
@ -105,7 +105,7 @@ div.dump {
<ul class="map"> <ul class="map">
<#list map?keys as key> <#list map?keys as key>
<li class="item"> <li class="item">
${key} => <@valueDiv map[key] /> ${key} => <@valueDiv><@doMap map[key] /></@valueDiv>
</li> </li>
</#list> </#list>
</ul> </ul>
@ -125,21 +125,39 @@ div.dump {
<#macro doHelp help=""> <#macro doHelp help="">
<#if help?has_content> <#if help?has_content>
<p><strong>Help:</strong><p>
<ul class="help"> <ul class="help">
<#list help?keys as key> <#list help?keys as key>
<li> <li>
<p><strong>${key}</strong></p> <#local value = help[key]>
<#--<@valueDiv help[key] />--> <@valueDiv>
<#if value?is_string><p><strong>${key?capitalize}:</strong> ${value}</p>
<#else>
<p><strong>${key?capitalize}:</strong></p>
<ul>
<#if value?is_sequence>
<#list value as item>
<li>${item}</li>
</#list>
<#elseif value?is_hash_ex>
<#list value?keys as key>
<li><strong>${key}:</strong> ${value[key]}</li>
</#list>
</#if>
</ul>
</#if>
</@valueDiv>
</li> </li>
</#list> </#list>
</ul> </ul>
</#if> </#if>
</#macro> </#macro>
<#macro valueDiv value> <#macro valueDiv>
<div class="value"><@doMap value /></div> <div class="value"><#nested></div>
</#macro> </#macro>
<#-- This will work after we move stylesheets to Configuration sharedVariables <#-- This will work after we move stylesheets to Configuration sharedVariables
${stylesheets.add('<link rel="stylesheet" href="/css/fmdump.css">')} ${stylesheets.add('<link rel="stylesheet" href="/css/fmdump.css">')}
--> -->