]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/FileSplitter.java
Version 4, January 2008
[GpsPrune.git] / tim / prune / load / FileSplitter.java
1 package tim.prune.load;
2
3 /**
4  * Class responsible for splitting the file contents into an array
5  * based on the selected delimiter character
6  */
7 public class FileSplitter
8 {
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;
14
15
16         /**
17          * Constructor
18          * @param inCacher FileCacher object holding file contents
19          */
20         public FileSplitter(FileCacher inCacher)
21         {
22                 _cacher = inCacher;
23         }
24
25         /**
26          * Split the FileCacher's contents into a 2d array
27          * @param inDelim delimiter character
28          * @return 2d Object array
29          */
30         public String[][] splitFieldData(char inDelim)
31         {
32                 _firstFullRow = null;
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                                         _firstFullRow = splitLine;
50                                 }
51                         }
52                 }
53                 _numColumns = maxFields;
54                 _columnStates = new boolean[maxFields];
55
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++)
60                 {
61                         result[i] = new String[maxFields];
62                         if (contents[i] != null)
63                         {
64                                 String wholeLine = contents[i].trim();
65                                 if (!wholeLine.equals(""))
66                                 {
67                                         String[] splitLine = wholeLine.split(delimStr);
68                                         if (splitLine != null)
69                                         {
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++)
73                                                 {
74                                                         if (!_columnStates[j] && splitLine[j].trim().length() > 0)
75                                                         {
76                                                                 _columnStates[j] = true;
77                                                         }
78                                                 }
79                                         }
80                                 }
81                         }
82                 }
83                 return result;
84         }
85
86
87         /**
88          * @return the number of rows in the data
89          */
90         public int getNumRows()
91         {
92                 return _numRows;
93         }
94
95
96         /**
97          * @return the number of columns in the data
98          */
99         public int getNumColumns()
100         {
101                 return _numColumns;
102         }
103
104         /**
105          * @return the fields in the first full row
106          */
107         public String[] getFirstFullRow()
108         {
109                 return _firstFullRow; 
110         }
111
112
113         /**
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
117          */
118         public boolean isColumnBlank(int inColumnNum)
119         {
120                 // Should probably trap out of range values
121                 return !_columnStates[inColumnNum];
122         }
123 }