commit 4aa81b24ae1219f6de6e0f944b07900bf6d8fce4 Author: Tilman Date: Wed Oct 26 08:38:22 2011 +0200 Initial commit diff --git a/Testdaten.txt b/Testdaten.txt new file mode 100644 index 0000000..679e504 --- /dev/null +++ b/Testdaten.txt @@ -0,0 +1,12 @@ +"""Always forgive your enemies; nothing annoys them so much.""", Oscar Wilde +"Zweite Zeile", völlig in, Ordnung +"Escaped Chars\"", "in Quoted Fields \\", und \, außerhalb + Spaces , und Tabs +#Kommentarzeile, gefolgt von leerer Zeile + +"#feld mit", "#Kommentarzeichen", #in den Feldern +"field", "with +CR inside" +test1, te"st2, test3 +#"fehlerhafte", "Zeile +#"gefolgt von", "korrekter Zeile" diff --git a/Testdaten2.txt b/Testdaten2.txt new file mode 100644 index 0000000..2372abb --- /dev/null +++ b/Testdaten2.txt @@ -0,0 +1,11 @@ +"Buchungsdatum";"Valuta";"Betrag";"Waehrung";"Empfaengername1";"Empfaengername2";"Primanota";"BLZ Empfaenger";"Kontonummer Empfaenger";"Verwendungszweck 1";"Verwendungszweck 2" +"27.07.05";"27.07.05";-29,00;"EUR";"KARTENSERVICE";"";"999010";"10050000";"5231240003";"VISA NR. 4555787019929887";"ABRECHNUNG VOM 22.07" +"29.07.05";"29.07.05";-51,13;"EUR";"BHW BAUSPARKASSE HAMELN";"";"999010";"25410200";"471862300";"BHW 0471862 3 00 K CBA-05";"" +"01.08.05";"01.08.05";200,00;"EUR";"BAEUERLE-WALTHER,ERIKA";"";"999101";"10050000";"1320184258";"UNTERHALT";"" +"01.08.05";"01.08.05";115,00;"EUR";"WALTHER-STRATMANN,SIEGMUND";"";"853001";"10050000";"710150091";"UNTERHALT AUGUST 2005";"" +"01.08.05";"31.07.05";-20,00;"EUR";"";"";"954011";"10050000";"5310031253";"GA NR00001253 BLZ10050000 9";"31.07/09.23UHR FIL. 125" +"02.08.05";"02.08.05";-13,20;"EUR";"CALL A PIZZA, BERLIN";"";"999011";"37010050";"555619501";"EC 68005963 30.07 19.56 ME9";"" +"02.08.05";"02.08.05";-25,32;"EUR";"VODAFONE D2";"";"999010";"30050000";"6444111";"000010753003 000265200517 0";"RECHNUNGSNR 103397270879" + +"gebuchter Saldo";"02.08.05";1574,22;"EUR" +"alter Saldo";"19.07.05";1397,87;"EUR" diff --git a/ToDo.txt b/ToDo.txt new file mode 100644 index 0000000..6e89ac4 --- /dev/null +++ b/ToDo.txt @@ -0,0 +1,3 @@ +Direktes Verarbeiten braucht Funktionalität eines Pull-Parsers: + while ((row = parser.getNextRow()) != null) useRow; +Weiterverarbeitung ins TableModel etc. wird von Wrappe-Klasse übernommen \ No newline at end of file diff --git a/de/tilman/csvparser/CsvFile.java b/de/tilman/csvparser/CsvFile.java new file mode 100644 index 0000000..d4d644b --- /dev/null +++ b/de/tilman/csvparser/CsvFile.java @@ -0,0 +1,150 @@ +package de.tilman.csvparser; + + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Iterator; +import java.util.LinkedList; + +/** + * Repräsentiert eine CSV-Datei + * + * @author Tilman Walther + */ +public class CsvFile { + + // TODO english comments + + /* TODO andere Ausgabeformate. + * http://www.ricebridge.com/products/csvman/api/com/ricebridge/csvman/CsvManager.html + * The CsvManager class provides the following ways to load and save CSV data: + * # As a List of String[] arrays - load(File), save(File,List) + * # As a List of Lists of Strings - loadAsLists(File), saveAsLists(File,List) + * # As a ResultSet - loadResultSet(File,boolean), save(File,ResultSet,boolean) + * # As a TableModel - loadTableModel(File,boolean), save(File,TableModel,boolean) + * # From callback interfaces - LineListener, FieldListener, LineProvider + */ + + CsvFileProperties properties; + CsvParser parser; + + File csvFile; + int lineCounter = 0; + + String[] header = null; // enthält die Tabellendefinition als Folge von Strings + LinkedList data; // enthält die Zeilen der Tabelle als String-Arrays + + /** + * Der Konstruktor für den "default mode". Die CSV-Datei wird geöffnet und eingelesen, dabei + * werden folgende Annahmen gemacht: + * + * + * @param csvFile the CSV file + * @throws IOException + * @throws CsvParseException + */ + public CsvFile(File csvFile) throws IOException, CsvParseException { + properties = new CsvFileProperties(); + //parser = new CsvLineParser(properties); + this.csvFile = csvFile; + readData(); + } + + public CsvFile(File csvFile, CsvFileProperties properties) throws IOException, CsvParseException { + this.properties = properties; + //parser = new CsvLineParser(properties); + this.csvFile = csvFile; + readData(); + } + + /** + * Gibt die Daten aus der CSV-Datei zurück. Die LinkedList enthält die + * Zeilen der Daten-Tabelle als String-Arrays. + * @return the data of the csv file + */ + public LinkedList getData() { + return data; + } + + /** + * Gibt die Spaltendefinition (d.h. die erste Zeile der Daten-Tabelle) zurück. + * Falls die CSV-Datei laut CsvProperties keinen Header hat, wird null zurückgegeben. + * @return the definition of the data table columns + */ + public String[] getHeader() { + return header; + } + + /** + * Liest die Daten aus der CSV-Datei + * @throws CsvParseException + * @throws Exception + */ + protected void readData() throws IOException, CsvParseException { + + BufferedReader file; + + try { + file = new BufferedReader(new InputStreamReader(new FileInputStream(csvFile))); + } + catch (FileNotFoundException fnfe) { + try { + file = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(csvFile.getAbsolutePath()))); + } + catch (NullPointerException npe) { + throw new IOException("Die Datei '"+csvFile+"' konnte nicht geöffnet werden."); + } + } + + CsvParser parser = new CsvParser(file, properties); + + + String line; + + try + { + // Spaltendefinition einlesen + if (properties.headerInFirstLine()) { + header = parser.getNextRow(); + if (header == null) { + throw new IOException("Die Datei '"+csvFile+"' enthält keine Daten."); + } + } + + data = new LinkedList(); + String[] row; + + while (((row = parser.getNextRow()) != null) && (lineCounter < 50)) + { + lineCounter++; + data.add(row); + } + //System.out.println("Line counter at "+lineCounter); + } + catch (IOException ioe) + { + throw ioe; + } + finally + { + try + { + file.close(); + } + catch (IOException ioe) + { + ioe.printStackTrace(); + } + } + } + +} diff --git a/de/tilman/csvparser/CsvFileProperties.java b/de/tilman/csvparser/CsvFileProperties.java new file mode 100644 index 0000000..45103f5 --- /dev/null +++ b/de/tilman/csvparser/CsvFileProperties.java @@ -0,0 +1,171 @@ +package de.tilman.csvparser; + + +/** + * Properties of a CSV file. + * + * @author Tilman Walther + */ +public class CsvFileProperties { + + // TODO Comments + + public static final char DEFAULT_SEPARATOR = ','; + public static final char DEFAULT_QUOTED_FIELD_MARKER = '"'; + public static final char DEFAULT_COMMENT_LINE_MARKER = '#'; + public static final char DEFAULT_ESCAPED_CHAR_MARKER = '\\'; + + /** + * true, if the first line is the table header + */ + private boolean headerInFirstLine; + + /** + * the field separator character + */ + private char separator; + + /** + * true, if fields can be wrapped in quotes + */ + private boolean quotedFields; + + /** + * the character that wraps quoted fields + */ + private char quotedFieldMarker; + +// private boolean doubleQuoteEscape; + + private boolean commentLines; + private char commentLineMarker; + + private boolean escapedChars; + private char escapedCharMarker; + + private boolean ignoreEmptyLines; + + + /** + * Constructor for the default mode: + *