Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plainTextStrategy on maven & additional strategy #180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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