You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2008/09/30 12:29:46 UTC

svn commit: r700407 - /geronimo/gshell/trunk/gshell-support/gshell-vfs/src/main/java/org/apache/geronimo/gshell/vfs/FileObjectNameCompleter.java

Author: jdillon
Date: Tue Sep 30 03:29:45 2008
New Revision: 700407

URL: http://svn.apache.org/viewvc?rev=700407&view=rev
Log:
Don't throw exceptions when completing, just mark it as a non-completion
Fixed up some handling of edge cases when completing, got a bit hairy, but it seems to be working for the cases I can think of, pending clean up

Modified:
    geronimo/gshell/trunk/gshell-support/gshell-vfs/src/main/java/org/apache/geronimo/gshell/vfs/FileObjectNameCompleter.java

Modified: geronimo/gshell/trunk/gshell-support/gshell-vfs/src/main/java/org/apache/geronimo/gshell/vfs/FileObjectNameCompleter.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-support/gshell-vfs/src/main/java/org/apache/geronimo/gshell/vfs/FileObjectNameCompleter.java?rev=700407&r1=700406&r2=700407&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-support/gshell-vfs/src/main/java/org/apache/geronimo/gshell/vfs/FileObjectNameCompleter.java (original)
+++ geronimo/gshell/trunk/gshell-support/gshell-vfs/src/main/java/org/apache/geronimo/gshell/vfs/FileObjectNameCompleter.java Tue Sep 30 03:29:45 2008
@@ -22,6 +22,7 @@
 import jline.Completor;
 import org.apache.commons.vfs.FileFilter;
 import org.apache.commons.vfs.FileFilterSelector;
+import org.apache.commons.vfs.FileName;
 import org.apache.commons.vfs.FileObject;
 import org.apache.commons.vfs.FileSelectInfo;
 import org.apache.commons.vfs.FileSystemException;
@@ -30,7 +31,6 @@
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 
-import java.io.File;
 import java.util.Collections;
 import java.util.List;
 
@@ -42,6 +42,8 @@
 public class FileObjectNameCompleter
     implements Completor
 {
+    private static final String PARENT_TOKEN = "..";
+
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     @Autowired
@@ -57,25 +59,57 @@
         
         try {
             assert fileSystemAccess != null;
-            FileObject dir = fileSystemAccess.resolveFile(path);
+            FileObject file = fileSystemAccess.resolveFile(path);
+
+            log.trace("Resolved file: {}", file);
 
             final String search;
 
-            // If we have resolved to a directory which does not exist, then base the selection on its parent and set the
-            // search criteria to the name of the non-existant file
-            if (!dir.exists()) {
-                search = dir.getName().getBaseName();
-                dir = dir.getParent();
+            if (!file.exists() || file.getType() == FileType.FILE) {
+                search = file.getName().getBaseName();
+                
+                if (!path.endsWith(FileName.SEPARATOR)) {
+                    file = file.getParent();
+                }
+            }
+            else if (file.getType() == FileType.FOLDER && !path.endsWith(FileName.SEPARATOR)) {
+                // Handle the special cases when we resolved to a directory, with out a trailing seperator,
+                // complete to the directory + "/" first.
+
+                StringBuilder buff = new StringBuilder();
+
+                // Another special case here with "..", don't use the file name, just use the ".."
+                if (path.endsWith(PARENT_TOKEN)) {
+                    buff.append(PARENT_TOKEN);
+                }
+                else {
+                    buff.append(file.getName().getBaseName());
+                }
+
+                buff.append(FileName.SEPARATOR);
+
+                //
+                // TODO: Need to encode spaces, once the parser can handle escaped spaces.
+                //
+                
+                // noinspection unchecked
+                candidates.add(buff.toString());
+
+                int result = path.lastIndexOf(FileName.SEPARATOR) + 1;
+
+                log.trace("Result: {}", result);
+                
+                return result;
             }
             else {
                 // Else we want to show all contents
                 search = null;
             }
 
-            log.trace("Dir: {}", dir);
+            log.trace("Base File: {}", file);
             log.trace("Search: {}", search);
 
-            if (dir != null) {
+            if (file != null) {
                 FileObject[] files;
 
                 if (search != null) {
@@ -91,10 +125,10 @@
                         }
                     };
 
-                    files = dir.findFiles(new FileFilterSelector(filter));
+                    files = file.findFiles(new FileFilterSelector(filter));
                 }
                 else {
-                    files = dir.getChildren();
+                    files = file.getChildren();
                 }
 
                 if (files == null || files.length == 0) {
@@ -103,14 +137,18 @@
                 else {
                     log.trace("Found {} matching files:", files.length);
 
-                    for (FileObject file : files) {
-                        log.trace("    {}", file);
+                    for (FileObject child : files) {
+                        log.trace("    {}", child);
 
                         StringBuilder buff = new StringBuilder();
-                        buff.append(file.getName().getBaseName());
+                        buff.append(child.getName().getBaseName());
+
+                        //
+                        // TODO: Need to encode spaces, once the parser can handle escaped spaces.
+                        //
 
-                        if (files.length == 1 && file.getType() == FileType.FOLDER) {
-                            buff.append(File.separator);
+                        if (files.length == 1 && child.getType() == FileType.FOLDER) {
+                            buff.append(FileName.SEPARATOR);
                         }
                         else {
                             buff.append(" ");
@@ -125,10 +163,22 @@
                 }
             }
 
-            return path.lastIndexOf(File.separator) + File.separator.length();
+            int result;
+
+            if (search == null) {
+                result  = path.length();
+            }
+            else {
+                result = path.lastIndexOf(FileName.SEPARATOR) + 1;
+            }
+
+            log.trace("Result: {}", result);
+
+            return result;
         }
         catch (FileSystemException e) {
-            throw new RuntimeException("Unable to complete path: " + path, e);
+            log.trace("Unable to complete path: " + path, e);
+            return -1;
         }
     }
 }
\ No newline at end of file