1 package org
.argeo
.cms
.util
;
3 import java
.io
.IOException
;
4 import java
.io
.OutputStream
;
5 import java
.io
.OutputStreamWriter
;
6 import java
.io
.UnsupportedEncodingException
;
8 import java
.nio
.charset
.Charset
;
9 import java
.util
.Iterator
;
10 import java
.util
.List
;
12 /** Write in CSV format. */
13 public class CsvWriter
{
14 private final Writer out
;
16 private char separator
= ',';
17 private char quote
= '\"';
20 * Creates a CSV writer.
22 * @param out the stream to write to. Caller is responsible for closing it.
24 * @deprecated Use {@link #CsvWriter(OutputStream, Charset)} instead.
28 public CsvWriter(OutputStream out
) {
29 this.out
= new OutputStreamWriter(out
);
33 * Creates a CSV writer.
35 * @param out the stream to write to. Caller is responsible for closing it.
36 * @param encoding the encoding to use.
38 * @deprecated Use {@link #CsvWriter(OutputStream, Charset)} instead.
41 public CsvWriter(OutputStream out
, String encoding
) {
43 this.out
= new OutputStreamWriter(out
, encoding
);
44 } catch (UnsupportedEncodingException e
) {
45 throw new IllegalArgumentException(e
);
50 * Creates a CSV writer.
52 * @param out the stream to write to. Caller is responsible for closing it.
53 * @param charset the charset to use
55 public CsvWriter(OutputStream out
, Charset charset
) {
56 this.out
= new OutputStreamWriter(out
, charset
);
60 * Creates a CSV writer.
62 * @param out the stream to write to. Caller is responsible for closing it.
64 public CsvWriter(Writer writer
) {
69 * Write a CSV line. Also used to write a header if needed (this is transparent
70 * for the CSV writer): simply call it first, before writing the lines.
72 public void writeLine(List
<?
> tokens
) {
74 Iterator
<?
> it
= tokens
.iterator();
75 while (it
.hasNext()) {
76 Object obj
= it
.next();
77 writeToken(obj
!= null ? obj
.toString() : null);
83 } catch (IOException e
) {
84 throw new RuntimeException("Could not write " + tokens
, e
);
89 * Write a CSV line. Also used to write a header if needed (this is transparent
90 * for the CSV writer): simply call it first, before writing the lines.
92 public void writeLine(Object
... tokens
) {
94 for (int i
= 0; i
< tokens
.length
; i
++) {
95 if (tokens
[i
] == null) {
98 writeToken(tokens
[i
].toString());
100 if (i
!= (tokens
.length
- 1))
101 out
.write(separator
);
105 } catch (IOException e
) {
106 throw new RuntimeException("Could not write " + tokens
, e
);
110 protected void writeToken(String token
) throws IOException
{
112 // TODO configure how to deal with null
116 // +2 for possible quotes, another +2 assuming there would be an already
117 // quoted string where quotes needs to be duplicated
118 // another +2 for safety
119 // we don't want to increase buffer size while writing
120 StringBuffer buf
= new StringBuffer(token
.length() + 6);
121 char[] arr
= token
.toCharArray();
122 boolean shouldQuote
= false;
141 if (shouldQuote
== true)
143 out
.write(buf
.toString());
144 if (shouldQuote
== true)
148 public void setSeparator(char separator
) {
149 this.separator
= separator
;
152 public void setQuote(char quote
) {