1 package tim.prune.load;
3 import tim.prune.I18nManager;
4 import tim.prune.data.Field;
7 * Class responsible for splitting the file contents into an array
8 * based on the selected delimiter character
10 public class FileSplitter
12 private FileCacher _cacher = null;
13 private int _numRows = 0;
14 private int _numColumns = 0;
15 private boolean[] _columnStates = null;
19 * @param inCacher FileCacher object holding file contents
21 public FileSplitter(FileCacher inCacher)
27 * Split the FileCacher's contents into a 2d array
28 * @param inDelim delimiter character
29 * @return 2d Object array
31 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;
52 _numColumns = maxFields;
53 _columnStates = new boolean[maxFields];
55 // Create array and populate it
56 // Note that array will be rectangular even if data is ragged
57 String[][] result = new String[_numRows][];
58 for (int i=0; i<contents.length; i++)
60 result[i] = new String[maxFields];
61 if (contents[i] != null)
63 String wholeLine = contents[i].trim();
64 if (!wholeLine.equals(""))
66 String[] splitLine = wholeLine.split(delimStr);
67 if (splitLine != null)
69 System.arraycopy(splitLine, 0, result[i], 0, splitLine.length);
70 // Check if columns are blank or not
71 for (int j=0; j<splitLine.length; j++)
73 if (!_columnStates[j] && splitLine[j].trim().length() > 0)
75 _columnStates[j] = true;
87 * @return the number of rows in the data
89 public int getNumRows()
96 * @return the number of columns in the data
98 public int getNumColumns()
105 * Check if the specified column of the data is blank
106 * @param inColumnNum number of column, starting with 0
107 * @return true if no data exists in this column
109 public boolean isColumnBlank(int inColumnNum)
111 // Should probably trap out of range values
112 return !_columnStates[inColumnNum];
117 * @return a Field array to use as defaults for the data
119 public Field[] makeDefaultFields()
121 Field[] fields = null;
124 fields = new Field[_numColumns];
127 fields[0] = Field.LATITUDE;
128 fields[1] = Field.LONGITUDE;
129 fields[2] = Field.ALTITUDE;
130 fields[3] = Field.WAYPT_NAME;
131 fields[4] = Field.WAYPT_TYPE;
132 String customPrefix = I18nManager.getText("fieldname.prefix") + " ";
135 fields[i] = new Field(customPrefix + (i+1));
138 catch (ArrayIndexOutOfBoundsException finished)
140 // Finished populating array
144 fields = new Field[0];