View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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  
18  package org.apache.commons.cli;
19  
20  /**
21   * Validates an Option string.
22   *
23   * @author John Keyes ( john at integralsource.com )
24   * @version $Revision: 680644 $, $Date: 2008-07-29 09:13:48 +0100 (Tue, 29 Jul 2008) $
25   * @since 1.1
26   */
27  class OptionValidator
28  {
29      /**
30       * Validates whether <code>opt</code> is a permissable Option
31       * shortOpt.  The rules that specify if the <code>opt</code>
32       * is valid are:
33       *
34       * <ul>
35       *  <li><code>opt</code> is not NULL</li>
36       *  <li>a single character <code>opt</code> that is either
37       *  ' '(special case), '?', '@' or a letter</li>
38       *  <li>a multi character <code>opt</code> that only contains
39       *  letters.</li>
40       * </ul>
41       *
42       * @param opt The option string to validate
43       * @throws IllegalArgumentException if the Option is not valid.
44       */
45      static void validateOption(String opt) throws IllegalArgumentException
46      {
47          // check that opt is not NULL
48          if (opt == null)
49          {
50              return;
51          }
52  
53          // handle the single character opt
54          else if (opt.length() == 1)
55          {
56              char ch = opt.charAt(0);
57  
58              if (!isValidOpt(ch))
59              {
60                  throw new IllegalArgumentException("illegal option value '" + ch + "'");
61              }
62          }
63  
64          // handle the multi character opt
65          else
66          {
67              char[] chars = opt.toCharArray();
68  
69              for (int i = 0; i < chars.length; i++)
70              {
71                  if (!isValidChar(chars[i]))
72                  {
73                      throw new IllegalArgumentException("opt contains illegal character value '" + chars[i] + "'");
74                  }
75              }
76          }
77      }
78  
79      /**
80       * Returns whether the specified character is a valid Option.
81       *
82       * @param c the option to validate
83       * @return true if <code>c</code> is a letter, ' ', '?' or '@',
84       *         otherwise false.
85       */
86      private static boolean isValidOpt(char c)
87      {
88          return isValidChar(c) || c == ' ' || c == '?' || c == '@';
89      }
90  
91      /**
92       * Returns whether the specified character is a valid character.
93       *
94       * @param c the character to validate
95       * @return true if <code>c</code> is a letter.
96       */
97      private static boolean isValidChar(char c)
98      {
99          return Character.isJavaIdentifierPart(c);
100     }
101 }