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    
020    package org.crsh.cmdline.matcher;
021    
022    import org.crsh.cmdline.ArgumentDescriptor;
023    import org.crsh.cmdline.ClassDescriptor;
024    import org.crsh.cmdline.OptionDescriptor;
025    import org.crsh.cmdline.binding.ClassFieldBinding;
026    import org.crsh.cmdline.ParameterDescriptor;
027    
028    import java.io.IOException;
029    import java.lang.reflect.Field;
030    import java.util.ArrayList;
031    import java.util.HashSet;
032    import java.util.List;
033    import java.util.Map;
034    import java.util.Set;
035    
036    public class ClassMatch<T> extends CommandMatch<T, ClassDescriptor<T>, ClassFieldBinding> {
037    
038      /** . */
039      private final ClassDescriptor<T> descriptor;
040    
041      public ClassMatch(
042        ClassDescriptor<T> descriptor,
043        List<OptionMatch<ClassFieldBinding>> optionMatches,
044        List<ArgumentMatch<ClassFieldBinding>> argumentMatches,
045        String rest) {
046        super(optionMatches, argumentMatches, rest);
047    
048        //
049        this.descriptor = descriptor;
050      }
051    
052      @Override
053      public ClassDescriptor<T> getDescriptor() {
054        return descriptor;
055      }
056    
057      @Override
058      public void printMan(Appendable writer) throws IOException {
059        descriptor.printMan(writer);
060      }
061    
062      @Override
063      public void printUsage(Appendable writer) throws IOException {
064        descriptor.printUsage(writer);
065      }
066    
067      @Override
068      public Set<ParameterDescriptor<?>> getParameters() {
069        Set<ParameterDescriptor<?>> unused = new HashSet<ParameterDescriptor<?>>();
070        unused.addAll(descriptor.getArguments());
071        unused.addAll(descriptor.getOptions());
072        return unused;
073      }
074    
075      @Override
076      public List<ParameterMatch<?, ?>> getParameterMatches() {
077        List<ParameterMatch<?, ?>> matches = new ArrayList<ParameterMatch<?, ?>>();
078        matches.addAll(getOptionMatches());
079        matches.addAll(getArgumentMatches());
080        return matches;
081      }
082    
083      @Override
084      protected Object doInvoke(Resolver context, T command, Map<ParameterDescriptor<?>, Object> values) throws CmdInvocationException, CmdSyntaxException {
085        for (ParameterDescriptor<ClassFieldBinding> parameter : descriptor.getParameters()) {
086          Object value = values.get(parameter);
087    
088          //
089          if (value == null) {
090            if (parameter.isRequired()) {
091              if (parameter instanceof ArgumentDescriptor) {
092                ArgumentDescriptor<?> argument = (ArgumentDescriptor<?>)parameter;
093                throw new CmdSyntaxException("Missing argument " + argument.getName());
094              } else {
095                OptionDescriptor<?> option = (OptionDescriptor<?>)parameter;
096                throw new CmdSyntaxException("Missing option " + option.getNames());
097              }
098            }
099          } else {
100            Field f = parameter.getBinding().getField();
101            try {
102              f.setAccessible(true);
103              f.set(command, value);
104            }
105            catch (Exception e) {
106              throw new CmdInvocationException(e.getMessage(), e);
107            }
108          }
109        }
110    
111        //
112        return null;
113      }
114    }