forked from LupusNobilis/java-org.hwo
236 lines
4.4 KiB
Java
236 lines
4.4 KiB
Java
package org.hwo.csv;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStreamWriter;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.util.ArrayList;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.Vector;
|
|
|
|
import javassist.bytecode.EnclosingMethodAttribute;
|
|
|
|
public class CSV {
|
|
|
|
List<String[]> records;
|
|
|
|
private char separator;
|
|
private char enclosedBy;
|
|
|
|
boolean isEnclosed;
|
|
List<String> parserRecord;
|
|
List<Character> parserField;
|
|
char lastChar;
|
|
|
|
public CSV()
|
|
{
|
|
this.records = new LinkedList<String[]>();
|
|
this.setSeparator(';');
|
|
this.setEnclosedBy('\"');
|
|
|
|
this.parserRecord = new ArrayList<String>();
|
|
this.parserField = new ArrayList<Character>();
|
|
}
|
|
|
|
private void nextRecord()
|
|
{
|
|
nextField();
|
|
|
|
this.records.add(parserRecord.toArray(new String[]{}));
|
|
parserRecord.clear();
|
|
}
|
|
|
|
private void nextField()
|
|
{
|
|
char fieldChars[] = new char[ parserField.size() ];
|
|
|
|
for (int i=0;i<parserField.size(); i++)
|
|
fieldChars[i] = parserField.get(i);
|
|
|
|
parserRecord.add(String.valueOf(fieldChars));
|
|
parserField.clear();
|
|
}
|
|
|
|
private void feeder(char ch)
|
|
{
|
|
if (ch == getEnclosedBy())
|
|
{
|
|
isEnclosed = !isEnclosed;
|
|
|
|
if (isEnclosed && lastChar == getEnclosedBy())
|
|
parserField.add(getEnclosedBy());
|
|
|
|
} else
|
|
{
|
|
if (isEnclosed)
|
|
parserField.add(ch);
|
|
else
|
|
{
|
|
if (ch == '\r')
|
|
{
|
|
// Ignore \r
|
|
} else if (ch == '\n')
|
|
nextRecord();
|
|
else if (ch == getSeparator())
|
|
nextField();
|
|
else
|
|
parserField.add(ch);
|
|
}
|
|
};
|
|
|
|
lastChar = ch;
|
|
}
|
|
|
|
public void readFromFile(String filename)
|
|
{
|
|
try {
|
|
readFromStream(new FileInputStream(filename));
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void readFromStream(InputStream is) throws IOException
|
|
{
|
|
this.readFromStream(new InputStreamReader(is,"ISO-8859-1"));
|
|
}
|
|
|
|
public void readFromStream(InputStreamReader isr) throws IOException
|
|
{
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
isEnclosed = false;
|
|
records.clear();
|
|
parserField.clear();
|
|
parserRecord.clear();
|
|
|
|
while (br.ready())
|
|
feeder((char)br.read());
|
|
}
|
|
|
|
|
|
public void saveToFile(String filename)
|
|
{
|
|
saveToFile(new File(filename));
|
|
}
|
|
|
|
public void saveToFile(File output)
|
|
{
|
|
try {
|
|
FileOutputStream fos = new FileOutputStream(output,false);
|
|
saveToStream(fos);
|
|
} catch (FileNotFoundException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void saveToStream(OutputStream output)
|
|
{
|
|
OutputStreamWriter osw;
|
|
try {
|
|
osw = new OutputStreamWriter(output,"UTF8");
|
|
saveToStream(osw);
|
|
} catch (UnsupportedEncodingException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void saveToStream(OutputStreamWriter output)
|
|
{
|
|
BufferedWriter writer = new BufferedWriter(output);
|
|
|
|
try {
|
|
|
|
for (String[] record:this.records)
|
|
{
|
|
for (int i=0;i<record.length;i++)
|
|
{
|
|
if (i>0)
|
|
writer.write(separator);
|
|
|
|
if (record[i] != null)
|
|
{
|
|
if (String.class.isInstance(record[i]))
|
|
writer.write(enclosedBy);
|
|
|
|
writer.write(record[i]);
|
|
|
|
if (String.class.isInstance(record[i]))
|
|
writer.write(enclosedBy);
|
|
}
|
|
}
|
|
writer.write("\n");
|
|
}
|
|
|
|
writer.close();
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public String getValue(int row,int column)
|
|
{
|
|
try
|
|
{
|
|
return this.records.get(row)[column];
|
|
} catch (ArrayIndexOutOfBoundsException ex)
|
|
{
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public List<String[]> getCells()
|
|
{
|
|
return this.records;
|
|
}
|
|
|
|
public List<Object[]> getCellsAsObjects()
|
|
{
|
|
List<Object[]> ol = new ArrayList<Object[]>();
|
|
|
|
for (String[] record:records)
|
|
{
|
|
Object[] ro = new Object[record.length];
|
|
for (int i=0;i<ro.length;i++)
|
|
ro[i] = record[i];
|
|
ol.add(ro);
|
|
}
|
|
|
|
return ol;
|
|
}
|
|
|
|
public char getSeparator() {
|
|
return separator;
|
|
}
|
|
|
|
public void setSeparator(char separator) {
|
|
this.separator = separator;
|
|
}
|
|
|
|
public char getEnclosedBy() {
|
|
return enclosedBy;
|
|
}
|
|
|
|
public void setEnclosedBy(char enclosedBy) {
|
|
this.enclosedBy = enclosedBy;
|
|
}
|
|
|
|
|
|
}
|