Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Groovy TemplateEngines with large (>64k) template

Tags:

groovy

How can I use SimpleTemplateEngine or GStringTemplateEngine to process a template larger than 65535 characters?

I receive the following error:

groovy.lang.GroovyRuntimeException: Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed: SimpleTemplateScript1.groovy: 5614: String too long. The given string is 198495 Unicode code units long, but only a maximum of 65535 is allowed.

I'm building the template using the following code:

def templateEngine = new SimpleTemplateEngine()
def binding = [:]
templateEngine
    .createTemplate(new FileReader("input.txt))
    .make(binding)
    .writeTo(new FileWriter(new File("output.txt")))

I found JIRA 3487 related to this issue: GStringTemplateEngine fails to work with >64K strings .

I've thought about chunking the input but that brings it's own complications like making sure not to break in the middle of an expression.

I would appreciate any other suggestions.

like image 664
James Allman Avatar asked Oct 02 '22 13:10

James Allman


2 Answers

Found some replacements for GStringTemplateEngine These can can handle large Strings.

https://github.com/mbjarland/groovy-streaming-template-engine

https://github.com/mbknor/gt-engine

like image 79
sumnulu Avatar answered Oct 05 '22 02:10

sumnulu


Try using a GStringTemplateEngine instead. From the javadoc:

Processes template source files substituting variables and expressions into placeholders in a template source text to produce the desired output using a streaming approach. This engine has equivalent functionality to the SimpleTemplateEngine but creates the template using writable closures making it potentially more scalable for large templates or in streaming scenarios.

You will probably get away with simply replacing new SimpleTemplateEngine() with new GStringTemplateEngine(), but should of course test it.

like image 26
Steinar Avatar answered Oct 05 '22 03:10

Steinar