Changeset 245
- Timestamp:
- 09/02/08 10:18:30 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
bbtracker_common/trunk/src/org/bbtracker/CsvWriter.java
r224 r245 21 21 * The CsvWriter is used produce a CSV stream according to RFC 4180. 22 22 */ 23 class CsvWriter {23 public class CsvWriter { 24 24 private final StringBuffer buffer = new StringBuffer(); 25 25 … … 39 39 buffer.append(','); 40 40 } 41 appendQuoted(buffer, s); 42 return this; 43 } 44 45 public static String quote(final String s) { 46 final StringBuffer buf = appendQuoted(null, s); 47 if (buf == null) { 48 return s; 49 } else { 50 return buf.toString(); 51 } 52 } 53 54 private static StringBuffer appendQuoted(StringBuffer buf, final String s) { 41 55 final boolean hasWhitespace = s.indexOf(' ') != -1 || s.indexOf('\r') != -1 || s.indexOf('\n') != -1; 42 56 final boolean hasQuote = s.indexOf('"') != -1; 43 57 if (hasWhitespace || hasQuote) { 44 buffer.append('"'); 58 if (buf == null) { 59 // quoted is at least 2 longer, let's give it some more space, 60 // in 61 // case it contains quotes 62 buf = new StringBuffer(s.length() + 4); 63 } 64 buf.append('"'); 45 65 if (hasQuote) { 46 int offset = buf fer.length();47 buf fer.append(s);66 int offset = buf.length(); 67 buf.append(s); 48 68 int i = s.indexOf('"'); 49 69 while (i != -1) { 50 buf fer.insert(offset + i, '"');70 buf.insert(offset + i, '"'); 51 71 offset++; 52 72 i = s.indexOf('"', i + 1); 53 73 } 54 74 } else { 55 buf fer.append(s);75 buf.append(s); 56 76 } 57 buf fer.append('"');58 } else {59 buf fer.append(s);77 buf.append('"'); 78 } else if (buf != null) { 79 buf.append(s); 60 80 } 61 return this;81 return buf; 62 82 } 63 83
Note: See TracChangeset
for help on using the changeset viewer.