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.CommandDescriptor;
024 import org.crsh.cmdline.OptionDescriptor;
025 import org.crsh.cmdline.ParameterDescriptor;
026 import org.crsh.cmdline.binding.TypeBinding;
027
028 import java.io.IOException;
029 import java.util.HashMap;
030 import java.util.List;
031 import java.util.Map;
032 import java.util.Set;
033
034 public abstract class CommandMatch<C, D extends CommandDescriptor<C, B>, B extends TypeBinding> {
035
036 /** . */
037 private final List<OptionMatch<B>> optionMatches;
038
039 /** . */
040 private final List<ArgumentMatch<B>> argumentMatches;
041
042 /** . */
043 private final String rest;
044
045 public CommandMatch(List<OptionMatch<B>> optionMatches, List<ArgumentMatch<B>> argumentMatches, String rest) {
046 this.optionMatches = optionMatches;
047 this.argumentMatches = argumentMatches;
048 this.rest = rest;
049 }
050
051 public abstract D getDescriptor();
052
053 public final Object invoke(C command) throws CmdInvocationException, CmdSyntaxException {
054 return invoke(Resolver.EMPTY, command);
055 }
056
057 public final Object invoke(Resolver context, C command) throws CmdInvocationException, CmdSyntaxException {
058
059 //
060 Set<ParameterDescriptor<?>> unused = getParameters();
061 Map<ParameterDescriptor<?>, Object> parameterValues = new HashMap<ParameterDescriptor<?>, Object>();
062
063 //
064 for (ParameterMatch<?, ?> parameterMatch : getParameterMatches()) {
065 ParameterDescriptor<?> parameter = parameterMatch.getParameter();
066 if (!unused.remove(parameter)) {
067 if (parameter instanceof ArgumentDescriptor) {
068 ArgumentDescriptor<?> argument = (ArgumentDescriptor<?>)parameter;
069 throw new CmdSyntaxException("Missing argument " + argument.getName());
070 } else {
071 OptionDescriptor<?> option = (OptionDescriptor<?>)parameter;
072 throw new CmdSyntaxException("Missing option " + option.getNames());
073 }
074 }
075 Object v = parameterMatch.computeValue();
076 if (v != null) {
077 parameterValues.put(parameter, v);
078 }
079 }
080
081 //
082 return doInvoke(context, command, parameterValues);
083 }
084
085 protected abstract Object doInvoke(Resolver context, C command, Map<ParameterDescriptor<?>, Object> values) throws CmdInvocationException, CmdSyntaxException;
086
087 public abstract Set<ParameterDescriptor<?>> getParameters();
088
089 public abstract List<ParameterMatch<?, ?>> getParameterMatches();
090
091 public abstract void printMan(Appendable writer) throws IOException;
092
093 public abstract void printUsage(Appendable writer) throws IOException;
094
095 public List<OptionMatch<B>> getOptionMatches() {
096 return optionMatches;
097 }
098
099 public List<ArgumentMatch<B>> getArgumentMatches() {
100 return argumentMatches;
101 }
102
103 public String getRest() {
104 return rest;
105 }
106 }