1 23 24 package com.sun.enterprise.cli.framework; 25 26 import java.util.HashMap ; 27 import java.util.Vector ; 28 import java.util.Iterator ; 29 30 35 public class CommandValidator { 36 private static final String COMMAND_NAME_REGEXP = "^[a-z\\-][a-z0-9\\-\\_\\ ]*$"; 38 39 private static final String OPTION_NAME_REGEXP = "^\\w[-\\w]*"; 41 42 private static final String NUMBER_OF_OPERANDS_QUANTIFIER_REGEXP = "^[\\*\\?\\+]"; 44 45 private static final String NUMBER_OF_OPERANDS_REGEXP = "^\\+?\\d+"; 47 48 private static final String NUMBER_OF_OPERANDS_INCLUSION_REGEXP = "^\\d+\\-\\d+"; 50 51 private static final String BOOLEAN = "boolean"; 53 54 private static final String TRUE = "true"; 56 57 private static final String FALSE = "false"; 59 60 private static final String PLUS = "+"; 62 63 private static final String QUESTION_MARK = "?"; 65 66 private static final String DASH = "-"; 68 69 70 public CommandValidator() 71 { 72 } 73 74 75 82 public void validateCommandAndOptions(final ValidCommand validCommand, 83 final HashMap optionsList, 84 final Vector operands) 85 throws CommandValidationException 86 { 87 if (validCommand == null || optionsList == null || operands == null) 88 { 89 throw new CommandValidationException(getLocalizedString("CouldNotValidateCommand", 90 null)); 91 } 92 93 verifyCommandName(validCommand.getName()); 94 95 verifyNumberOfOperands(validCommand.getNumberOfOperands(), 97 operands.size()); 98 99 verifyOptionsAreValid(validCommand, optionsList); 100 101 verifyRequiredOptions(validCommand.getRequiredOptions(), optionsList, 102 validCommand.getName()); 103 104 verifyDeprecatedOptions(validCommand.getDeprecatedOptions(), 105 optionsList, validCommand.getName()); 106 107 verifyBooleanOptions(validCommand.getRequiredOptions(), optionsList); 109 } 110 111 112 118 private boolean verifyCommandName(final String commandName) 119 throws CommandValidationException 120 { 121 if (!commandName.matches(COMMAND_NAME_REGEXP)) 122 { 123 throw new CommandValidationException(getLocalizedString("InvalidCommand", 124 new Object [] {commandName})); 125 } 126 return true; 127 } 128 129 130 135 private boolean verifyOptionName(final String optionName) 136 { 137 return optionName.matches(OPTION_NAME_REGEXP); 138 } 139 140 141 148 private boolean verifyDeprecatedOptions(final Vector deprecatedOptionsList, 149 final HashMap optionsList, 150 final String commandName) 151 throws CommandValidationException 152 { 153 for (int ii=0; ii<deprecatedOptionsList.size(); ii++) 154 { 155 final String optionName = 156 ((ValidOption)deprecatedOptionsList.get(ii)).getName(); 157 if (!verifyOptionName(optionName)) 158 { 159 throw new CommandValidationException(getLocalizedString("InvalidOption", 160 new Object [] {optionName, 161 commandName})); 162 } 163 164 if (optionsList.containsKey(optionName)) 166 { 167 CLILogger.getInstance().printWarning(getLocalizedString("OptionDeprecated", 168 new Object [] {optionName})); 169 } 170 } 171 return true; 172 } 173 174 175 182 private boolean verifyRequiredOptions(final Vector requiredOptionsList, 183 final HashMap optionsList, 184 final String commandName) 185 throws CommandValidationException 186 { 187 for (int ii=0; ii<requiredOptionsList.size(); ii++) 188 { 189 final String optionName = 190 ((ValidOption)requiredOptionsList.get(ii)).getName(); 191 if (!verifyOptionName(optionName)) 192 { 193 throw new CommandValidationException(getLocalizedString("InvalidOption", 194 new Object [] {optionName, 195 commandName})); 196 } 197 198 if (!optionsList.containsKey(optionName) && 201 !((ValidOption)requiredOptionsList.get(ii)).hasDefaultValue()) 202 { 203 throw new CommandValidationException(getLocalizedString("OptionIsRequired", 204 new Object [] {optionName})); 205 } 206 } 207 return true; 208 } 209 210 211 218 private boolean verifyOptionsAreValid(final ValidCommand validCommand, 219 final HashMap optionsList) 220 throws CommandValidationException 221 { 222 223 final Iterator iter = (optionsList.keySet()).iterator(); 224 while (iter.hasNext()) 225 { 226 final String optionName = (String )iter.next(); 227 if (!(validCommand.hasValidOption(optionName) || 228 validCommand.hasRequiredOption(optionName) || 229 validCommand.hasDeprecatedOption(optionName))) 230 { 231 throw new CommandValidationException(getLocalizedString("InvalidOption", 232 new Object [] {optionName, 233 validCommand.getName()})); 234 } 235 if (optionsList.get(optionName) == null) 238 throw new CommandValidationException(getLocalizedString("OptionValueNotSpecified", 239 new Object [] {optionName} )); 240 } 241 return true; 242 } 243 244 245 252 private boolean verifyBooleanOptions(final Vector baseOptionsList, 253 final HashMap optionsList) 254 throws CommandValidationException 255 { 256 for (int ii=0; ii<baseOptionsList.size(); ii++) 257 { 258 final String optionType = ((ValidOption)baseOptionsList.get(ii)).getType(); 259 final String optionName = ((ValidOption)baseOptionsList.get(ii)).getName(); 260 if (optionType.compareToIgnoreCase(BOOLEAN) == 0 && 261 optionsList.containsKey(optionName)) 262 { 263 final String optionValue = 266 ((String )optionsList.get(optionName)).trim(); 267 if (optionValue == null || 268 !(optionValue.compareToIgnoreCase(TRUE) == 0 || 269 optionValue.compareToIgnoreCase(FALSE) == 0 ) ) 270 { 271 throw new CommandValidationException(getLocalizedString( 272 "OptionIsBoolean", 273 new Object [] {optionName})); 274 } 275 276 } 277 } 278 return true; 279 } 280 281 282 299 private boolean verifyNumberOfOperands(final String numberOfOperands, 300 final int operandSize) 301 throws CommandValidationException 302 { 303 if (numberOfOperands.matches(NUMBER_OF_OPERANDS_QUANTIFIER_REGEXP)) 304 { 305 if (numberOfOperands.equals(QUESTION_MARK) && 307 (operandSize < 0 || operandSize > 1)) 308 { 309 throw new CommandValidationException(getLocalizedString("ZeroOrOneOperand", null)); 310 } 311 else if (numberOfOperands.equals(PLUS) && !(operandSize >= 1) ) 312 { 313 throw new CommandValidationException(getLocalizedString("GreaterThanOneOperand", null)); 314 } 315 } 316 else if (numberOfOperands.matches(NUMBER_OF_OPERANDS_REGEXP)) 317 { 318 if (numberOfOperands.startsWith(PLUS) && 319 Integer.parseInt(numberOfOperands.substring(1)) > operandSize) 320 { 321 throw new CommandValidationException(getLocalizedString( 322 "GreaterOrEqualToOperand", 323 new Object [] { 324 numberOfOperands.substring(1)})); 325 } 326 else if (!numberOfOperands.startsWith(PLUS) && 327 Integer.parseInt(numberOfOperands) == 1 && 328 operandSize < 1) 329 { 330 throw new CommandValidationException(getLocalizedString("OperandIsRequired", null)); 331 } 332 else if (!numberOfOperands.startsWith(PLUS) && 333 Integer.parseInt(numberOfOperands) != operandSize) 334 { 335 throw new CommandValidationException(getLocalizedString("EqualToOperand", 336 new Object [] {numberOfOperands})); 337 } 338 } 339 else if (numberOfOperands.matches(NUMBER_OF_OPERANDS_INCLUSION_REGEXP)) 340 { 341 final int index = numberOfOperands.indexOf(DASH); 342 final int min = Integer.parseInt(numberOfOperands.substring(0,index)); 343 final int max = Integer.parseInt(numberOfOperands.substring(index+1)); 344 if (min > operandSize || max < operandSize) 345 { 346 throw new CommandValidationException(getLocalizedString( 347 "BetweenNumberOperand", 348 new Object [] { 349 String.valueOf(min), 350 String.valueOf(max)})); 351 } 352 } 353 else 354 { 355 throw new CommandValidationException(getLocalizedString( 356 "InvalidSytanxForNumberOfOperands", 357 new Object [] {numberOfOperands})); 358 } 359 return true; 360 } 361 362 363 370 private String getLocalizedString(String key, Object [] toInsert) 371 { 372 try 373 { 374 final LocalStringsManager lsm = 375 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 376 if (toInsert == null) 377 return lsm.getString(key); 378 else 379 return lsm.getString(key, toInsert); 380 } 381 catch (CommandValidationException cve) 382 { 383 return LocalStringsManager.DEFAULT_STRING_VALUE; 384 } 385 } 386 387 388 } 389 | Popular Tags |