diff --git a/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/JavaGenerator.java b/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/JavaGenerator.java index c4cce37..4781c79 100644 --- a/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/JavaGenerator.java +++ b/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/JavaGenerator.java @@ -373,6 +373,15 @@ else if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLO .append(chunk.getKey()) .append(";") .append(CRLF); + } + else if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS) { + tab(w, indent).append("static private final byte[] ") + .append(chunk.getKey()) + .append(" = ") + .append(RockerUtil.getTextAsJavaByteArrayInitializer(chunk.getValue(),model.getOptions().getTargetCharset())) + .append(";") + .append(CRLF); + } } diff --git a/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/PlainTextStrategy.java b/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/PlainTextStrategy.java index 55cacbe..28e0e05 100644 --- a/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/PlainTextStrategy.java +++ b/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/PlainTextStrategy.java @@ -23,10 +23,13 @@ public enum PlainTextStrategy { // as strings (chunked to get around java length limits) STATIC_STRINGS, - + // as byte arrays (loaded at runtime via an unloaded class to prevent // both the string constant in the class file + the byte array from // using heap/permgen memory) - STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS + STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS, + + // as byte arrays (chunked to get around java length limits) + STATIC_BYTE_ARRAYS } diff --git a/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/RockerUtil.java b/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/RockerUtil.java index bd17efc..77d0dbe 100644 --- a/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/RockerUtil.java +++ b/rocker-compiler/src/main/java/com/fizzed/rocker/compiler/RockerUtil.java @@ -15,14 +15,10 @@ */ package com.fizzed.rocker.compiler; -import com.fizzed.rocker.ContentType; -import com.fizzed.rocker.model.JavaVersion; -import com.fizzed.rocker.model.TemplateModel; -//import org.apache.commons.lang3.text.translate.*; - import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -35,11 +31,18 @@ import java.util.List; import java.util.Set; import java.util.regex.Pattern; +import java.util.stream.Collectors; + //import javax.xml.bind.DatatypeConverter; import org.apache.commons.lang3.text.translate.CharSequenceTranslator; import org.apache.commons.lang3.text.translate.EntityArrays; import org.apache.commons.lang3.text.translate.LookupTranslator; +import com.fizzed.rocker.ContentType; +import com.fizzed.rocker.model.JavaVersion; +import com.fizzed.rocker.model.TemplateModel; +//import org.apache.commons.lang3.text.translate.*; + public class RockerUtil { private static final Pattern VALID_JAVA_IDENTIFIER = Pattern @@ -237,6 +240,24 @@ static public List stringIntoChunks(String s, int chunkSize) { return strings; } + + public static String getTextAsJavaByteArrayInitializer(String text,String charsetName) throws UnsupportedEncodingException{ + + byte[] bytes = text.getBytes(charsetName); + StringBuilder s = new StringBuilder(); + s.append("new byte[] { "); + boolean first = true; + for (byte b:bytes){ + if (!first){ + s.append(", "); + } + appendByteAsJavaByteInitializer(s, b); + first = false; + } + s.append(" }"); + return s.toString(); + + } static public List getTextAsJavaByteArrayInitializer(String text, String charsetName, int maxArraySize) throws UnsupportedEncodingException { byte[] bytes = text.getBytes(charsetName); diff --git a/rocker-maven-plugin/src/main/java/com/fizzed/rocker/maven/GenerateMojo.java b/rocker-maven-plugin/src/main/java/com/fizzed/rocker/maven/GenerateMojo.java index a98e76c..c4b8aed 100644 --- a/rocker-maven-plugin/src/main/java/com/fizzed/rocker/maven/GenerateMojo.java +++ b/rocker-maven-plugin/src/main/java/com/fizzed/rocker/maven/GenerateMojo.java @@ -1,6 +1,7 @@ package com.fizzed.rocker.maven; import com.fizzed.rocker.compiler.JavaGeneratorRunnable; +import com.fizzed.rocker.compiler.PlainTextStrategy; import com.fizzed.rocker.model.JavaVersion; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -88,6 +89,9 @@ public class GenerateMojo extends AbstractMojo { @Parameter( property = "rocker.postProcessing", required = false) protected String[] postProcessing; + @Parameter(property =" rocker.plainTextStrategy", required=false, defaultValue = "STATIC_BYTE_ARRAYS_VIA_UNLOADED_CLASS") + protected PlainTextStrategy plainTextStrategy; + /** * Weather or not to mark the generated classes as {@code @Generated}. * */ @@ -133,6 +137,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { jgr.getParser().getConfiguration().setTemplateDirectory(templateDirectory); jgr.getGenerator().getConfiguration().setOutputDirectory(outputDirectory); jgr.getGenerator().getConfiguration().setClassDirectory(classDirectory); + jgr.getGenerator().setPlainTextStrategy(plainTextStrategy); //jgr.getGenerator().getConfiguration().setCompileDirectory(compileDirectory); jgr.setFailOnError(failOnError);