CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-groovy--groovy-templates

Template engines for Apache Groovy including SimpleTemplateEngine, StreamingTemplateEngine, XmlTemplateEngine, and MarkupTemplateEngine

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Apache Groovy Templates

Apache Groovy Templates provides a comprehensive template framework that enables dynamic text generation through multiple template engines. It includes SimpleTemplateEngine for basic JSP-style templates, StreamingTemplateEngine for handling large templates, GStringTemplateEngine for closure-based templating, XmlTemplateEngine for XML template processing, and MarkupTemplateEngine as an optimized solution for markup generation.

Package Information

  • Package Name: org.apache.groovy:groovy-templates
  • Package Type: Maven
  • Language: Java/Groovy
  • Installation: implementation 'org.apache.groovy:groovy-templates:5.0.0' (Gradle) or <dependency><groupId>org.apache.groovy</groupId><artifactId>groovy-templates</artifactId><version>5.0.0</version></dependency> (Maven)

Core Imports

import groovy.text.*;
import groovy.text.markup.*;

Common imports for specific engines:

import groovy.text.SimpleTemplateEngine;
import groovy.text.StreamingTemplateEngine;
import groovy.text.GStringTemplateEngine;
import groovy.text.XmlTemplateEngine;
import groovy.text.markup.MarkupTemplateEngine;
import groovy.text.markup.TemplateConfiguration;
import groovy.text.markup.BaseTemplate;
import groovy.text.markup.TemplateResolver;
import groovy.text.markup.DelegatingIndentWriter;

Exception handling imports:

import groovy.text.TemplateExecutionException;
import groovy.text.TemplateParseException;

Groovy core imports commonly needed:

import groovy.lang.Writable;
import groovy.lang.Closure;
import groovy.util.XmlParser;
import groovy.lang.GroovyShell;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.CompilationFailedException;

Basic Usage

import groovy.text.SimpleTemplateEngine;
import groovy.text.Template;
import groovy.lang.Writable;
import java.util.Map;
import java.util.HashMap;

// Create a simple template engine
SimpleTemplateEngine engine = new SimpleTemplateEngine();

// Template with JSP-style syntax
String templateText = """
Dear <%= firstname %> $lastname,

We <% if (accepted) print 'are pleased' else print 'regret' %> 
to inform you that your paper entitled
'$title' was ${ accepted ? 'accepted' : 'rejected' }.

The conference committee.
""";

// Create template from string
Template template = engine.createTemplate(templateText);

// Create binding data
Map<String, Object> binding = new HashMap<>();
binding.put("firstname", "Grace");
binding.put("lastname", "Hopper");
binding.put("accepted", true);
binding.put("title", "Groovy for COBOL programmers");

// Generate output
Writable result = template.make(binding);
System.out.println(result.toString());

Architecture

Apache Groovy Templates is built around several key components:

  • TemplateEngine: Abstract base class providing factory methods for creating templates from various sources
  • Template: Interface representing compiled templates that can be bound to data
  • Template Engines: Five specialized implementations optimized for different use cases
  • Exception Handling: Comprehensive error reporting with line number information for debugging
  • Configuration System: Advanced configuration options for the MarkupTemplateEngine

Capabilities

Simple Template Engine

Basic JSP-style template engine supporting <% %> scriptlets and <%= %> expressions, ideal for simple templating scenarios.

public class SimpleTemplateEngine extends TemplateEngine {
    public SimpleTemplateEngine();
    public SimpleTemplateEngine(boolean verbose);
    public SimpleTemplateEngine(ClassLoader parentLoader);
    public SimpleTemplateEngine(GroovyShell groovyShell);
    
    public void setVerbose(boolean verbose);
    public boolean isVerbose();
    public void setEscapeBackslash(boolean escapeBackslash);
    public boolean isEscapeBackslash();
}

Simple Templates

Streaming Template Engine

Closure-based template engine optimized for large templates (>64k characters) with enhanced error reporting and scalable performance.

public class StreamingTemplateEngine extends TemplateEngine {
    public StreamingTemplateEngine();
    public StreamingTemplateEngine(ClassLoader parentLoader);
}

Streaming Templates

GString Template Engine

Template engine using GString closures for streaming scenarios, providing efficient template storage and execution.

public class GStringTemplateEngine extends TemplateEngine {
    public GStringTemplateEngine();
    public GStringTemplateEngine(ClassLoader parentLoader);
}

GString Templates

XML Template Engine

Specialized template engine for XML templates with <gsp:scriptlet> and <gsp:expression> support, including automatic XML escaping and indentation.

public class XmlTemplateEngine extends TemplateEngine {
    public static final String DEFAULT_INDENTATION = "  ";
    
    public XmlTemplateEngine() throws SAXException, ParserConfigurationException;
    public XmlTemplateEngine(String indentation, boolean validating) throws SAXException, ParserConfigurationException;
    public XmlTemplateEngine(XmlParser xmlParser, ClassLoader parentLoader);
    public XmlTemplateEngine(XmlParser xmlParser, GroovyShell groovyShell);
    
    public String getIndentation();
    public void setIndentation(String indentation);
    public void setConfigurePrinter(Closure configurePrinter);
}

XML Templates

Markup Template Engine

Advanced template engine leveraging StreamingMarkupBuilder for optimized XML/XHTML generation with compile-time type checking and extensive configuration options.

public class MarkupTemplateEngine extends TemplateEngine {
    public MarkupTemplateEngine();
    public MarkupTemplateEngine(TemplateConfiguration config);
    public MarkupTemplateEngine(ClassLoader parentLoader, TemplateConfiguration config);
    public MarkupTemplateEngine(ClassLoader parentLoader, TemplateConfiguration config, TemplateResolver resolver);
    public MarkupTemplateEngine(ClassLoader parentLoader, File templateDirectory, TemplateConfiguration tplConfig);
    
    public Template createTemplate(Reader reader, String sourceName) throws CompilationFailedException, ClassNotFoundException, IOException;
    public Template createTemplateByPath(String templatePath) throws CompilationFailedException, ClassNotFoundException, IOException;
    
    // Type-checked template methods
    public Template createTypeCheckedModelTemplate(String source, Map<String, String> modelTypes) throws CompilationFailedException, ClassNotFoundException, IOException;
    public Template createTypeCheckedModelTemplate(String source, String sourceName, Map<String, String> modelTypes) throws CompilationFailedException, ClassNotFoundException, IOException;
    public Template createTypeCheckedModelTemplate(Reader reader, Map<String, String> modelTypes) throws CompilationFailedException, ClassNotFoundException, IOException;
    public Template createTypeCheckedModelTemplate(Reader reader, String sourceName, Map<String, String> modelTypes) throws CompilationFailedException, ClassNotFoundException, IOException;
    public Template createTypeCheckedModelTemplateByPath(String templatePath, Map<String, String> modelTypes) throws CompilationFailedException, ClassNotFoundException, IOException;
    public Template createTypeCheckedModelTemplate(URL resource, Map<String, String> modelTypes) throws CompilationFailedException, ClassNotFoundException, IOException;
    
    // Utility methods
    public GroovyClassLoader getTemplateLoader();
    public CompilerConfiguration getCompilerConfiguration();
    public TemplateConfiguration getTemplateConfiguration();
    public URL resolveTemplate(String templatePath) throws IOException;
}

Markup Templates

Core Interfaces

Template Engine Base Class

Abstract base class providing template creation methods from various sources.

public abstract class TemplateEngine {
    public abstract Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException;
    public Template createTemplate(String templateText) throws CompilationFailedException, ClassNotFoundException, IOException;
    public Template createTemplate(File file) throws CompilationFailedException, ClassNotFoundException, IOException;
    public Template createTemplate(File file, Charset cs) throws CompilationFailedException, ClassNotFoundException, IOException;
    public Template createTemplate(URL url) throws CompilationFailedException, ClassNotFoundException, IOException;
    public Template createTemplate(URL url, Charset cs) throws CompilationFailedException, ClassNotFoundException, IOException;
}

Template Interface

Interface for compiled templates that can be bound to data and rendered.

public interface Template {
    Writable make();
    Writable make(Map binding);
}

Exception Handling

Template Execution Exception

Exception thrown during template execution with line number information.

public class TemplateExecutionException extends Exception {
    public TemplateExecutionException(int lineNumber);
    public TemplateExecutionException(int lineNumber, String message);
    public TemplateExecutionException(int lineNumber, String message, Throwable cause);
    public TemplateExecutionException(int lineNumber, Throwable cause);
    
    public int getLineNumber();
}

Template Parse Exception

Runtime exception thrown during template parsing with detailed location information.

public class TemplateParseException extends RuntimeException {
    public TemplateParseException(int lineNumber, int column);
    public TemplateParseException(String message, int lineNumber, int column);
    public TemplateParseException(String message, Throwable cause, int lineNumber, int column);
    public TemplateParseException(Throwable t, int lineNumber, int column);
    
    public int getLineNumber();
    public int getColumn();
}
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.apache.groovy/groovy-templates@5.0.x
Publish Source
CLI
Badge
tessl/maven-org-apache-groovy--groovy-templates badge