View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package it.jnrpe.server.console;
17  
18  import static jline.internal.Preconditions.checkNotNull;
19  import it.jnrpe.commands.CommandDefinition;
20  import it.jnrpe.commands.CommandRepository;
21  import it.jnrpe.plugins.IPluginRepository;
22  import it.jnrpe.plugins.PluginDefinition;
23  
24  import java.util.Collection;
25  import java.util.List;
26  import java.util.SortedSet;
27  import java.util.TreeSet;
28  
29  import jline.console.completer.Completer;
30  
31  /**
32   * The command completer.
33   * We cannot use a simple StringCompleter since we have to perform case
34   * insensitive completion.
35   *   
36   * @author Massimiliano Ziccardi
37   */
38  class CommandCompleter implements Completer {
39  
40          private final SortedSet<String> strings = new TreeSet<String>();
41  
42          public CommandCompleter(final IPluginRepository pluginRepository, final CommandRepository commandRepository) {
43              
44              for (PluginDefinition pd : pluginRepository.getAllPlugins()) {
45                  strings.add(PluginCommand.NAME + pd.getName().toLowerCase());
46              }
47              
48              for (CommandDefinition cd : commandRepository.getAllCommands()) {
49                  strings.add(CommandConsoleCommand.NAME + cd.getName().toLowerCase());
50              }
51              
52              strings.add(ExitCommand.NAME.toLowerCase());
53              strings.add(HelpCommand.NAME.toLowerCase());
54          }
55          
56          public Collection<String> getStrings() {
57              return strings;
58          }
59  
60          public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) {
61              // buffer could be null
62              checkNotNull(candidates);
63  
64              if (buffer == null) {
65                  candidates.addAll(strings);
66              }
67              else {
68                  for (String match : strings.tailSet(buffer.toLowerCase())) {
69                      if (!match.startsWith(buffer.toLowerCase())) {
70                          break;
71                      }
72  
73                      candidates.add(match);
74                  }
75              }
76  
77              if (candidates.size() == 1) {
78                  candidates.set(0, candidates.get(0) + " ");
79              }
80  
81              return candidates.isEmpty() ? -1 : 0;
82          }
83  }