Source for net.dpml.cli.commandline.CommandLineImpl

   1: /*
   2:  * Copyright 2003-2005 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.Collections;
  20: import java.util.Iterator;
  21: import java.util.List;
  22: 
  23: import net.dpml.cli.CommandLine;
  24: import net.dpml.cli.Option;
  25: import net.dpml.cli.resource.ResourceConstants;
  26: import net.dpml.cli.resource.ResourceHelper;
  27: 
  28: /**
  29:  * Instances of CommandLine represent a command line that has been processed
  30:  * according to the definition supplied to the parser.
  31:  *
  32:  * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
  33:  * @version @PROJECT-VERSION@
  34:  */
  35: public abstract class CommandLineImpl implements CommandLine
  36: {
  37:     /**
  38:      * Detects the presence of an option with the specified trigger in this 
  39:      * CommandLine.
  40:      * 
  41:      * @param trigger the trigger to search for
  42:      * @return true iff an option with this trigger is present
  43:      */
  44:     public final boolean hasOption( final String trigger )
  45:     {
  46:         return hasOption( getOption( trigger ) );
  47:     }
  48: 
  49:     /**
  50:      * Retrieves the Argument values associated with the specified Option
  51:      * 
  52:      * @param trigger a trigger used to lookup the Option
  53:      * @return a list of values or an empty List if none are found
  54:      */
  55:     public final List getValues( final String trigger )
  56:     {
  57:         return getValues( getOption( trigger ), Collections.EMPTY_LIST );
  58:     }
  59: 
  60:     /**
  61:      * Retrieves the Argument values associated with the specified Option
  62:      * 
  63:      * @param trigger a trigger used to lookup the Option
  64:      * @param defaultValues the result to return if no values are found
  65:      * @return a list of values or defaultValues if none are found
  66:      */
  67:     public final List getValues(
  68:       final String trigger, final List defaultValues )
  69:     {
  70:         return getValues( getOption( trigger ), defaultValues );
  71:     }
  72: 
  73:     /**
  74:      * Retrieves the Argument values associated with the specified Option
  75:      * 
  76:      * @param option the Option associated with the values
  77:      * @return a list of values or an empty List if none are found
  78:      */
  79:     public final List getValues( final Option option )
  80:     {
  81:         return getValues( option, Collections.EMPTY_LIST );
  82:     }
  83: 
  84:     /**
  85:      * Retrieves the single Argument value associated with the specified Option
  86:      * 
  87:      * @param trigger a trigger used to lookup the Option
  88:      * @return the matching value or null if none exists
  89:      * @throws IllegalStateException if more than one values are found
  90:      */
  91:     public final Object getValue( final String trigger ) throws IllegalStateException
  92:     {
  93:         return getValue( getOption( trigger ), null );
  94:     }
  95: 
  96:     /**
  97:      * Retrieves the single Argument value associated with the specified Option
  98:      * 
  99:      * @param trigger a trigger used to lookup the Option
 100:      * @param defaultValue the result to use if no values are found
 101:      * @return the matching value or defaultValue if none exists
 102:      * @throws IllegalStateException if more than one values are found
 103:      */
 104:     public final Object getValue(
 105:       final String trigger, final Object defaultValue ) throws IllegalStateException
 106:     {
 107:         return getValue( getOption( trigger ), defaultValue );
 108:     }
 109: 
 110:     /**
 111:      * Retrieves the single Argument value associated with the specified Option
 112:      * 
 113:      * @param option the Option associated with the value
 114:      * @return the matching value or null if none exists
 115:      * @throws IllegalStateException if more than one values are found
 116:      */
 117:     public final Object getValue( final Option option ) throws IllegalStateException
 118:     {
 119:         return getValue( option, null );
 120:     }
 121: 
 122:     /**
 123:      * Retrieves the single Argument value associated with the specified Option
 124:      * 
 125:      * @param option the Option associated with the value
 126:      * @param defaultValue the result to use if no values are found
 127:      * @return the matching value or defaultValue if none exists
 128:      * @throws IllegalStateException if more than one value is found
 129:      */
 130:     public final Object getValue( final Option option, final Object defaultValue )
 131:       throws IllegalStateException
 132:     {
 133:         final List values;
 134:         if( defaultValue == null )
 135:         {
 136:             values = getValues( option );
 137:         }
 138:         else
 139:         {
 140:             values = getValues( option, Collections.singletonList( defaultValue ) );
 141:         }
 142:         if( values.size() > 1 )
 143:         {
 144:             throw new IllegalStateException(
 145:               ResourceHelper.getResourceHelper().getMessage(
 146:                 ResourceConstants.ARGUMENT_TOO_MANY_VALUES ) );
 147:         }
 148:         if( values.isEmpty() )
 149:         {
 150:             return defaultValue;
 151:         }
 152:         return values.get( 0 );
 153:     }
 154: 
 155:     /**
 156:      * Retrieves the Boolean value associated with the specified Switch
 157:      * 
 158:      * @param trigger a trigger used to lookup the Option
 159:      * @return the Boolean associated with trigger or null if none exists
 160:      */
 161:     public final Boolean getSwitch( final String trigger )
 162:     {
 163:         return getSwitch( getOption( trigger ), null );
 164:     }
 165: 
 166:     /**
 167:      * Retrieves the Boolean value associated with the specified Switch
 168:      * 
 169:      * @param trigger a trigger used to lookup the Option
 170:      * @param defaultValue the Boolean to use if none match
 171:      * @return the Boolean associated with trigger or defaultValue if none exists
 172:      */
 173:     public final Boolean getSwitch(
 174:       final String trigger, final Boolean defaultValue )
 175:     {
 176:         return getSwitch( getOption( trigger ), defaultValue );
 177:     }
 178: 
 179:     /**
 180:      * Retrieves the Boolean value associated with the specified Switch
 181:      * 
 182:      * @param option the Option associated with the value
 183:      * @return the Boolean associated with option or null if none exists
 184:      */
 185:     public final Boolean getSwitch( final Option option )
 186:     {
 187:         return getSwitch( option, null );
 188:     }
 189: 
 190:     /**
 191:      * Retrieves the value associated with the specified property 
 192:      * 
 193:      * @param property the property name to lookup
 194:      * @return the value of the property or null
 195:      */
 196:     public final String getProperty( final String property )
 197:     {
 198:         return getProperty( property, null );
 199:     }
 200: 
 201:     /**
 202:      * Retrieves the number of times the specified Option appeared in this 
 203:      * CommandLine
 204:      * 
 205:      * @param trigger a trigger used to lookup the Option
 206:      * @return the number of occurrences of the option
 207:      */
 208:     public final int getOptionCount( final String trigger )
 209:     {
 210:         return getOptionCount( getOption( trigger ) );
 211:     }
 212: 
 213:     /**
 214:      * Retrieves the number of times the specified Option appeared in this 
 215:      * CommandLine
 216:      * 
 217:      * @param option the Option associated to check
 218:      * @return the number of occurrences of the option
 219:      */
 220:     public final int getOptionCount( final Option option )
 221:     {
 222:         if( option == null )
 223:         {
 224:             return 0;
 225:         }
 226:         int count = 0;
 227:         for( Iterator i = getOptions().iterator(); i.hasNext();) 
 228:         {
 229:             if( option.equals( i.next() ) )
 230:             {
 231:                 ++count;
 232:             }
 233:         }
 234:         return count;
 235:     }
 236: }