X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2FCsvWriter.java;h=c3b3a3ad7b43afa5989871fcaf0f223b1259a763;hb=3c1cdc594d954520b14646102b366290bdad58c7;hp=41ea65dd4903a88af162c640475160929d2c5210;hpb=1ceeaa36a22d5bcb9ad3c577e2e729b1ff31ae2c;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/CsvWriter.java b/org.argeo.util/src/org/argeo/util/CsvWriter.java index 41ea65dd4..c3b3a3ad7 100644 --- a/org.argeo.util/src/org/argeo/util/CsvWriter.java +++ b/org.argeo.util/src/org/argeo/util/CsvWriter.java @@ -56,6 +56,15 @@ public class CsvWriter { this.out = new OutputStreamWriter(out, charset); } + /** + * Creates a CSV writer. + * + * @param out the stream to write to. Caller is responsible for closing it. + */ + public CsvWriter(Writer writer) { + this.out = writer; + } + /** * Write a CSV line. Also used to write a header if needed (this is transparent * for the CSV writer): simply call it first, before writing the lines. @@ -64,7 +73,8 @@ public class CsvWriter { try { Iterator it = tokens.iterator(); while (it.hasNext()) { - writeToken(it.next().toString()); + Object obj = it.next(); + writeToken(obj != null ? obj.toString() : null); if (it.hasNext()) out.write(separator); } @@ -83,8 +93,7 @@ public class CsvWriter { try { for (int i = 0; i < tokens.length; i++) { if (tokens[i] == null) { - // TODO configure how to deal with null - writeToken(""); + writeToken(null); } else { writeToken(tokens[i].toString()); } @@ -99,6 +108,11 @@ public class CsvWriter { } protected void writeToken(String token) throws IOException { + if (token == null) { + // TODO configure how to deal with null + out.write(""); + return; + } // +2 for possible quotes, another +2 assuming there would be an already // quoted string where quotes needs to be duplicated // another +2 for safety