NIHVIVO-2479 Dump templates

This commit is contained in:
ryounes 2011-04-21 17:17:28 +00:00
parent 20e65852d5
commit 2f884cec25
5 changed files with 103 additions and 39 deletions

View file

@ -50,9 +50,10 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
private static final Log log = LogFactory.getLog(BaseDumpDirective.class); private static final Log log = LogFactory.getLog(BaseDumpDirective.class);
protected static final String TEMPLATE_DEFAULT = "dump1.ftl"; // change to dump.ftl when old dump is removed private static final String TEMPLATE_DEFAULT = "dump1.ftl"; // change to dump.ftl when old dump is removed
private static final Pattern PROPERTY_NAME_PATTERN = Pattern.compile("^(get|is)\\w");
protected static final String VALUE_UNDEFINED = "Undefined"; protected static final String VALUE_UNDEFINED = "Undefined";
protected static final Pattern PROPERTY_NAME_PATTERN = Pattern.compile("^(get|is)\\w");
enum Key { enum Key {
DATE_TYPE("dateType"), DATE_TYPE("dateType"),
@ -449,7 +450,7 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
private Map<String, Object> getTemplateModelDump(TemplateModel model) throws TemplateModelException { private Map<String, Object> getTemplateModelDump(TemplateModel model) throws TemplateModelException {
// One of the more specific cases should have applied. Track whether this actually occurs. // One of the more specific cases should have applied. Track whether this actually occurs.
log.debug("Found model with no known type"); log.debug("Found template model of type " + model.getClass().getName());
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
Object unwrappedModel = DeepUnwrap.permissiveUnwrap(model); Object unwrappedModel = DeepUnwrap.permissiveUnwrap(model);
map.put(Key.TYPE.toString(), unwrappedModel.getClass().getName()); map.put(Key.TYPE.toString(), unwrappedModel.getClass().getName());
@ -457,13 +458,24 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
return map; return map;
} }
protected void dump(String templateName, Map<String, Object> dump, Environment env) protected void dump(Map<String, Object> dump, Environment env, String title)
throws TemplateException, IOException {
dump(dump, env, title, TEMPLATE_DEFAULT);
}
protected void dump(Map<String, Object> dump, Environment env, String title, String templateName)
throws TemplateException, IOException { throws TemplateException, IOException {
// Wrap the dump in another map so the template has a handle to iterate through // Wrap the dump in another map so the template has a handle to iterate through
// the values: <#list dump?keys as key>...</#list> // the values: <#list dump?keys as key>...</#list>
Map<String, Map<String, Object>> map = new HashMap<String, Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>();
map.put("dump", dump); map.put("dump", dump);
map.put("title", title);
writeDump(map, env, templateName);
}
protected void writeDump(Map<String, Object> map, Environment env, String templateName)
throws TemplateException, IOException {
Template template = env.getConfiguration().getTemplate(templateName); Template template = env.getConfiguration().getTemplate(templateName);
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
template.process(map, sw); template.process(map, sw);

View file

@ -46,8 +46,9 @@ public class DumpAllDirective extends BaseDumpDirective {
"The dump directive doesn't allow nested content."); "The dump directive doesn't allow nested content.");
} }
SortedMap<String, Object> dataModelDump = getDataModelDump(env); SortedMap<String, Object> dump = getDataModelDump(env);
dump(TEMPLATE_DEFAULT, dataModelDump, env); String title = "Template data model dump for " + env.getTemplate().getName();
dump(dump, env, title);
} }
SortedMap<String, Object> getDataModelDump(Environment env) throws TemplateModelException { SortedMap<String, Object> getDataModelDump(Environment env) throws TemplateModelException {

View file

@ -51,8 +51,8 @@ public class DumpDirective extends BaseDumpDirective {
String varName = o.toString(); //((SimpleScalar)o).getAsString(); String varName = o.toString(); //((SimpleScalar)o).getAsString();
Map<String, Object> map = getTemplateVariableDump(varName, env); Map<String, Object> map = getTemplateVariableDump(varName, env);
String title = "Template variable dump";
dump(TEMPLATE_DEFAULT, map, env); dump(map, env, title);
} }
@Override @Override

View file

@ -50,18 +50,16 @@ public class HelpDirective extends BaseDumpDirective {
TemplateHashModel dataModel = env.getDataModel(); TemplateHashModel dataModel = env.getDataModel();
Object templateModel = dataModel.get(varName); 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)) { if (! (templateModel instanceof TemplateMethodModel || templateModel instanceof TemplateDirectiveModel)) {
throw new TemplateModelException( throw new TemplateModelException(
"Value of parameter '" + varName + "' must be the name of a directive or method"); "Value of parameter '" + varName + "' must be the name of a directive or method");
} }
Map<String, Object> map = getTemplateVariableDump(varName, env); Map<String, Object> map = getTemplateVariableDump(varName, env);
dump(TEMPLATE_DEFAULT, map, env);
String type = templateModel instanceof TemplateMethodModel ? "method" : "directive";
String title = "Help for " + type;
dump(map, env, title);
} }
@Override @Override

View file

@ -2,37 +2,90 @@
<#-- Template for dump directives --> <#-- Template for dump directives -->
<#-- Styles here are temporary; move to css file once stylesheets.add() works --> <#-- Styles here are temporary; use stylesheets.add() once that's working (see below) -->
<style> <style>
ul.dump { div.dump {
padding-top: .75em; margin-bottom: .5em;
padding-bottom: .75em;
border-top: 1px solid #ccc; border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc; border-bottom: 1px solid #ccc;
margin-bottom: .5em; padding-top: .75em;
padding-bottom: .75em;
} }
ul.dump li p { .dump ul ul {
margin-bottom: .5em; margin-left: 1.5em;
}
.dump ul li {
list-style: none;
}
.dump ul li p {
margin-bottom: .25em;
}
.dump ul.sequence li.list_item {
margin-bottom: 1.25em;
} }
</style> </style>
<#-- <pre><@dumper.dump dump /></pre> --> <#--
<#import "lib-dump.ftl" as dumper>
<pre><@dumper.dump dump /></pre>
-->
<div class="dump">
<h3>${title}</h3>
<@doDump dump />
</div>
<#macro doDump dump>
<#if dump?keys?has_content> <#if dump?keys?has_content>
<ul class="dump"> <ul>
<#list dump?keys as key> <#list dump?keys as key>
<li> <li>
<#assign value = dump[key] /> <#local dumpVal = dump[key] />
<p><strong>Variable name:</strong> ${key}</p> <p><strong>Variable name:</strong> ${key}</p>
<#if value.type??><p><strong>Type:</strong> ${value.type}</p></#if>
<#-- <p><strong>Value:</strong> ${value.value}</p> --> <@doMap dumpVal />
<#-- What to do here depends on time. Test either ${var.type} or ${var.value} -->
<#-- <p><strong>Value:</strong> ${var.value}</p> -->
</li> </li>
</#list> </#list>
</ul> </ul>
</#if> </#if>
</#macro>
<#macro doMap map>
<#if map.type?has_content>
<p><strong>Type:</strong> ${map.type}</p>
<#if map.dateType?has_content>
<p><strong>Date type:</strong> ${map.dateType}</p>
</#if>
</#if>
<#if map.value?has_content>
<p>
<strong>Value:</strong>
<#local value = map.value>
<#if value?is_string || value?is_number>${value}
<#elseif value?is_boolean || value?is_number>${value?string}
<#elseif value?is_date>${value?string("EEEE, MMMM dd, yyyy hh:mm:ss a zzz")}
<#elseif value?is_sequence><@doSequence value />
</#if>
</p>
</#if>
</#macro>
<#macro doSequence seq>
<#if seq?has_content>
<ul class="sequence">
<#list seq as item>
<li class="list_item">Item ${item_index}: <@doMap item /></li>
</#list>
</ul>
</#if>
</#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">')}