/*
* Copyright (C) 2010 takaji
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
//package dakside.csv;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* CSV file writer
*
* @author takaji
*
*/
public final class CSVFileWriter {
protected CSVFormat format;
protected final BufferedWriter writer;
int row = 1;
//
public CSVFileWriter(Writer writer) {
this.writer = new BufferedWriter(writer);
this.format = new CSVFormat();
}
public CSVFileWriter(Writer writer, CSVFormat format)
throws FileNotFoundException {
this.writer = new BufferedWriter(writer);
this.format = format;
}
public CSVFileWriter(BufferedWriter writer) {
this.writer = writer;
this.format = new CSVFormat();
}
public CSVFileWriter(BufferedWriter writer, CSVFormat format)
throws FileNotFoundException {
this.writer = writer;
this.format = format;
}
public CSVFileWriter(String filename) {
try {
this.writer = new BufferedWriter(new FileWriter(new File(filename)));
this.format = new CSVFormat();
} catch (IOException ex) {
Logger.getLogger(CSVFileWriter.class.getName()).log(Level.SEVERE, null, ex);
throw new CSVException(ex);
}
}
public CSVFileWriter(File f) {
try {
this.writer = new BufferedWriter(new FileWriter(f));
this.format = new CSVFormat();
} catch (IOException ex) {
Logger.getLogger(CSVFileWriter.class.getName()).log(Level.SEVERE, null, ex);
throw new CSVException(ex);
}
}
public CSVFileWriter(OutputStream input) {
writer = new BufferedWriter(new OutputStreamWriter(input));
this.format = new CSVFormat();
}
public CSVFileWriter(String filename, CSVFormat format) {
try {
this.writer = new BufferedWriter(new FileWriter(filename));
this.format = format;
} catch (IOException ex) {
Logger.getLogger(CSVFileWriter.class.getName()).log(Level.SEVERE, null, ex);
throw new CSVException(ex);
}
}
public CSVFileWriter(File f, CSVFormat format) throws IOException {
this.writer = new BufferedWriter(new FileWriter(f));
this.format = format;
}
public CSVFileWriter(OutputStream input, CSVFormat format) {
writer = new BufferedWriter(new OutputStreamWriter(input));
this.format = format;
}
//
/**
* Write a line to CSV
*
* @throws IOException
*/
public CSVFileWriter writeLine(CSVLine line) throws CSVException {
if (line == null) {
return this;
}
synchronized (this) {
// if current line index > 1
// terminate previous line first
if (row > 1) {
carriageReturn();
}
if (!line.isEmpty()) {
// try to write each cell in line
try {
Object[] elements = line.getElements();
for (int i = 0; i < elements.length; i++) {
// not first element -> write field delimiter
if (i > 0) {
writer.append(format.getFieldDelimiter());
}
// write cell content
Object cell = elements[i];
if (cell != null) {
writer.append(escapeString(cell.toString()));
} else {
writer.append("");
}
row++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
throw new CSVException(e);
}
} else {
row++;
}
}
return this;
}
/**
* Return carriage
*
* @throws IOException
*/
public CSVFileWriter carriageReturn() throws CSVException {
try {
synchronized (writer) {
writer.append(format.getLineTerminator());
}
} catch (IOException e) {
throw new CSVException("Error while returning carriage.", e);
}
return this;
}
/**
* Write a file to underline CSV file
*
* @param file
*/
public void writeFile(CSVFile file) throws CSVException {
if (file == null) {
// no need to write
return;
}
CSVLine[] lines = file.getLines();
synchronized (this) {
for (int i = 0; i < lines.length; i++) {
CSVLine line = lines[i];
writeLine(line);
}
}
}
/**
* escape a string with text delimiter if needed
*
* @param str
* @return
*/
private String escapeString(String str) {
if (str.indexOf(format.getTextDelimiter()) >= 0
|| str.indexOf(format.getLineTerminator()) >= 0
|| str.indexOf(format.getFieldDelimiter()) >= 0) {
return format.getTextDelimiter()
+ str.replace("" + format.getTextDelimiter(), ""
+ format.getTextDelimiter()
+ format.getTextDelimiter())
+ format.getTextDelimiter();
} else {
// no need to escape
return str;
}
}
/**
* close session
*/
public void close() {
try {
flush();
} catch (CSVException ex) {
//do nothing
}
try {
//auto flush
if (writer != null) {
writer.close();
}
} catch (Exception ex) {
Logger.getLogger(CSVFileWriter.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Flush buffer to underline stream
*/
public void flush() {
try {
synchronized (writer) {
writer.flush();
}
} catch (Exception ex) {
Logger.getLogger(CSVFileWriter.class.getName()).log(Level.SEVERE, null, ex);
throw new CSVException("Cannot flush", ex);
}
}
}
class CSVFile {
private ArrayList lines; //lines inside a CSV file
/**
* Default constructor
*/
public CSVFile() {
this.lines = new ArrayList();
}
public CSVLine[] getLines() {
return this.lines.toArray(new CSVLine[0]);
}
/**
* Get line at a specified index
* @param idx
* @return
* @throws IndexOutOfBoundsException
*/
public CSVLine getLine(int idx) throws IndexOutOfBoundsException{
return this.lines.get(idx);
}
/**
* Add a new line to current csv
* @return new line object
*/
public CSVLine newLine() {
CSVLine line = new CSVLine();
this.lines.add(line);
return line;
}
/**
* get number of line
* @return number of line
*/
public int size() {
return this.lines.size();
}
/**
* discard a line at the specified index
* @param idx
*/
public void discard(int idx) {
if (idx >= 0 && idx < size()) {
this.lines.remove(idx);
}
}
/**
* discard empty record
*/
public void discardEmpty() {
for (int i = this.lines.size() - 1; i > -1; i--) {
if (this.lines.get(i) == null || this.lines.get(i).isEmpty()) {
discard(i);
}
}
}
/**
* append a CSV line to file (line is ignored if null)
* @param line
*/
void append(CSVLine line) {
if (line == null) {
return;
}
this.lines.add(line);
}
}
class CSVException extends RuntimeException {
private static final long serialVersionUID = 1L;
public CSVException() {
}
public CSVException(Throwable throwable) {
super(throwable);
}
public CSVException(String message) {
super(message);
}
public CSVException(String message, Throwable throwable) {
super(message, throwable);
}
}
class CSVLine {
private ArrayList