Source for net.dpml.cli.builder.PatternBuilder

   1: /**
   2:  * Copyright 2003-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.builder;
  18: 
  19: import java.util.HashSet;
  20: import java.util.Iterator;
  21: import java.util.Set;
  22: 
  23: import net.dpml.cli.Argument;
  24: import net.dpml.cli.Option;
  25: import net.dpml.cli.validation.ClassValidator;
  26: import net.dpml.cli.validation.DateValidator;
  27: import net.dpml.cli.validation.FileValidator;
  28: import net.dpml.cli.validation.NumberValidator;
  29: import net.dpml.cli.validation.URLValidator;
  30: import net.dpml.cli.validation.Validator;
  31: 
  32: /**
  33:  * Builds Options using a String pattern
  34:  *
  35:  * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
  36:  * @version @PROJECT-VERSION@
  37:  */
  38: public class PatternBuilder 
  39: {
  40:     private final GroupBuilder m_gbuilder;
  41:     private final DefaultOptionBuilder m_obuilder;
  42:     private final ArgumentBuilder m_abuilder;
  43:     private final Set m_options = new HashSet();
  44: 
  45:     /**
  46:      * Creates a new PatternBuilder
  47:      */
  48:     public PatternBuilder()
  49:     {
  50:         this(
  51:             new GroupBuilder(),
  52:             new DefaultOptionBuilder(),
  53:             new ArgumentBuilder() );
  54:     }
  55: 
  56:     /**
  57:      * Creates a new PatternBuilder
  58:      * @param gbuilder the GroupBuilder to use
  59:      * @param obuilder the DefaultOptionBuilder to use
  60:      * @param abuilder the ArgumentBuilder to use
  61:      */
  62:     public PatternBuilder(
  63:         final GroupBuilder gbuilder,
  64:         final DefaultOptionBuilder obuilder,
  65:         final ArgumentBuilder abuilder )
  66:     {
  67:         m_gbuilder = gbuilder;
  68:         m_obuilder = obuilder;
  69:         m_abuilder = abuilder;
  70:     }
  71: 
  72:     /**
  73:      * Creates a new Option instance.
  74:      * @return a new Option instance
  75:      */
  76:     public Option create()
  77:     {
  78:         final Option option;
  79:         if( m_options.size() == 1 )
  80:         {
  81:             option = (Option) m_options.iterator().next();
  82:         }
  83:         else
  84:         {
  85:             m_gbuilder.reset();
  86:             for( final Iterator i = m_options.iterator(); i.hasNext();)
  87:             {
  88:                 m_gbuilder.withOption( (Option) i.next() );
  89:             }
  90:             option = m_gbuilder.create();
  91:         }
  92:         reset();
  93:         return option;
  94:     }
  95: 
  96:     /**
  97:      * Resets this builder
  98:      * @return the builder
  99:      */
 100:     public PatternBuilder reset()
 101:     {
 102:         m_options.clear();
 103:         return this;
 104:     }
 105: 
 106:     private void createOption( final char type, final boolean required, final char opt ) 
 107:     {
 108:         final Argument argument;
 109:         if( type != ' ' )
 110:         {
 111:             m_abuilder.reset();
 112:             m_abuilder.withValidator( validator( type ) );
 113:             if( required )
 114:             {
 115:                 m_abuilder.withMinimum( 1 );
 116:             }
 117:             if( type != '*' )
 118:             {
 119:                 m_abuilder.withMaximum( 1 );
 120:             }
 121:             argument = m_abuilder.create();
 122:         }
 123:         else
 124:         {
 125:             argument = null;
 126:         }
 127: 
 128:         m_obuilder.reset();
 129:         m_obuilder.withArgument( argument );
 130:         m_obuilder.withShortName( String.valueOf( opt ) );
 131:         m_obuilder.withRequired( required );
 132:         m_options.add( m_obuilder.create() );
 133:     }
 134: 
 135:     /**
 136:      * Builds an Option using a pattern string.
 137:      * @param pattern the pattern to build from
 138:      */
 139:     public void withPattern( final String pattern )
 140:     {
 141:         int sz = pattern.length();
 142:         char opt = ' ';
 143:         char ch = ' ';
 144:         char type = ' ';
 145:         boolean required = false;
 146: 
 147:         for( int i=0; i < sz; i++ )
 148:         {
 149:             ch = pattern.charAt( i );
 150:             switch( ch ) 
 151:             {
 152:                 case '!' :
 153:                     required = true;
 154:                     break;
 155:                 case '@' :
 156:                 case ':' :
 157:                 case '%' :
 158:                 case '+' :
 159:                 case '#' :
 160:                 case '<' :
 161:                 case '>' :
 162:                 case '*' :
 163:                 case '/' :
 164:                     type = ch;
 165:                     break;
 166:                 default :
 167:                     if( opt != ' ' )
 168:                     {
 169:                         createOption( type, required, opt );
 170:                         required = false;
 171:                         type = ' ';
 172:                     }
 173:                     opt = ch;
 174:             }
 175:         }
 176:         if( opt != ' ' )
 177:         {
 178:             createOption( type, required, opt );
 179:         }
 180:     }
 181: 
 182:     private static Validator validator( final char c )
 183:     {
 184:         switch( c )
 185:         {
 186:             case '@' :
 187:                 final ClassValidator classv = new ClassValidator();
 188:                 classv.setInstance( true );
 189:                 return classv;
 190:             case '+' :
 191:                 final ClassValidator instancev = new ClassValidator();
 192:                 return instancev;
 193:                 //case ':':// no validator needed for a string
 194:             case '%' :
 195:                 return NumberValidator.getNumberInstance();
 196:             case '#' :
 197:                 return DateValidator.getDateInstance();
 198:             case '<' :
 199:                 final FileValidator existingv = new FileValidator();
 200:                 existingv.setExisting( true );
 201:                 existingv.setFile( true );
 202:                 return existingv;
 203:             case '>' :
 204:             case '*' :
 205:                 return new FileValidator();
 206:             case '/' :
 207:                 return new URLValidator();
 208:             default :
 209:                 return null;
 210:         }
 211:     }
 212: }