Changeset 2640
- Timestamp:
- 02/14/10 20:07:12 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
molgenis/3.3/src/org/molgenis/util/CsvBufferedReader.java
r2638 r2640 19 19 { 20 20 /** default separators */ 21 public static char[] separators = 22 { ',', '\t', ';', ' ' }; 21 public static char[] separators = {',', '\t', ';', ' '}; 23 22 24 23 /** Wrapper around the resource that is read */ … … 60 59 * @param reader 61 60 */ 62 public CsvBufferedReader(BufferedReader reader) 63 { 61 public CsvBufferedReader(BufferedReader reader) { 64 62 this.reader = reader; 65 63 } … … 160 158 { 161 159 // colnames[0] = ROWNAME_COLUMN; 162 } 163 else if (dataline.length > colnames.length + 1) 164 { 165 throw new Exception( 166 "Data has more columns than there are headers (" 167 + dataline.length 168 + ">" 169 + colnames.length 170 + "). Only the first column may be empty. Check whether you have data separators in your data values"); 160 } else if (dataline.length > colnames.length + 1) 161 { 162 throw new Exception("Data has more columns than there are headers (" + dataline.length + ">" + colnames.length 163 + "). Only the first column may be empty. Check whether you have data separators in your data values"); 171 164 } 172 165 … … 179 172 for (String header : result) 180 173 { 181 if (header.length() == 0 && result.indexOf(header) != 0) throw new IOException(182 "nameless header found at index " + result.indexOf(header) + ": " + result);174 if (header.length() == 0 && result.indexOf(header) != 0) 175 throw new IOException("nameless header found at index " + result.indexOf(header) + ": " + result); 183 176 } 184 177 … … 207 200 { 208 201 List<String> headers = null; 209 if (hasHeader) headers = colnames(); 202 if (hasHeader) 203 headers = colnames(); 210 204 String line; 211 205 if (this.separator == 0) … … 223 217 // skip to start 224 218 goToBlockStart(reader); 225 if (hasHeader) line = reader.readLine(); // skip header line 219 if (hasHeader) 220 line = reader.readLine(); // skip header line 226 221 this.isParsing = true; 227 222 228 223 logger.debug("parsing with separator = '" + separator + "' and headers =" + headers); 229 } 230 else 224 } else 231 225 { 232 226 logger.debug("restarted parsing with limit " + noElements); … … 239 233 // template of the tuple 240 234 Tuple t; 241 if (hasHeader) t = new SimpleTuple(headers); 235 if (hasHeader) 236 t = new SimpleTuple(headers); 242 237 else 243 238 t = new SimpleTuple(); … … 251 246 { 252 247 logger.warn("found empty line: " + lineCount); 253 } 254 else if (rows == null || rows.contains(lineCount)) 248 } else if (rows == null || rows.contains(lineCount)) 255 249 { 256 250 … … 265 259 if (hasHeader && values.length > headers.size()) 266 260 { 267 throw new Exception("Row " + lineCount + " has more columns than there are headers (" 268 + values.length + ">" + headers.size() + "). Put double \" around columns that have '" 261 throw new Exception("Row " + lineCount + " has more columns than there are headers (" + values.length + ">" + headers.size() + "). Put double \" around columns that have '" 269 262 + separator + "' in their value. \nRow is: " + line); 270 263 } … … 285 278 // values in subsequent last columns: previous values are 286 279 // copied into empty next ones until a new non-empty one 287 if (values.length == headers.size() - 1) 288 { 289 String[] valuesNew = new String[values.length + 1]; 280 // FIX 2: in fact, this applies to any character shorter than 281 // header length.. ie. whole null lines are copied over with 282 // values from the previous line! hopefully fixed now. 283 if (values.length < headers.size()) 284 { 285 String[] valuesNew = new String[values.length + headers.size()]; 290 286 for (int i = 0; i < values.length; i++) 291 287 { 292 288 valuesNew[i] = values[i]; 293 289 } 294 valuesNew[values.length] = null; 290 291 for(int i = values.length; i < values.length+headers.size(); i++){ 292 valuesNew[i] = null; 293 } 294 295 // valuesNew[values.length] = null; 296 295 297 values = valuesNew; 296 298 } … … 305 307 { 306 308 logger.warn("EMPTY LINE on " + lineCount + ", skipped."); 307 } 308 else 309 } else 309 310 { 310 311 try 311 312 { 312 313 listener.handleLine(lineCount, t); 313 } 314 catch (Exception e) 314 } catch (Exception e) 315 315 { 316 316 // e.printStackTrace(); … … 439 439 // if(separator != 0) return separator; 440 440 441 if (aLine == null || aLine.length() == 0) throw new IOException("could not guess separator from line '" + aLine442 + "'");441 if (aLine == null || aLine.length() == 0) 442 throw new IOException("could not guess separator from line '" + aLine + "'"); 443 443 444 444 char result = '\t'; … … 459 459 sep_count++; 460 460 startIndex = index + 1; 461 } 462 else 461 } else 463 462 { 464 463 break; // no more separators found … … 499 498 while ((line = in.readLine()) != null) 500 499 { 501 if (line.startsWith(blockStart)) return; 500 if (line.startsWith(blockStart)) 501 return; 502 502 // FIXME: make regexp? 503 503 } … … 516 516 private boolean isBlockEnd(String line) 517 517 { 518 if (line.equals(blockEnd)) return true; 518 if (line.equals(blockEnd)) 519 return true; 519 520 return false; 520 521 } … … 555 556 { 556 557 List<String> colnames = this.colnames(); 557 if (colnames.contains(from)) colnames.set(colnames.indexOf(from), to); 558 if (colnames.contains(from)) 559 colnames.set(colnames.indexOf(from), to); 558 560 else 559 561 {
Note: See TracChangeset
for help on using the changeset viewer.