|
| 1 | +package hr.element.doit.csv; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.Writer; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +public class CSVWriter { |
| 8 | + public final CSVFactory config; |
| 9 | + public final Writer writer; |
| 10 | + |
| 11 | + CSVWriter(final CSVFactory config, final Writer w) { |
| 12 | + this.writer = w; |
| 13 | + this.config = config; |
| 14 | + } |
| 15 | + public CSVWriter write(String[] line) throws IOException{ |
| 16 | + int i = 0; |
| 17 | + while(true){ |
| 18 | + final String van = getStringToWrite(line[i]); |
| 19 | + writer.write(van); |
| 20 | + |
| 21 | + if(++i == line.length) break; |
| 22 | + writer.write(config.delimiter); |
| 23 | + } |
| 24 | + writer.write(config.newLine); |
| 25 | + return this; |
| 26 | + } |
| 27 | + private String getStringToWrite(final String value) { |
| 28 | + |
| 29 | + int quoteCount = 0; { |
| 30 | + final boolean hasNewLine = -1 != value.indexOf(config.newLine); |
| 31 | + final boolean hasDelimiter = -1 != value.indexOf(config.delimiter); |
| 32 | + int valuePivot = -1; |
| 33 | + while(true){ |
| 34 | + valuePivot = value.indexOf(config.quotes, valuePivot + 1); |
| 35 | + if(valuePivot == -1) break ; |
| 36 | + quoteCount++; |
| 37 | + } |
| 38 | + |
| 39 | + if ( !hasNewLine && !hasDelimiter && (quoteCount == 0)) |
| 40 | + return value; |
| 41 | + } |
| 42 | + final int qouteLength = config.quotes.length(); |
| 43 | + final int newValueLength = value.length() + (quoteCount + 2) * qouteLength; |
| 44 | + |
| 45 | + final char[] newValue = new char[newValueLength]; |
| 46 | + final char[] oldValue = value.toCharArray(); |
| 47 | + final char[] qoutesArray = config.quotes.toCharArray(); |
| 48 | + |
| 49 | + System.arraycopy(qoutesArray, 0, newValue, 0, qouteLength); |
| 50 | + System.arraycopy(qoutesArray, 0, newValue, newValueLength - qouteLength, qouteLength); |
| 51 | + |
| 52 | + int newValuePivot = qouteLength; |
| 53 | + int oldValueTailPivot = -1; |
| 54 | + |
| 55 | + while(true) { |
| 56 | + final int oldValueHeadPivot = oldValueTailPivot < 0 ? 0 :oldValueTailPivot; |
| 57 | + |
| 58 | + oldValueTailPivot = value.indexOf(config.quotes, oldValueTailPivot+1); |
| 59 | + |
| 60 | + if (oldValueTailPivot == -1) { |
| 61 | + System.arraycopy(oldValue, oldValueHeadPivot, newValue, newValuePivot, oldValue.length - oldValueHeadPivot); |
| 62 | + break; |
| 63 | + } |
| 64 | + |
| 65 | + final int copyLength = oldValueTailPivot - oldValueHeadPivot; |
| 66 | + System.arraycopy(oldValue, oldValueHeadPivot, newValue, newValuePivot, copyLength); |
| 67 | + newValuePivot += copyLength; |
| 68 | + |
| 69 | + System.arraycopy(qoutesArray, 0, newValue, newValuePivot, qouteLength); |
| 70 | + newValuePivot += qouteLength; |
| 71 | + } |
| 72 | + return new String(newValue); |
| 73 | + } |
| 74 | + |
| 75 | + public CSVWriter write(String[][] lines) throws IOException{ |
| 76 | + for( String[] line : lines){ |
| 77 | + write(line); |
| 78 | + } |
| 79 | + return this; |
| 80 | + } |
| 81 | + public CSVWriter write(List<String[]> lines) throws IOException{ |
| 82 | + for( String[] line : lines){ |
| 83 | + write(line); |
| 84 | + } |
| 85 | + return this; |
| 86 | + } |
| 87 | + public <T> CSVWriter writeAll(List<T> list){ |
| 88 | + return this; |
| 89 | + } |
| 90 | + public CSVWriter writeHeader(){ |
| 91 | + return this; |
| 92 | + } |
| 93 | + public void close() throws IOException{ |
| 94 | + writer.close(); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments