]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/load/FileSplitter.java
Version 8, September 2009
[GpsPrune.git] / tim / prune / load / FileSplitter.java
index 99464f705c44000054cc3b48b92b1cbcb7508c3d..60c2c5dbcf5e429e21d4d3d76a18460d273b0525 100644 (file)
@@ -33,7 +33,7 @@ public class FileSplitter
                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;
@@ -61,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)
                                        {
@@ -120,4 +122,19 @@ public class FileSplitter
                // Should probably trap out of range values
                return !_columnStates[inColumnNum];
        }
+
+       /**
+        * Check the delimiter for proper regular expression matching
+        * @param inDelim character selected as delimiter
+        * @return regular expression for splitting
+        */
+       private static String checkDelimiter(char inDelim)
+       {
+               String result = "" + inDelim;
+               // Don't pass asterisks or dots without escaping them for RE
+               if (inDelim == '*' || inDelim == '.') {
+                       result = "\\" + result;
+               }
+               return result;
+       }
 }