commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1084623 - in /commons/proper/net/trunk/src: changes/changes.xml main/java/org/apache/commons/net/ftp/FTPFile.java
Date Wed, 23 Mar 2011 16:12:53 GMT
Author: sebb
Date: Wed Mar 23 16:12:53 2011
New Revision: 1084623

URL: http://svn.apache.org/viewvc?rev=1084623&view=rev
Log:
NET-390 FTPFile.toString() should return formatted version of its fields, not the unformatted
original

Modified:
    commons/proper/net/trunk/src/changes/changes.xml
    commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPFile.java

Modified: commons/proper/net/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1084623&r1=1084622&r2=1084623&view=diff
==============================================================================
--- commons/proper/net/trunk/src/changes/changes.xml (original)
+++ commons/proper/net/trunk/src/changes/changes.xml Wed Mar 23 16:12:53 2011
@@ -57,6 +57,9 @@ The <action> type attribute can be add,u
 
     <body>
         <release version="3.0" date="TBA" description="TBA">
+            <action issue="NET-390" dev="sebb" type="update">
+            FTPFile.toString() should return formatted version of its fields, not the unformatted
original
+            </action>
             <action issue="NET-389" dev="sebb" type="fix">
             Unix parser should ignore "total nnn" lines.
             </action>

Modified: commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPFile.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPFile.java?rev=1084623&r1=1084622&r2=1084623&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPFile.java (original)
+++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPFile.java Wed Mar
23 16:12:53 2011
@@ -18,6 +18,7 @@
 package org.apache.commons.net.ftp;
 import java.io.Serializable;
 import java.util.Calendar;
+import java.util.Formatter;
 
 /***
  * The FTPFile class is used to represent information about files stored
@@ -63,7 +64,7 @@ public class FTPFile implements Serializ
     long _size;
     String _rawListing, _user, _group, _name, _link;
     Calendar _date;
-    boolean[] _permissions[];
+    boolean[] _permissions[]; // e.g. _permissions[USER_ACCESS][READ_PERMISSION]
 
     /*** Creates an empty FTPFile. ***/
     public FTPFile()
@@ -73,8 +74,8 @@ public class FTPFile implements Serializ
         _type = UNKNOWN_TYPE;
         _hardLinkCount = 0;
         _size = 0;
-        _user = null;
-        _group = null;
+        _user = "";
+        _group = "";
         _date = null;
         _name = null;
     }
@@ -368,26 +369,61 @@ public class FTPFile implements Serializ
 
 
     /***
-     * Returns a string representation of the FTPFile information.  This
-     * will be the raw FTP server listing that was used to initialize the
-     * FTPFile instance.
-     * <p>
+     * Returns a string representation of the FTPFile information.
+     *
      * @return A string representation of the FTPFile information.
-     ***/
+     */
     @Override
     public String toString()
     {
-        return _rawListing;
+        StringBuilder sb = new StringBuilder();
+        Formatter fmt = new Formatter(sb);
+        sb.append(formatType());
+        sb.append(permissionToString(USER_ACCESS));
+        sb.append(permissionToString(GROUP_ACCESS));
+        sb.append(permissionToString(WORLD_ACCESS));
+        fmt.format(" %4d %-8s %-8s", Integer.valueOf(getHardLinkCount()), getGroup(), getUser());
+        fmt.format(" %8d", Long.valueOf(getSize()));
+        Calendar timestamp = getTimestamp();
+        if (timestamp != null) {
+            fmt.format(" %1$tb %1$td  %1$tY", timestamp);
+            fmt.format(" %1$tZ", timestamp);
+            sb.append(' ');
+        }
+        sb.append(' ');
+        sb.append(getName());
+        return sb.toString();
+    }
+
+    private char formatType(){
+        switch(_type) {
+            case FILE_TYPE:
+                return '-';
+            case DIRECTORY_TYPE:
+                return 'd';
+            case SYMBOLIC_LINK_TYPE:
+                return 'l';
+        }
+        return '?';
     }
 
-    /** @since 3.0 */
-    public String toFormattedString(){
+    private String permissionToString(int access ){
         StringBuilder sb = new StringBuilder();
-        sb.append(getName());
-        sb.append(' ');
-        sb.append(getSize());
-        sb.append(' ');
-        sb.append(getTimestamp().getTime().toString());
+        if (hasPermission(access, READ_PERMISSION)) {
+            sb.append('r');
+        } else {
+            sb.append('-');
+        }
+        if (hasPermission(access, WRITE_PERMISSION)) {
+            sb.append('w');
+        } else {
+            sb.append('-');
+        }
+        if (hasPermission(access, EXECUTE_PERMISSION)) {
+            sb.append('x');
+        } else {
+            sb.append('-');
+        }
         return sb.toString();
     }
 }



Mime
View raw message