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 Description description;
058
059 /** . */
060 private final ParameterType<?> type;
061
062 /** . */
063 private final boolean required;
064
065 /** . */
066 private final boolean password;
067
068 /** . */
069 private final Class<? extends Completer> completerType;
070
071 /** The annotation when it exists. */
072 private final Annotation annotation;
073
074 /** . */
075 private final boolean unquote;
076
077 public ParameterDescriptor(
078 ParameterType<?> type,
079 Description description,
080 boolean required,
081 boolean password,
082 boolean unquote,
083 Class<? extends Completer> completerType,
084 Annotation annotation) throws IllegalValueTypeException, IllegalParameterException {
085
086 //
087 if (completerType == EmptyCompleter.class) {
088 completerType = type.getValueType().getCompleter();
089 }
090
091 //
092 this.description = description;
093 this.required = required;
094 this.password = password;
095 this.completerType = completerType;
096 this.annotation = annotation;
097 this.unquote = unquote;
098 this.type = type;
099 }
100
101 public Object parse(String s) throws Exception {
102 return type.parse(s);
103 }
104
105 public abstract Object parse(List<String> values) throws SyntaxException;
106
107 public Class<?> getDeclaredType() {
108 return type.getDeclaredType();
109 }
110
111 public final String getUsage() {
112 return description != null ? description.getUsage() : "";
113 }
114
115 public Description getDescription() {
116 return description;
117 }
118
119 public Annotation getAnnotation() {
120 return annotation;
121 }
122
123 public final boolean isRequired() {
124 return required;
125 }
126
127 public boolean isUnquote() {
128 return unquote;
129 }
130
131 public final boolean isPassword() {
132 return password;
133 }
134
135 public final ValueType getType() {
136 return type.getValueType();
137 }
138
139 public final Multiplicity getMultiplicity() {
140 return type.getMultiplicity();
141 }
142
143 public final boolean isSingleValued() {
144 return getMultiplicity() == Multiplicity.SINGLE;
145 }
146
147 public final boolean isMultiValued() {
148 return getMultiplicity() == Multiplicity.MULTI;
149 }
150
151 public final Class<? extends Completer> getCompleterType() {
152 return completerType;
153 }
154
155 public abstract void printUsage(Appendable writer) throws IOException;
156 }