Multiple user referentials working with IPA.
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / CsvWriter.java
index 41ea65dd4903a88af162c640475160929d2c5210..c3b3a3ad7b43afa5989871fcaf0f223b1259a763 100644 (file)
@@ -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