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;
021
022 import org.crsh.cmdline.binding.TypeBinding;
023 import org.crsh.cmdline.completers.EmptyCompleter;
024 import org.crsh.cmdline.matcher.CmdSyntaxException;
025 import org.crsh.cmdline.spi.Completer;
026 import org.crsh.cmdline.type.ValueType;
027
028 import java.io.IOException;
029 import java.lang.annotation.Annotation;
030 import java.util.List;
031
032 public abstract class ParameterDescriptor<B extends TypeBinding> {
033
034 /** . */
035 private final B binding;
036
037 /** . */
038 private final Description description;
039
040 /** . */
041 private final ParameterType<?> type;
042
043 /** . */
044 private final boolean required;
045
046 /** . */
047 private final boolean password;
048
049 /** . */
050 private final Class<? extends Completer> completerType;
051
052 /** The annotation when it exists. */
053 private final Annotation annotation;
054
055 /** . */
056 private final boolean unquote;
057
058 /** . */
059 CommandDescriptor<?, B> owner;
060
061 public ParameterDescriptor(
062 B binding,
063 ParameterType<?> type,
064 Description description,
065 boolean required,
066 boolean password,
067 boolean unquote,
068 Class<? extends Completer> completerType,
069 Annotation annotation) throws IllegalValueTypeException, IllegalParameterException {
070
071 //
072 if (completerType == EmptyCompleter.class) {
073 completerType = type.getValueType().getCompleter();
074 }
075
076 //
077 this.binding = binding;
078 this.description = description;
079 this.required = required;
080 this.password = password;
081 this.completerType = completerType;
082 this.annotation = annotation;
083 this.unquote = unquote;
084 this.type = type;
085 }
086
087 public Object parse(String s) throws Exception {
088 return type.parse(s);
089 }
090
091 public abstract Object parse(List<String> values) throws CmdSyntaxException;
092
093 public CommandDescriptor<?, B> getOwner() {
094 return owner;
095 }
096
097 public Class<?> getDeclaredType() {
098 return type.getDeclaredType();
099 }
100
101 public final B getBinding() {
102 return binding;
103 }
104
105 public final String getUsage() {
106 return description != null ? description.getUsage() : "";
107 }
108
109 public Description getDescription() {
110 return description;
111 }
112
113 public Annotation getAnnotation() {
114 return annotation;
115 }
116
117 public final boolean isRequired() {
118 return required;
119 }
120
121 public boolean isUnquote() {
122 return unquote;
123 }
124
125 public final boolean isPassword() {
126 return password;
127 }
128
129 public final ValueType getType() {
130 return type.getValueType();
131 }
132
133 public final Multiplicity getMultiplicity() {
134 return type.getMultiplicity();
135 }
136
137 public final boolean isSingleValued() {
138 return getMultiplicity() == Multiplicity.SINGLE;
139 }
140
141 public final boolean isMultiValued() {
142 return getMultiplicity() == Multiplicity.MULTI;
143 }
144
145 public final Class<? extends Completer> getCompleterType() {
146 return completerType;
147 }
148
149 public abstract void printUsage(Appendable writer) throws IOException;
150 }