Skip to content

Commit

Permalink
Merge pull request #180 from ppedregal/master
Browse files Browse the repository at this point in the history
plainTextStrategy on maven & additional strategy
  • Loading branch information
jjlauer authored Nov 6, 2024
2 parents 1f31c99 + d567260 commit 55e63c7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -237,6 +240,24 @@ static public List<String> 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<String> getTextAsJavaByteArrayInitializer(String text, String charsetName, int maxArraySize) throws UnsupportedEncodingException {
byte[] bytes = text.getBytes(charsetName);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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}.
* */
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 55e63c7

Please sign in to comment.