Skip to content

Commit 49528dc

Browse files
committed
U revert changes but keep support for both approaches (with and without bom)
1 parent 04cebe4 commit 49528dc

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

src/main/java/io/github/fvarrui/javapackager/utils/VelocityUtils.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import static org.apache.commons.io.FileUtils.writeStringToFile;
44

5-
import java.io.*;
5+
import java.io.File;
6+
import java.io.IOException;
67
import java.util.ArrayList;
78
import java.util.UUID;
89

9-
import org.apache.commons.lang3.StringUtils;
1010
import org.apache.velocity.Template;
1111
import org.apache.velocity.VelocityContext;
1212
import org.apache.velocity.app.VelocityEngine;
@@ -30,14 +30,14 @@ private static VelocityEngine getVelocityEngine() {
3030
velocityEngine = new VelocityEngine();
3131

3232
// specify resource loaders to use
33-
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file,class");
33+
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADERS, "file,class");
3434

3535
// for the loader 'file', set the FileResourceLoader as the class to use and use 'assets' directory for templates
36-
velocityEngine.setProperty("file.resource.loader.class", FileResourceLoader.class.getName());
37-
velocityEngine.setProperty("file.resource.loader.path", assetsDir.getAbsolutePath());
36+
velocityEngine.setProperty("resource.loader.file.class", FileResourceLoader.class.getName());
37+
velocityEngine.setProperty("resource.loader.file.path", assetsDir.getAbsolutePath());
3838

3939
// for the loader 'class', set the ClasspathResourceLoader as the class to use
40-
velocityEngine.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
40+
velocityEngine.setProperty("resource.loader.class.class", ClasspathResourceLoader.class.getName());
4141

4242
velocityEngine.init();
4343

@@ -63,25 +63,16 @@ public static void setAssetsDir(File assetsDir) {
6363
}
6464

6565
public static void render(String templatePath, File output, Object info) throws Exception {
66-
try {
67-
String data = render(templatePath, info);
68-
data = data.replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
69-
writeString(output,data);
70-
} catch (IOException e) {
71-
throw new Exception(e.getMessage(), e);
72-
}
66+
render(templatePath, output, info, false);
7367
}
74-
75-
public static void writeString(File output,String data) throws Exception{
76-
if(!output.exists()){
77-
output.getParentFile().mkdirs();
68+
69+
public static void render(String templatePath, File output, Object info, boolean includeBom) throws Exception {
70+
String data = render(templatePath, info);
71+
if (!includeBom) {
72+
writeStringToFile(output, StringUtils.dosToUnix(data), "UTF-8");
73+
} else {
74+
FileUtils.writeStringToFileWithBOM(output, data);
7875
}
79-
FileOutputStream fileOutputStream = new FileOutputStream(output);
80-
// write utf-8 BOM
81-
byte[] uft8bom={(byte)0xef,(byte)0xbb,(byte)0xbf};
82-
fileOutputStream.write(uft8bom);
83-
fileOutputStream.write(data.getBytes());
84-
fileOutputStream.close();
8576
}
8677

8778
}

0 commit comments

Comments
 (0)