Author: chatellier Date: 2010-12-02 16:25:11 +0000 (Thu, 02 Dec 2010) New Revision: 340 Log: Retrait de la d?\195?\169pendance a opencsv (au niveau m?\195?\169moire, code optimis?\195?\169) Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/storage/MemoryDataStorage.java Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/storage/MemoryDataStorage.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/storage/MemoryDataStorage.java 2010-12-02 15:23:52 UTC (rev 339) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/storage/MemoryDataStorage.java 2010-12-02 16:25:11 UTC (rev 340) @@ -25,22 +25,20 @@ package fr.ifremer.coser.storage; -import java.io.IOException; -import java.io.StringReader; -import java.io.StringWriter; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import au.com.bytecode.opencsv.CSVReader; -import au.com.bytecode.opencsv.CSVWriter; - /** * Stockage mémoire des String[] sous forme de String simple. * * Beaucoup moins couteux en mémoire que les String[] sans pour antant * que le temps de parcours soit augmenté. * + * Les String[] sont encodés en CSV et stocké en mémoire en String. + * Le code CSV a été repris de opencsv, mais sans les ioexception, reader et + * writer inutiles ici. + * * @author chatellier * @version $Revision$ * @@ -129,34 +127,6 @@ lineIndexStorage.add(index, data[0]); } - protected String arrayToString(String[] data) { - StringWriter writer = new StringWriter(); - CSVWriter csvWriter = new CSVWriter(writer); - csvWriter.writeNext(data); - //TODO echatellier 20101022 replace with custom parsing code - try { - csvWriter.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return writer.toString(); - } - - protected String[] stringToArray(String data) { - StringReader reader = new StringReader(data); - CSVReader csvReader = new CSVReader(reader); - String[] dataArray = null; - //TODO echatellier 20101022 replace with custom parsing code - try { - dataArray = csvReader.readNext(); - csvReader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - return dataArray; - } - /* * @see fr.ifremer.coser.storage.DataStorage#getData(int) */ @@ -194,4 +164,148 @@ public String toString() { return listStorage.toString(); } + + protected String arrayToString(String[] nextLine) { + return writeNext(nextLine); + } + + protected String[] stringToArray(String data) { + return parseLine(data); + } + + protected static final char separator = ','; + protected static final char quotechar = '\"'; + protected static final char escapechar = '\"'; + + // Copied from CSVWriter and improved + protected String writeNext(String[] nextLine) { + StringBuilder sb = new StringBuilder(128); + for (int i = 0; i < nextLine.length; i++) { + + if (i != 0) { + sb.append(separator); + } + + String nextElement = nextLine[i]; + if (nextElement == null) { + continue; + } + sb.append(quotechar); + + if (nextElement.indexOf(quotechar) != -1 || nextElement.indexOf(escapechar) != -1) { + sb.append(processLine(nextElement)); + } + else { + sb.append(nextElement); + } + sb.append(quotechar); + } + return sb.toString(); + } + + // Copied from CSVWriter and improved + protected StringBuilder processLine(String nextElement) { + StringBuilder sb = new StringBuilder(128); + for (int j = 0; j < nextElement.length(); j++) { + char nextChar = nextElement.charAt(j); + if (/*escapechar != NO_ESCAPE_CHARACTER && */nextChar == quotechar) { + sb.append(escapechar).append(nextChar); + } else if (/*escapechar != NO_ESCAPE_CHARACTER && */nextChar == escapechar) { + sb.append(escapechar).append(nextChar); + } else { + sb.append(nextChar); + } + } + + return sb; + } + + // Copied from CSVParser and improved + protected String[] parseLine(String nextLine) { + + /*if (!multi && pending != null) { + pending = null; + } + + if (nextLine == null) { + if (pending != null) { + String s = pending; + pending = null; + return new String[] {s}; + } else { + return null; + } + }*/ + + List<String> tokensOnThisLine = new ArrayList<String>(); + StringBuilder sb = new StringBuilder(128); + boolean inQuotes = false; + /*if (pending != null) { + sb.append(pending); + pending = null; + inQuotes = true; + }*/ + for (int i = 0; i < nextLine.length(); i++) { + + char c = nextLine.charAt(i); + if (c == escapechar) { + if( isNextCharacterEscapable(nextLine, inQuotes, i) ){ + sb.append(nextLine.charAt(i+1)); + i++; + } + } else if (c == quotechar) { + if( isNextCharacterEscapedQuote(nextLine, inQuotes, i) ){ + sb.append(nextLine.charAt(i+1)); + i++; + }else{ + inQuotes = !inQuotes; + // the tricky case of an embedded quote in the middle: a,bc"d"ef,g + //if (!strictQuotes) { + if(i>2 //not on the beginning of the line + && nextLine.charAt(i-1) != separator //not at the beginning of an escape sequence + && nextLine.length()>(i+1) && + nextLine.charAt(i+1) != separator //not at the end of an escape sequence + ){ + sb.append(c); + } + //} + } + } else if (c == separator && !inQuotes) { + tokensOnThisLine.add(sb.toString()); + sb = new StringBuilder(128); // start work on next token + } else { + //if (!strictQuotes || inQuotes) + sb.append(c); + } + } + /*// line is done - check status + if (inQuotes) { + if (multi) { + // continuing a quoted section, re-append newline + sb.append("\n"); + pending = sb.toString(); + sb = null; // this partial content is not to be added to field list yet + } else { + throw new IOException("Un-terminated quoted field at end of CSV line"); + } + }*/ + //if (sb != null) { + tokensOnThisLine.add(sb.toString()); + //} + return tokensOnThisLine.toArray(new String[tokensOnThisLine.size()]); + } + + // Copied from CSVParser + protected boolean isNextCharacterEscapedQuote(String nextLine, boolean inQuotes, int i) { + return inQuotes // we are in quotes, therefore there can be escaped quotes in here. + && nextLine.length() > (i+1) // there is indeed another character to check. + && nextLine.charAt(i+1) == quotechar; + } + + // Copied from CSVParser and improved + protected boolean isNextCharacterEscapable(String nextLine, boolean inQuotes, int i) { + return inQuotes // we are in quotes, therefore there can be escaped quotes in here. + && nextLine.length() > (i+1) // there is indeed another character to check. + && ( nextLine.charAt(i+1) == quotechar || nextLine.charAt(i+1) == escapechar); + } }
participants (1)
-
chatellier@users.labs.libre-entreprise.org