Clover coverage report -
Coverage timestamp: Mon May 5 2008 11:56:20 GMT-05:00
file stats: LOC: 175   Methods: 3
NCLOC: 82   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CSVReader.java 37.5% 76.7% 100% 67.7%
coverage coverage
 1    /*
 2    * Copyright 2005 The Regents of the University of Michigan
 3    *
 4    * Licensed under the Apache License, Version 2.0 (the "License");
 5    * you may not use this file except in compliance with the License.
 6    * You may obtain a copy of the License at
 7    *
 8    * http://www.apache.org/licenses/LICENSE-2.0
 9    *
 10    * Unless required by applicable law or agreed to in writing, software
 11    * distributed under the License is distributed on an "AS IS" BASIS,
 12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13    * See the License for the specific language governing permissions and
 14    * limitations under the License.
 15    */
 16   
 17    package org.proteomecommons.tranche.tasks;
 18   
 19    import java.io.BufferedReader;
 20    import java.io.File;
 21    import java.io.FileReader;
 22    import java.io.IOException;
 23    import java.util.ArrayList;
 24    import java.util.HashMap;
 25    import java.util.List;
 26    import java.util.Map;
 27    import org.proteomecommons.tranche.util.BigHash;
 28    import org.proteomecommons.tranche.util.ExcelSavvyLineSplitter;
 29    import org.proteomecommons.tranche.util.IOUtil;
 30   
 31    /**
 32    * <p>Used to read in a CSV file.</p>
 33    *
 34    * @author Bryan Smith - bryanesmith@gmail.com
 35    * @version %I%, %G%
 36    * @since 1.0
 37    */
 38    public class CSVReader {
 39   
 40    private File file;
 41   
 42    private CSVFile csvObj;
 43   
 44    private List<String> legend;
 45    private List<Map<String,String>> records;
 46   
 47    private static final String COMMA_TOKEN = "COMMA_TOKEN";
 48   
 49    /**
 50    * @param file the file received
 51    * @since 1.0
 52    */
 53  1 public CSVReader(File file) {
 54  1 this.file = file;
 55  1 this.legend = new ArrayList();
 56  1 this.records = new ArrayList();
 57    }
 58   
 59    /**
 60    * @returns the new file
 61    * @throws Exception if any exception occurs
 62    * @since 1.0
 63    */
 64  1 public CSVFile read() throws Exception {
 65   
 66  1 BufferedReader in = null;
 67   
 68    // Just to make more useful error messages
 69  1 int line = 1;
 70   
 71  1 try {
 72   
 73  1 in = new BufferedReader(new FileReader(this.file));
 74  1 String str;
 75   
 76    // If not a legend, we'll need to reset the stream
 77  1 boolean isLegend = true;
 78   
 79    // Get legend
 80  ? while ((str = in.readLine()) != null) {
 81   
 82    // Skip blank entries
 83  1 if (!str.trim().equals("")) {
 84   
 85  1 String[] keys = this.getEntries(str);
 86   
 87  1 for (String key : keys) {
 88  3 this.legend.add(key.trim());
 89   
 90    // Best-effort attempt to determine whether there is a legend
 91  3 try {
 92    // If there is a hash in this line, no legend
 93  3 BigHash.createHashFromString(key);
 94   
 95    // If get here, there is no legend
 96  0 isLegend = false;
 97  0 break;
 98    }
 99   
 100    catch(Exception e) { /* Nope, good, probably a legend */ }
 101    }
 102   
 103    // Done after first non-empty row
 104  1 line++;
 105  1 break;
 106    }
 107   
 108  0 line++;
 109    }
 110   
 111    // If no legend, need to create one
 112  1 if (!isLegend) {
 113   
 114    // Could use reset with mark, but line may be long...
 115  0 IOUtil.safeClose(in);
 116  0 in = new BufferedReader(new FileReader(this.file));
 117   
 118  0 this.legend.clear();
 119   
 120    // First entry still in memory... how many items are in it?
 121  0 int numEntries = getEntries(str).length;
 122   
 123  0 for (int i = 1; i <= numEntries; i++) {
 124  0 this.legend.add("Column #"+i);
 125    }
 126    }
 127   
 128    // Get remaining rows
 129  ? while ((str = in.readLine()) != null) {
 130   
 131    // Skip blank entries
 132  4 if (!str.trim().equals("")) {
 133   
 134  4 String[] items = this.getEntries(str);
 135   
 136  4 if (items.length != this.legend.size()) {
 137  0 throw new IOException("Wrong number of entries on line " + line +": expecting " + this.legend.size() +", found " +items.length);
 138    }
 139   
 140    // Create a map representation
 141  4 Map<String,String> record = new HashMap();
 142   
 143  4 for (int i = 0; i < items.length; i++) {
 144  12 String key = this.legend.get(i);
 145  12 String value = items[i].trim();
 146   
 147  12 record.put(key, value);
 148    }
 149   
 150  4 this.records.add(record);
 151    }
 152   
 153  4 line++;
 154    }
 155   
 156    }
 157   
 158    finally {
 159  1 IOUtil.safeClose(in);
 160    }
 161   
 162  1 return new CSVFile(this.file, this.legend, this.records);
 163    }
 164   
 165    /**
 166    * Breaks up a line into entries based on commas. However, commas appearing with quotes aren't used to tokenize.
 167    *
 168    * @param the entry to split by
 169    * @return the entries that constitiute the line
 170    * @since 1.0
 171    */
 172  5 private String[] getEntries(String entry) {
 173  5 return ExcelSavvyLineSplitter.split(entry);
 174    }
 175    }