]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/load/FileSplitter.java
Version 20.4, May 2021
[GpsPrune.git] / src / 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 ContentCacher _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 cacher object holding file contents
19          */
20         public FileSplitter(ContentCacher 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 = checkDelimiter(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];
65                                 if (!wholeLine.trim().equals(""))
66                                 {
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)
71                                         {
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++)
75                                                 {
76                                                         if (!_columnStates[j] && splitLine[j].trim().length() > 0)
77                                                         {
78                                                                 _columnStates[j] = true;
79                                                         }
80                                                 }
81                                         }
82                                 }
83                         }
84                 }
85                 return result;
86         }
87
88
89         /**
90          * @return the number of rows in the data
91          */
92         public int getNumRows()
93         {
94                 return _numRows;
95         }
96
97
98         /**
99          * @return the number of columns in the data
100          */
101         public int getNumColumns()
102         {
103                 return _numColumns;
104         }
105
106         /**
107          * @return the fields in the first full row
108          */
109         public String[] getFirstFullRow()
110         {
111                 return _firstFullRow;
112         }
113
114
115         /**
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
119          */
120         public boolean isColumnBlank(int inColumnNum)
121         {
122                 // Should probably trap out of range values
123                 return !_columnStates[inColumnNum];
124         }
125
126         /**
127          * Check the delimiter for proper regular expression matching
128          * @param inDelim character selected as delimiter
129          * @return regular expression for splitting
130          */
131         private static String checkDelimiter(char inDelim)
132         {
133                 String result = "" + inDelim;
134                 // Don't pass asterisks or dots without escaping them for RE
135                 if (inDelim == '*' || inDelim == '.') {
136                         result = "\\" + result;
137                 }
138                 return result;
139         }
140 }