001 package org.maltparser.core.options.option;
002
003 import java.util.Formatter;
004 import java.util.HashMap;
005 import java.util.TreeSet;
006
007 import org.maltparser.core.exception.MaltChainedException;
008 import org.maltparser.core.options.OptionException;
009 import org.maltparser.core.options.OptionGroup;
010
011 /**
012 * A string enum type option is an option that can only contain string value that corresponds to another string.
013 *
014 * @author Johan Hall
015 * @since 1.0
016 **/
017 public class StringEnumOption extends Option{
018 private String defaultValue;
019 private TreeSet<String> legalValues;
020 private HashMap<String,String> legalValueDesc;
021 private HashMap<String,String> valueMapto;
022 private HashMap<String, String> maptoValue;
023
024 /**
025 * Creates a stringenum type option description
026 *
027 * @param group a reference to the option group.
028 * @param name the name of the option.
029 * @param shortDescription a short description of the option.
030 * @param flag a short string that can be used in the command line.
031 * @param usage a string that explains the usage of the option.
032 * @throws OptionException
033 */
034 public StringEnumOption(OptionGroup group,
035 String name,
036 String shortDescription,
037 String flag,
038 String usage) throws MaltChainedException {
039 super(group, name, shortDescription, flag, usage);
040 legalValues = new TreeSet<String>();
041 legalValueDesc = new HashMap<String,String>();
042 valueMapto = new HashMap<String,String>();
043 maptoValue = new HashMap<String, String>();
044 }
045
046 /* (non-Javadoc)
047 * @see org.maltparser.core.options.option.Option#getValueObject(java.lang.String)
048 */
049 public Object getValueObject(String value) throws MaltChainedException {
050 if (value == null) {
051 return null;
052 } else if (legalValues.contains(value)) {
053 return new String(valueMapto.get(value));
054 } else {
055 return new String(value);
056 }
057 }
058
059 /* (non-Javadoc)
060 * @see org.maltparser.core.options.option.Option#getDefaultValueObject()
061 */
062 public Object getDefaultValueObject() throws MaltChainedException {
063 return new String(defaultValue);
064 }
065
066 /**
067 * Returns the legal value identifier name (an enumerate string value)
068 *
069 * @param value the mapped legal value
070 * @return the legal value identifier name, null if it could not be found
071 * @throws MaltChainedException
072 */
073 public String getLegalValueString(String value) throws MaltChainedException {
074 return new String(maptoValue.get(value));
075 }
076
077 /**
078 * Returns the mapped legal value
079 *
080 * @param value an enumerate string value
081 * @return the mapped legal value, null if it could not be found
082 * @throws MaltChainedException
083 */
084 public String getLegalValueMapToString(String value) throws MaltChainedException {
085 return new String(valueMapto.get(value));
086 }
087
088 /* (non-Javadoc)
089 * @see org.maltparser.core.options.option.Option#setDefaultValue(java.lang.String)
090 */
091 public void setDefaultValue(String defaultValue) throws MaltChainedException {
092 if (defaultValue == null) {
093 if (legalValues.isEmpty()) {
094 throw new OptionException("The default value is null and the legal value set is empty for the '"+getName()+"' option. ");
095 } else {
096 this.defaultValue = valueMapto.get(((TreeSet<String>)valueMapto.keySet()).first());
097 }
098 } else if (legalValues.contains(defaultValue.toLowerCase())) {
099 this.defaultValue = valueMapto.get(defaultValue.toLowerCase());
100 } else if (defaultValue.equals("")) {
101 this.defaultValue = defaultValue;
102 } else {
103 throw new OptionException("The default value '"+defaultValue+"' for the '"+getName()+"' option is not a legal value. ");
104 }
105 }
106
107 /* (non-Javadoc)
108 * @see org.maltparser.core.options.option.Option#getDefaultValueString()
109 */
110 public String getDefaultValueString() {
111 return defaultValue.toString();
112 }
113
114 /**
115 * Returns the mapped legal value that corresponds to the enumerate string value.
116 *
117 * @param value an enumerate string value
118 * @return the mapped legal value that corresponds to the enumerate string value.
119 */
120 public String getMapto(String value) {
121 return new String(valueMapto.get(value));
122 }
123
124 /**
125 * Adds a legal value that corresponds to another string
126 *
127 * @param value a legal value name
128 * @param desc a short description of the legal value
129 * @param mapto a mapto string value
130 * @throws OptionException
131 */
132 public void addLegalValue(String value, String desc, String mapto) throws MaltChainedException {
133 if (value == null || value.equals("")) {
134 throw new OptionException("The legal value is missing for the option "+getName()+".");
135 } else if (legalValues.contains(value.toLowerCase())) {
136 throw new OptionException("The legal value "+value+" already exists for the option "+getName()+". ");
137 } else {
138 legalValues.add(value.toLowerCase());
139 if (desc == null || desc.equals("")) {
140 legalValueDesc.put(value.toLowerCase(), "Description is missing. ");
141 } else {
142 legalValueDesc.put(value.toLowerCase(), desc);
143 }
144 if (mapto == null || mapto.equals("")) {
145 throw new OptionException("A mapto value is missing for the option "+getName()+". ");
146 } else {
147 valueMapto.put(value, mapto);
148 maptoValue.put(mapto, value);
149 }
150 }
151 }
152
153 /* (non-Javadoc)
154 * @see org.maltparser.core.options.option.Option#getStringRepresentation(java.lang.Object)
155 */
156 public String getStringRepresentation(Object value) {
157 if (value instanceof String) {
158 if (legalValues.contains(value)) {
159 return valueMapto.get(value);
160 } else {
161 return value.toString();
162 }
163 }
164 return null;
165 }
166
167 /* (non-Javadoc)
168 * @see org.maltparser.core.options.option.Option#toString()
169 */
170 public String toString() {
171 final StringBuilder sb = new StringBuilder();
172 sb.append(super.toString());
173 Formatter formatter = new Formatter(sb);
174 for (String value : legalValues) {
175 formatter.format("%2s%-10s - %-20s\n", "", value, legalValueDesc.get(value));
176 }
177 return sb.toString();
178 }
179 }