Multiple user referentials working with IPA.
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / CsvWriter.java
index ba5b3c710ebd928ee5645041cdc3aa53550e5a72..c3b3a3ad7b43afa5989871fcaf0f223b1259a763 100644 (file)
@@ -5,6 +5,7 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
+import java.nio.charset.Charset;
 import java.util.Iterator;
 import java.util.List;
 
@@ -18,9 +19,12 @@ public class CsvWriter {
        /**
         * Creates a CSV writer.
         * 
-        * @param out
-        *            the stream to write to. Caller is responsible for closing it.
+        * @param out the stream to write to. Caller is responsible for closing it.
+        * 
+        * @deprecated Use {@link #CsvWriter(OutputStream, Charset)} instead.
+        * 
         */
+       @Deprecated
        public CsvWriter(OutputStream out) {
                this.out = new OutputStreamWriter(out);
        }
@@ -28,48 +32,68 @@ public class CsvWriter {
        /**
         * Creates a CSV writer.
         * 
-        * @param out
-        *            the stream to write to. Caller is responsible for closing it.
+        * @param out      the stream to write to. Caller is responsible for closing it.
+        * @param encoding the encoding to use.
+        * 
+        * @deprecated Use {@link #CsvWriter(OutputStream, Charset)} instead.
         */
+       @Deprecated
        public CsvWriter(OutputStream out, String encoding) {
                try {
                        this.out = new OutputStreamWriter(out, encoding);
                } catch (UnsupportedEncodingException e) {
-                       throw new UtilsException("Cannot initialize CSV writer", e);
+                       throw new IllegalArgumentException(e);
                }
        }
 
        /**
-        * 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.
+        * Creates a CSV writer.
+        * 
+        * @param out     the stream to write to. Caller is responsible for closing it.
+        * @param charset the charset to use
+        */
+       public CsvWriter(OutputStream out, Charset charset) {
+               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.
         */
        public void writeLine(List<?> tokens) {
                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);
                        }
                        out.write('\n');
                        out.flush();
                } catch (IOException e) {
-                       throw new UtilsException("Could not write " + tokens, e);
+                       throw new RuntimeException("Could not write " + tokens, e);
                }
        }
 
        /**
-        * 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.
+        * 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.
         */
        public void writeLine(Object[] tokens) {
                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());
                                }
@@ -79,11 +103,16 @@ public class CsvWriter {
                        out.write('\n');
                        out.flush();
                } catch (IOException e) {
-                       throw new UtilsException("Could not write " + tokens, e);
+                       throw new RuntimeException("Could not write " + tokens, e);
                }
        }
 
        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