Source for net.dpml.cli.commandline.PropertiesCommandLine

   1: /**
   2:  * Copyright 2004 The Apache Software Foundation
   3:  * Copyright 2005 Stephen McConnell
   4:  *
   5:  * Licensed under the Apache License, Version 2.0 (the "License");
   6:  * you may not use this file except in compliance with the License.
   7:  * You may obtain a copy of the License at
   8:  *
   9:  *     http://www.apache.org/licenses/LICENSE-2.0
  10:  *
  11:  * Unless required by applicable law or agreed to in writing, software
  12:  * distributed under the License is distributed on an "AS IS" BASIS,
  13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14:  * See the License for the specific language governing permissions and
  15:  * limitations under the License.
  16:  */
  17: package net.dpml.cli.commandline;
  18: 
  19: import java.util.ArrayList;
  20: import java.util.Collections;
  21: import java.util.HashSet;
  22: import java.util.Iterator;
  23: import java.util.List;
  24: import java.util.Properties;
  25: import java.util.Set;
  26: import java.util.StringTokenizer;
  27: 
  28: import net.dpml.cli.Option;
  29: 
  30: /**
  31:  * A CommandLine implementation using a java Properties instance, useful for
  32:  * constructing a complex DefaultingCommandLine
  33:  *
  34:  * Options are keyed from their property name and presence in the Properties
  35:  * instance is taken as presence in the CommandLine.  Argument values are taken
  36:  * from the property value and are optionally separated using the separator
  37:  * char, defined at construction time.  Switch values can be specified using a
  38:  * simple value of <code>true</code> or <code>false</code>; obviously this means
  39:  * that Switches with Arguments are not supported by this implementation.
  40:  *
  41:  * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
  42:  * @version @PROJECT-VERSION@
  43:  * @see java.util.Properties
  44:  * @see net.dpml.cli.commandline.DefaultingCommandLine
  45:  * @see net.dpml.cli.Option#getPreferredName() 
  46:  */
  47: public class PropertiesCommandLine extends CommandLineImpl
  48: {
  49:     
  50:     private static final char NUL = '\0';
  51:     private final Properties m_properties;
  52:     private final Option m_root;
  53:     private final char m_separator;
  54:     
  55:     /**
  56:      * Creates a new PropertiesCommandLine using the specified root Option,
  57:      * Properties instance.  The character 0 is used as the value separator.
  58:      *
  59:      * @param root the CommandLine's root Option
  60:      * @param properties the Properties instance to get values from
  61:      */
  62:     public PropertiesCommandLine( final Option root, final Properties properties )
  63:     {
  64:         this( root, properties, NUL );
  65:     }
  66:     
  67:     /**
  68:      * Creates a new PropertiesCommandLine using the specified root Option,
  69:      * Properties instance and value separator.
  70:      *
  71:      * @param root the CommandLine's root Option
  72:      * @param properties the Properties instance to get values from
  73:      * @param separator the character to split argument values
  74:      */
  75:     public PropertiesCommandLine( final Option root, final Properties properties, final char separator )
  76:     {
  77:         m_root = root;
  78:         m_properties = properties;
  79:         m_separator = separator;
  80:     }
  81:     
  82:     /**
  83:      * Detects the presence of an option in this CommandLine.
  84:      * 
  85:      * @param option the Option to search for
  86:      * @return true iff the option is present
  87:      */
  88:     public boolean hasOption( Option option )
  89:     {
  90:         if( option==null )
  91:         {
  92:             return false;
  93:         }
  94:         else
  95:         {
  96:             return m_properties.containsKey( option.getPreferredName() );
  97:         }
  98:     }
  99: 
 100:     /**
 101:      * Finds the Option with the specified trigger
 102:      * 
 103:      * @param trigger the name of the option to retrieve
 104:      * @return the Option matching the trigger or null if none exists
 105:      */
 106:     public Option getOption( String trigger )
 107:     {
 108:         return m_root.findOption( trigger );
 109:     }
 110: 
 111:     /**
 112:      * Retrieves the Argument values associated with the specified Option
 113:      * 
 114:      * @param option the Option associated with the values
 115:      * @param defaultValues the result to return if no values are found
 116:      * @return a list of values or defaultValues if none are found
 117:      */
 118:     public List getValues( final Option option, final List defaultValues )
 119:     {
 120:         final String value = m_properties.getProperty( option.getPreferredName() );
 121:         
 122:         if( value==null )
 123:         {
 124:             return defaultValues;
 125:         }
 126:         else if( m_separator > NUL )
 127:         {
 128:             final List values = new ArrayList();
 129:             final StringTokenizer tokens = new StringTokenizer( value, String.valueOf( m_separator ) );
 130:             
 131:             while( tokens.hasMoreTokens() )
 132:             {
 133:                 values.add( tokens.nextToken() );
 134:             }
 135:             return values;
 136:         }
 137:         else
 138:         {
 139:             return Collections.singletonList( value );
 140:         }
 141:     }
 142: 
 143:     /**
 144:      * Retrieves the Boolean value associated with the specified Switch
 145:      * 
 146:      * @param option the Option associated with the value
 147:      * @param defaultValue the Boolean to use if none match
 148:      * @return the Boolean associated with option or defaultValue if none exists
 149:      */
 150:     public Boolean getSwitch( final Option option, final Boolean defaultValue ) 
 151:     {
 152:         final String value = m_properties.getProperty( option.getPreferredName() );
 153:         if( "true".equals( value ) )
 154:         {
 155:             return Boolean.TRUE;
 156:         }
 157:         else if( "false".equals( value ) )
 158:         {
 159:             return Boolean.FALSE;
 160:         }
 161:         else
 162:         {
 163:             return defaultValue;
 164:         }
 165:     }
 166:     
 167:     /**
 168:      * Retrieves the value associated with the specified property 
 169:      * 
 170:      * @param property the property name to lookup
 171:      * @param defaultValue the value to use if no other is found
 172:      * @return the value of the property or defaultValue
 173:      */
 174:     public String getProperty( final String property, final String defaultValue )
 175:     {
 176:         return m_properties.getProperty( property, defaultValue );
 177:     }
 178: 
 179:     /**
 180:      * Retrieves the set of all property names associated with this CommandLine
 181:      * 
 182:      * @return a none null set of property names 
 183:      */
 184:     public Set getProperties()
 185:     {
 186:         return m_properties.keySet();
 187:     }
 188: 
 189:     /**
 190:      * Retrieves a list of all Options found in this CommandLine
 191:      * 
 192:      * @return a none null list of Options
 193:      */
 194:     public List getOptions()
 195:     {
 196:         final List options = new ArrayList();
 197:         final Iterator keys = m_properties.keySet().iterator();
 198:         while( keys.hasNext() )
 199:         {
 200:             final String trigger = (String) keys.next();
 201:             final Option option = m_root.findOption( trigger );
 202:             if( option!=null )
 203:             {
 204:                 options.add( option );
 205:             }
 206:         }
 207:         return Collections.unmodifiableList( options );
 208:     }
 209: 
 210:     /**
 211:      * Retrieves a list of all Option triggers found in this CommandLine
 212:      * 
 213:      * @return a none null list of Option triggers
 214:      */
 215:     public Set getOptionTriggers()
 216:     {
 217:         final Set triggers = new HashSet();
 218:         final Iterator options = getOptions().iterator();
 219:         while( options.hasNext() ) 
 220:         {
 221:             final Option option = (Option) options.next();
 222:             triggers.addAll( option.getTriggers() );
 223:         }
 224:         return Collections.unmodifiableSet( triggers );
 225:     }
 226: }