Improve CSV and tabular
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 4 Oct 2011 17:10:56 +0000 (17:10 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 4 Oct 2011 17:10:56 +0000 (17:10 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@4796 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/CsvWriter.java
basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/tabular/CsvTabularWriter.java
basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/tabular/TabularColumn.java
basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/tabular/TabularWriter.java

index e7afe952b1fdf27f1b3a04793b35c04701002bff..85356e4fed07c4f24e9f980698a983c85d1a17d9 100644 (file)
@@ -47,6 +47,25 @@ public class CsvWriter {
                }
        }
 
+       /**
+        * 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++) {
+                               writeToken(tokens[i].toString());
+                               if (i != (tokens.length - 1))
+                                       out.print(separator);
+                       }
+                       out.print('\n');
+                       out.flush();
+               } catch (IOException e) {
+                       throw new ArgeoException("Could not write " + tokens, e);
+               }
+       }
+
        protected void writeToken(String token) throws IOException {
                // +2 for possible quotes, another +2 assuming there would be an already
                // quoted string where quotes needs to be duplicated
index 20a275a442e5cd5917bffeabcc472141dddbdf81..a16c8b2ad7ebc5964afe604279c57ec17e00a8e9 100644 (file)
@@ -1,7 +1,6 @@
 package org.argeo.util.tabular;
 
 import java.io.OutputStream;
-import java.util.List;
 
 import org.argeo.util.CsvWriter;
 
@@ -13,7 +12,7 @@ public class CsvTabularWriter implements TabularWriter {
                this.csvWriter = new CsvWriter(out);
        }
 
-       public void appendRow(List<?> row) {
+       public void appendRow(Object[] row) {
                csvWriter.writeLine(row);
        }
 
index 4a7abf7298247b752b0771c0e7b4f747593f3991..ab3b74ba48f94e885b979a6e4f8c23631d1ecd9d 100644 (file)
@@ -10,6 +10,12 @@ public class TabularColumn {
         */
        private Integer type;
 
+       /** column with default type */
+       public TabularColumn(String name) {
+               super();
+               this.name = name;
+       }
+
        public TabularColumn(String name, Integer type) {
                super();
                this.name = name;
index ab493592baf3c375ca0ac557804173685b06d72c..dc911c029d598a566df9286592e9fbe7147802f5 100644 (file)
@@ -1,11 +1,10 @@
 package org.argeo.util.tabular;
 
-import java.util.List;
 
 /** Write to a tabular content */
 public interface TabularWriter {
        /** Append a new row of data */
-       public void appendRow(List<?> row);
+       public void appendRow(Object[] row);
 
        /** Finish persisting data and release resources */
        public void close();