1 22 package net.sf.anupam.csv; 23 24 import com.Ostermiller.util.CSVParse; 25 import com.Ostermiller.util.ExcelCSVParser; 26 import org.apache.commons.collections.CollectionUtils; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import java.io.IOException ; 31 import java.io.Reader ; 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.NoSuchElementException ; 36 37 52 class CSVReader implements Iterable <List <String >> { 53 54 57 private static final Log LOG = LogFactory.getLog(CSVReader.class); 58 59 62 private CSVParse parser; 63 64 67 private boolean readingComplete; 68 69 72 private boolean headerPresent; 73 74 82 public CSVReader(final Reader csvReader, final boolean headerPresent) { 83 super(); 84 this.headerPresent = headerPresent; 85 86 parser = new ExcelCSVParser(csvReader); 87 88 } 89 90 93 public void close() { 94 try { 95 if (parser != null) { 96 parser.close(); 97 LOG.debug("Closed the CSV Reader"); 98 } 99 } catch (final IOException e) { 100 } finally { 102 parser = null; 103 } 104 } 105 106 112 @Override 113 protected void finalize() throws Throwable { 114 super.finalize(); 115 close(); 116 } 117 118 124 public Iterator <List <String >> iterator() { 125 return new LineIterator(); 126 } 127 128 131 134 private class LineIterator implements Iterator <List <String >> { 135 138 141 private String [] parsedValues; 142 143 146 private boolean haveReadPreviousLine; 147 148 151 public LineIterator() { 152 super(); 153 if (isHeaderPresent()) { 154 readOneLine(); 155 } 156 } 157 158 164 public boolean hasNext() { 165 if (isReadingComplete()) { 166 return false; 167 } 168 169 if (!haveReadPreviousLine) { 170 readOneLine(); 171 haveReadPreviousLine = true; 172 } 173 return !isReadingComplete(); 174 } 175 176 182 public List <String > next() { 183 184 if (!haveReadPreviousLine) { 185 readOneLine(); 186 } else { 187 haveReadPreviousLine = false; 188 } 189 190 if (isReadingComplete()) { 191 throw new NoSuchElementException (); 192 } 193 194 final List <String > valueList = new ArrayList <String >( 195 parsedValues.length); 196 CollectionUtils.addAll(valueList, parsedValues); 197 198 return valueList; 199 200 } 201 202 206 private void readOneLine() { 207 try { 208 parsedValues = getParser().getLine(); 209 if (parsedValues == null) { 210 readingIsComplete(); 211 } 212 } catch (final IOException e) { 213 LOG.warn("Error in reading a line from the CSV stream ", e); 214 readingIsComplete(); 215 } 216 217 } 218 219 224 public void remove() { 225 LOG 226 .debug("Invalid call to the unsupported remove() method on the iterator"); 227 throw new UnsupportedOperationException ( 228 "This method is not supported"); 229 } 230 } 231 232 237 public boolean isHeaderPresent() { 238 return this.headerPresent; 239 } 240 241 246 public boolean isReadingComplete() { 247 return this.readingComplete; 248 } 249 250 253 protected void readingIsComplete() { 254 this.readingComplete = true; 255 } 256 257 262 protected CSVParse getParser() { 263 return this.parser; 264 } 265 } 266 | Popular Tags |