1 21 package com.Ostermiller.util; 22 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 41 public class LabeledCSVParser implements CSVParse { 42 43 48 private CSVParse parse; 49 50 56 private String [] labels; 57 58 64 private Map <String ,Integer > labelMap; 65 66 71 private String [] lastLine; 72 73 79 private int nextValueLine = -2; 80 81 89 public LabeledCSVParser(CSVParse parse) throws IOException { 90 this.parse = parse; 91 } 92 93 104 public void changeDelimiter(char newDelim) throws BadDelimiterException { 105 parse.changeDelimiter(newDelim); 106 } 107 108 119 public void changeQuote(char newQuote) throws BadQuoteException { 120 parse.changeQuote(newQuote); 121 } 122 123 142 public String [][] getAllValues() throws IOException { 143 if (labels == null) setLabels(); 144 String [][] allValues = parse.getAllValues(); 145 if (allValues == null){ 146 lastLine = null; 147 } else { 148 lastLine = allValues[allValues.length-1]; 149 } 150 return allValues; 151 } 152 153 165 public int getLastLineNumber(){ 166 return lastLineNumber(); 167 } 168 169 181 public int lastLineNumber(){ 182 int lineNum = parse.getLastLineNumber(); 183 if (lineNum <= -1) return -1; if (lineNum == 1) return -1; return lineNum - 1; } 187 188 210 public String [] getLine() throws IOException { 211 if (labels == null) setLabels(); 212 lastLine = parse.getLine(); 213 return lastLine; 214 } 215 216 229 public String nextValue() throws IOException { 230 if (labels == null) setLabels(); 231 String nextValue = parse.nextValue(); 232 nextValueLine = getLastLineNumber(); 233 return nextValue; 234 } 235 236 244 private void setLabels() throws IOException { 245 labels = parse.getLine(); 246 if (labels == null) return; 247 labelMap = new HashMap <String ,Integer >(); 248 for (int i = 0; i < labels.length; i++){ 249 labelMap.put(labels[i], new Integer (i)); 250 } 251 } 252 253 261 public String [] getLabels() throws IOException { 262 if (labels == null) setLabels(); 263 return labels; 264 } 265 266 279 public int getLabelIndex(String label){ 280 try { 281 return getLabelIdx(label); 282 } catch (IOException iox){ 283 return -1; 284 } 285 } 286 287 299 public int getLabelIdx(String label) throws IOException { 300 if (labels == null) setLabels(); 301 if (labelMap == null) return -1; 302 if (!labelMap.containsKey(label)) return -1; 303 return ((Integer )labelMap.get(label)).intValue(); 304 } 305 306 316 public String getValueByLabel(String label) throws IllegalStateException { 317 if (nextValueLine == getLastLineNumber()) throw new IllegalStateException ("nextValue() was used to get values from this line."); 318 if (lastLine == null) return null; 319 int fieldIndex; 320 try { 321 fieldIndex = getLabelIdx(label); 322 } catch (IOException iox){ 323 throw new RuntimeException (iox); 325 } 326 if (fieldIndex == -1) return null; 327 if (fieldIndex >= lastLine.length) return null; 328 return lastLine[fieldIndex]; 329 } 330 331 338 public void close() throws IOException { 339 parse.close(); 340 } 341 } 342 | Popular Tags |