Changeset 2640


Ignore:
Timestamp:
02/14/10 20:07:12 (2 years ago)
Author:
jvelde
Message:

Hopefully resolved the bug where values of previous line are copied unintentionally to next line's null values.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molgenis/3.3/src/org/molgenis/util/CsvBufferedReader.java

    r2638 r2640  
    1919{ 
    2020        /** default separators */ 
    21         public static char[] separators = 
    22         { ',', '\t', ';', ' ' }; 
     21        public static char[] separators = {',', '\t', ';', ' '}; 
    2322 
    2423        /** Wrapper around the resource that is read */ 
     
    6059         * @param reader 
    6160         */ 
    62         public CsvBufferedReader(BufferedReader reader) 
    63         { 
     61        public CsvBufferedReader(BufferedReader reader) { 
    6462                this.reader = reader; 
    6563        } 
     
    160158                { 
    161159                        // 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"); 
    171164                } 
    172165 
     
    179172                for (String header : result) 
    180173                { 
    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); 
    183176                } 
    184177 
     
    207200        { 
    208201                List<String> headers = null; 
    209                 if (hasHeader) headers = colnames(); 
     202                if (hasHeader) 
     203                        headers = colnames(); 
    210204                String line; 
    211205                if (this.separator == 0) 
     
    223217                        // skip to start 
    224218                        goToBlockStart(reader); 
    225                         if (hasHeader) line = reader.readLine(); // skip header line 
     219                        if (hasHeader) 
     220                                line = reader.readLine(); // skip header line 
    226221                        this.isParsing = true; 
    227222 
    228223                        logger.debug("parsing with separator = '" + separator + "' and headers =" + headers); 
    229                 } 
    230                 else 
     224                } else 
    231225                { 
    232226                        logger.debug("restarted parsing with limit " + noElements); 
     
    239233                // template of the tuple 
    240234                Tuple t; 
    241                 if (hasHeader) t = new SimpleTuple(headers); 
     235                if (hasHeader) 
     236                        t = new SimpleTuple(headers); 
    242237                else 
    243238                        t = new SimpleTuple(); 
     
    251246                        { 
    252247                                logger.warn("found empty line: " + lineCount); 
    253                         } 
    254                         else if (rows == null || rows.contains(lineCount)) 
     248                        } else if (rows == null || rows.contains(lineCount)) 
    255249                        { 
    256250 
     
    265259                                if (hasHeader && values.length > headers.size()) 
    266260                                { 
    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 '" 
    269262                                                        + separator + "' in their value. \nRow is: " + line); 
    270263                                } 
     
    285278                                // values in subsequent last columns: previous values are 
    286279                                // 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()]; 
    290286                                        for (int i = 0; i < values.length; i++) 
    291287                                        { 
    292288                                                valuesNew[i] = values[i]; 
    293289                                        } 
    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                                         
    295297                                        values = valuesNew; 
    296298                                } 
     
    305307                                        { 
    306308                                                logger.warn("EMPTY LINE on " + lineCount + ", skipped."); 
    307                                         } 
    308                                         else 
     309                                        } else 
    309310                                        { 
    310311                                                try 
    311312                                                { 
    312313                                                        listener.handleLine(lineCount, t); 
    313                                                 } 
    314                                                 catch (Exception e) 
     314                                                } catch (Exception e) 
    315315                                                { 
    316316                                                        // e.printStackTrace(); 
     
    439439                // if(separator != 0) return separator; 
    440440 
    441                 if (aLine == null || aLine.length() == 0) throw new IOException("could not guess separator from line '" + aLine 
    442                                 + "'"); 
     441                if (aLine == null || aLine.length() == 0) 
     442                        throw new IOException("could not guess separator from line '" + aLine + "'"); 
    443443 
    444444                char result = '\t'; 
     
    459459                                        sep_count++; 
    460460                                        startIndex = index + 1; 
    461                                 } 
    462                                 else 
     461                                } else 
    463462                                { 
    464463                                        break; // no more separators found 
     
    499498                        while ((line = in.readLine()) != null) 
    500499                        { 
    501                                 if (line.startsWith(blockStart)) return; 
     500                                if (line.startsWith(blockStart)) 
     501                                        return; 
    502502                                // FIXME: make regexp? 
    503503                        } 
     
    516516        private boolean isBlockEnd(String line) 
    517517        { 
    518                 if (line.equals(blockEnd)) return true; 
     518                if (line.equals(blockEnd)) 
     519                        return true; 
    519520                return false; 
    520521        } 
     
    555556        { 
    556557                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); 
    558560                else 
    559561                { 
Note: See TracChangeset for help on using the changeset viewer.