]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/FileSplitter.java
216e027c16b2562e6bc52c75483021a30d4292c9
[GpsPrune.git] / tim / prune / load / FileSplitter.java
1 package tim.prune.load;
2
3 import tim.prune.I18nManager;
4 import tim.prune.data.Field;
5
6 /**
7  * Class responsible for splitting the file contents into an array
8  * based on the selected delimiter character
9  */
10 public class FileSplitter
11 {
12         private FileCacher _cacher = null;
13         private int _numRows = 0;
14         private int _numColumns = 0;
15         private boolean[] _columnStates = null;
16
17         /**
18          * Constructor
19          * @param inCacher FileCacher object holding file contents
20          */
21         public FileSplitter(FileCacher inCacher)
22         {
23                 _cacher = inCacher;
24         }
25
26         /**
27          * Split the FileCacher's contents into a 2d array
28          * @param inDelim delimiter character
29          * @return 2d Object array
30          */
31         public String[][] splitFieldData(char inDelim)
32         {
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
38                 _numRows = 0;
39                 int maxFields = 0;
40                 for (int i=0; i<contents.length; i++)
41                 {
42                         if (contents[i] != null && !contents[i].trim().equals(""))
43                         {
44                                 _numRows++;
45                                 String[] splitLine = contents[i].split(delimStr);
46                                 if (splitLine != null && splitLine.length > maxFields)
47                                 {
48                                         maxFields = splitLine.length;
49                                 }
50                         }
51                 }
52                 _numColumns = maxFields;
53                 _columnStates = new boolean[maxFields];
54
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++)
59                 {
60                         result[i] = new String[maxFields];
61                         if (contents[i] != null)
62                         {
63                                 String wholeLine = contents[i].trim();
64                                 if (!wholeLine.equals(""))
65                                 {
66                                         String[] splitLine = wholeLine.split(delimStr);
67                                         if (splitLine != null)
68                                         {
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++)
72                                                 {
73                                                         if (!_columnStates[j] && splitLine[j].trim().length() > 0)
74                                                         {
75                                                                 _columnStates[j] = true;
76                                                         }
77                                                 }
78                                         }
79                                 }
80                         }
81                 }
82                 return result;
83         }
84
85
86         /**
87          * @return the number of rows in the data
88          */
89         public int getNumRows()
90         {
91                 return _numRows;
92         }
93
94
95         /**
96          * @return the number of columns in the data
97          */
98         public int getNumColumns()
99         {
100                 return _numColumns;
101         }
102
103
104         /**
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
108          */
109         public boolean isColumnBlank(int inColumnNum)
110         {
111                 // Should probably trap out of range values
112                 return !_columnStates[inColumnNum];
113         }
114
115
116         /**
117          * @return a Field array to use as defaults for the data
118          */
119         public Field[] makeDefaultFields()
120         {
121                 Field[] fields = null;
122                 if (_numColumns > 0)
123                 {
124                         fields = new Field[_numColumns];
125                         try
126                         {
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") + " ";
133                                 for (int i=5;; i++)
134                                 {
135                                         fields[i] = new Field(customPrefix + (i+1));
136                                 }
137                         }
138                         catch (ArrayIndexOutOfBoundsException finished)
139                         {
140                                 // Finished populating array
141                         }
142                 }
143                 else
144                         fields = new Field[0];
145                 return fields;
146         }
147 }