CommandValidatorpublic class CommandValidator extends Object The CommandValidator object validates the command line
against the CLI Specification |
Fields Summary |
---|
private static final String | COMMAND_NAME_REGEXP | private static final String | OPTION_NAME_REGEXP | private static final String | NUMBER_OF_OPERANDS_QUANTIFIER_REGEXP | private static final String | NUMBER_OF_OPERANDS_REGEXP | private static final String | NUMBER_OF_OPERANDS_INCLUSION_REGEXP | private static final String | BOOLEAN | private static final String | TRUE | private static final String | FALSE | private static final String | PLUS | private static final String | QUESTION_MARK | private static final String | DASH |
Constructors Summary |
---|
public CommandValidator()Creates a new Command Validator
|
Methods Summary |
---|
private java.lang.String | getLocalizedString(java.lang.String key, java.lang.Object[] toInsert)returns the localized string from framework's LocalStrings.properties.
Calls the LocalStringsManagerFactory.getFrameworkLocalStringsManager()
method, returns "Key not found" if it cannot find the key
try
{
final LocalStringsManager lsm =
LocalStringsManagerFactory.getFrameworkLocalStringsManager();
if (toInsert == null)
return lsm.getString(key);
else
return lsm.getString(key, toInsert);
}
catch (CommandValidationException cve)
{
return LocalStringsManager.DEFAULT_STRING_VALUE;
}
| public void | validateCommandAndOptions(ValidCommand validCommand, java.util.Map optionsList, java.util.Vector operands)Validates the options and operands entered in the command line.
if (validCommand == null || optionsList == null || operands == null)
{
throw new CommandValidationException(getLocalizedString("CouldNotValidateCommand",
null));
}
verifyCommandName(validCommand.getName());
verifyOptionsAreValid(validCommand, optionsList);
verifyRequiredOptions(validCommand.getRequiredOptions(), optionsList,
validCommand.getName());
verifyDeprecatedOptions(validCommand.getDeprecatedOptions(),
optionsList, validCommand.getName());
//check for boolean required options
verifyBooleanOptions(validCommand.getRequiredOptions(), optionsList);
//check for the number of operands
verifyNumberOfOperands(validCommand.getNumberOfOperands(),
operands.size());
| private boolean | verifyBooleanOptions(java.util.Vector baseOptionsList, java.util.Map optionsList)verify that options with type boolean contains the value true or false
for (int ii=0; ii<baseOptionsList.size(); ii++)
{
final String optionType = ((ValidOption)baseOptionsList.get(ii)).getType();
final String optionName = ((ValidOption)baseOptionsList.get(ii)).getName();
if (optionType.compareToIgnoreCase(BOOLEAN) == 0 &&
optionsList.containsKey(optionName))
{
//if the option type is boolean, the value should be
//either true or false
final String optionValue =
((String)optionsList.get(optionName)).trim();
if (optionValue == null ||
!(optionValue.compareToIgnoreCase(TRUE) == 0 ||
optionValue.compareToIgnoreCase(FALSE) == 0 ) )
{
throw new CommandValidationException(getLocalizedString(
"OptionIsBoolean",
new Object[] {optionName}));
}
}
}
return true;
| private boolean | verifyCommandName(java.lang.String commandName)Verify that the command name must be in lower-cose and that it
must be alpha-numeric.
if (!commandName.matches(COMMAND_NAME_REGEXP))
{
throw new CommandValidationException(getLocalizedString("InvalidCommand",
new Object[] {commandName}));
}
return true;
| private boolean | verifyDeprecatedOptions(java.util.Vector deprecatedOptionsList, java.util.Map optionsList, java.lang.String commandName)verify if the deprecated options are entered in the command line
for (int ii=0; ii<deprecatedOptionsList.size(); ii++)
{
final String optionName =
((ValidOption)deprecatedOptionsList.get(ii)).getName();
if (!verifyOptionName(optionName))
{
throw new CommandValidationException(getLocalizedString("InvalidOption",
new Object[] {optionName,
commandName}));
}
//check if deprecated option name is in the optionsList
if (optionsList.containsKey(optionName))
{
CLILogger.getInstance().printWarning(getLocalizedString("OptionDeprecated",
new Object[] {optionName}));
}
}
return true;
| private boolean | verifyNumberOfOperands(java.lang.String numberOfOperands, int operandSize)verify the number of operands is compliant with CLI specification
the number of operands should follow the following convention:
* - 0 or more
? - 0 or 1
+ - 1 or more
number - the number of operand
+number - the number of operand should be equal to or
greater than the number
number1-number2 - the number of operand should be greater than
or equal to number1 and less than or equal to
number2
if (numberOfOperands.matches(NUMBER_OF_OPERANDS_QUANTIFIER_REGEXP))
{
//if numberofoperand is ?, then the operand size should be 0 or 1
if (numberOfOperands.equals(QUESTION_MARK) &&
(operandSize < 0 || operandSize > 1))
{
throw new CommandValidationException(getLocalizedString("ZeroOrOneOperand", null));
}
else if (numberOfOperands.equals(PLUS) && !(operandSize >= 1) )
{
throw new CommandValidationException(getLocalizedString("GreaterThanOneOperand", null));
}
}
else if (numberOfOperands.matches(NUMBER_OF_OPERANDS_REGEXP))
{
if (numberOfOperands.startsWith(PLUS) &&
Integer.parseInt(numberOfOperands.substring(1)) > operandSize)
{
throw new CommandValidationException(getLocalizedString(
"GreaterOrEqualToOperand",
new Object[] {
numberOfOperands.substring(1)}));
}
else if (!numberOfOperands.startsWith(PLUS) &&
Integer.parseInt(numberOfOperands) == 1 &&
operandSize < 1)
{
throw new CommandValidationException(getLocalizedString("OperandIsRequired", null));
}
else if (!numberOfOperands.startsWith(PLUS) &&
Integer.parseInt(numberOfOperands) != operandSize)
{
throw new CommandValidationException(getLocalizedString("EqualToOperand",
new Object[] {numberOfOperands}));
}
}
else if (numberOfOperands.matches(NUMBER_OF_OPERANDS_INCLUSION_REGEXP))
{
final int index = numberOfOperands.indexOf(DASH);
final int min = Integer.parseInt(numberOfOperands.substring(0,index));
final int max = Integer.parseInt(numberOfOperands.substring(index+1));
if (min > operandSize || max < operandSize)
{
throw new CommandValidationException(getLocalizedString(
"BetweenNumberOperand",
new Object[] {
String.valueOf(min),
String.valueOf(max)}));
}
}
else
{
throw new CommandValidationException(getLocalizedString(
"InvalidSytanxForNumberOfOperands",
new Object[] {numberOfOperands}));
}
return true;
| private boolean | verifyOptionName(java.lang.String optionName)Verify that the option name is valid. Option name must be
alpha-numeric with either a "-" between words.
return optionName.matches(OPTION_NAME_REGEXP);
| private boolean | verifyOptionsAreValid(ValidCommand validCommand, java.util.Map optionsList)verify options from the command line are valid
also verifies that option values is not null.
final Iterator iter = (optionsList.keySet()).iterator();
while (iter.hasNext())
{
final String optionName = (String)iter.next();
if (!(validCommand.hasValidOption(optionName) ||
validCommand.hasRequiredOption(optionName) ||
validCommand.hasDeprecatedOption(optionName)))
{
throw new CommandValidationException(getLocalizedString("InvalidOption",
new Object[] {optionName,
validCommand.getName()}));
}
//verify that the option value is not null
//if null then throw an CommandValidationException
if (optionsList.get(optionName) == null)
throw new CommandValidationException(getLocalizedString("OptionValueNotSpecified",
new Object[] {optionName} ));
}
return true;
| private boolean | verifyRequiredOptions(java.util.Vector requiredOptionsList, java.util.Map optionsList, java.lang.String commandName)verify if the required options are entered in the command line
for (int ii=0; ii<requiredOptionsList.size(); ii++)
{
final String optionName =
((ValidOption)requiredOptionsList.get(ii)).getName();
if (!verifyOptionName(optionName))
{
throw new CommandValidationException(getLocalizedString("InvalidOption",
new Object[] {optionName,
commandName}));
}
//check if required option name is in the optionsList and if it has a
//default value
if (!optionsList.containsKey(optionName) &&
!((ValidOption)requiredOptionsList.get(ii)).hasDefaultValue())
{
throw new CommandValidationException(getLocalizedString("OptionIsRequired",
new Object[] {optionName}));
}
}
return true;
|
|