001 package org.crsh.cmdline.matcher.impl;
002
003 import org.crsh.cmdline.EmptyCompleter;
004 import org.crsh.cmdline.ParameterDescriptor;
005 import org.crsh.cmdline.matcher.CmdCompletionException;
006 import org.crsh.cmdline.matcher.tokenizer.Termination;
007 import org.crsh.cmdline.spi.Completer;
008
009 import java.util.Collections;
010 import java.util.HashMap;
011 import java.util.Map;
012
013 /**
014 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
015 */
016 class ParameterCompletion extends Completion {
017
018 /** . */
019 private final String prefix;
020
021 /** . */
022 private final Termination termination;
023
024 /** . */
025 private final ParameterDescriptor<?> parameter;
026
027 /** . */
028 private final Completer completer;
029
030 ParameterCompletion(String prefix, Termination termination, ParameterDescriptor<?> parameter, Completer completer) {
031 this.prefix = prefix;
032 this.termination = termination;
033 this.parameter = parameter;
034 this.completer = completer;
035 }
036
037 Map<String, String> complete() throws CmdCompletionException {
038
039 Class<? extends Completer> completerType = parameter.getCompleterType();
040 Completer completer = this.completer;
041
042 // Use the most adapted completer
043 if (completerType != EmptyCompleter.class) {
044 try {
045 completer = completerType.newInstance();
046 }
047 catch (Exception e) {
048 throw new CmdCompletionException(e);
049 }
050 }
051
052 //
053 if (completer != null) {
054 try {
055 Map<String, Boolean> res = completer.complete(parameter, prefix);
056 Map<String, String> delimiter = new HashMap<String, String>();
057 for (Map.Entry<String, Boolean> entry : res.entrySet()) {
058 delimiter.put(entry.getKey(), entry.getValue() ? termination.getEnd() : "");
059 }
060 return delimiter;
061 }
062 catch (Exception e) {
063 throw new CmdCompletionException(e);
064 }
065 } else {
066 return Collections.emptyMap();
067 }
068 }
069 }