Author: sebb
Date: Sat Jan 17 15:06:53 2015
New Revision: 1652621

URL: http://svn.apache.org/r1652621
Log:
NET-566 UnixFTPEntryParser Drops Leading Spaces from File Names
Add option to parser to not skip leading spaces
TODO make this the default but still allow reversion

Modified:
    
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParser.java
    
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParserTest.java

Modified: 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParser.java
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParser.java?rev=1652621&r1=1652620&r2=1652621&view=diff
==============================================================================
--- 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParser.java
 (original)
+++ 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParser.java
 Sat Jan 17 15:06:53 2015
@@ -122,11 +122,15 @@ public class UnixFTPEntryParser extends
         */
         + "(\\d+(?::\\d+)?)" // (20)
 
-        + "\\s+" // separator
+        + "\\s" // separator
 
         + "(.*)"; // the rest (21)
 
 
+    // if true, leading spaces are trimmed from file names
+    // this was the case for the original implementation
+    private final boolean trimLeadingSpaces;
+
     /**
      * The default constructor for a UnixFTPEntryParser object.
      *
@@ -154,8 +158,27 @@ public class UnixFTPEntryParser extends
      */
     public UnixFTPEntryParser(FTPClientConfig config)
     {
+        this(config, true); // retain original behaviour (for now)
+    }
+
+    /**
+     * This constructor allows the creation of a UnixFTPEntryParser object with
+     * something other than the default configuration.
+     *
+     * @param config The {@link FTPClientConfig configuration} object used to
+     * configure this parser.
+     * @param trimLeadingSpaces if {@code true}, trim leading spaces from file 
names
+     * @exception IllegalArgumentException
+     * Thrown if the regular expression is unparseable.  Should not be seen
+     * under normal conditions.  It it is seen, this is a sign that
+     * <code>REGEX</code> is  not a valid regular expression.
+     * @since 1.4
+     */
+    public UnixFTPEntryParser(FTPClientConfig config, boolean 
trimLeadingSpaces)
+    {
         super(REGEX);
         configure(config);
+        this.trimLeadingSpaces = trimLeadingSpaces;
     }
 
     /**
@@ -199,6 +222,9 @@ public class UnixFTPEntryParser extends
             String filesize = group(18);
             String datestr = group(19) + " " + group(20);
             String name = group(21);
+            if (trimLeadingSpaces) {
+                name = name.replaceFirst("^\\s+", "");
+            }
 
             try
             {

Modified: 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParserTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParserTest.java?rev=1652621&r1=1652620&r2=1652621&view=diff
==============================================================================
--- 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParserTest.java
 (original)
+++ 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/UnixFTPEntryParserTest.java
 Sat Jan 17 15:06:53 2015
@@ -166,6 +166,18 @@ public class UnixFTPEntryParserTest exte
         assertEquals(f.getName(), "zxbox     ");
     }
 
+    public void testLeadingSpacesOriginal() { // this is the original 
(non-ideal) behaviour
+        FTPFile f = getParser().parseFTPEntry("drwxr-xr-x   2 john smith     
group         4096 Mar  2 15:13   zxbox");
+        assertNotNull(f);
+        assertEquals("zxbox", f.getName() ); // leading spaces dropped
+    }
+
+    public void testLeadingSpacesNET566() { // check new behaviour
+        FTPFile f = new UnixFTPEntryParser(null, 
false).parseFTPEntry("drwxr-xr-x   2 john smith     group         4096 Mar  2 
15:13   zxbox");
+        assertNotNull(f);
+        assertEquals("  zxbox", f.getName() ); // leading spaces retained
+    }
+
     public void testNameWIthPunctuation() {
         FTPFile f = getParser().parseFTPEntry("drwx------ 4 maxm Domain Users 
512 Oct 2 10:59 abc(test)123.pdf");
         assertNotNull(f);


Reply via email to