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 FileCacher _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 FileCacher object holding file contents
20 public FileSplitter(FileCacher 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 = "" + 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].trim();
65 if (!wholeLine.equals(""))
67 String[] splitLine = wholeLine.split(delimStr);
68 if (splitLine != null)
70 System.arraycopy(splitLine, 0, result[i], 0, splitLine.length);
71 // Check if columns are blank or not
72 for (int j=0; j<splitLine.length; j++)
74 if (!_columnStates[j] && splitLine[j].trim().length() > 0)
76 _columnStates[j] = true;
88 * @return the number of rows in the data
90 public int getNumRows()
97 * @return the number of columns in the data
99 public int getNumColumns()
105 * @return the fields in the first full row
107 public String[] getFirstFullRow()
109 return _firstFullRow;
114 * Check if the specified column of the data is blank
115 * @param inColumnNum number of column, starting with 0
116 * @return true if no data exists in this column
118 public boolean isColumnBlank(int inColumnNum)
120 // Should probably trap out of range values
121 return !_columnStates[inColumnNum];