X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Fload%2FFileSplitter.java;fp=src%2Ftim%2Fprune%2Fload%2FFileSplitter.java;h=60c2c5dbcf5e429e21d4d3d76a18460d273b0525;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/load/FileSplitter.java b/src/tim/prune/load/FileSplitter.java new file mode 100644 index 0000000..60c2c5d --- /dev/null +++ b/src/tim/prune/load/FileSplitter.java @@ -0,0 +1,140 @@ +package tim.prune.load; + +/** + * Class responsible for splitting the file contents into an array + * based on the selected delimiter character + */ +public class FileSplitter +{ + private FileCacher _cacher = null; + private int _numRows = 0; + private int _numColumns = 0; + private boolean[] _columnStates = null; + private String[] _firstFullRow = null; + + + /** + * Constructor + * @param inCacher FileCacher object holding file contents + */ + public FileSplitter(FileCacher inCacher) + { + _cacher = inCacher; + } + + /** + * Split the FileCacher's contents into a 2d array + * @param inDelim delimiter character + * @return 2d Object array + */ + public String[][] splitFieldData(char inDelim) + { + _firstFullRow = null; + if (_cacher == null) return null; + String[] contents = _cacher.getContents(); + if (contents == null || contents.length == 0) return null; + String delimStr = checkDelimiter(inDelim); + // Count non-blank rows and max field count + _numRows = 0; + int maxFields = 0; + for (int i=0; i maxFields) + { + maxFields = splitLine.length; + _firstFullRow = splitLine; + } + } + } + _numColumns = maxFields; + _columnStates = new boolean[maxFields]; + + // Create array and populate it + // Note that array will be rectangular even if data is ragged + String[][] result = new String[_numRows][]; + for (int i=0; i 0) + { + _columnStates[j] = true; + } + } + } + } + } + } + return result; + } + + + /** + * @return the number of rows in the data + */ + public int getNumRows() + { + return _numRows; + } + + + /** + * @return the number of columns in the data + */ + public int getNumColumns() + { + return _numColumns; + } + + /** + * @return the fields in the first full row + */ + public String[] getFirstFullRow() + { + return _firstFullRow; + } + + + /** + * Check if the specified column of the data is blank + * @param inColumnNum number of column, starting with 0 + * @return true if no data exists in this column + */ + public boolean isColumnBlank(int inColumnNum) + { + // Should probably trap out of range values + return !_columnStates[inColumnNum]; + } + + /** + * Check the delimiter for proper regular expression matching + * @param inDelim character selected as delimiter + * @return regular expression for splitting + */ + private static String checkDelimiter(char inDelim) + { + String result = "" + inDelim; + // Don't pass asterisks or dots without escaping them for RE + if (inDelim == '*' || inDelim == '.') { + result = "\\" + result; + } + return result; + } +}