Primitive first draft of dump directives for FreeMarker templates.
This commit is contained in:
parent
b767a81f76
commit
72a6658116
4 changed files with 88 additions and 39 deletions
|
@ -139,6 +139,8 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
|
|||
setThemeInfo(themeDir);
|
||||
setScriptAndStylesheetObjects(themeDir);
|
||||
|
||||
setSharedVariable("dump", new edu.cornell.mannlib.vitro.webapp.web.directives.DumpDirective());
|
||||
setSharedVariable("dumpDataModel", new edu.cornell.mannlib.vitro.webapp.web.directives.DumpDataModelDirective());
|
||||
}
|
||||
|
||||
private void setScriptAndStylesheetObjects(String themeDir) {
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
package edu.cornell.mannlib.vitro.webapp.web.directives;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import freemarker.core.Environment;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.SimpleScalar;
|
||||
import freemarker.template.TemplateDirectiveBody;
|
||||
import freemarker.template.TemplateDirectiveModel;
|
||||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateHashModel;
|
||||
import freemarker.template.TemplateModel;
|
||||
import freemarker.template.TemplateModelException;
|
||||
import freemarker.template.utility.DeepUnwrap;
|
||||
|
||||
public class DumpDataModelDirective implements TemplateDirectiveModel {
|
||||
|
||||
@SuppressWarnings({ "unchecked", "deprecation" })
|
||||
@Override
|
||||
public void execute(Environment env, Map params, TemplateModel[] loopVars,
|
||||
TemplateDirectiveBody body) throws TemplateException, IOException {
|
||||
|
||||
if (params.size() != 0) {
|
||||
throw new TemplateModelException(
|
||||
"The dump directive doesn't allow parameters.");
|
||||
}
|
||||
if (loopVars.length != 0) {
|
||||
throw new TemplateModelException(
|
||||
"The dump directive doesn't allow loop variables.");
|
||||
}
|
||||
if (body != null) {
|
||||
throw new TemplateModelException(
|
||||
"The dump directive doesn't allow nested content.");
|
||||
}
|
||||
|
||||
TemplateHashModel dataModel = env.getDataModel();
|
||||
String output = "Data model: ";
|
||||
|
||||
Map<String, Object> dm = (Map<String, Object>) DeepUnwrap.permissiveUnwrap(dataModel);
|
||||
Set varNames = dm.keySet();
|
||||
for (Object varName : varNames) {
|
||||
output += (String) varName + ", ";
|
||||
}
|
||||
|
||||
// Add shared variables
|
||||
Configuration config = env.getConfiguration();
|
||||
Set sharedVars = config.getSharedVariableNames();
|
||||
Iterator i = sharedVars.iterator();
|
||||
while (i.hasNext()) {
|
||||
String sv = (String) i.next();
|
||||
TemplateModel tm = config.getSharedVariable(sv);
|
||||
if (tm instanceof TemplateDirectiveModel ||
|
||||
// Legacy built-ins that are added to all configurations
|
||||
tm instanceof freemarker.template.utility.CaptureOutput ||
|
||||
tm instanceof freemarker.template.utility.StandardCompress ||
|
||||
tm instanceof freemarker.template.utility.HtmlEscape ||
|
||||
tm instanceof freemarker.template.utility.NormalizeNewlines ||
|
||||
tm instanceof freemarker.template.utility.XmlEscape) {
|
||||
continue;
|
||||
}
|
||||
output += sv + ", ";
|
||||
}
|
||||
|
||||
output = output.replaceAll(", $", ".");
|
||||
|
||||
|
||||
// RY Improve by making presentation of various types more nuanced
|
||||
// Also merge to a template for formatting
|
||||
// get config from environment; get a template from config
|
||||
// merge as in FreeMarkerHttpServlet.mergeToTemplate()
|
||||
Writer out = env.getOut();
|
||||
out.write(output + "<br />");
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -41,41 +41,8 @@ public class DumpDirective implements TemplateDirectiveModel {
|
|||
String var = ((SimpleScalar)o).getAsString();
|
||||
|
||||
TemplateHashModel dataModel = env.getDataModel();
|
||||
String output;
|
||||
|
||||
if (var.equals("data_model")) {
|
||||
output = "Data model: ";
|
||||
Map<String, Object> dm = (Map<String, Object>) DeepUnwrap.permissiveUnwrap(dataModel);
|
||||
Set varNames = dm.keySet();
|
||||
for (Object varName : varNames) {
|
||||
output += (String) varName + ", ";
|
||||
}
|
||||
|
||||
// Add shared variables
|
||||
Configuration config = env.getConfiguration();
|
||||
Set sharedVars = config.getSharedVariableNames();
|
||||
Iterator i = sharedVars.iterator();
|
||||
while (i.hasNext()) {
|
||||
String sv = (String) i.next();
|
||||
TemplateModel tm = config.getSharedVariable(sv);
|
||||
if (tm instanceof TemplateDirectiveModel ||
|
||||
// Legacy built-ins that are added to all configurations
|
||||
tm instanceof freemarker.template.utility.CaptureOutput ||
|
||||
tm instanceof freemarker.template.utility.StandardCompress ||
|
||||
tm instanceof freemarker.template.utility.HtmlEscape ||
|
||||
tm instanceof freemarker.template.utility.NormalizeNewlines ||
|
||||
tm instanceof freemarker.template.utility.XmlEscape) {
|
||||
continue;
|
||||
}
|
||||
output += sv + ", ";
|
||||
}
|
||||
|
||||
output = output.replaceAll(", $", ".");
|
||||
|
||||
} else {
|
||||
TemplateModel val = dataModel.get(var);
|
||||
output = var + ": " + val.toString();
|
||||
}
|
||||
String output = var + ": " + val.toString();
|
||||
|
||||
// RY Improve by making presentation of various types more nuanced
|
||||
// Also merge to a template for formatting
|
||||
|
|
|
@ -25,6 +25,6 @@
|
|||
|
||||
<p><strong>Animal:</strong> ${animal}</p>
|
||||
|
||||
<@dump var="fruit" />
|
||||
<@dump var="now" />
|
||||
<@dump var="urls" />
|
||||
<@dump var="data_model" />
|
||||
<@dump var="fruit" />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue