|
| 1 | +/** |
| 2 | + * Created by zbrown on 7/8/2018. |
| 3 | + */ |
| 4 | + |
| 5 | +public class CSVReader { |
| 6 | + // CONSTRUCTORS |
| 7 | + public CSVReader( String inCsvString ) |
| 8 | + { |
| 9 | + this(inCsvString,false); |
| 10 | + } |
| 11 | + |
| 12 | + public CSVReader( String inCsvString, Boolean inHasHeaders ) |
| 13 | + { |
| 14 | + this.csvString = inCsvString; |
| 15 | + this.csvStringLength = inCsvString.length(); |
| 16 | + this.hasHeaders = inHasHeaders; |
| 17 | + |
| 18 | + parseToMap(); |
| 19 | + } |
| 20 | + |
| 21 | + |
| 22 | + // PUBLIC PROPERTIES |
| 23 | + public Integer recordCount { |
| 24 | + get{ |
| 25 | + Integer returnValue = csvArray.size(); |
| 26 | + |
| 27 | + if(hasHeaders && (returnValue > 0)){ returnValue = returnValue - 1; } |
| 28 | + |
| 29 | + return returnValue; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public List<String> columnList { get; private set; } |
| 34 | + |
| 35 | + public String find(String thisColumn,Integer thisRow){ |
| 36 | + String returnValue; |
| 37 | + |
| 38 | + returnValue = csvMap.get(thisColumn)[thisRow]; |
| 39 | + return returnValue; |
| 40 | + } |
| 41 | + |
| 42 | + public List<List<string>> csvArray { get; private set; } |
| 43 | + public Map<String,List<String>> csvMap { get; private set; } |
| 44 | + |
| 45 | + |
| 46 | + // BEGIN PRIVATE VARIABLES |
| 47 | + |
| 48 | + private boolean EOF { |
| 49 | + get |
| 50 | + { |
| 51 | + if( this.position < this.csvStringLength ) |
| 52 | + return false; |
| 53 | + else |
| 54 | + return true; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private String tokenString { |
| 59 | + get { |
| 60 | + if(!EOF){ |
| 61 | + return this.csvString.mid(this.position,1); |
| 62 | + } else { |
| 63 | + return null; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private String lookAheadString { |
| 69 | + get { |
| 70 | + if((this.position + 1) < this.csvStringLength){ |
| 71 | + return this.csvString.mid(this.position+1,1); |
| 72 | + } else { |
| 73 | + return null; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private integer columnWidth = 0; |
| 79 | + private boolean hasHeaders; |
| 80 | + private string csvString; |
| 81 | + private integer csvStringLength; |
| 82 | + private integer position = 0; |
| 83 | + |
| 84 | + // STATIC CONSTANTS |
| 85 | + private static string COMMA = ','; |
| 86 | + private static string NL = '\n'; |
| 87 | + private static string QUOTE = '"'; |
| 88 | + private static string DOUBLE_QUOTE = '""'; |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + /** |
| 95 | + * Reads up to a unquoted newline delimiter |
| 96 | + * |
| 97 | + * @return list of cell contents |
| 98 | + */ |
| 99 | + private List<String> parseLine(){ |
| 100 | + List<String> returnValue = new List<String>(); |
| 101 | + |
| 102 | + do { |
| 103 | + |
| 104 | + returnValue.add(parseCell()); |
| 105 | + // throw away comma |
| 106 | + if( this.tokenString == COMMA){ this.position++; } |
| 107 | + } while((this.tokenString != null) && (this.tokenString != NL )); |
| 108 | + |
| 109 | + this.position++; |
| 110 | + |
| 111 | + if(returnValue.size() > columnWidth){ columnWidth = returnValue.size(); } |
| 112 | + |
| 113 | + return returnValue; |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + /** |
| 118 | + * Reads up to an unquoted comma or newline |
| 119 | + * |
| 120 | + * @return string of cell contents |
| 121 | + */ |
| 122 | + private String parseCell(){ |
| 123 | + String returnValue = ''; |
| 124 | + Boolean isQuoteMode = false; |
| 125 | + Set<String> terminatorSet = new Set<String>(); |
| 126 | + terminatorSet.add(COMMA); |
| 127 | + terminatorSet.add(NL); |
| 128 | + terminatorSet.add(null); |
| 129 | + |
| 130 | + do { |
| 131 | + if(isQuoteMode){ |
| 132 | + if((this.tokenString == QUOTE) && (this.lookAheadString == QUOTE)){ |
| 133 | + returnValue = returnValue + QUOTE; |
| 134 | + this.position = this.position + 2; // Jump ahead two |
| 135 | + } else if(this.tokenString == QUOTE){ |
| 136 | + this.position = this.position + 1; // Jump ahead one |
| 137 | + isQuoteMode = false; |
| 138 | + } else { |
| 139 | + returnValue = returnValue + this.tokenString; |
| 140 | + this.position = this.position + 1; // Jump ahead one |
| 141 | + } |
| 142 | + |
| 143 | + } else { |
| 144 | + if(this.tokenString == QUOTE){ |
| 145 | + this.position = this.position + 1; // Jump ahead one |
| 146 | + isQuoteMode = true; |
| 147 | + } else { |
| 148 | + returnValue = returnValue + this.tokenString; |
| 149 | + this.position = this.position + 1; // Jump ahead one |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + } while(!terminatorSet.contains(this.tokenString) || (isQuoteMode == true)); |
| 155 | + |
| 156 | + |
| 157 | + return returnValue; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Convers an integer to an excel column |
| 162 | + * |
| 163 | + * @param columnNumber |
| 164 | + * |
| 165 | + * @return An excel header string |
| 166 | + */ |
| 167 | + private String computeHeaderFromNumber(Integer columnNumber){ |
| 168 | + String returnValue; |
| 169 | + String alphaList = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'; |
| 170 | + List<String> alphaArray = alphaList.split(','); |
| 171 | + |
| 172 | + returnValue = alphaArray[math.mod(columnNumber,26)]; |
| 173 | + |
| 174 | + Integer pow = 1; |
| 175 | + while(columnNumber > Integer.valueOf(math.pow(26,pow))){ |
| 176 | + returnValue = alphaArray[(columnNumber/Integer.valueOf(math.pow(26,pow)))] + returnValue; |
| 177 | + pow++; |
| 178 | + } |
| 179 | + |
| 180 | + return returnValue; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Parses the raw data into an double array of strings |
| 185 | + */ |
| 186 | + private void parseToArray() |
| 187 | + { |
| 188 | + this.csvArray = new List<List<string>>(); |
| 189 | + |
| 190 | + |
| 191 | + while(!EOF){ |
| 192 | + this.csvArray.add(parseLine()); |
| 193 | + } |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Converts the CSV array to a Map |
| 199 | + */ |
| 200 | + private void parseToMap() |
| 201 | + { |
| 202 | + this.csvMap = new Map<String,List<String>>(); |
| 203 | + |
| 204 | + parseToArray(); |
| 205 | + this.columnList = new List<String>(); |
| 206 | + Integer startIndex = 0; |
| 207 | + |
| 208 | + |
| 209 | + if(this.csvArray.size() > 0){ |
| 210 | + if(this.hasHeaders){ |
| 211 | + for(String mapKey : this.csvArray[0]){ |
| 212 | + this.csvMap.put(mapKey,new List<String>()); |
| 213 | + this.columnList.add(mapKey); |
| 214 | + } |
| 215 | + startIndex = 1; |
| 216 | + } |
| 217 | + else{ |
| 218 | + for(Integer colnumber = 0; colnumber < this.csvArray[0].size(); colnumber++){ |
| 219 | + String mapKey = computeHeaderFromNumber(colnumber); |
| 220 | + this.csvMap.put(mapKey,new List<String>()); |
| 221 | + this.columnList.add(mapKey); |
| 222 | + } |
| 223 | + } |
| 224 | + if(this.csvArray.size() > startIndex + 1){ |
| 225 | + for(Integer row = startIndex; row < this.csvArray.size() ; row++ ){ |
| 226 | + List<String> thisRow = this.csvArray[row]; |
| 227 | + |
| 228 | + for(Integer index = 0; index < thisRow.size(); index++){ |
| 229 | + if(index < columnList.size()){ |
| 230 | + List<String> currentColumn = this.csvMap.get(this.columnList[index]); |
| 231 | + currentColumn.add(thisRow[index]); |
| 232 | + this.csvMap.put(this.columnList[index],currentColumn); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments