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  final class OptionValidator {
24      /* package private for testing access */
25      /** The array of additional characters allowed as the first character in the option but not in the rest of the option */
26      static final char[] ADDITIONAL_OPTION_CHARS = {'?', '@'};
27      /** The array of additional characters allowed in the rest of the option but not in the first position */
28      static final char[] ADDITIONAL_LONG_CHARS = {'-'};
29  
30      /**
31       * Returns whether the specified character is a valid character.
32       * A character is valid if any of the following conditions are true:
33       * <ul>
34       * <li>it is a letter</li>
35       * <li>it is a currency symbol (such as '$')</li>
36       * <li>it is a connecting punctuation character (such as '_')</li>
37       * <li>it is a digit</li>
38       * <li>it is a numeric letter (such as a Roman numeral character)</li>
39       * <li>it is a combining mark</li>
40       * <li>it is a non-spacing mark</li>
41       * <li>isIdentifierIgnorable returns true for the character</li>
42       * <li>it is a hyphen/dash ('-')</li>
43       * </ul>
44       * @param c the character to validate
45       * @return true if {@code c} is a valid character letter.
46       */
47      private static boolean isValidChar(final char c) {
48          return Character.isJavaIdentifierPart(c) || search(ADDITIONAL_LONG_CHARS, c);
49      }
50  
51      /**
52       * Returns whether the specified character is a valid Option.
53       * A character is valid if any of the following conditions are true:
54       * <ul>
55       * <li>it is a letter</li>
56       * <li>it is a currency symbol (such as '$')</li>
57       * <li>it is a connecting punctuation character (such as '_')</li>
58       * <li>it is a digit</li>
59       * <li>it is a numeric letter (such as a Roman numeral character)</li>
60       * <li>it is a combining mark</li>
61       * <li>it is a non-spacing mark</li>
62       * <li>isIdentifierIgnorable returns true for the character</li>
63       * <li>it is a question mark or 'at' sign ('?' or '@')</li>
64       * </ul>
65       * @param c the option to validate
66       * @return true if {@code c} is a letter, '?' or '@', otherwise false.
67       */
68      private static boolean isValidOpt(final char c) {
69          return Character.isJavaIdentifierPart(c) || search(ADDITIONAL_OPTION_CHARS, c);
70      }
71  
72      /**
73       * Checks the char array for a matching char.
74       * @param chars the char array to search
75       * @param c the char to look for.
76       * @return {@code true} if {@code c} was in {@code ary}, {@code false} otherwise.
77       */
78      private static boolean search(final char[] chars, final char c) {
79          for (final char a : chars) {
80              if (a == c) {
81                  return true;
82              }
83          }
84          return false;
85      }
86  
87      /**
88       * Validates whether {@code opt} is a permissible Option shortOpt. The rules that specify if the {@code opt}
89       * is valid are:
90       *
91       * <ul>
92       * <li>a single character {@code opt} that is either Chars.SP(special case), '?', '@' or a letter</li>
93       * <li>a multi character {@code opt} that only contains valid characters</li>
94       * </ul>
95       * </p><p>
96       * A character is valid if any of the following conditions are true:
97       * <ul>
98       * <li>it is a letter</li>
99       * <li>it is a currency symbol (such as '$')</li>
100      * <li>it is a connecting punctuation character (such as '_')</li>
101      * <li>it is a digit</li>
102      * <li>it is a numeric letter (such as a Roman numeral character)</li>
103      * <li>it is a combining mark</li>
104      * <li>it is a non-spacing mark</li>
105      * <li>isIdentifierIgnorable returns true for the character</li>
106      * <li>it is a hyphen/dash ('-')</li>
107      * </ul>
108      * </p><p>
109      * In case {@code opt} is {@code null} no further validation is performed.
110      *
111      * @param option The option string to validate, may be null
112      * @throws IllegalArgumentException if the Option is not valid.
113      */
114     static String validate(final String option) throws IllegalArgumentException {
115         // if opt is null do not check further.
116         if (option == null) {
117             return null;
118         }
119         if (option.isEmpty()) {
120             throw new IllegalArgumentException("Empty option name.");
121         }
122         final char[] chars = option.toCharArray();
123         final char ch0 = chars[0];
124         if (!isValidOpt(ch0)) {
125             throw new IllegalArgumentException(String.format("Illegal option name '%s'.", ch0));
126         }
127         // handle the multi-character opt
128         if (option.length() > 1) {
129             for (int i = 1; i < chars.length; i++) {
130                 final char ch = chars[i];
131                 if (!isValidChar(ch)) {
132                     throw new IllegalArgumentException(String.format("The option '%s' contains an illegal " + "character : '%s'.", option, ch));
133                 }
134             }
135         }
136         return option;
137     }
138 }