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 /*
021 * Copyright (C) 2012 eXo Platform SAS.
022 *
023 * This is free software; you can redistribute it and/or modify it
024 * under the terms of the GNU Lesser General Public License as
025 * published by the Free Software Foundation; either version 2.1 of
026 * the License, or (at your option) any later version.
027 *
028 * This software is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
031 * Lesser General Public License for more details.
032 *
033 * You should have received a copy of the GNU Lesser General Public
034 * License along with this software; if not, write to the Free
035 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
036 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
037 */
038
039 package org.crsh.cli.descriptor;
040
041 import org.crsh.cli.impl.descriptor.IllegalParameterException;
042 import org.crsh.cli.impl.descriptor.IllegalValueTypeException;
043 import org.crsh.cli.impl.Multiplicity;
044 import org.crsh.cli.impl.ParameterType;
045 import org.crsh.cli.SyntaxException;
046 import org.crsh.cli.completers.EmptyCompleter;
047 import org.crsh.cli.spi.Completer;
048 import org.crsh.cli.type.ValueType;
049
050 import java.io.IOException;
051 import java.lang.annotation.Annotation;
052 import java.util.List;
053
054 public abstract class ParameterDescriptor {
055
056 /** . */
057 private final Object binding;
058
059 /** . */
060 private final Description description;
061
062 /** . */
063 private final ParameterType<?> type;
064
065 /** . */
066 private final boolean required;
067
068 /** . */
069 private final boolean password;
070
071 /** . */
072 private final Class<? extends Completer> completerType;
073
074 /** The annotation when it exists. */
075 private final Annotation annotation;
076
077 /** . */
078 private final boolean unquote;
079
080 public ParameterDescriptor(
081 Object binding,
082 ParameterType<?> type,
083 Description description,
084 boolean required,
085 boolean password,
086 boolean unquote,
087 Class<? extends Completer> completerType,
088 Annotation annotation) throws IllegalValueTypeException, IllegalParameterException {
089
090 //
091 if (completerType == EmptyCompleter.class) {
092 completerType = type.getValueType().getCompleter();
093 }
094
095 //
096 this.binding = binding;
097 this.description = description;
098 this.required = required;
099 this.password = password;
100 this.completerType = completerType;
101 this.annotation = annotation;
102 this.unquote = unquote;
103 this.type = type;
104 }
105
106 public Object parse(String s) throws Exception {
107 return type.parse(s);
108 }
109
110 public abstract Object parse(List<String> values) throws SyntaxException;
111
112 public Class<?> getDeclaredType() {
113 return type.getDeclaredType();
114 }
115
116 public final Object getBinding() {
117 return binding;
118 }
119
120 public final String getUsage() {
121 return description != null ? description.getUsage() : "";
122 }
123
124 public Description getDescription() {
125 return description;
126 }
127
128 public Annotation getAnnotation() {
129 return annotation;
130 }
131
132 public final boolean isRequired() {
133 return required;
134 }
135
136 public boolean isUnquote() {
137 return unquote;
138 }
139
140 public final boolean isPassword() {
141 return password;
142 }
143
144 public final ValueType getType() {
145 return type.getValueType();
146 }
147
148 public final Multiplicity getMultiplicity() {
149 return type.getMultiplicity();
150 }
151
152 public final boolean isSingleValued() {
153 return getMultiplicity() == Multiplicity.SINGLE;
154 }
155
156 public final boolean isMultiValued() {
157 return getMultiplicity() == Multiplicity.MULTI;
158 }
159
160 public final Class<? extends Completer> getCompleterType() {
161 return completerType;
162 }
163
164 public abstract void printUsage(Appendable writer) throws IOException;
165 }