[#4517] Allow to configure the output file encoding in the code generator

This commit is contained in:
lukaseder 2016-02-11 14:47:13 +01:00
parent dcbb7035ee
commit cc85f94b2a
7 changed files with 45 additions and 4 deletions

View File

@ -84,6 +84,7 @@ abstract class AbstractGenerator implements Generator {
boolean generateTableValuedFunctions = false;
protected GeneratorStrategyWrapper strategy;
protected String targetEncoding = "UTF-8";
final Language language;
AbstractGenerator(Language language) {
@ -448,6 +449,16 @@ abstract class AbstractGenerator implements Generator {
return strategy.getTargetPackage();
}
@Override
public String getTargetEncoding() {
return targetEncoding;
}
@Override
public void setTargetEncoding(String encoding) {
this.targetEncoding = encoding;
}
/**
* If file is a directory, recursively empty its children.
* If file is a file, delete it.

View File

@ -409,9 +409,12 @@ public class GenerationTool {
g.getTarget().setPackageName("org.jooq.generated");
if (StringUtils.isBlank(g.getTarget().getDirectory()))
g.getTarget().setDirectory("target/generated-sources/jooq");
if (StringUtils.isBlank(g.getTarget().getEncoding()))
g.getTarget().setEncoding("UTF-8");
generator.setTargetPackage(g.getTarget().getPackageName());
generator.setTargetDirectory(g.getTarget().getDirectory());
generator.setTargetEncoding(g.getTarget().getEncoding());
// [#1394] The <generate/> element should be optional
if (g.getGenerate() == null)

View File

@ -389,6 +389,16 @@ public interface Generator {
*/
void setTargetDirectory(String directory);
/**
* The target encoding
*/
String getTargetEncoding();
/**
* Initialise the target encoding
*/
void setTargetEncoding(String encoding);
/**
* @return Get the target package for the current configuration
*/

View File

@ -80,15 +80,21 @@ public abstract class GeneratorWriter<W extends GeneratorWriter<W>> {
private final File file;
private final String encoding;
private final StringBuilder sb;
private int indentTabs;
private String tabString = " ";
private boolean newline = true;
protected GeneratorWriter(File file) {
this(file, null);
}
protected GeneratorWriter(File file, String encoding) {
file.getParentFile().mkdirs();
this.file = file;
this.encoding = encoding;
this.sb = new StringBuilder();
}
@ -259,7 +265,7 @@ public abstract class GeneratorWriter<W extends GeneratorWriter<W>> {
old = new RandomAccessFile(file, "r");
byte[] oldBytes = new byte[(int) old.length()];
old.readFully(oldBytes);
oldContent = new String(oldBytes, "UTF-8");
oldContent = new String(oldBytes, encoding());
}
finally {
if (old != null)
@ -268,7 +274,7 @@ public abstract class GeneratorWriter<W extends GeneratorWriter<W>> {
}
if (oldContent == null || !oldContent.equals(newContent)) {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), encoding()));
writer.append(newContent);
writer.flush();
@ -283,6 +289,10 @@ public abstract class GeneratorWriter<W extends GeneratorWriter<W>> {
}
}
protected String encoding() {
return encoding != null ? encoding : "UTF-8";
}
protected String beforeClose(String string) {
return string;
}

View File

@ -5127,7 +5127,7 @@ public class JavaGenerator extends AbstractGenerator {
if (scala)
file = new File(file.getParentFile(), file.getName().replace(".java", ".scala"));
return new JavaWriter(file, generateFullyQualifiedTypes());
return new JavaWriter(file, generateFullyQualifiedTypes(), targetEncoding);
}
// [#4626] Users may need to call this method

View File

@ -37,7 +37,11 @@ public class JavaWriter extends GeneratorWriter<JavaWriter> {
private final Pattern PLAIN_GENERIC_TYPE_PATTERN = Pattern.compile("[<\\[]((?:[\\p{L}_$][\\p{L}\\p{N}_$]*\\.)*[\\p{L}_$][\\p{L}\\p{N}_$]*)[>\\]]");
public JavaWriter(File file, String fullyQualifiedTypes) {
super(file);
this(file, fullyQualifiedTypes, null);
}
public JavaWriter(File file, String fullyQualifiedTypes, String encoding) {
super(file, encoding);
this.className = file.getName().replaceAll("\\.(java|scala)$", "");
this.isJava = file.getName().endsWith(".java");

View File

@ -855,6 +855,9 @@
<!-- The destination directory of your generated classes -->
<element name="directory" type="string" default="target/generated-sources/jooq" />
<!-- The file encoding to be used with all output files. -->
<element name="encoding" type="string" default="UTF-8"/>
</all>
</complexType>