avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ran...@apache.org
Subject cvs commit: jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/util VirtualDirectory.java
Date Thu, 10 Oct 2002 15:59:04 GMT
rana_b      2002/10/10 08:59:04

  Modified:    ftpserver/src/java/org/apache/avalon/ftpserver/util
                        VirtualDirectory.java
  Log:
  directory listing bug fixed
  
  Revision  Changes    Path
  1.7       +127 -122  jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/util/VirtualDirectory.java
  
  Index: VirtualDirectory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/util/VirtualDirectory.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- VirtualDirectory.java	20 May 2002 10:20:18 -0000	1.6
  +++ VirtualDirectory.java	10 Oct 2002 15:59:04 -0000	1.7
  @@ -9,9 +9,11 @@
   package org.apache.avalon.ftpserver.util;
   
   import java.io.File;
  -import java.io.Serializable;
   import java.io.Writer;
   import java.io.IOException;
  +import java.io.Serializable;
  +import java.util.Comparator;
  +import java.util.Arrays;
   import java.util.Date;
   import java.util.StringTokenizer;
   
  @@ -67,9 +69,6 @@
        * Set root directory.
        */
       public void setRootDirectory(String root) throws IOException {
  -        //File rootFile = new File(root).getCanonicalFile();
  -        //setRootDirectory(rootFile);
  -
          mstRoot = normalizeSeparateChar(root);
   
          // if not ends with '/' - add one
  @@ -239,69 +238,21 @@
        */
       public boolean printList(String argument, Writer out) throws IOException {
   
  -        String lsDirName = "./";
  -        String options = "";
  -        String pattern   = "*";
  -
  -        // get options, directory name and pattern
  -        if(argument != null) {
  -            argument = argument.trim();
  -            StringBuffer optionsSb = new StringBuffer(4);
  -            StringTokenizer st = new StringTokenizer(argument, " ");
  -            while(st.hasMoreTokens()) {
  -                String token = st.nextToken();
  -                if(token.charAt(0) == '-') {
  -                   if (token.length() > 1) {
  -                       optionsSb.append(token.substring(1));
  -                   }
  -                }
  -                else {
  -                   lsDirName = token;
  -                }
  -            }
  -            options = optionsSb.toString();
  -        }
  -
  -        // check options
  -        boolean bAll = options.indexOf('a') != -1;
  -        boolean bDetail = options.indexOf('l') != -1;
  -
  -        // check pattern
  -        lsDirName = getPhysicalName(lsDirName);
  -        int slashIndex = lsDirName.lastIndexOf('/');
  -        if( (slashIndex != -1) && (slashIndex != (lsDirName.length() -1)) ) {
  -            pattern = lsDirName.substring(slashIndex+1);
  -            lsDirName = lsDirName.substring(0, slashIndex+1);
  -        }
  -
  -        // check directory
  -        File lstDirObj = new File(lsDirName);
  -        if(!lstDirObj.exists()) {
  -            return false;
  -        }
  -        if(!lstDirObj.isDirectory()) {
  +        FileLister lister = new FileLister(argument);
  +        File[] flLst = lister.getFiles();
  +        if (flLst == null) {
               return false;
           }
  -
  -        // now print
  -        File flLst[];
  -        if ( (pattern == null) || pattern.equals("*") || pattern.equals("") ) {
  -            flLst = lstDirObj.listFiles();
  -        }
           else {
  -            flLst = lstDirObj.listFiles(new FileRegularFilter(pattern));
  -        }
  -
  -        if(flLst != null) {
               for(int i=0; i<flLst.length; i++) {
  -                if ( (!bAll) && flLst[i].isHidden() ) {
  +                if ( (!lister.isAll()) && flLst[i].isHidden() ) {
                       continue;
                   }
                   printLine(flLst[i], out);
                   out.write(NEWLINE);
               }
  +            return true;
           }
  -        return true;
       }
   
   
  @@ -315,66 +266,17 @@
        */
       public boolean printNList(String argument, Writer out) throws IOException {
   
  -        String lsDirName = "./";
  -        String options = "";
  -        String pattern   = "*";
  -
  -        // get options, directory name and pattern
  -        if(argument != null) {
  -            argument = argument.trim();
  -            StringBuffer optionsSb = new StringBuffer(4);
  -            StringTokenizer st = new StringTokenizer(argument, " ");
  -            while(st.hasMoreTokens()) {
  -                String token = st.nextToken();
  -                if(token.charAt(0) == '-') {
  -                    if (token.length() > 1) {
  -                        optionsSb.append(token.substring(1));
  -                    }
  -                }
  -                else {
  -                   lsDirName = token;
  -                }
  -            }
  -            options = optionsSb.toString();
  -        }
  -
  -        // check options
  -        boolean bAll = options.indexOf('a') != -1;
  -        boolean bDetail = options.indexOf('l') != -1;
  -
  -        // check pattern
  -        lsDirName = getPhysicalName(lsDirName);
  -        int slashIndex = lsDirName.lastIndexOf('/');
  -        if( (slashIndex != -1) && (slashIndex != (lsDirName.length() -1)) ) {
  -            pattern = lsDirName.substring(slashIndex+1);
  -            lsDirName = lsDirName.substring(0, slashIndex+1);
  -        }
  -
  -        // check directory
  -        File lstDirObj = new File(lsDirName);
  -        if(!lstDirObj.exists()) {
  -            return false;
  -        }
  -        if(!lstDirObj.isDirectory()) {
  +        FileLister lister = new FileLister(argument);        
  +        File[] flLst = lister.getFiles();
  +        if (flLst == null) {
               return false;
           }
  -
  -        // get file list
  -        File flLst[];
  -        if ( (pattern == null) || pattern.equals("*") || pattern.equals("") ) {
  -            flLst = lstDirObj.listFiles();
  -        }
           else {
  -            flLst = lstDirObj.listFiles(new FileRegularFilter(pattern));
  -        }
  -
  -        // print file list
  -        if (flLst != null) {
               for(int i=0; i<flLst.length; i++) {
  -                if ( (!bAll) && flLst[i].isHidden() ) {
  +                if ( (!lister.isAll()) && flLst[i].isHidden() ) {
                       continue;
                   }
  -                if(bDetail) {
  +                if(lister.isDetail()) {
                       printLine(flLst[i], out);
                   }
                   else {
  @@ -382,8 +284,8 @@
                   }
                   out.write(NEWLINE);
               }
  +            return true;
           }
  -        return true;
       }
   
       /**
  @@ -441,20 +343,12 @@
           return DateUtils.getUnixDate(date);
       }
   
  +    
       /**
        * Get file name.
        */
       private String getName(File fl) {
  -        String flName = fl.getName();
  -        flName = normalizeSeparateChar(flName);
  -
  -        int lastIndex = flName.lastIndexOf("/");
  -        if(lastIndex == -1) {
  -            return flName;
  -        }
  -        else {
  -            return flName.substring(lastIndex + 1);
  -        }
  +        return fl.getName();
       }
   
   
  @@ -595,4 +489,115 @@
           return str;
       }
   
  +    
  +    
  +    //////////////////////////////////////////////////////////////////////
  +    //////////////////////////////////////////////////////////////////////
  +    /**
  +     * Inner class to compare files
  +     */
  +    private class FileComparator implements Comparator {
  +        public int compare(Object o1, Object o2) {
  +            String s1 = ((File)o1).getName();
  +            String s2 = ((File)o2).getName();
  +            return s1.compareTo(s2);
  +        }
  +    } 
  +        
  +        
  +    /////////////////////////////////////////////////////////////////////
  +    /////////////////////////////////////////////////////////////////////
  +    /**
  +     * File lister to list files properly
  +     */    
  +    private class FileLister {
  +        
  +        private File[] files = null;
  +        private boolean bAll = false;
  +        private boolean bDetail = false; 
  +        
  +        /**
  +         * Parse arguments - get options and get file list.
  +         */
  +        public FileLister(String argument) {
  +            String lsDirName = "./";
  +            String options = "";
  +            String pattern   = "*";
  +        
  +            // get options, directory name and pattern
  +            if(argument != null) {
  +                argument = argument.trim();
  +                StringBuffer optionsSb = new StringBuffer(4);
  +                StringTokenizer st = new StringTokenizer(argument, " ");
  +                while(st.hasMoreTokens()) {
  +                    String token = st.nextToken();
  +                    if(token.charAt(0) == '-') {
  +                        if (token.length() > 1) {
  +                            optionsSb.append(token.substring(1));
  +                        }
  +                    }
  +                    else {
  +                       lsDirName = token;
  +                    }
  +                }
  +                options = optionsSb.toString();
  +            }
  +
  +            // check options
  +            bAll = options.indexOf('a') != -1;
  +            bDetail = options.indexOf('l') != -1;
  +
  +            // check pattern
  +            lsDirName = getPhysicalName(lsDirName);
  +            File lstDirObj = new File(lsDirName);
  +            if ( !(lstDirObj.exists() && lstDirObj.isDirectory()) ) {
  +                int slashIndex = lsDirName.lastIndexOf('/');
  +                if( (slashIndex != -1) && (slashIndex != (lsDirName.length() -1))
) {
  +                    pattern = lsDirName.substring(slashIndex+1);
  +                    lsDirName = lsDirName.substring(0, slashIndex+1);
  +                }
  +            }
  +        
  +            // check directory
  +            lstDirObj = new File(lsDirName);
  +            if(!lstDirObj.exists()) {
  +                return;
  +            }
  +            if(!lstDirObj.isDirectory()) {
  +                return;
  +            }
  +
  +            // get file list
  +            if ( (pattern == null) || pattern.equals("*") || pattern.equals("") ) {
  +                files = lstDirObj.listFiles();
  +            }
  +            else {
  +                files = lstDirObj.listFiles(new FileRegularFilter(pattern));
  +            }
  +            //Arrays.sort(files, new FileComparator());
  +        }
  +        
  +        
  +        /**
  +         * Get files
  +         */
  +        public File[] getFiles() {
  +            return files;
  +        } 
  +        
  +        /**
  +         * Display all flag
  +         */
  +        public boolean isAll() {
  +            return bAll;
  +        } 
  +        
  +        /**
  +         * Display detail flag
  +         */
  +        public boolean isDetail() {
  +            return bDetail;
  +        } 
  +    }
  +        
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message