001    package org.maltparser.core.options.option;
002    
003    import org.maltparser.core.exception.MaltChainedException;
004    import org.maltparser.core.options.OptionException;
005    import org.maltparser.core.options.OptionGroup;
006    
007    /**
008     * A boolean option is an option that can only contain a boolean value (true or false). 
009     *
010     * @author Johan Hall
011     * @since 1.0
012    **/
013    public class BoolOption extends Option{
014            private Boolean defaultValue;
015            
016            /**
017             * Creates a boolean option description
018             * 
019             * @param group a reference to the option group.
020             * @param name  the name of the option.
021             * @param shortDescription      a short description of the option.
022             * @param flag  a short string that can be used in the command line.
023             * @param usage a string that explains the usage of the option.
024             * @param defaultValue  a default value string (true or false).
025             * @throws OptionException
026             */
027            public BoolOption(OptionGroup group, 
028                            String name, 
029                            String shortDescription, 
030                            String flag, 
031                            String usage, 
032                            String defaultValue) throws MaltChainedException {
033                    super(group, name, shortDescription, flag, usage);
034                    setDefaultValue(defaultValue);
035            }
036    
037            /* (non-Javadoc)
038             * @see org.maltparser.core.options.option.Option#getValueObject(java.lang.String)
039             */
040            public Object getValueObject(String value) throws MaltChainedException {
041                    if (value.equalsIgnoreCase("true")) {
042                            return new Boolean(true);
043                    } else if (value.equalsIgnoreCase("false")) {
044                            return new Boolean(false);
045                    } else {
046                            throw new OptionException("Illegal boolean value '"+value+"' for the '"+getName()+"' option. ");
047                    }
048            }
049            
050            public Object getDefaultValueObject() throws MaltChainedException {
051                    return new Boolean(defaultValue);
052            }
053            
054            public void setDefaultValue(String defaultValue) throws MaltChainedException {
055                    if (defaultValue.equalsIgnoreCase("true")) {
056                            this.defaultValue = true; 
057                    } else if (defaultValue.equalsIgnoreCase("false")) {
058                            this.defaultValue = false; 
059                    } else {
060                            throw new OptionException("Illegal boolean default value '"+defaultValue+"' for the '"+getName()+"' option. ");
061                    }
062            }
063            
064            public String getDefaultValueString() {
065                    return defaultValue.toString();
066            }
067            
068            public String getStringRepresentation(Object value) {
069                    if (value instanceof Boolean) {
070                            return value.toString();
071                    }
072                    return null;
073            }
074            
075            public String toString() {
076                    final StringBuilder sb = new StringBuilder();
077                    sb.append(super.toString());
078                    sb.append("-----------------------------------------------------------------------------\n");
079                    return sb.toString();
080            }
081    }
082