001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020package org.crsh.cli.impl.completion;
021
022import org.crsh.cli.impl.Delimiter;
023import org.crsh.cli.completers.EmptyCompleter;
024import org.crsh.cli.descriptor.ParameterDescriptor;
025import org.crsh.cli.spi.Completer;
026
027import java.lang.reflect.Constructor;
028import java.lang.reflect.InvocationTargetException;
029import java.lang.reflect.Modifier;
030
031class ParameterCompletion extends Completion {
032
033  /** . */
034  private final String prefix;
035
036  /** . */
037  private final Delimiter delimiter;
038
039  /** . */
040  private final ParameterDescriptor parameter;
041
042  /** . */
043  private final Completer completer;
044
045  public ParameterCompletion(String prefix, Delimiter delimiter, ParameterDescriptor parameter, Completer completer) {
046    this.prefix = prefix;
047    this.delimiter = delimiter;
048    this.parameter = parameter;
049    this.completer = completer;
050  }
051
052  public CompletionMatch complete() throws CompletionException {
053
054    //
055    Class<? extends Completer> completerType = parameter.getCompleterType();
056    Completer completer = null;
057    if (completerType != EmptyCompleter.class) {
058
059      // If the provided completer instance matches the parameter completer type
060      // then we use it
061      if (completerType.isInstance(this.completer)) {
062        completer = this.completer;
063      } else {
064        // Otherwise we instantiate it
065        Constructor<? extends Completer> ctor;
066        try {
067          ctor = completerType.getDeclaredConstructor();
068        }
069        catch (NoSuchMethodException ignore) {
070          throw new CompletionException("The completer " + completerType.getName() + " does not provide a no arg constructor");
071        }
072        if (Modifier.isPublic(ctor.getModifiers())) {
073          try {
074            completer = ctor.newInstance();
075          }
076          catch (InstantiationException e) {
077            throw new CompletionException("The completer " + completerType.getName() + " cannot be abstract");
078          }
079          catch (InvocationTargetException e) {
080            throw new CompletionException(e.getCause());
081          }
082          catch (Exception e) {
083            throw new CompletionException(e);
084          }
085        } else {
086          throw new CompletionException("The completer " + completerType.getName() + " constructor must be public");
087        }
088      }
089    }
090
091    //
092    if (completer != null) {
093      try {
094        return new CompletionMatch(delimiter, completer.complete(parameter, prefix));
095      }
096      catch (Exception e) {
097        throw new CompletionException(e);
098      }
099    } else {
100      return new CompletionMatch(delimiter, org.crsh.cli.spi.Completion.create());
101    }
102  }
103}