Source for net.dpml.cli.builder.CommandBuilder

   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.Command;
  25: import net.dpml.cli.resource.ResourceConstants;
  26: import net.dpml.cli.resource.ResourceHelper;
  27: 
  28: /**
  29:  * Builds Command instances
  30:  *
  31:  * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
  32:  * @version @PROJECT-VERSION@
  33:  */
  34: public class CommandBuilder
  35: {
  36:     /** the preferred name of the command */
  37:     private String m_preferredName;
  38: 
  39:     /** the description of the command */
  40:     private String m_description;
  41: 
  42:     /** the aliases of the command */
  43:     private Set m_aliases;
  44: 
  45:     /** whether the command is required or not */
  46:     private boolean m_required;
  47: 
  48:     /** the argument of the command */
  49:     private Argument m_argument;
  50: 
  51:     /** the children of the command */
  52:     private Group m_children;
  53: 
  54:     /** the id of the command */
  55:     private int m_id;
  56: 
  57:     /**
  58:      * Creates a new <code>CommandBuilder</code> instance.
  59:      */
  60:     public CommandBuilder()
  61:     {
  62:         reset();
  63:     }
  64: 
  65:     /**
  66:      * Creates a new <code>Command</code> instance using the properties of the
  67:      * <code>CommandBuilder</code>.
  68:      *
  69:      * @return the new Command instance
  70:      */
  71:     public Command create()
  72:     {
  73:         // check we have a valid name
  74:         if( m_preferredName == null )
  75:         {
  76:             throw new IllegalStateException(
  77:               ResourceHelper.getResourceHelper().getMessage(
  78:                 ResourceConstants.OPTION_NO_NAME ) );
  79:         }
  80: 
  81:         // build the command
  82:         final Command option =
  83:           new Command( 
  84:             m_preferredName, m_description, m_aliases, m_required, m_argument, m_children, m_id );
  85: 
  86:         // reset the builder
  87:         reset();
  88:         return option;
  89:     }
  90: 
  91:     /**
  92:      * Resets the CommandBuilder to the defaults for a new Command.
  93:      *
  94:      * This method is called automatically at the end of the
  95:      * {@link #create() create} method.
  96:      * @return this <code>CommandBuilder</code>.
  97:      */
  98:     public CommandBuilder reset()
  99:     {
 100:         m_preferredName = null;
 101:         m_description = null;
 102:         m_aliases = new HashSet();
 103:         m_required = false;
 104:         m_argument = null;
 105:         m_children = null;
 106:         m_id = 0;
 107:         return this;
 108:     }
 109: 
 110:     /**
 111:      * Specifies the name for the next <code>Command</code>
 112:      * that is created.  The first name is used as the preferred
 113:      * display name for the <code>Command</code> and then
 114:      * later names are used as aliases.
 115:      *
 116:      * @param name the name for the next <code>Command</code>
 117:      * that is created.
 118:      * @return this <code>CommandBuilder</code>.
 119:      */
 120:     public CommandBuilder withName( final String name )
 121:     {
 122:         if( m_preferredName == null )
 123:         {
 124:             m_preferredName = name;
 125:         }
 126:         else
 127:         {
 128:             m_aliases.add( name );
 129:         }
 130:         return this;
 131:     }
 132: 
 133:     /**
 134:      * Specifies the description for the next <code>Command</code>
 135:      * that is created.  This description is used to produce
 136:      * help documentation for the <code>Command</code>.
 137:      *
 138:      * @param newDescription the description for the next
 139:      * <code>Command</code> that is created.
 140:      * @return this <code>CommandBuilder</code>.
 141:      */
 142:     public CommandBuilder withDescription( final String newDescription )
 143:     {
 144:         m_description = newDescription;
 145:         return this;
 146:     }
 147: 
 148:     /**
 149:      * Specifies whether the next <code>Command</code> created is
 150:      * required or not.
 151:      * @param newRequired whether the next <code>Command</code> created is
 152:      * required or not.
 153:      * @return this <code>CommandBuilder</code>.
 154:      */
 155:     public CommandBuilder withRequired( final boolean newRequired )
 156:     {
 157:         m_required = newRequired;
 158:         return this;
 159:     }
 160: 
 161:     /**
 162:      * Specifies the children for the next <code>Command</code>
 163:      * that is created.
 164:      *
 165:      * @param newChildren the child options for the next <code>Command</code>
 166:      * that is created.
 167:      * @return this <code>CommandBuilder</code>.
 168:      */
 169:     public CommandBuilder withChildren( final Group newChildren )
 170:     {
 171:         m_children = newChildren;
 172:         return this;
 173:     }
 174: 
 175:     /**
 176:      * Specifies the argument for the next <code>Command</code>
 177:      * that is created.
 178:      *
 179:      * @param newArgument the argument for the next <code>Command</code>
 180:      * that is created.
 181:      * @return this <code>CommandBuilder</code>.
 182:      */
 183:     public CommandBuilder withArgument( final Argument newArgument )
 184:     {
 185:         m_argument = newArgument;
 186:         return this;
 187:     }
 188: 
 189:     /**
 190:      * Specifies the id for the next <code>Command</code> that is created.
 191:      *
 192:      * @param newId the id for the next <code>Command</code> that is created.
 193:      * @return this <code>CommandBuilder</code>.
 194:      */
 195:     public final CommandBuilder withId( final int newId )
 196:     {
 197:         m_id = newId;
 198:         return this;
 199:     }
 200: }