X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=basic%2Fruntime%2Forg.argeo.basic.nodeps%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Futil%2FCsvWriter.java;h=b3f383883970a09ebee6feb624ae6f60cc4f216a;hb=e39d1cffbca5b2a6d5d7b6e20e7d75417a71c9b8;hp=19086d613a8dafca5e6509bd1b27cc9486cb1572;hpb=7215a19b3ee12587e9d8298c9afd576bf087b017;p=lgpl%2Fargeo-commons.git diff --git a/basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/CsvWriter.java b/basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/CsvWriter.java index 19086d613..b3f383883 100644 --- a/basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/CsvWriter.java +++ b/basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/CsvWriter.java @@ -1,8 +1,25 @@ +/* + * Copyright (C) 2007-2012 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.argeo.util; import java.io.IOException; import java.io.OutputStream; -import java.io.PrintWriter; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.io.Writer; import java.util.Iterator; import java.util.List; @@ -10,21 +27,33 @@ import org.argeo.ArgeoException; /** Write in CSV format. */ public class CsvWriter { - private final PrintWriter out; + private final Writer out; private char separator = ','; private char quote = '\"'; /** - * Creates a CSV writer. The header will be written immediately to the - * stream. + * Creates a CSV writer. * * @param out * the stream to write to. Caller is responsible for closing it. */ public CsvWriter(OutputStream out) { - super(); - this.out = new PrintWriter(out); + this.out = new OutputStreamWriter(out); + } + + /** + * Creates a CSV writer. + * + * @param out + * the stream to write to. Caller is responsible for closing it. + */ + public CsvWriter(OutputStream out, String encoding) { + try { + this.out = new OutputStreamWriter(out, encoding); + } catch (UnsupportedEncodingException e) { + throw new ArgeoException("Cannot initialize CSV writer", e); + } } /** @@ -38,9 +67,33 @@ public class CsvWriter { while (it.hasNext()) { writeToken(it.next().toString()); if (it.hasNext()) - out.print(separator); + out.write(separator); } - out.print('\n'); + out.write('\n'); + out.flush(); + } catch (IOException e) { + throw new ArgeoException("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. + */ + 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(""); + } else { + writeToken(tokens[i].toString()); + } + if (i != (tokens.length - 1)) + out.write(separator); + } + out.write('\n'); out.flush(); } catch (IOException e) { throw new ArgeoException("Could not write " + tokens, e); @@ -51,6 +104,7 @@ public class CsvWriter { // +2 for possible quotes, another +2 assuming there would be an already // quoted string where quotes needs to be duplicated // another +2 for safety + // we don't want to increase buffer size while writing StringBuffer buf = new StringBuffer(token.length() + 6); char[] arr = token.toCharArray(); boolean shouldQuote = false; @@ -73,9 +127,18 @@ public class CsvWriter { } if (shouldQuote == true) - out.print(quote); - out.print(buf.toString()); + out.write(quote); + out.write(buf.toString()); if (shouldQuote == true) - out.print(quote); + out.write(quote); + } + + public void setSeparator(char separator) { + this.separator = separator; } + + public void setQuote(char quote) { + this.quote = quote; + } + }