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   * @version $Id: OptionValidator.java 1544819 2013-11-23 15:34:31Z tn $
24   * @since 1.1
25   */
26  final class OptionValidator
27  {
28      /**
29       * Validates whether <code>opt</code> is a permissible Option
30       * shortOpt.  The rules that specify if the <code>opt</code>
31       * is valid are:
32       *
33       * <ul>
34       *  <li>a single character <code>opt</code> that is either
35       *  ' '(special case), '?', '@' or a letter</li>
36       *  <li>a multi character <code>opt</code> that only contains
37       *  letters.</li>
38       * </ul>
39       * <p>
40       * In case {@code opt} is {@code null} no further validation is performed.
41       *
42       * @param opt The option string to validate, may be null
43       * @throws IllegalArgumentException if the Option is not valid.
44       */
45      static void validateOption(String opt) throws IllegalArgumentException
46      {
47          // if opt is NULL do not check further
48          if (opt == null)
49          {
50              return;
51          }
52          
53          // handle the single character opt
54          if (opt.length() == 1)
55          {
56              char ch = opt.charAt(0);
57  
58              if (!isValidOpt(ch))
59              {
60                  throw new IllegalArgumentException("Illegal option name '" + ch + "'");
61              }
62          }
63  
64          // handle the multi character opt
65          else
66          {
67              for (char ch : opt.toCharArray())
68              {
69                  if (!isValidChar(ch))
70                  {
71                      throw new IllegalArgumentException("The option '" + opt + "' contains an illegal "
72                                                         + "character : '" + ch + "'");
73                  }
74              }
75          }
76      }
77  
78      /**
79       * Returns whether the specified character is a valid Option.
80       *
81       * @param c the option to validate
82       * @return true if <code>c</code> is a letter, '?' or '@', otherwise false.
83       */
84      private static boolean isValidOpt(char c)
85      {
86          return isValidChar(c) || c == '?' || c == '@';
87      }
88  
89      /**
90       * Returns whether the specified character is a valid character.
91       *
92       * @param c the character to validate
93       * @return true if <code>c</code> is a letter.
94       */
95      private static boolean isValidChar(char c)
96      {
97          return Character.isJavaIdentifierPart(c);
98      }
99  }