001 package org.maltparser.core.options.option;
002
003 import java.util.Formatter;
004
005 import org.maltparser.core.exception.MaltChainedException;
006 import org.maltparser.core.options.OptionException;
007 import org.maltparser.core.options.OptionGroup;
008
009 /**
010 * Abstract class that contains description of an option that are the same over all option types.
011 *
012 * @author Johan Hall
013 * @since 1.0
014 **/
015 public abstract class Option implements Comparable<Option>{
016 public static final int NONE = 0;
017 /**
018 * The option is only relevant during learning
019 */
020 public static final int TRAIN = 1;
021 /**
022 * The option is only relevant during processing (parsing)
023 */
024 public static final int PROCESS = 2;
025 /**
026 * The option is relevant both during learning and processing (parsing)
027 */
028 public static final int BOTH = 3;
029 /**
030 * The option is saved during learning and cannot be overloaded during processing (parsing)
031 */
032 public static final int SAVE = 4;
033
034 private OptionGroup group;
035 private String name;
036 private String shortDescription;
037 private String flag;
038 private int usage;
039 private boolean ambiguous;
040
041 /**
042 * Creates an option description
043 *
044 * @param group a reference to the option group.
045 * @param name the name of the option.
046 * @param shortDescription a short description of the option.
047 * @param flag a flag that can be used in the command line.
048 * @param usage a string that explains the usage of the option.
049 * @throws OptionException
050 */
051 public Option(OptionGroup group, String name, String shortDescription, String flag, String usage) throws MaltChainedException {
052 setGroup(group);
053 setName(name);
054 setShortDescription(shortDescription);
055 setFlag(flag);
056 setUsage(usage);
057 setAmbiguous(false);
058 }
059
060 /**
061 * Returns the corresponding object for the option value (specified as a string value).
062 *
063 * @param value the string option value
064 * @return the corresponding object for the option value (specified as a string value).
065 * @throws OptionException
066 */
067 public abstract Object getValueObject(String value) throws MaltChainedException;
068
069 /**
070 * Returns the object for the default value for option.
071 *
072 * @return the object for the default value for option.
073 * @throws OptionException
074 */
075 public abstract Object getDefaultValueObject() throws MaltChainedException;
076
077 /**
078 * Returns a string representation of the default value.
079 *
080 * @return a string representation of the default value
081 */
082 public abstract String getDefaultValueString();
083
084 /**
085 * Sets the default value for the option.
086 *
087 * @param defaultValue the string default value
088 * @throws OptionException
089 */
090 public abstract void setDefaultValue(String defaultValue) throws MaltChainedException;
091
092
093 /**
094 * Returns a string representation of the option value.
095 *
096 * @param value an option value object
097 * @return a string representation of the option value, if the option value could not be found null is returned.
098 */
099 public abstract String getStringRepresentation(Object value);
100
101 /**
102 * Returns a reference to a option group.
103 *
104 * @return a reference to a option group.
105 */
106 public OptionGroup getGroup() {
107 return group;
108 }
109 /**
110 * Sets the reference to the option group
111 * @param group a reference to a option group
112 */
113 public void setGroup(OptionGroup group) {
114 this.group = group;
115 }
116 /**
117 * Returns the name of the option.
118 *
119 * @return the name of the option.
120 */
121 public String getName() {
122 return name;
123 }
124 /**
125 * Sets the name of the option.
126 *
127 * @param name the name of the option.
128 * @throws OptionException
129 */
130 public void setName(String name) throws MaltChainedException {
131 if (name == null || name.length() == 0) {
132 throw new OptionException("The option name has no value. ");
133 }
134 this.name = name.toLowerCase();
135 }
136 /**
137 * Returns a short description of the option
138 *
139 * @return a short description of the option
140 */
141 public String getShortDescription() {
142 return shortDescription;
143 }
144 /**
145 * Sets a short description of the option
146 *
147 * @param shortDescription a short description of the option
148 */
149 public void setShortDescription(String shortDescription) {
150 this.shortDescription = shortDescription;
151 }
152 /**
153 * Returns a character that is used as a flag for the command line input
154 *
155 * @return a character that is used as a flag for the command line input
156 */
157 public String getFlag() {
158 return flag;
159 }
160 /**
161 * Sets a character that is used as a flag for the command line input
162 *
163 * @param flag a character that is used as a flag for the command line input
164 * @throws OptionException
165 */
166 public void setFlag(String flag) throws MaltChainedException {
167 if (flag == null) {
168 this.flag = null;
169 } else {
170 this.flag = flag;
171 }
172 }
173 /**
174 * Returns the usage of the option.
175 *
176 * @return the usage of the option.
177 */
178 public int getUsage() {
179 return usage;
180 }
181 /**
182 * Sets the usage of the option.
183 *
184 * @param usage the usage of the option.
185 * @throws OptionException
186 */
187 public void setUsage(String usage) throws MaltChainedException {
188 if (usage == null || usage.equals("") || usage.toLowerCase().equals("none")) {
189 this.usage = Option.NONE;
190 } else if (usage.toLowerCase().equals("train")) {
191 this.usage = Option.TRAIN;
192 } else if (usage.toLowerCase().equals("process")) {
193 this.usage = Option.PROCESS;
194 } else if (usage.toLowerCase().equals("both")) {
195 this.usage = Option.BOTH;
196 } else if (usage.toLowerCase().equals("save")) {
197 this.usage = Option.SAVE;
198 } else {
199 throw new OptionException("Illegal use of the usage attibute value: "+usage+" for the '"+getName()+"' option. ");
200 }
201 }
202 /**
203 * Sets the usage of the option.
204 *
205 * @param usage the usage of the option.
206 * @throws OptionException
207 */
208 public void setUsage(int usage) throws MaltChainedException {
209 if (usage >= 0 && usage <= 4) {
210 this.usage = usage;
211 } else {
212 throw new OptionException("Illegal use of the usage attibute value: "+usage+" for the '"+getName()+"' option. ");
213 }
214 }
215
216 /**
217 * Returns true if the option name is ambiguous over all option groups, otherwise false.
218 *
219 * @return true if the option name is ambiguous over all option groups, otherwise false.
220 */
221 public boolean isAmbiguous() {
222 return ambiguous;
223 }
224
225 /**
226 * Sets true if the option name is ambiguous over all option groups, otherwise false.
227 *
228 * @param ambiguous true if the option name is ambiguous over all option groups, otherwise false.
229 */
230 public void setAmbiguous(boolean ambiguous) {
231 this.ambiguous = ambiguous;
232 }
233
234 public int compareTo(Option o) {
235 if (group.getName().equals(o.group.getName())) {
236 return name.compareTo(o.getName());
237 }
238 return group.getName().compareTo(o.group.getName());
239 }
240
241 /* (non-Javadoc)
242 * @see java.lang.Object#toString()
243 */
244 public String toString() {
245 int splitsize = 45;
246 final StringBuilder sb = new StringBuilder();
247 Formatter formatter = new Formatter(sb);
248 formatter.format("%-20s ", getName());
249 if (isAmbiguous()) {
250 formatter.format("*");
251 } else {
252 sb.append(" ");
253 }
254 if (getFlag() != null) {
255 formatter.format("(%4s) : ", "-"+getFlag());
256 } else {
257 sb.append(" : ");
258 }
259 int r = shortDescription.length() / splitsize;
260 for (int i = 0; i <= r; i++) {
261 if (shortDescription.substring(splitsize*i).length() <= splitsize) {
262 formatter.format(((i==0)?"%s":"%28s")+"%-45s\n", "", shortDescription.substring(splitsize*i));
263 } else {
264 formatter.format(((i==0)?"%s":"%28s")+"%-45s\n", "", shortDescription.substring(splitsize*i, splitsize*i+splitsize));
265 }
266 }
267 return sb.toString();
268 }
269 }