1 21 22 package org.armedbear.j; 23 24 import java.util.List ; 25 import java.util.ArrayList ; 26 27 public final class Completion 28 { 29 private String input; 30 private String toBeCompleted; 31 private boolean ignoreCase; 32 private boolean cygnify; 33 34 private ArrayList list = new ArrayList (); 35 36 public Completion(File dir, String input, String shellCommand) 37 { 38 if (Platform.isPlatformWindows()) { 39 if (shellCommand != null && shellCommand.toLowerCase().indexOf("cmd.exe") < 0) 40 cygnify = true; 41 } 42 final char separatorChar = getSeparatorChar(); 43 this.input = input; 44 toBeCompleted = input; 45 if (Platform.isPlatformWindows()) { 46 if (cygnify) { 47 if (toBeCompleted.startsWith("~/")) { 48 String home = Utilities.getUserHome(); 49 if (!home.startsWith("/")) 50 home = Utilities.cygnify(home); 51 toBeCompleted = Utilities.uncygnify(home + toBeCompleted.substring(1)); 52 } else if (toBeCompleted.startsWith("../")) 53 toBeCompleted = File.normalize(toBeCompleted); 54 else 55 toBeCompleted = Utilities.uncygnify(toBeCompleted); 56 } else { 57 if (toBeCompleted.startsWith("~/")) { 58 String home = Utilities.getUserHome(); 59 toBeCompleted = home + toBeCompleted.substring(1); 60 } else 61 toBeCompleted = File.normalize(toBeCompleted); 62 } 63 ignoreCase = true; 64 } 65 FilenameCompletion c = new FilenameCompletion(dir, toBeCompleted, null, 66 ignoreCase); 67 List files = c.listFiles(); 68 if (files != null) { 69 String home = Utilities.getUserHome() + "/"; 70 String prefix = dir.canonicalPath() + LocalFile.getSeparator(); 71 int skip = prefix.length(); 72 String toBeAdded; 73 int limit = files.size(); 74 for (int i = 0; i < limit; i++) { 75 File file = (File) files.get(i); 76 toBeAdded = file.getAbsolutePath(); 77 if (!Utilities.isFilenameAbsolute(toBeCompleted)) { 78 if (toBeAdded.startsWith(prefix)) { 79 toBeAdded = toBeAdded.substring(skip); 80 if (input.startsWith("./")) 81 toBeAdded = "./" + toBeAdded; 82 } 83 } 84 if (cygnify) 85 toBeAdded = Utilities.cygnify(toBeAdded); 86 if (input.startsWith("~/")) { 87 if (toBeAdded.startsWith(home)) { 88 toBeAdded = "~/" + toBeAdded.substring(home.length()); 89 } 90 } else if (input.startsWith("..")) { 91 String remaining = input; 92 File parentDir = dir; 93 String parentPrefix = ""; 94 while (remaining.startsWith("../")) { 95 parentDir = parentDir.getParentFile(); 96 parentPrefix += "../"; 97 remaining = remaining.substring(3); 98 } 99 String parentDirName = parentDir.canonicalPath(); 100 if (cygnify) 101 parentDirName = Utilities.cygnify(parentDirName); 102 if (!parentDirName.endsWith("/")) 103 parentDirName += "/"; 104 if (toBeAdded.startsWith(parentDirName)) 105 toBeAdded = parentPrefix + toBeAdded.substring(parentDirName.length()); 106 } 107 toBeAdded = escapeSpaces(toBeAdded); 108 if (file.isDirectory()) 109 toBeAdded += separatorChar; 110 list.add(toBeAdded); 111 } 112 } 113 } 114 115 private static final String escapeSpaces(String s) 117 { 118 final int length = s.length(); 119 FastStringBuffer sb = new FastStringBuffer(length * 2); 120 for (int i = 0; i < length; i++) { 121 char c = s.charAt(i); 122 if (c == ' ') 123 sb.append('\\'); 124 sb.append(c); 125 } 126 return sb.toString(); 127 } 128 129 private final char getSeparatorChar() 130 { 131 if (Platform.isPlatformWindows() && !cygnify) 132 return '\\'; 133 return '/'; 134 } 135 136 public final List getCompletions() 137 { 138 return list; 139 } 140 141 private boolean isUnique() 142 { 143 return list.size() == 1; 144 } 145 146 private String getLongestCommonPrefix() 147 { 148 String s = input; 149 if (list.size() != 0) { 150 if (list.size() == 1) { 151 s = (String ) list.get(0); 152 } else { 153 String first = (String ) list.get(0); 154 int length = toBeCompleted.length() + 1; 155 while (true) { 156 if (length > first.length()) 157 return s; 158 String maybe = first.substring(0, length); 159 for (int i = 1; i < list.size(); i++) { 160 String toBeChecked = (String ) list.get(i); 161 if (!maybe.regionMatches(ignoreCase, 0, toBeChecked, 0, length)) { 162 if (cygnify) 163 s = Utilities.cygnify(s); 164 return s; 165 } 166 } 167 s = maybe; 168 ++length; 169 } 170 } 171 } 172 if (cygnify) 173 s = Utilities.cygnify(s); 174 return s; 175 } 176 177 public String toString() 178 { 179 if (isUnique()) { 180 String s = (String ) list.get(0); 181 if (!s.endsWith("/") && !s.endsWith("\\")) 183 s += ' '; 184 return s; 185 } 186 return getLongestCommonPrefix(); 187 } 188 } 189 | Popular Tags |