]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/load/FileSplitter.java
Version 8, September 2009
[GpsPrune.git] / tim / prune / load / FileSplitter.java
index 216e027c16b2562e6bc52c75483021a30d4292c9..60c2c5dbcf5e429e21d4d3d76a18460d273b0525 100644 (file)
@@ -1,8 +1,5 @@
 package tim.prune.load;
 
-import tim.prune.I18nManager;
-import tim.prune.data.Field;
-
 /**
  * Class responsible for splitting the file contents into an array
  * based on the selected delimiter character
@@ -13,6 +10,8 @@ public class FileSplitter
        private int _numRows = 0;
        private int _numColumns = 0;
        private boolean[] _columnStates = null;
+       private String[] _firstFullRow = null;
+
 
        /**
         * Constructor
@@ -30,10 +29,11 @@ public class FileSplitter
         */
        public String[][] splitFieldData(char inDelim)
        {
+               _firstFullRow = null;
                if (_cacher == null) return null;
                String[] contents = _cacher.getContents();
                if (contents == null || contents.length == 0) return null;
-               String delimStr = "" + inDelim;
+               String delimStr = checkDelimiter(inDelim);
                // Count non-blank rows and max field count
                _numRows = 0;
                int maxFields = 0;
@@ -46,6 +46,7 @@ public class FileSplitter
                                if (splitLine != null && splitLine.length > maxFields)
                                {
                                        maxFields = splitLine.length;
+                                       _firstFullRow = splitLine;
                                }
                        }
                }
@@ -60,9 +61,11 @@ public class FileSplitter
                        result[i] = new String[maxFields];
                        if (contents[i] != null)
                        {
-                               String wholeLine = contents[i].trim();
-                               if (!wholeLine.equals(""))
+                               String wholeLine = contents[i];
+                               if (!wholeLine.trim().equals(""))
                                {
+                                       // Don't use trimmed string here because you'll lose empty fields at beginning
+                                       // if separated by spaces or tabs
                                        String[] splitLine = wholeLine.split(delimStr);
                                        if (splitLine != null)
                                        {
@@ -100,6 +103,14 @@ public class FileSplitter
                return _numColumns;
        }
 
+       /**
+        * @return the fields in the first full row
+        */
+       public String[] getFirstFullRow()
+       {
+               return _firstFullRow;
+       }
+
 
        /**
         * Check if the specified column of the data is blank
@@ -112,36 +123,18 @@ public class FileSplitter
                return !_columnStates[inColumnNum];
        }
 
-
        /**
-        * @return a Field array to use as defaults for the data
+        * Check the delimiter for proper regular expression matching
+        * @param inDelim character selected as delimiter
+        * @return regular expression for splitting
         */
-       public Field[] makeDefaultFields()
+       private static String checkDelimiter(char inDelim)
        {
-               Field[] fields = null;
-               if (_numColumns > 0)
-               {
-                       fields = new Field[_numColumns];
-                       try
-                       {
-                               fields[0] = Field.LATITUDE;
-                               fields[1] = Field.LONGITUDE;
-                               fields[2] = Field.ALTITUDE;
-                               fields[3] = Field.WAYPT_NAME;
-                               fields[4] = Field.WAYPT_TYPE;
-                               String customPrefix = I18nManager.getText("fieldname.prefix") + " ";
-                               for (int i=5;; i++)
-                               {
-                                       fields[i] = new Field(customPrefix + (i+1));
-                               }
-                       }
-                       catch (ArrayIndexOutOfBoundsException finished)
-                       {
-                               // Finished populating array
-                       }
+               String result = "" + inDelim;
+               // Don't pass asterisks or dots without escaping them for RE
+               if (inDelim == '*' || inDelim == '.') {
+                       result = "\\" + result;
                }
-               else
-                       fields = new Field[0];
-               return fields;
+               return result;
        }
 }