NIHVIVO-564 Work on dump directive
This commit is contained in:
parent
86bdfaf1cd
commit
83938a2c19
11 changed files with 138 additions and 33 deletions
|
@ -24,7 +24,7 @@ import freemarker.template.Configuration;
|
||||||
public class FreemarkerComponentGenerator extends FreemarkerHttpServlet {
|
public class FreemarkerComponentGenerator extends FreemarkerHttpServlet {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private static final Log log = LogFactory.getLog(FreemarkerHttpServlet.class.getName());
|
private static final Log log = LogFactory.getLog(FreemarkerComponentGenerator.class);
|
||||||
|
|
||||||
private static ServletContext context = null;
|
private static ServletContext context = null;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import freemarker.template.Configuration;
|
||||||
|
import freemarker.template.Template;
|
||||||
|
import freemarker.template.TemplateException;
|
||||||
|
|
||||||
|
public class FreemarkerHelper {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(FreemarkerHelper.class);
|
||||||
|
|
||||||
|
public StringWriter mergeToTemplate(String templateName, Map<String, Object> map, Configuration config) {
|
||||||
|
|
||||||
|
Template template = null;
|
||||||
|
try {
|
||||||
|
template = config.getTemplate(templateName);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Cannot get template " + templateName);
|
||||||
|
}
|
||||||
|
StringWriter sw = new StringWriter();
|
||||||
|
if (template != null) {
|
||||||
|
try {
|
||||||
|
template.process(map, sw);
|
||||||
|
} catch (TemplateException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sw;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String mergeMapToTemplate(String templateName, Map<String, Object> map, Configuration config) {
|
||||||
|
return mergeToTemplate(templateName, map, config).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -50,7 +50,7 @@ import freemarker.template.TemplateModelException;
|
||||||
public class FreemarkerHttpServlet extends VitroHttpServlet {
|
public class FreemarkerHttpServlet extends VitroHttpServlet {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private static final Log log = LogFactory.getLog(FreemarkerHttpServlet.class.getName());
|
private static final Log log = LogFactory.getLog(FreemarkerHttpServlet.class);
|
||||||
private static final int FILTER_SECURITY_LEVEL = LoginFormBean.EDITOR;
|
private static final int FILTER_SECURITY_LEVEL = LoginFormBean.EDITOR;
|
||||||
|
|
||||||
public void doGet( HttpServletRequest request, HttpServletResponse response )
|
public void doGet( HttpServletRequest request, HttpServletResponse response )
|
||||||
|
@ -376,26 +376,8 @@ public class FreemarkerHttpServlet extends VitroHttpServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected StringWriter mergeToTemplate(String templateName, Map<String, Object> map, Configuration config) {
|
protected StringWriter mergeToTemplate(String templateName, Map<String, Object> map, Configuration config) {
|
||||||
|
FreemarkerHelper helper = new FreemarkerHelper();
|
||||||
Template template = null;
|
return helper.mergeToTemplate(templateName, map, config);
|
||||||
try {
|
|
||||||
template = config.getTemplate(templateName);
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("Cannot get template " + templateName);
|
|
||||||
}
|
|
||||||
StringWriter sw = new StringWriter();
|
|
||||||
if (template != null) {
|
|
||||||
try {
|
|
||||||
template.process(map, sw);
|
|
||||||
} catch (TemplateException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sw;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String mergeBodyToTemplate(String templateName, Map<String, Object> map, Configuration config) {
|
protected String mergeBodyToTemplate(String templateName, Map<String, Object> map, Configuration config) {
|
||||||
|
|
|
@ -69,6 +69,9 @@ public class TestController extends FreemarkerHttpServlet {
|
||||||
body.put("zoo1", zoo1);
|
body.put("zoo1", zoo1);
|
||||||
body.put("zoo2", zoo2);
|
body.put("zoo2", zoo2);
|
||||||
|
|
||||||
|
body.put("trueStatement", true);
|
||||||
|
body.put("falseStatement", false);
|
||||||
|
|
||||||
getBerries(body);
|
getBerries(body);
|
||||||
|
|
||||||
body.put("bookTitle", "Pride and Prejudice");
|
body.put("bookTitle", "Pride and Prejudice");
|
||||||
|
|
|
@ -4,20 +4,22 @@ package edu.cornell.mannlib.vitro.webapp.web.directives;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.Iterator;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHelper;
|
||||||
import freemarker.core.Environment;
|
import freemarker.core.Environment;
|
||||||
import freemarker.template.Configuration;
|
import freemarker.template.Configuration;
|
||||||
|
import freemarker.template.SimpleDate;
|
||||||
import freemarker.template.SimpleScalar;
|
import freemarker.template.SimpleScalar;
|
||||||
|
import freemarker.template.SimpleSequence;
|
||||||
|
import freemarker.template.TemplateBooleanModel;
|
||||||
import freemarker.template.TemplateDirectiveBody;
|
import freemarker.template.TemplateDirectiveBody;
|
||||||
import freemarker.template.TemplateDirectiveModel;
|
import freemarker.template.TemplateDirectiveModel;
|
||||||
import freemarker.template.TemplateException;
|
import freemarker.template.TemplateException;
|
||||||
import freemarker.template.TemplateHashModel;
|
import freemarker.template.TemplateHashModel;
|
||||||
import freemarker.template.TemplateModel;
|
import freemarker.template.TemplateModel;
|
||||||
import freemarker.template.TemplateModelException;
|
import freemarker.template.TemplateModelException;
|
||||||
import freemarker.template.utility.DeepUnwrap;
|
|
||||||
|
|
||||||
public class DumpDirective implements TemplateDirectiveModel {
|
public class DumpDirective implements TemplateDirectiveModel {
|
||||||
|
|
||||||
|
@ -44,14 +46,44 @@ public class DumpDirective implements TemplateDirectiveModel {
|
||||||
|
|
||||||
TemplateHashModel dataModel = env.getDataModel();
|
TemplateHashModel dataModel = env.getDataModel();
|
||||||
TemplateModel val = dataModel.get(var);
|
TemplateModel val = dataModel.get(var);
|
||||||
String output = var + ": " + val.toString();
|
|
||||||
|
|
||||||
// RY Improve by making presentation of various types more nuanced
|
Configuration config = env.getConfiguration();
|
||||||
// Also merge to a template for formatting:
|
String templateName = "dump-var.ftl";
|
||||||
// get config from environment; get a template from config
|
String includeTemplate;
|
||||||
// merge as in FreeMarkerHttpServlet.mergeToTemplate()
|
Object value = val;
|
||||||
|
String type = null;
|
||||||
|
|
||||||
|
if (val instanceof SimpleScalar) {
|
||||||
|
includeTemplate = "dump-string.ftl";
|
||||||
|
type = "string";
|
||||||
|
} else if (val instanceof SimpleDate) {
|
||||||
|
includeTemplate = "dump-string.ftl";
|
||||||
|
value = value.toString();
|
||||||
|
type = "date";
|
||||||
|
} else if (val instanceof TemplateBooleanModel) {
|
||||||
|
includeTemplate = "dump-string.ftl";
|
||||||
|
value = ((TemplateBooleanModel) val).getAsBoolean() ? "true" : "false";
|
||||||
|
type = "boolean";
|
||||||
|
} else if (val instanceof SimpleSequence){
|
||||||
|
includeTemplate = "dump-array.ftl";
|
||||||
|
type = "array";
|
||||||
|
} else {
|
||||||
|
includeTemplate = "dump-string.ftl";
|
||||||
|
value = value.toString();
|
||||||
|
type = "object";
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Object> map = new HashMap<String, Object>();
|
||||||
|
map.put("var", var);
|
||||||
|
map.put("value", value);
|
||||||
|
map.put("includeTemplate", includeTemplate);
|
||||||
|
map.put("type", type);
|
||||||
|
|
||||||
|
FreemarkerHelper helper = new FreemarkerHelper();
|
||||||
|
String output = helper.mergeMapToTemplate(templateName, map, config);
|
||||||
|
|
||||||
Writer out = env.getOut();
|
Writer out = env.getOut();
|
||||||
out.write(output + "<br />");
|
out.write(output);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
9
webapp/web/css/dump.css
Normal file
9
webapp/web/css/dump.css
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
|
||||||
|
/* Styles for Freemarker template variable dump */
|
||||||
|
|
||||||
|
.dump {
|
||||||
|
border-top: 1px solid #ccc;
|
||||||
|
border-bottom: 1px solid #ccc;
|
||||||
|
padding: 5em 0;
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
|
||||||
|
|
||||||
|
<#-- Template for dumping string values -->
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<#list value as item>
|
||||||
|
<li>${item_index}: ${item}</li>
|
||||||
|
</#list>
|
||||||
|
</ul>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
|
||||||
|
|
||||||
|
<#-- Template for dumping string values -->
|
||||||
|
|
||||||
|
${value}
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
|
||||||
|
|
||||||
|
<#-- Template for dumping a template variable -->
|
||||||
|
|
||||||
|
<hr />
|
||||||
|
<div class="dump">
|
||||||
|
|
||||||
|
<h6>Freemarker template variable dump</h6>
|
||||||
|
|
||||||
|
<strong>Variable <em>${var}</em>:</strong><br/ >
|
||||||
|
<strong>Type:</strong> ${type}<br />
|
||||||
|
<strong>Value:</strong>
|
||||||
|
<#include includeTemplate />
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<hr />
|
|
@ -49,6 +49,8 @@
|
||||||
<@dump var="now" />
|
<@dump var="now" />
|
||||||
<@dump var="urls" />
|
<@dump var="urls" />
|
||||||
<@dump var="fruit" />
|
<@dump var="fruit" />
|
||||||
|
<@dump var="trueStatement" />
|
||||||
|
<@dump var="falseStatement" />
|
||||||
<@dumpDataModel />
|
<@dumpDataModel />
|
||||||
|
|
||||||
${stylesheets.addFromTheme("/sstest.css", "/sstest2.css")}
|
${stylesheets.addFromTheme("/sstest.css", "/sstest2.css")}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue