    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     * An enumerate option is an option that can only contain string value, which is in the legal value set.
    013     *
    014     * @author Johan Hall
    015     * @since 1.0
    016    **/
    017    public class EnumOption extends Option {
    018            private String defaultValue;
    019            private TreeSet<String> legalValues;
    020            private HashMap<String,String> legalValueDesc;
    021
    022            /**
    023             * Creates an enumerate option description
    024             *
    025             * @param group a reference to the option group.
    026             * @param name  the name of the option.
    027             * @param shortDescription      a short description of the option.
    028             * @param flag  a short string that can be used in the command line.
    029             * @param usage a string that explains the usage of the option.
    030             * @throws OptionException
    031             */
    032            public EnumOption(OptionGroup group,
    033                                                    String name,
    034                                                    String shortDescription,
    035                                                    String flag,
    036                                                    String usage) throws MaltChainedException {
    037                    super(group, name, shortDescription, flag, usage);
    038                    legalValues = new TreeSet<String>();
    039                    legalValueDesc = new HashMap<String,String>();
    040            }
    041
    042            /* (non-Javadoc)
    043             * @see org.maltparser.core.options.option.Option#getValueObject(java.lang.String)
    044             */
    045            public Object getValueObject(String value) throws MaltChainedException {
    046                    if (value == null) {
    047                            return null;
    048                    } else if (legalValues.contains(value)) {
    049                            return new String(value);
    050                    } else {
    051                            throw new OptionException("'"+value+"' is not a legal value for the '"+getName()+"' option. ");
    052                    }
    053            }
    054
    055            /* (non-Javadoc)
    056             * @see org.maltparser.core.options.option.Option#getDefaultValueObject()
    057             */
    058            public Object getDefaultValueObject() throws MaltChainedException {
    059                    return new String(defaultValue);
    060            }
    061
    062            /* (non-Javadoc)
    063             * @see org.maltparser.core.options.option.Option#getDefaultValueString()
    064             */
    065            public String getDefaultValueString() {
    066                    return defaultValue.toString();
    067            }
    068
    069            /* (non-Javadoc)
    070             * @see org.maltparser.core.options.option.Option#setDefaultValue(java.lang.String)
    071             */
    072            public void setDefaultValue(String defaultValue) throws MaltChainedException {
    073                    if (defaultValue == null) {
    074                            if (legalValues.isEmpty()) {
    075                                    throw new OptionException("The default value of the '"+getName()+"' option is null and the legal value set is empty.");
    076                            } else {
    077                                    this.defaultValue = legalValues.first();
    078                            }
    079                    } else if (legalValues.contains(defaultValue.toLowerCase())) {
    080                            this.defaultValue = defaultValue.toLowerCase();
    081                    } else {
    082                            throw new OptionException("The default value '"+defaultValue+"' for the '"+getName()+"' option is not a legal value. ");
    083                    }
    084            }
    085
    086            /**
    087             * Adds a legal value
    088             *
    089             * @param value a legal value name
    090             * @param desc  a short description of the legal value
    091             * @throws OptionException
    092             */
    093            public void addLegalValue(String value, String desc) throws MaltChainedException {
    094                    if (value == null || value.equals("")) {
    095                            throw new OptionException("The legal value is missing for the '"+getName()+"' option. ");
    096                    } else if (legalValues.contains(value.toLowerCase())) {
    097                            throw new OptionException("The legal value '"+value+"' already exists for the '"+getName()+"' option. ");
    098                    } else {
    099                            legalValues.add(value.toLowerCase());
    100                            if (desc == null || desc.equals("")) {
    101                                    legalValueDesc.put(value.toLowerCase(), "Description is missing. ");
    102                            } else {
    103                                    legalValueDesc.put(value.toLowerCase(), desc);
    104                            }
    105                    }
    106            }
    107
    108            /**
    109             * Adds a legal value without a description
    110             *
    111             * @param value a legal value name
    112             * @throws OptionException
    113             */
    114            public void addLegalValue(String value) throws MaltChainedException {
    115                    addLegalValue(value, null);
    116            }
    117
    118            /* (non-Javadoc)
    119             * @see org.maltparser.core.options.option.Option#getStringRepresentation(java.lang.Object)
    120             */
    121            public String getStringRepresentation(Object value) {
    122                    if (value instanceof String && legalValues.contains(value)) {
    123                            return value.toString();
    124                    }
    125                    return null;
    126            }
    127
    128            /* (non-Javadoc)
    129             * @see org.maltparser.core.options.option.Option#toString()
    130             */
    131            public String toString() {
    132                    final StringBuilder sb = new StringBuilder();
    133                    sb.append(super.toString());
    134                    Formatter formatter = new Formatter(sb);
    135                    for (String value : legalValues) {
    136                            formatter.format("%2s%-10s - %-20s\n", "", value, legalValueDesc.get(value));
    137                    }
    138                    sb.append("-----------------------------------------------------------------------------\n");
    139                    return sb.toString();
    140            }
    141    }