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