From 69a559761a60fa9ebc9ed13eb47c15d9f117a07d Mon Sep 17 00:00:00 2001 From: erak1609 Date: Wed, 5 May 2021 16:47:18 +0300 Subject: [PATCH] Fix encoding problem when generating extent reports --- .../extentreports/io/BufferedWriterWriter.java | 12 ++++++++---- .../extentreports/reporter/AbstractFileReporter.java | 3 ++- .../extentreports/reporter/ExtentSparkReporter.java | 2 +- .../extentreports/templating/FreemarkerTemplate.java | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/aventstack/extentreports/io/BufferedWriterWriter.java b/src/main/java/com/aventstack/extentreports/io/BufferedWriterWriter.java index 5661df3..f3345de 100644 --- a/src/main/java/com/aventstack/extentreports/io/BufferedWriterWriter.java +++ b/src/main/java/com/aventstack/extentreports/io/BufferedWriterWriter.java @@ -2,7 +2,9 @@ import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; import java.util.logging.Level; import java.util.logging.Logger; @@ -20,10 +22,12 @@ private WriterInstance() { private BufferedWriterWriter() { } - public synchronized void write(final File f, String text) { - try (BufferedWriter writer = new BufferedWriter(new FileWriter(f))) { + public synchronized void write(final File f, String text, String encoding) { + try (BufferedWriter writer = new BufferedWriter( + new OutputStreamWriter( + new FileOutputStream(f), encoding))) { writer.write(text); - } catch (Exception e) { + } catch (IOException e) { logger.log(Level.SEVERE, f.getPath(), e); } } diff --git a/src/main/java/com/aventstack/extentreports/reporter/AbstractFileReporter.java b/src/main/java/com/aventstack/extentreports/reporter/AbstractFileReporter.java index 8f6d48d..953e84e 100644 --- a/src/main/java/com/aventstack/extentreports/reporter/AbstractFileReporter.java +++ b/src/main/java/com/aventstack/extentreports/reporter/AbstractFileReporter.java @@ -69,8 +69,9 @@ protected void loadTemplateModel() { } } - protected void processTemplate(Template template, File outputFile) throws TemplateException, IOException { + protected void processTemplate(Template template, File outputFile, String encoding) throws TemplateException, IOException { FreemarkerTemplate freemarkerTemplate = new FreemarkerTemplate(getFreemarkerConfig()); + freemarkerConfig.setDefaultEncoding(encoding); freemarkerTemplate.writeTemplate(template, templateModel, outputFile); } diff --git a/src/main/java/com/aventstack/extentreports/reporter/ExtentSparkReporter.java b/src/main/java/com/aventstack/extentreports/reporter/ExtentSparkReporter.java index 8f60dba..1f2d402 100644 --- a/src/main/java/com/aventstack/extentreports/reporter/ExtentSparkReporter.java +++ b/src/main/java/com/aventstack/extentreports/reporter/ExtentSparkReporter.java @@ -146,7 +146,7 @@ private void flush(ReportEntity value) { createFreemarkerConfig(TEMPLATE_LOCATION, ENCODING); final String filePath = getFileNameAsExt(FILE_NAME, new String[]{".html", ".htm"}); final Template template = getFreemarkerConfig().getTemplate(SPA_TEMPLATE_NAME); - processTemplate(template, new File(filePath)); + processTemplate(template, new File(filePath), conf.getEncoding()); return; } catch (IOException | TemplateException e) { disposable.dispose(); diff --git a/src/main/java/com/aventstack/extentreports/templating/FreemarkerTemplate.java b/src/main/java/com/aventstack/extentreports/templating/FreemarkerTemplate.java index 218fb83..4afa2c1 100644 --- a/src/main/java/com/aventstack/extentreports/templating/FreemarkerTemplate.java +++ b/src/main/java/com/aventstack/extentreports/templating/FreemarkerTemplate.java @@ -43,7 +43,7 @@ public String getSource(Template template, Map templateMap) thro public void writeTemplate(Template template, Map templateMap, File outputFile) throws TemplateException, IOException { String source = getSource(template, templateMap); - BufferedWriterWriter.getInstance().write(outputFile, source); + BufferedWriterWriter.getInstance().write(outputFile, source, freemarkerConfig.getDefaultEncoding()); } private String processTemplate(Template template, Map templateMap)