Skip to content
Other versions

Loading…

TemplateUtils

Utility class with static helper methods for processing FreeMarker templates and performing simple variable substitution. Templates are loaded from the OpenKM home directory (Config.HOME_DIR). All methods are static.

Description:

Method Return values Description
getConfig() Configuration Returns the singleton FreeMarker Configuration object, initializing it on first call. The configuration loads templates from Config.HOME_DIR.

Description:

Method Return values Description
templateExists(String name) boolean Returns true if a FreeMarker template with the given name exists in Config.HOME_DIR.
  • name: The file name of the template relative to Config.HOME_DIR.

Example:

package com.openkm;
import com.openkm.util.TemplateUtils;
public class Test {
public static void main(String[] args) {
try {
System.out.println("Template exists: " + TemplateUtils.templateExists("OpenKM - NOTIFICATION"));
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
replace(String name, String template, Map<String, Object> model) String Processes a FreeMarker template string using the given model and returns the rendered result. name is used only as an identifier for error messages.
  • name: A label used to identify the template in error messages.
  • template: The FreeMarker template content as a string.
  • model: A map of variable names to values for substitution.

Description:

Method Return values Description
replace(String name, InputStream input, Map<String, Object> model, OutputStream out) void Reads a FreeMarker template from input, processes it with the given model, and writes the rendered result to out.
  • name: A label used to identify the template in error messages.
  • input: An InputStream containing the FreeMarker template.
  • model: A map of variable names to values for substitution.
  • out: The OutputStream to write the rendered output to.

Description:

Method Return values Description
replace(String template, Map<String, String> model) String Replaces all occurrences of ${key} in the template string with the corresponding value from model. This method does not use FreeMarker — it is a simple literal string substitution. Values are trimmed before substitution.
  • template: A string containing ${key} placeholders.
  • model: A map of key names to replacement values.

Example:

package com.openkm;
import com.openkm.core.Config;
import com.openkm.util.TemplateUtils;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
try {
// FreeMarker replace
Map<String, Object> model = new HashMap<>();
model.put("fileIn", "/tmp/input.xls");
String cmd = TemplateUtils.replace("SYSTEM_XLS2CSV", Config.SYSTEM_CATDOC_XLS2CSV, model);
System.out.println("Command: " + cmd);
// Simple string substitution
Map<String, String> vars = new HashMap<>();
vars.put("user", "jsmith");
vars.put("doc", "report.pdf");
String msg = TemplateUtils.replace("Hello ${user}, your document ${doc} is ready.", vars);
System.out.println(msg); // "Hello jsmith, your document report.pdf is ready."
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
toVar(String id) String Wraps the given identifier in ${...} to produce a template variable reference. For example, toVar("user") returns "${user}".

Description:

Method Return values Description
useMailTemplate(String body) String Wraps the given HTML body in the built-in OpenKM mail template (/tpl/mail.ftlh) and returns the fully rendered HTML. Throws IOException on template processing errors.
  • body: The HTML content to embed in the mail template.

Description:

Method Return values Description
useNotesTemplate(String docName, List notes) String Renders the built-in OpenKM notes template (/tpl/notes.ftlh) with the given document name and list of notes. Returns the rendered HTML. Throws IOException on template processing errors.
  • docName: The name of the document whose notes are being rendered.
  • notes: A list of NodeNote objects, each with author, date, and text fields.

Example:

package com.openkm;
import com.openkm.util.TemplateUtils;
import com.openkm.util.TemplateUtils.NodeNote;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
try {
// Wrap HTML content in the mail template
String html = TemplateUtils.useMailTemplate("<p>Your document has been approved.</p>");
System.out.println(html);
// Render notes
List<NodeNote> notes = new ArrayList<>();
NodeNote n = new NodeNote();
n.setAuthor("jsmith");
n.setDate("2024-03-15");
n.setText("Reviewed and approved.");
notes.add(n);
String notesHtml = TemplateUtils.useNotesTemplate("report.pdf", notes);
System.out.println(notesHtml);
} catch (Exception e) {
e.printStackTrace();
}
}
}