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  package org.apache.commons.cli;
18  
19  /***
20   * Validates an Option string.
21   *
22   * @author John Keyes ( john at integralsource.com )
23   */
24  public class OptionValidator {
25  
26      /***
27       * <p>Validates whether <code>opt</code> is a permissable Option
28       * shortOpt.  The rules that specify if the <code>opt</code>
29       * is valid are:</p>
30       * <ul>
31       *  <li><code>opt</code> is not NULL</li>
32       *  <li>a single character <code>opt</code> that is either
33       *  ' '(special case), '?', '@' or a letter</li>
34       *  <li>a multi character <code>opt</code> that only contains
35       *  letters.</li>
36       * </ul>
37       *
38       * @param opt The option string to validate
39       * @throws IllegalArgumentException if the Option is not valid.
40       */
41      static void validateOption(String opt)
42                          throws IllegalArgumentException
43      {
44          // check that opt is not NULL
45          if (opt == null)
46          {
47              return;
48          }
49  
50          // handle the single character opt
51          else if (opt.length() == 1)
52          {
53              char ch = opt.charAt(0);
54  
55              if (!isValidOpt(ch))
56              {
57                  throw new IllegalArgumentException("illegal option value '" + ch
58                                                     + "'");
59              }
60          }
61  
62          // handle the multi character opt
63          else
64          {
65              char[] chars = opt.toCharArray();
66  
67              for (int i = 0; i < chars.length; i++)
68              {
69                  if (!isValidChar(chars[i]))
70                  {
71                      throw new IllegalArgumentException(
72                              "opt contains illegal character value '" + chars[i]
73                              + "'");
74                  }
75              }
76          }
77      }
78  
79      /***
80       * <p>Returns whether the specified character is a valid Option.</p>
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       * <p>Returns whether the specified character is a valid character.</p>
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 }