litesoft
@ 942
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.commonfoundation.csv; import org.litesoft.commonfoundation.exceptions.*; import java.util.*; /** * CSV == Comma Separated Value<p> * <p/> * A CSV line consists of one or more fields seperated by a comma. * Each field does NOT include any leading and trailing spaces. * To include a comma or leading or trailing spaces the field MUST * be wrapped in double quotes (eg "field").<p> * <p/> * To place a double quote within a double quoted field the double * quote must be doubled up (eg the proper way to format start,"end * is "start,""end").<p> * * @author George Smith * @version 1.0 7/28/01 */ public class CsvSupport { /** * Constructor primarily used with decode, and encode with NO support * for null substitution.<p> * <p/> * Because this constructor does NOT provide any null substitution * String(s), any null in the encode array will cause an * IllegalArgumentException to be thrown (see encode).<p> * * @see #CsvSupport(String) * @see #encode(String[]) * @see #decode(String) */ public CsvSupport() { this( null ); } /** * Constructor primarily used with encode with common String * for null substitution.<p> * <p/> * Any null in the encode array is substituted with the parameter. * Note: Should the substitution String be null, and it is substituted, * then an IllegalArgumentException is thrown (see encode).<p> * * @param pForNulls common null substitution String (null OK).<p> * * @see #CsvSupport(String, String) * @see #encode(String[]) * @see #decode(String) */ public CsvSupport( String pForNulls ) { this( pForNulls, pForNulls ); } /** * Constructor primarily used with encode with different Strings * for null substitution.<p> * <p/> * Nulls on the end of the encode array is substituted with the * <i>pForEndNulls</i> parameter, AND if this parameter is itself * null, then the array is effectively shortened (possibly resulting * in an empty array).<p> * <p/> * All other nulls in the encode array are substituted with the * <i>pForMidNulls</i> parameter. Note: Should the substitution * String itself be null, then an IllegalArgumentException is * thrown (see encode).<p> * * @param pForMidNulls null substitution String for nulls NOT on the * <i>end</i> of the encode array (null OK). * @param pForEndNulls null substitution String for nulls on the * <i>end</i> of the encode array (null OK).<p> * * @see #CsvSupport(String, String) * @see #encode(String[]) * @see #decode(String) */ public CsvSupport( String pForMidNulls, String pForEndNulls ) { zForMidNulls = pForMidNulls; zForEndNulls = pForEndNulls; } private String zForMidNulls; private String zForEndNulls; private String[] replaceNulls( String[] source ) { int i, sLen = source.length; if ( zForEndNulls == null ) { for ( i = sLen - 1; (i >= 0) && (source[i] == null); i-- ) { sLen--; } } String[] newArray = new String[sLen]; for ( i = sLen - 1; (i >= 0) && (source[i] == null); i-- ) { newArray[i] = zForEndNulls; } for (; i >= 0; i-- ) { if ( null == (newArray[i] = (source[i] == null) ? zForMidNulls : source[i]) ) { throw new IllegalArgumentException( "null unacceptable in element: " + i ); } } return newArray; } private String[] deNull( String[] source ) { if ( source != null ) { for ( int i = source.length; i-- > 0; ) { if ( source[i] == null ) { return replaceNulls( source ); } } } return source; } private int sumLengths( String[] source ) { int retval = 0; for ( int i = source.length; i-- > 0; ) { retval += source[i].length(); } return retval; } /** * Encode a String Array into a CSV line/String.<p> * <p/> * To support orthogonality with decode an empty array returns a null.<p> * <p/> * Nulls in the array are replaced by either <i>mid</i> or <i>end</i> * null substitution strings. If the <i>end null substitution string</i> * is itself a null, then the array is effectively shortened until no nulls * remain on the end of the array (note: this can result in an empty array). * If a non-end null is substituted by the <i>mid null substitution string</i> * but is still null, then an IllegalArgumentException is thrown.<p> * * @param pSource array of <i>fields</i> (!null).<p> * * @return a CSV encoded string of the source fields.<p> * * @throws NullPointerException if the passed in array is null. * @throws IllegalArgumentException if a null is attempted to be inserted in the ouput String (after substitution).<p> * @see #decode(String) * @see #CsvSupport(String) * @see #CsvSupport(String, String) */ public String encode( String[] pSource ) throws NullPointerException, IllegalArgumentException { pSource = deNull( pSource ); // might produce a new Array int sLen = pSource.length; // can throw NullPointerException if ( sLen == 0 ) { return null; } StringBuilder sb = new StringBuilder( sLen + sumLengths( pSource ) ); appendCsvField( pSource[0], sb ); for ( int i = 1; i < sLen; i++ ) { sb.append( ',' ); appendCsvField( pSource[i], sb ); } return sb.toString(); } /** * Decode a CSV line/String of fields into an Array.<p> * <p/> * To support orthogonality with encode a null returns an empty array.<p> * <p/> * Since a CSV line consists of one or more fields, an array is always * returned (eg "" would return an array with one entry of ""). However, * if the source parameter is null, then an empty array is returned.<p> * * @param pSource CSV line of <i>fields</i> (null OK).<p> * * @return an array of decoded CSV string fields.<p> * * @throws UnclosedQuoteException (subclass of QuoteException subclass of * IllegalArgumentException if an * appearently quoted field is * does not appear to have the closing quote.<p> * @throws MalformedQuotedFieldException (subclass of QuoteException subclass of * IllegalArgumentException if an * appearently quoted field is * encounters the 'closing' quote, but there * is more following it.<p> * @see #encode(String[]) */ public String[] decode( String pSource ) throws IllegalArgumentException { List<String> list = new ArrayList<String>(); while ( pSource != null ) { pSource = extractAndAddCsvField( pSource.trim(), list ); } return list.toArray( new String[list.size()] ); } private String extractAndAddCsvField( String source, List<String> list ) { if ( (source.length() > 0) && (source.charAt( 0 ) == '"') ) { return extractAndAddCsvQuotedField( source, list ); } // Handle Not Quoted int comma = source.indexOf( ',' ); if ( comma == -1 ) // No Comma, then must be last field { // *** The use of new String() is to Force a local underlying char buffer //noinspection RedundantStringConstructorCall list.add( new String( source ) ); // *** Already trimmed return null; } // *** The use of new String() is to Force a local underlying char buffer //noinspection RedundantStringConstructorCall list.add( new String( source.substring( 0, comma ).trim() ) ); // *** Possible spaces before the comma return source.substring( comma + 1 ); } private String extractAndAddCsvQuotedField( String source, List<String> list ) { String retval, field; int comma = findCommaAfterQuoted( source ); if ( comma == -1 ) // No Comma, then must be last field { field = source; retval = null; } else { field = source.substring( 0, comma ); retval = source.substring( comma + 1 ); } field = removeWrappingQuotes( field ); field = removeDoubleQuotes( field ); // *** The use of new String() is to Force a local underlying char buffer //noinspection RedundantStringConstructorCall list.add( new String( field ) ); return retval; } private int findCommaAfterQuoted( String source ) { int sLen = source.length(); int startIndex = 0; do { int quote = source.indexOf( '"', ++startIndex ); if ( quote == -1 ) { throw new UnclosedQuoteException( "Apparently Quoted Field no closing quote, starting at: " + source ); } startIndex = quote + 1; } while ( (startIndex < sLen) && ('"' == source.charAt( startIndex )) ); return source.indexOf( ',', startIndex ); // Find comma AFTER closing quote } private String removeDoubleQuotes( String field ) { int startIndex = 0; for ( int doubleQuote; 0 != (doubleQuote = field.indexOf( "\"\"", startIndex ) + 1); startIndex = doubleQuote ) { field = field.substring( 0, doubleQuote ) + field.substring( doubleQuote + 1 ); } return field; } private String removeWrappingQuotes( String field ) { int sLast = (field = field.trim()).length() - 1; if ( field.charAt( sLast ) != '"' ) { throw new MalformedQuotedFieldException( "Appearently Quoted Field, but something after closing quote, in field: " + field ); } return field.substring( 1, sLast ); } private void appendCsvField( String field, StringBuilder sb ) { if ( field.length() == 0 ) { return; } int quote = field.indexOf( '"' ); if ( (quote == -1) && !field.startsWith( " " ) && !field.endsWith( " " ) && (-1 == field.indexOf( ',' )) ) { sb.append( field ); return; } // quote it! int from = 0; sb.append( '"' ); for (; quote != -1; quote = field.indexOf( '"', from + 1 ) ) // + 1 to skip the '"' at from { sb.append( field.substring( from, quote + 1 ) ); // part thru '"' from = quote; // will cause the next part to include the '"' } sb.append( field.substring( from ) ); // remainder sb.append( '"' ); } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/commonfoundation/csv/CsvSupport.java
Revision | Author | Commited | Message |
---|---|---|---|
942 Diff | GeorgeS | Mon 02 Jun, 2014 23:41:46 +0000 | Extracting commonfoundation |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
151 Diff | GeorgeS | Thu 17 Mar, 2011 04:16:22 +0000 | |
50 Diff | GeorgeS | Tue 13 Apr, 2010 11:51:38 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |