/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* User: andy
* Date: 30/07/2006
* Time: 08:37
*/
package uk.org.aspellclark.common.file;
/// <summary>
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVStrategy;
/**
* <p>This is a base class that handles all interaction with a delimited text file.
* It can be sub classed to provide handling for more specific files.</p><p>
* It contains member functions to open and close a file, to read in the first and subsequent
* lines of the file as field lists as well as simple strings.</p>
* @author andy
*
*/
public class DelimitedFile extends AsciiFileReader {
/**
* the character used to delimit the fields in the file
*/
protected String m_sDelimiter = ",";
protected String m_sFilepath = "./";
public DelimitedFile() {
}
public DelimitedFile(String psDelimiter) {
this.m_sDelimiter = psDelimiter;
}
public ArrayList<String> convertLineToFields(String line) {
// Log.w("DelimitedFile", "convertLineToFields converting line [" + line +
// "] to fields");
ArrayList<String> alFields = new ArrayList<String>();
CSVStrategy csvStrat = new CSVStrategy(this.m_sDelimiter.charAt(0), '\"', '#');
csvStrat.setIgnoreLeadingWhitespaces(true);
CSVParser csvParser = new CSVParser(new StringReader(line), csvStrat);
String[][] st;
try {
st = csvParser.getAllValues();
for (int lineNbr = 0; lineNbr < st.length; lineNbr++) {
for (int tokenNbr = 0; tokenNbr < st[lineNbr].length; tokenNbr++) {
alFields.add(st[lineNbr][tokenNbr]);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return alFields;
}
// <editor-fold desc="Properties">
public String getDelimiter() {
return this.m_sDelimiter;
}
// </editor-fold>
/**
* reads the first line of the file
*
* @return a list of the fields in the first line of the file
*/
public ArrayList<String> readFirstLineFromFile() {
ArrayList<String> alFields = null;
String line = super.FirstLineFromFile();
if (line != null) {
alFields = new ArrayList<String>();
alFields = this.convertLineToFields(line);
}
return alFields;
}
/**
*
* @return a list of the fields in the next line of the file
*/
public ArrayList<String> readNextLineFromFile() {
ArrayList<String> alFields = new ArrayList<String>();
String line = super.NextLineFromFile();
alFields = this.convertLineToFields(line);
return alFields;
}
public void setDelimiter(String aDelim) {
this.m_sDelimiter = aDelim;
}
} // class()
|