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 org.hwo.StringHelper; import org.hwo.text.LineReader; public class CSV { List records; private char separator; private char enclosedBy; private boolean ignoreDoubleSeparator; public CSV() { this.records = new LinkedList(); this.setSeparator(';'); this.setEnclosedBy('\"'); this.ignoreDoubleSeparator = false; } public boolean isIgnoreDoubleSeparator() { return ignoreDoubleSeparator; } public void setIgnoreDoubleSeparator(boolean ignoreDoubleSeparator) { this.ignoreDoubleSeparator = ignoreDoubleSeparator; } 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); records.clear(); while (br.ready()) { CSVRecord row = textToRow(br.readLine()); records.add(row); } } private Object textToObject(String text){ if (text.indexOf('.')>=0){ try { Double d = Double.parseDouble(text); return d; } catch (Exception ex2){ } } else { try { Integer i = Integer.parseInt(text); return i; } catch (Exception ex){ } } return text; } private CSVRecord textToRow(String line){ CSVRecord record = new CSVRecord(); LineReader reader = new LineReader(line); ArrayList cell = null; boolean enclosed = false; while (!reader.endOfLine()){ char ch = reader.read(); if ((this.separator == ch) && (!enclosed) ){ if (cell != null){ String celltext = StringHelper.fromCharacters(cell.toArray(new Character[0])); record.appendValue(textToObject(celltext)); } cell = new ArrayList(); } else if (this.enclosedBy == ch){ if (enclosed){ if (reader.peek() == ch){ cell.add(reader.read()); } else { enclosed = false; String celltext = StringHelper.fromCharacters(cell.toArray(new Character[0])); record.appendValue(celltext); cell = null; } } else { if (cell.size() == 0){ enclosed = true; } else { cell.add(ch); } } } else { if (cell == null){ cell = new ArrayList(); } cell.add(ch); } } if (cell != null){ String celltext = StringHelper.fromCharacters(cell.toArray(new Character[0])); record.appendValue(celltext); } return record; } /* 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 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 (CSVRecord record:this.records) { for (int i=0;i0) writer.write(separator); if (record.getValue(i) != null) { if (String.class.isInstance(record.getValue(i))) writer.write(enclosedBy); if (Float.class.isInstance(record.getValue(i))){ writer.write(String.format("%.4f", record.getValue(i))); } else { writer.write(record.getValue(i).toString()); } if (String.class.isInstance(record.getValue(i))) writer.write(enclosedBy); } } writer.write("\n"); } writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Object getValue(int row,int column) { try { return this.records.get(row).getValue(column).toString(); } catch (ArrayIndexOutOfBoundsException ex) { return null; } } public List getRecords() { return this.records; } public CSVRecord getRecord(int row){ if (row >= records.size()){ int size = records.size(); for (int i=0;i<=(row-size);i++){ records.add(new CSVRecord()); } } return records.get(row); } public List getRecordsAsArray(){ List list = new ArrayList(); for (CSVRecord record: getRecords()){ list.add(record.getValuesAsArray()); } return list; } 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; } }