1 package tim.prune.load;
4 * Class responsible for splitting the file contents into an array
5 * based on the selected delimiter character
7 public class FileSplitter
9 private ContentCacher _cacher = null;
10 private int _numRows = 0;
11 private int _numColumns = 0;
12 private boolean[] _columnStates = null;
13 private String[] _firstFullRow = null;
18 * @param inCacher cacher object holding file contents
20 public FileSplitter(ContentCacher inCacher)
26 * Split the FileCacher's contents into a 2d array
27 * @param inDelim delimiter character
28 * @return 2d Object array
30 public String[][] splitFieldData(char inDelim)
33 if (_cacher == null) return null;
34 String[] contents = _cacher.getContents();
35 if (contents == null || contents.length == 0) return null;
36 String delimStr = checkDelimiter(inDelim);
37 // Count non-blank rows and max field count
40 for (int i=0; i<contents.length; i++)
42 if (contents[i] != null && !contents[i].trim().equals(""))
45 String[] splitLine = contents[i].split(delimStr);
46 if (splitLine != null && splitLine.length > maxFields)
48 maxFields = splitLine.length;
49 _firstFullRow = splitLine;
53 _numColumns = maxFields;
54 _columnStates = new boolean[maxFields];
56 // Create array and populate it
57 // Note that array will be rectangular even if data is ragged
58 String[][] result = new String[_numRows][];
59 for (int i=0; i<contents.length; i++)
61 result[i] = new String[maxFields];
62 if (contents[i] != null)
64 String wholeLine = contents[i];
65 if (!wholeLine.trim().equals(""))
67 // Don't use trimmed string here because you'll lose empty fields at beginning
68 // if separated by spaces or tabs
69 String[] splitLine = wholeLine.split(delimStr);
70 if (splitLine != null)
72 System.arraycopy(splitLine, 0, result[i], 0, splitLine.length);
73 // Check if columns are blank or not
74 for (int j=0; j<splitLine.length; j++)
76 if (!_columnStates[j] && splitLine[j].trim().length() > 0)
78 _columnStates[j] = true;
90 * @return the number of rows in the data
92 public int getNumRows()
99 * @return the number of columns in the data
101 public int getNumColumns()
107 * @return the fields in the first full row
109 public String[] getFirstFullRow()
111 return _firstFullRow;
116 * Check if the specified column of the data is blank
117 * @param inColumnNum number of column, starting with 0
118 * @return true if no data exists in this column
120 public boolean isColumnBlank(int inColumnNum)
122 // Should probably trap out of range values
123 return !_columnStates[inColumnNum];
127 * Check the delimiter for proper regular expression matching
128 * @param inDelim character selected as delimiter
129 * @return regular expression for splitting
131 private static String checkDelimiter(char inDelim)
133 String result = "" + inDelim;
134 // Don't pass asterisks or dots without escaping them for RE
135 if (inDelim == '*' || inDelim == '.') {
136 result = "\\" + result;