001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.util;
032:
033: import java.io.File;
034: import java.io.FileOutputStream;
035: import java.io.IOException;
036: import java.io.OutputStreamWriter;
037:
038: /**
039: * helper class to write table data to a csv-file (comma separated values).
040: * the first line in file is a list of fieldnames, all following lines
041: * are data lines.
042: * a descptiontion of file format can be found on: http://www.wotsit.org/
043: * usage: create a object using the constructor. call writeHeader
044: * for writing the filename header then add data with writeData.
045: * at the end close() closes the file.
046: *
047: *@author jeberle@users
048: */
049: public class CSVWriter {
050:
051: private String newline = System.getProperty("line.separator");
052: private OutputStreamWriter writer = null;
053: private int nbrCols = 0;
054: private int nbrRows = 0;
055:
056: /**
057: * constructor.
058: * creates a csv file for writing data to it
059: * @param file the file to write data to
060: * @param encoding encoding to use or null (=defualt)
061: */
062: public CSVWriter(File file, String encoding) throws IOException {
063:
064: if (encoding == null) {
065: encoding = System.getProperty("file.encoding");
066: }
067:
068: FileOutputStream fout = new FileOutputStream(file);
069:
070: writer = new OutputStreamWriter(fout, encoding);
071: }
072:
073: /**
074: * writes the csv header (fieldnames). should be called after
075: * construction one time.
076: * @param header String[] with fieldnames
077: */
078: public void writeHeader(String[] header) throws IOException {
079:
080: this .nbrCols = header.length;
081:
082: doWriteData(header);
083: }
084:
085: /**
086: * writes a data-record to the file. note that data[] must have
087: * same number of elements as the header had.
088: *
089: * @param data data to write to csv-file
090: */
091: public void writeData(String[] data) throws IOException {
092: doWriteData(data);
093: }
094:
095: /**
096: * closes the csv file.
097: */
098: public void close() throws IOException {
099: this .writer.close();
100: }
101:
102: private void doWriteData(String[] values) throws IOException {
103:
104: for (int i = 0; i < values.length; i++) {
105: if (i > 0) {
106: this .writer.write(";");
107: }
108:
109: if (values[i] != null) {
110: this .writer.write("\"");
111: this .writer.write(this .toCsvValue(values[i]));
112: this .writer.write("\"");
113: }
114: }
115:
116: this .writer.write(newline);
117:
118: this .nbrRows++;
119: }
120:
121: private String toCsvValue(String str) {
122:
123: StringBuffer sb = new StringBuffer();
124:
125: for (int i = 0; i < str.length(); i++) {
126: char c = str.charAt(i);
127:
128: sb.append(c);
129:
130: switch (c) {
131:
132: case '"':
133: sb.append('"');
134: break;
135: }
136: }
137:
138: return sb.toString();
139: }
140: }
|