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.CommandDescriptor;
023 import org.crsh.cmdline.Multiplicity;
024 import org.crsh.cmdline.ParameterDescriptor;
025 import org.crsh.cmdline.binding.TypeBinding;
026
027 import java.io.IOException;
028 import java.util.ArrayList;
029 import java.util.HashMap;
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 abstract class CommandMatch<C, D extends CommandDescriptor<C, B>, B extends TypeBinding> {
039
040 /** . */
041 private final List<OptionMatch<B>> optionMatches;
042
043 /** . */
044 private final List<ArgumentMatch<B>> argumentMatches;
045
046 /** . */
047 private final String rest;
048
049 public CommandMatch(List<OptionMatch<B>> optionMatches, List<ArgumentMatch<B>> argumentMatches, String rest) {
050 this.optionMatches = optionMatches;
051 this.argumentMatches = argumentMatches;
052 this.rest = rest;
053 }
054
055 public abstract D getDescriptor();
056
057 public final Object invoke(InvocationContext context, C command) throws CmdLineException {
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 throw new CmdSyntaxException("Could not find parameter " + parameter);
068 }
069 Object v = parameterMatch.computeValue();
070 if (v != null) {
071 parameterValues.put(parameter, v);
072 }
073 }
074
075 //
076 return doInvoke(context, command, parameterValues);
077 }
078
079 protected abstract Object doInvoke(InvocationContext context, C command, Map<ParameterDescriptor<?>, Object> values) throws CmdLineException;
080
081 public abstract Set<ParameterDescriptor<?>> getParameters();
082
083 public abstract List<ParameterMatch<?, ?>> getParameterMatches();
084
085 public abstract void printMan(Appendable writer) throws IOException;
086
087 public abstract void printUsage(Appendable writer) throws IOException;
088
089 public List<OptionMatch<B>> getOptionMatches() {
090 return optionMatches;
091 }
092
093 public List<ArgumentMatch<B>> getArgumentMatches() {
094 return argumentMatches;
095 }
096
097 public String getRest() {
098 return rest;
099 }
100 }