Source for net.dpml.cli.builder.DefaultOptionBuilder

   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.builder;
  18: 
  19: import java.util.HashSet;
  20: import java.util.Set;
  21: 
  22: import net.dpml.cli.Argument;
  23: import net.dpml.cli.Group;
  24: import net.dpml.cli.option.DefaultOption;
  25: import net.dpml.cli.resource.ResourceConstants;
  26: import net.dpml.cli.resource.ResourceHelper;
  27: 
  28: /**
  29:  * Builds DefaultOption instances.
  30:  *
  31:  * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
  32:  * @version @PROJECT-VERSION@
  33:  */
  34: public class DefaultOptionBuilder
  35: {
  36:     private final String m_shortPrefix;
  37:     private final String m_longPrefix;
  38:     private final boolean m_burstEnabled;
  39:     private String m_preferredName;
  40:     private Set m_aliases;
  41:     private Set m_burstAliases;
  42:     private boolean m_required;
  43:     private String m_description;
  44:     private Argument m_argument;
  45:     private Group m_children;
  46:     private int m_id;
  47: 
  48:     /**
  49:      * Creates a new DefaultOptionBuilder using defaults
  50:      * @see DefaultOption#DEFAULT_SHORT_PREFIX
  51:      * @see DefaultOption#DEFAULT_LONG_PREFIX
  52:      * @see DefaultOption#DEFAULT_BURST_ENABLED
  53:      */
  54:     public DefaultOptionBuilder()
  55:     {
  56:         this( 
  57:           DefaultOption.DEFAULT_SHORT_PREFIX, 
  58:           DefaultOption.DEFAULT_LONG_PREFIX,
  59:           DefaultOption.DEFAULT_BURST_ENABLED );
  60:     }
  61: 
  62:     /**
  63:      * Creates a new DefaultOptionBuilder
  64:      * @param shortPrefix the prefix to use for short options
  65:      * @param longPrefix the prefix to use for long options
  66:      * @param burstEnabled whether to allow gnu style bursting
  67:      * @throws IllegalArgumentException if either prefix is less than on
  68:      *                                  character long
  69:      */
  70:     public DefaultOptionBuilder( 
  71:       final String shortPrefix, final String longPrefix, final boolean burstEnabled )
  72:       throws IllegalArgumentException
  73:     {
  74:         if( ( shortPrefix == null ) || ( shortPrefix.length() == 0 ) )
  75:         {
  76:             throw new IllegalArgumentException(
  77:               ResourceHelper.getResourceHelper().getMessage(
  78:                 ResourceConstants.OPTION_ILLEGAL_SHORT_PREFIX ) );
  79:         }
  80: 
  81:         if( ( longPrefix == null ) || ( longPrefix.length() == 0 ) )
  82:         {
  83:             throw new IllegalArgumentException(
  84:               ResourceHelper.getResourceHelper().getMessage(
  85:                 ResourceConstants.OPTION_ILLEGAL_LONG_PREFIX ) );
  86:         }
  87: 
  88:         m_shortPrefix = shortPrefix;
  89:         m_longPrefix = longPrefix;
  90:         m_burstEnabled = burstEnabled;
  91:         reset();
  92:     }
  93: 
  94:     /**
  95:      * Creates a DefaultOption instance
  96:      * @return the new instance
  97:      * @throws IllegalStateException if no names have been supplied
  98:      */
  99:     public DefaultOption create() throws IllegalStateException
 100:     {
 101:         if( m_preferredName == null )
 102:         {
 103:             throw new IllegalStateException(
 104:               ResourceHelper.getResourceHelper().getMessage(
 105:                 ResourceConstants.OPTION_NO_NAME ) );
 106:         }
 107:         final DefaultOption option =
 108:             new DefaultOption(
 109:               m_shortPrefix, 
 110:               m_longPrefix, 
 111:               m_burstEnabled, 
 112:               m_preferredName, 
 113:               m_description,
 114:               m_aliases, 
 115:               m_burstAliases, 
 116:               m_required, 
 117:               m_argument, 
 118:               m_children, 
 119:               m_id );
 120:         reset();
 121:         return option;
 122:     }
 123: 
 124:     /**
 125:      * Resets the builder.
 126:      * @return this <code>DefaultOptionBuilder</code>.
 127:      */
 128:     public DefaultOptionBuilder reset()
 129:     {
 130:         m_preferredName = null;
 131:         m_description = null;
 132:         m_aliases = new HashSet();
 133:         m_burstAliases = new HashSet();
 134:         m_required = false;
 135:         m_argument = null;
 136:         m_children = null;
 137:         m_id = 0;
 138:         return this;
 139:     }
 140: 
 141:     /**
 142:      * Use this short option name. The first name is used as the preferred
 143:      * display name for the Command and then later names are used as aliases.
 144:      *
 145:      * @param shortName the name to use
 146:      * @return this builder
 147:      */
 148:     public DefaultOptionBuilder withShortName( final String shortName )
 149:     {
 150:         final String name = m_shortPrefix + shortName;
 151:         if( m_preferredName == null )
 152:         {
 153:             m_preferredName = name;
 154:         }
 155:         else
 156:         {
 157:             m_aliases.add( name );
 158:         }
 159:         if( m_burstEnabled && ( name.length() == ( m_shortPrefix.length() + 1 ) ) )
 160:         {
 161:             m_burstAliases.add( name );
 162:         }
 163:         return this;
 164:     }
 165: 
 166:     /**
 167:      * Use this long option name.  The first name is used as the preferred
 168:      * display name for the Command and then later names are used as aliases.
 169:      *
 170:      * @param longName the name to use
 171:      * @return this builder
 172:      */
 173:     public DefaultOptionBuilder withLongName( final String longName )
 174:     {
 175:         final String name = m_longPrefix + longName;
 176:         if( m_preferredName == null )
 177:         {
 178:             m_preferredName = name;
 179:         }
 180:         else
 181:         {
 182:             m_aliases.add( name );
 183:         }
 184:         return this;
 185:     }
 186: 
 187:     /**
 188:      * Use this option description
 189:      * @param newDescription the description to use
 190:      * @return this builder
 191:      */
 192:     public DefaultOptionBuilder withDescription( final String newDescription )
 193:     {
 194:         m_description = newDescription;
 195:         return this;
 196:     }
 197: 
 198:     /**
 199:      * Use this optionality
 200:      * @param newRequired true iff the Option is required
 201:      * @return this builder
 202:      */
 203:     public DefaultOptionBuilder withRequired( final boolean newRequired )
 204:     {
 205:         m_required = newRequired;
 206:         return this;
 207:     }
 208: 
 209:     /**
 210:      * Use this child Group
 211:      * @param newChildren the child Group to use
 212:      * @return this builder
 213:      */
 214:     public DefaultOptionBuilder withChildren( final Group newChildren )
 215:     {
 216:         m_children = newChildren;
 217:         return this;
 218:     }
 219: 
 220:     /**
 221:      * Use this Argument
 222:      * @param newArgument the argument to use
 223:      * @return this builder
 224:      */
 225:     public DefaultOptionBuilder withArgument( final Argument newArgument )
 226:     {
 227:         m_argument = newArgument;
 228:         return this;
 229:     }
 230: 
 231:     /**
 232:      * Sets the id
 233:      *
 234:      * @param newId
 235:      *            the id of the DefaultOption
 236:      * @return this DefaultOptionBuilder
 237:      */
 238:     public final DefaultOptionBuilder withId( final int newId )
 239:     {
 240:         m_id = newId;
 241:         return this;
 242:     }
 243: }