1 36 package jline; 37 38 import java.io.*; 39 import java.util.*; 40 41 42 57 public class SimpleCompletor 58 implements Completor, Cloneable 59 { 60 63 SortedSet candidates; 64 65 66 69 String delimiter; 70 71 final SimpleCompletorFilter filter; 72 73 74 78 public SimpleCompletor (final String candidateString) 79 { 80 this (new String [] { candidateString }); 81 } 82 83 84 88 public SimpleCompletor (final String [] candidateStrings) 89 { 90 this (candidateStrings, null); 91 } 92 93 94 public SimpleCompletor (final String [] strings, 95 final SimpleCompletorFilter filter) 96 { 97 this.filter = filter; 98 setCandidateStrings (strings); 99 } 100 101 102 105 public SimpleCompletor (final Reader reader) 106 throws IOException 107 { 108 this (getStrings (reader)); 109 } 110 111 112 116 public SimpleCompletor (final InputStream in) 117 throws IOException 118 { 119 this (getStrings (new InputStreamReader (in))); 120 } 121 122 123 private static String [] getStrings (final Reader in) 124 throws IOException 125 { 126 final Reader reader = in instanceof BufferedReader 127 ? in 128 : new BufferedReader (in); 129 130 List words = new LinkedList (); 131 String line; 132 while ((line = ((BufferedReader)reader).readLine ()) != null) 133 { 134 for (StringTokenizer tok = new StringTokenizer (line); 135 tok.hasMoreTokens (); words.add (tok.nextToken ())); 136 } 137 138 return (String [])words.toArray (new String [words.size ()]); 139 } 140 141 142 public int complete (final String buffer, final int cursor, 143 final List clist) 144 { 145 String start = buffer == null ? "" : buffer; 146 147 SortedSet matches = candidates.tailSet (start); 148 for (Iterator i = matches.iterator (); i.hasNext (); ) 149 { 150 String can = (String )i.next (); 151 if (!(can.startsWith (start))) 152 break; 153 154 if (delimiter != null) 155 { 156 int index = can.indexOf (delimiter, cursor); 157 if (index != -1) 158 can = can.substring (0, index + 1); 159 } 160 clist.add (can); 161 } 162 163 if (clist.size () == 1) 164 clist.set (0, ((String )clist.get (0)) + " "); 165 166 return clist.size () == 0 ? -1 : 0; 169 } 170 171 172 public void setDelimiter (final String delimiter) 173 { 174 this.delimiter = delimiter; 175 } 176 177 178 public String getDelimiter () 179 { 180 return this.delimiter; 181 } 182 183 184 185 public void setCandidates (final SortedSet candidates) 186 { 187 if (filter != null) 188 { 189 TreeSet filtered = new TreeSet (); 190 for (Iterator i = candidates.iterator (); i.hasNext (); ) 191 { 192 String element = (String )i.next (); 193 element = filter.filter (element); 194 if (element != null) 195 filtered.add (element); 196 } 197 198 this.candidates = filtered; 199 } 200 else 201 { 202 this.candidates = candidates; 203 } 204 } 205 206 207 public SortedSet getCandidates () 208 { 209 return Collections.unmodifiableSortedSet (this.candidates); 210 } 211 212 213 public void setCandidateStrings (final String [] strings) 214 { 215 setCandidates (new TreeSet (Arrays.asList (strings))); 216 } 217 218 219 public void addCandidateString (final String candidateString) 220 { 221 final String string = filter == null 222 ? candidateString 223 : filter.filter (candidateString); 224 225 if (string != null) 226 candidates.add (string); 227 } 228 229 230 public Object clone () 231 throws CloneNotSupportedException 232 { 233 return super.clone (); 234 } 235 236 237 242 public static interface SimpleCompletorFilter 243 { 244 248 public String filter (String element); 249 } 250 251 252 public static class NoOpFilter 253 implements SimpleCompletorFilter 254 { 255 public String filter (final String element) 256 { 257 return element; 258 } 259 } 260 } 261 | Popular Tags |