Source for net.dpml.cli.OptionException

   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;
  18: 
  19: import java.util.Collections;
  20: import java.util.Set;
  21: 
  22: import net.dpml.cli.resource.ResourceHelper;
  23: 
  24: /**
  25:  * A problem found while dealing with command line options.
  26:  *
  27:  * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
  28:  * @version @PROJECT-VERSION@
  29:  */
  30: public class OptionException extends Exception
  31: {
  32:    /**
  33:     * The settings used when displaying the related Option.
  34:     *
  35:     * @see DisplaySetting
  36:     */
  37:     public static final Set HELP_SETTINGS =
  38:         Collections.unmodifiableSet( 
  39:           Collections.singleton( DisplaySetting.DISPLAY_PROPERTY_OPTION ) );
  40: 
  41:     /** resource HELPER instance */
  42:     private static final ResourceHelper HELPER = ResourceHelper.getResourceHelper();
  43: 
  44:     /** The Option the exception relates to */
  45:     private final Option m_option;
  46: 
  47:     /** The message explaining the Exception */
  48:     private final String m_message;
  49: 
  50:     /**
  51:      * Creates a new OptionException.
  52:      *
  53:      * @param option the Option the exception relates to
  54:      */
  55:     public OptionException( final Option option )
  56:     {
  57:         this( option, null, null );
  58:     }
  59: 
  60:     /**
  61:      * Creates a new OptionException.
  62:      * @param option the Option the exception relates to
  63:      * @param messageKey the id of the message to display
  64:      */
  65:     public OptionException(
  66:       final Option option, final String messageKey )
  67:     {
  68:         this( option, messageKey, null );
  69:     }
  70: 
  71:     /**
  72:      * Creates a new OptionException.
  73:      * @param option the Option the exception relates to
  74:      * @param messageKey the id of the message to display
  75:      * @param value a value to display with the message
  76:      */
  77:     public OptionException(
  78:       final Option option, final String messageKey, final String value )
  79:     {
  80:         m_option = option;
  81:         if( messageKey != null )
  82:         {
  83:             final StringBuffer buffer = new StringBuffer();
  84:             if( value != null )
  85:             {
  86:                 buffer.append( HELPER.getMessage( messageKey, value ) );
  87:             }
  88:             else
  89:             {
  90:                 buffer.append( HELPER.getMessage( messageKey ) );
  91:             }
  92:             buffer.append( " " );
  93:             m_option.appendUsage( buffer, HELP_SETTINGS, null );
  94:             m_message = buffer.toString();
  95:         }
  96:         else
  97:         {
  98:             m_message = "";
  99:         }
 100:     }
 101: 
 102:     /**
 103:      * Gets the Option the exception relates to
 104:      *
 105:      * @return The related Option
 106:      */
 107:     public Option getOption()
 108:     {
 109:         return m_option;
 110:     }
 111: 
 112:    /**
 113:     * Return the exception message.
 114:     * @return the exception message
 115:     */
 116:     public String getMessage()
 117:     {
 118:         return m_message;
 119:     }
 120: }