Changeset 245


Ignore:
Timestamp:
09/02/08 10:18:30 (5 years ago)
Author:
saua
Message:

make CvsWriter? public to fix some compile problems

File:
1 edited

Legend:

Unmodified
Added
Removed
  • bbtracker_common/trunk/src/org/bbtracker/CsvWriter.java

    r224 r245  
    2121 * The CsvWriter is used produce a CSV stream according to RFC 4180. 
    2222 */ 
    23 class CsvWriter { 
     23public class CsvWriter { 
    2424        private final StringBuffer buffer = new StringBuffer(); 
    2525 
     
    3939                        buffer.append(','); 
    4040                } 
     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) { 
    4155                final boolean hasWhitespace = s.indexOf(' ') != -1 || s.indexOf('\r') != -1 || s.indexOf('\n') != -1; 
    4256                final boolean hasQuote = s.indexOf('"') != -1; 
    4357                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('"'); 
    4565                        if (hasQuote) { 
    46                                 int offset = buffer.length(); 
    47                                 buffer.append(s); 
     66                                int offset = buf.length(); 
     67                                buf.append(s); 
    4868                                int i = s.indexOf('"'); 
    4969                                while (i != -1) { 
    50                                         buffer.insert(offset + i, '"'); 
     70                                        buf.insert(offset + i, '"'); 
    5171                                        offset++; 
    5272                                        i = s.indexOf('"', i + 1); 
    5373                                } 
    5474                        } else { 
    55                                 buffer.append(s); 
     75                                buf.append(s); 
    5676                        } 
    57                         buffer.append('"'); 
    58                 } else { 
    59                         buffer.append(s); 
     77                        buf.append('"'); 
     78                } else if (buf != null) { 
     79                        buf.append(s); 
    6080                } 
    61                 return this; 
     81                return buf; 
    6282        } 
    6383 
Note: See TracChangeset for help on using the changeset viewer.