package com.kiasoft.util; /* ------------------------------------------------------------------ * Kiasoft, Inc. * http://kiasoft.com * * THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT * LIMITATION, WARRANTIES THAT THE PROGRAM IS FREE OF * DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR * NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND * PERFORMANCE OF THE PROGRAM IS WITH YOU. * * This code is protected under the GPL as it is a derivative work * of another GPL project * * A copy of the GPL can be found at http://kiasoft.com/opensource/GPL * ------------------------------------------------------------------ */ // JDK classes import java.util.List; import java.util.Vector; // file handling import java.io.InputStream; import java.io.File; import java.io.FileInputStream; // Turbine classes import org.apache.turbine.services.servlet.TurbineServlet; import org.apache.turbine.util.Log; // use Ostermiller's ExcelCSVParser to help us out import com.Ostermiller.util.ExcelCSVParser; /** * CsvParser.java * Provide simple interface to parse through csv files * @author: painter@kiasoft.com */ public class CsvParser implements java.util.Iterator { private Vector _columnNames = null; private Vector _rows = null; private List currentRow = null; /* * Constructor */ public CsvParser( File filename ) throws Exception { this._columnNames = new Vector(); this._rows = new Vector(); this.currentRow = null; // Setup the data from our Csv file // ------------------------------------------------------- // Parse the data InputStream in = new FileInputStream( filename ); // use ostermiller excel parser String[][] values = new ExcelCSVParser(in).getAllValues(); // load the data into lists for (int i=0; i 0 ) { return true; } else { return false; } } /* * implement Iterator interface next() */ public Object next() { return this.nextRow(); } /* * implement Iterator interface remove() */ public void remove() { this.getRows().remove( 0 ); return; } /* * return next row of data * also will just set currentRow so * we can use our getColumn() method */ public List nextRow() { List row = (Vector) this.getRows().get( 0 ); this.currentRow = row; this.remove(); return row; } /* * when using iterator mode, allow * to use getColumn() method - will return empty if cannot locate field location */ public String getColumn( String column ) { List c = this.getColumns(); for ( int i = 0; i < c.size(); i++ ) { if ( ( (String) c.get(i)).trim() .equals( column.trim() ) ) { return (String) this.currentRow.get(i); } } return ""; } /* * Set the columns with a vector */ public void setColumns( Vector columns ) { this._columnNames = columns; } /* * Return the columns list */ public List getColumns() { return this._columnNames; } /* * each row is a vector of data * stored in another vector */ public void addRow( Vector newrow ) { // make sure rows is not empty before adding if ( this._rows == null ) { this._rows = new Vector(); } this._rows.add( newrow ); return; } /* * return list of all rows */ public List getRows() { return this._rows; } }