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

feat: add UTF-8 with BOM support for CSV exported files #90

Merged
merged 2 commits into from
Feb 27, 2024
Merged
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.vaadin.addons.flowingcode</groupId>
<artifactId>grid-exporter-addon</artifactId>
<version>2.2.2-SNAPSHOT</version>
<version>2.3.0-SNAPSHOT</version>
<name>Grid Exporter Add-on</name>
<description>Grid Exporter Add-on for Vaadin Flow</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.OutputStreamWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -72,7 +73,14 @@ public InputStream createInputStream() {
PipedOutputStream out = new PipedOutputStream(in);
new Thread(
() -> {
try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(out))) {
try (
OutputStreamWriter os = new OutputStreamWriter(out, exporter.getCsvCharset());
CSVWriter writer = new CSVWriter(os)) {
if (StandardCharsets.UTF_8.equals(exporter.getCsvCharset())) {
// write BOM
os.write(0xfeff);
}

writer.writeNext(headers);
writer.writeAll(data);
if (footers.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.Comparator;
Expand Down Expand Up @@ -102,6 +103,8 @@ public class GridExporter<T> implements Serializable {

private List<FooterToolbarItem> footerToolbarItems;

private SerializableSupplier<Charset> csvCharset;

private GridExporter(Grid<T> grid) {
this.grid = grid;
}
Expand Down Expand Up @@ -554,4 +557,17 @@ public void setFooterToolbarItems(List<FooterToolbarItem> footerToolbarItems) {
this.footerToolbarItems = footerToolbarItems;
}

/**
* Charset to use when exporting the CSV file.
*
* @return CSV file charset or default one.
*/
public Charset getCsvCharset() {
return csvCharset == null ? Charset.defaultCharset() : csvCharset.get();
}

public void setCsvCharset(SerializableSupplier<Charset> charset) {
this.csvCharset = charset;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.vaadin.flow.router.Route;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
Expand Down Expand Up @@ -81,6 +82,7 @@ public GridExporterDemo() throws EncryptedDocumentException, IOException {
exporter.setTitle("People information");
exporter.setFileName(
"GridExport" + new SimpleDateFormat("yyyyddMM").format(Calendar.getInstance().getTime()));
exporter.setCsvCharset(() -> StandardCharsets.UTF_8);

TextField filterField = new TextField();
filterField.setPlaceholder("Filter by");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -46,7 +47,9 @@ private void testSerializationOf(Object obj) throws IOException, ClassNotFoundEx
public void testSerialization() throws ClassNotFoundException, IOException {
try {
Grid<String> grid = new Grid<>();
testSerializationOf(GridExporter.createFor(grid));
GridExporter<String> exporter = GridExporter.createFor(grid);
exporter.setCsvCharset(() -> StandardCharsets.ISO_8859_1);
testSerializationOf(exporter);
} catch (Exception e) {
Assert.fail("Problem while testing serialization: " + e.getMessage());
}
Expand Down