ValidationManagerpublic class ValidationManager extends Object {@link ValidationManager} is an object that provides Validation functionality,
through its validate( ) function.
{@link ValidationManagerFactory} creates
{@link ValidationManager} based on the Validation File.
Validation File specifies Validation rules i.e.
which Constraints to apply to which elements. Objects are validated by
applying specified Constraints to its elements. Validation file is xml file
based on validation.dtd . If no Validation File is specified,
ValidationManagerFactory returns default
ValidationManager . Default ValidationManager is
based on default Validation File. Default Validation File defines Constraints
for 8.0 Sun DTDs.
Validations are performed, recursively on the given Object.
Two types of Validations are perfomed, Structural validations and Specified
validations. Structural validations are expressed through
{@link CardinalConstraint} . CardinalConstraint is
an implicit Constraint. Its always applied to each of the element; you dont
need to specify it explicitly. Whereas , other Constaints need to be
explicitly specified for each element you wanted to apply it to.
Constraints to be applied are specified through Validation File.
You can also define, your own custom Constraint s and apply them
to any elements. When you define your own Constraint s, you
need to provide information about them to the framework. This information is
provided through Constraints File. Constraints File is xml file, based on
constraints.dtd . Constraints File to use is specified through
system property constraints.file . You can override
Constraint s provided by framework , by providing your own. |
Fields Summary |
---|
private static String | GET_PREFIXA prefix used to construct the getter method name of the bean. | private static String | SET_PREFIXA prefix used to construct the setter method name of the bean. | private static String | SIZE_PREFIXA prefix used to construct the size method name of the bean. | private static final String | defaultConstraintsFileA file that is used specify infromation for the constraints provided
by this framework. Constraint objects are built using the
information in this file. | private static final String | CARDINAL_CONSTRAINT_CLASSA Cardinal Constraint class name. | private String | validationFileA file that is used to specify Constraints to be
applied to various elements. xpaths are used to specify elements.
While constructing this object, clients of this framework can specify
their own file by providing the file name to the appropriate
constructor. If client do not, then the default file( specified by
defaultValidationFile ) is used. | private String | defaultValidationFileA default Validation file that specify Constraints to be
applied to various elements. xpaths are used to specify elements.
This file is used by framework if client do not specify one.
This file contains default validation rules. | private HashMap | xpathToValidatorA map that stores for each Validatee object, the xpath and
the corresponding Validator object. | private com.sun.enterprise.tools.common.validation.data.Validation | validationA root bean representing validation.xml | private com.sun.enterprise.tools.common.validation.constraints.data.Constraints | constraintsA root bean representing constaints.xml | private com.sun.enterprise.tools.common.validation.util.Utils | utilsAn object providing utilities |
Constructors Summary |
---|
public ValidationManager()Creates a new instance of ValidationManager
utils = new Utils();
| public ValidationManager(String validationFile)Creates a new instance of ValidationManager
if(null != validationFile){
this.validationFile = validationFile;
}
utils = new Utils();
|
Methods Summary |
---|
private com.sun.enterprise.tools.common.validation.constraints.Constraint | buildConstraint(com.sun.enterprise.tools.common.validation.data.Check check, com.sun.enterprise.tools.common.validation.constraints.data.CheckInfo checkInfo)Constructs Cosntraint object, from the given
Check and CheckInfo objects.
Check object identifies a particular
Constraint , whereas CheckInfo object
provides additional information about the Constraint ,
such as Constraint class name, Constraint constructor arguments &
thier types.
For a given element Check object is constructed from
the information specified in Validation File whereas the
corresponding CheckInfo object is built from the
infromation provided in Constraints File.
//Constraint objects are always created using the default constructor.
//Fields in Constraint object are then set using using the setter
//methods. In order to set the fields in the Constraint object, field
//values are fetched from the check object and the no of arguments,
//argument names and thier types are fetched from checkInfo object.
//Assumptions
// Every Constraint must have default constructor.
// Every Constraint that has fields must provide setter functions
// to set those fields.
// <name> field of <check> in Validation File is matched with
// <name> field of <check-info> in Constraints File to find out
// infromation of a particulatar Constraint.
// Number of <parameters> defined in <check> should match the
// number of <arguments> of corresponding <check-info>
//
Constraint constraint = null;
String classname = checkInfo.getClassname();
Arguments arguments = checkInfo.getArguments();
Class[] argumentTypeClass = new Class[1];
Object[] argumentValue = new Object[1];
String argumentName;
String argumentType;
constraint = (Constraint)utils.createObject(classname);
if(null != arguments){
int size = arguments.sizeArgument();
Parameters parameters = check.getParameters();
if((null != parameters) && (size == parameters.sizeParameter())){
Argument argument = null;
Parameter parameter = null;
for(int i=0; i<size; i++) {
argument = arguments.getArgument(i);
argumentName = argument.getName();
argumentType = argument.getType();
parameter =
getParameter(parameters, argumentName);
if(parameter == null){
String format = BundleReader.getValue(
"Warning_no_value_specified_for"); //NOI18N
Object[] substitutes =
new Object[]{argumentName, check.getName()};
System.out.println(
MessageFormat.format(format, substitutes));
continue;
}
if(null == argumentType){
argumentType = "java.lang.String"; //NOI18N
}
int noOfValues = 1;
if(argumentType.equals("java.lang.String[]")){ //NOI18N
Integer sz = (Integer)utils.getElement("value", //NOI18N
parameter, SIZE_PREFIX);
noOfValues = sz.intValue();
argumentType = "java.lang.String"; //NOI18N
}
for(int j=0; j<noOfValues; j++) {
argumentValue[0] = utils.getElement("value", //NOI18N
j, parameter);
argumentTypeClass[0] =
utils.getClass(argumentType);
String methodName =
utils.methodNameFromDtdName(argumentName,
SET_PREFIX);
Method method =
utils.getMethod(utils.getClass(constraint),
methodName, argumentTypeClass);
utils.invoke(constraint, method, argumentValue);
}
}
} else {
String format = BundleReader.getValue(
"MSG_Conflicting_Constraint_information"); //NOI18N
Object[] substitues = new Object[]{check.getName()}; //NOI18N
String message = MessageFormat.format(format, substitues);
assert false : message;
}
}
return constraint;
| private void | constructConstraints()Constructs Constraints object, representing the root of
Constraints File. Constraints File provide information about the
Constraint s i.e. Constraint class name, Constraint
constructor arguments & thier types.
Constraints customConstraints = null;
Constraints defaultConstraints = null;
String constraintsFile =
System.getProperty("constraints.file"); //NOI18N
///System.out.println(consFile);
if(constraintsFile != null){
customConstraints = getConstraints(constraintsFile);
}
if(defaultConstraintsFile != null){
defaultConstraints = getConstraints(defaultConstraintsFile);
}
if(customConstraints != null){
if(defaultConstraints != null){
int count = defaultConstraints.sizeCheckInfo();
CheckInfo checkInfo = null;
CheckInfo checkInfoClone = null;
for(int i=0; i<count; i++){
checkInfo = defaultConstraints.getCheckInfo(i);
///String str = checkInfo.dumpBeanNode();
checkInfoClone = (CheckInfo) checkInfo.clone();
///System.out.println(checkInfoClone.dumpBeanNode());
customConstraints.addCheckInfo(checkInfoClone);
}
constraints = customConstraints;
} else {
constraints = customConstraints;
}
} else {
constraints = defaultConstraints;
}
| private void | constructValidation()Constructs Validation object, representing the root of
xml defining Constraints to be applied to different
elements.
Clients of this framework can specify their own file.
If client do not, then the default file( specified by
defaultValidationFile ) is used.
//Create an InpurtStream object
URL url = null;
InputStream inputStream = null;
if(validationFile != null){
inputStream = getInputStream(validationFile);
if(inputStream == null){
String format =
BundleReader.getValue("MSG_using_the_default_file");//NOI18N
Object[] arguments = new Object[]{defaultValidationFile};
System.out.println(MessageFormat.format(format, arguments));
inputStream = getDafaultStream();
}
} else {
inputStream = getDafaultStream();
}
if(inputStream != null){
//Create graph
try {
validation = Validation.createGraph(inputStream);
} catch(Exception e) {
System.out.println(e.getMessage());
validation = null;
}
}
| private void | constructXpathToValidator()Constructs the xpath to Validator map.
Uses Validation File and Constraints File to build the map.
//read validation file and construct xPathToValidator Map
if(null == validation){
constructValidation();
}
if(null == validation){
String format =
BundleReader.getValue("Warning_Not_available"); //NOI18N
Object[] arguments = new Object[]{"Validation Data"}; //NOI18N
System.out.println(MessageFormat.format(format, arguments));
return;
}
xpathToValidator = new HashMap();
int noOfElements = validation.sizeElement();
Element element = null;
Validator validator = null;
String elementName = null;
String beanName = null;
Check check = null;
String checkName = null;
for(int i=0; i<noOfElements; i++) {
element = validation.getElement(i);
elementName = utils.getName(element.getName(),
Constants.XPATH_DELIMITER_CHAR);
beanName = utils.getParentName(element.getName(),
Constants.XPATH_DELIMITER_CHAR);
validator = (Validator) xpathToValidator.get(beanName);
///String format =
/// BundleReader.getValue("Name_Value_Pair_Format"); //NOI18N
///Object[] arguments;
if(null == validator) {
validator = new Validator();
xpathToValidator.put(beanName, validator);
///format =
/// BundleReader.getValue("Name_Value_Pair_Format"); //NOI18N
///arguments =
/// new Object[]{"Bean Name", beanName}; //NOI18N
///System.out.println(MessageFormat.format(format, arguments));
///arguments = new Object[]{"Validator", validator}; //NOI18N
///System.out.println(MessageFormat.format(format, arguments));
}
int noOfChecks = element.sizeCheck();
Constraint constraint = null;
for(int j=0; j<noOfChecks; j++){
check = element.getCheck(j);
constraint = getConstraint(check);
if(null != constraint){
validator.addElementConstraint(elementName, constraint);
///arguments =
/// new Object[]{"Element Name", elementName}; //NOI18N
///System.out.println(MessageFormat.format(format, arguments));
///arguments = new Object[]{"Check", check.getName()}; //NOI18N
///System.out.println(MessageFormat.format(format, arguments));
///arguments = new Object[]{"Constraint", constraint}; //NOI18N
///System.out.println(MessageFormat.format(format, arguments));
///constraint.print();
}
}
}
| private com.sun.enterprise.tools.common.validation.constraints.CardinalConstraint | getCardinalConstraint(int cardinal)Constructs CardinalCosntraint object, for the given
cardinal. Cardinal Constraint is implicit i.e user do not need to
define Cardinal Constraint for elements. Its applied implicitly to
all the elements. Where in case of other Constraints user must
specify which Constraints to apply to which elements. User specifies
this through Validation File.
Class[] argumentTypes = new Class[] {int.class};
Constructor constructor =
utils.getConstructor(CARDINAL_CONSTRAINT_CLASS, argumentTypes);
Integer parameter = new Integer(cardinal);
Object[] argumentValues = new Object[] {parameter};
return (CardinalConstraint) utils.createObject(constructor,
argumentValues);
| private com.sun.enterprise.tools.common.validation.constraints.data.CheckInfo | getCheckInfo(java.lang.String checkName, com.sun.enterprise.tools.common.validation.constraints.data.Constraints constraints)Returns the CheckInfo object for the given
Check object. Check object represents
the Constraint object. CheckInfo hold
information for a particular Constraint , for example,
Constraint class name, Constraint constructor arguments & thier types.
CheckInfo checkInfo = null;
int size = constraints.sizeCheckInfo();
for(int i=0; i<size; i++) {
checkInfo = constraints.getCheckInfo(i);
if(checkName.equals(checkInfo.getName())) {
return checkInfo;
}
}
return null;
| private com.sun.enterprise.tools.common.validation.constraints.Constraint | getConstraint(com.sun.enterprise.tools.common.validation.data.Check check)Constructs Constraint object, for the given
check . check object represents the
information of the constraint declared in Constraints File.
Constraints to be applied to different elements are
declared in Validation File. This method uses Constraints File to
construct Constraint object from the given check object.
Constraint constraint = null;
if(null == constraints) {
constructConstraints();
}
CheckInfo checkInfo = null;
if(null != constraints){
String checkName = check.getName();
checkInfo = getCheckInfo(checkName, constraints);
if(null != checkInfo){
constraint = buildConstraint(check, checkInfo);
} else {
String format =
BundleReader.getValue("MSG_No_definition_for"); //NOI18N
Object[] arguments =
new Object[]{"CheckInfo", checkName}; //NOI18N
System.out.println(MessageFormat.format(format, arguments));
}
} else {
String format =
BundleReader.getValue("MSG_Not_defined"); //NOI18N
Object[] arguments = new Object[]{"Constraints"}; //NOI18N
System.out.println(MessageFormat.format(format, arguments));
}
return constraint;
| private com.sun.enterprise.tools.common.validation.constraints.data.Constraints | getConstraints(java.lang.String constraintsFile)Constructs Constraints object, representing the root of
given Constraints File.
URL url = null;
InputStream inputStream = null;
Constraints constraints = null;
if(constraintsFile != null){
inputStream = getInputStream(constraintsFile);
}
//Create graph
if(inputStream != null){
try {
constraints = Constraints.createGraph(inputStream);
} catch(Exception e) {
System.out.println(e.getMessage());
constraints = null;
}
}
return constraints;
| private java.io.InputStream | getDafaultStream()Constructs InputStream object, for default Validation File
defining Constraints to be applied to different
elements. The default Validation File is represented by the private
attribute defaultValidationFile
InputStream inputStream = null;
URL url = null;
if(defaultValidationFile != null){
url = utils.getUrlObject(defaultValidationFile);
if(url != null) {
try {
inputStream = url.openStream();
} catch (IOException exception){
System.out.println(exception.getMessage());
}
} else {
assert false : (BundleReader.getValue(
"Error_control_should_never_reach_here")); //NOI18N
String format =
BundleReader.getValue("Error_can_not_access_file"); //NOI18N
Object[] arguments = new Object[]{defaultValidationFile};
String message = MessageFormat.format(format, arguments);
assert false : message;
}
} else {
assert false : (BundleReader.getValue(
"Error_control_should_never_reach_here")); //NOI18N
}
return inputStream;
| private java.io.InputStream | getInputStream(java.lang.String inputFile)Constructs InputStream object, for the given file.
//Create an InpurtStream object
InputStream inputStream = null;
if(inputFile.lastIndexOf(':") == -1){
URL url = null;
url = utils.getUrlObject(inputFile);
if(url != null) {
try {
inputStream = url.openStream();
} catch (IOException exception){
System.out.println(exception.getMessage());
}
} else {
String format = BundleReader.getValue(
"Error_specified_file_can_not_be_used"); //NOI18N
Object[] arguments = new Object[]{inputFile};
System.out.println(MessageFormat.format(format, arguments));
}
} else {
File file = new File(inputFile);
if(file.exists()){
try {
inputStream = new FileInputStream(file);
} catch(FileNotFoundException exception){
System.out.println(exception.getMessage());
inputStream = null;
}
} else {
String format = BundleReader.getValue(
"Error_specified_file_can_not_be_used"); //NOI18N
Object[] arguments = new Object[]{inputFile};
System.out.println(MessageFormat.format(format, arguments));
}
}
return inputStream;
| private com.sun.enterprise.tools.common.validation.data.Parameter | getParameter(com.sun.enterprise.tools.common.validation.data.Parameters parameters, java.lang.String name)Gets the element from this object with the given name.
int size = parameters.sizeParameter();
Parameter returnValue = null;
Parameter parameter = null;
String parameterName = null;
for(int i=0; i<size; i++){
parameter = (Parameter)utils.getElement("parameter", i, //NOI18N
parameters);
parameterName = parameter.getName();
if(parameterName.equals(name)){
returnValue = parameter;
break;
}
}
return returnValue;
| private java.lang.String | getValidateeImplementation(java.lang.Object object, java.lang.String propertiesFile)Gets the Validatee Implementation name for the given object from the
given properties file.
String returnVal = null;
Class classObject = utils.getClass(object);
String className = classObject.getName();
//switch the BundleReader to read from the given impl file
//validatee implementations are specified through this impl file
BundleReader.setBundle(propertiesFile);
String validateeImplName = BundleReader.getValue(className);
while(!(className.equals("java.lang.Object"))){ //NOI18N
if(!(validateeImplName.equals(className))){
returnVal = validateeImplName;
break;
} else {
classObject = classObject.getSuperclass();
className = classObject.getName();
validateeImplName = BundleReader.getValue(className);
}
}
return returnVal;
| private Validator | getValidator(java.lang.String xpath)Returns the Validator for the given xpath.
Returns null if there is no Validator
object for the given xpath.
ValidationManager constructs the Validator
objects for xpaths using Validation File and Constraints File.
Validator validator = null;
if(null == xpathToValidator){
constructXpathToValidator();
}
if(null != xpathToValidator) {
validator = (Validator)xpathToValidator.get(xpath);
}
return validator;
| private java.util.Collection | recurse(java.lang.String elementName, Validatee validatee)Recurses by calling the appropriate methods.
This method is called when the given element to be validated is
an object itself(Validatee ).
ArrayList failures = new ArrayList();
boolean isIndexed = validatee.isIndexed(elementName);
if(isIndexed){
failures.addAll(validateBeans(elementName, validatee));
} else {
failures.addAll(validateBean(elementName, validatee));
}
return failures;
| public java.util.Collection | validate(java.lang.Object object)Validates the given Object .
Validatee Implementation for the given object must be
provided. Validatee Implementation of an object is a
Validatee wrapper around it. Validatee Implementation
of an object is specified to framework through an Implementation File.
Implementation File is a .properties file, with name-value
pair entries in it. An entry in Implementation File specifies the object
name and the corresponding Validatee Implementation.
Implementation File to use, is specified to framework through system
property impl.file
//This method get called in case of Objects that are not Validatee.
//This method, essentially, gets the Object's Validatee Implementation
//and uses it instead.
Collection collection = new ArrayList();
if(object != null){
boolean validateeImplFound = false;
String validateeImpl = null;
String implFile =
System.getProperty("impl.file"); //NOI18N
///System.out.println(implFile);
//User specified impl file overrides the default imple file
//default impl file -- Constants.IMPL_FILE
if(implFile != null){
validateeImpl = getValidateeImplementation(object, implFile);
}
//Using default impl file; user specified not available
if(validateeImpl == null){
validateeImpl =
getValidateeImplementation(object, Constants.IMPL_FILE);
}
//switch the BundleReader back to read from bundle file
BundleReader.setBundle(Constants.BUNDLE_FILE);
if(validateeImpl != null){
Validatee validatee =
(Validatee)ObjectFactory.newInstance(validateeImpl, object);
if(validatee != null){
collection = validate(validatee);
}
} else {
Class classObject = utils.getClass(object);
String className = classObject.getName();
String format = BundleReader.getValue(
"MSG_given_object_is_not_validatee"); //NOI18N
Object[] arguments = new Object[]{className};
System.out.println(MessageFormat.format(format, arguments));
}
}
return collection;
| public java.util.Collection | validate(Validatee validatee)Validates the given Validatee .
//This method applies Cardinal constraint and Custom constraints defined
//for each of the elements of the validatee. It recurses for elements
//that are objects.
ArrayList failures = new ArrayList();
if(validatee != null){
String xpath = validatee.getXPath();
ArrayList elementNames = validatee.getElementNames();
ArrayList elementDtdNames = validatee.getElementDtdNames();
int noOfElements = elementNames.size();
String elementName = null;
String elementDtdName = null;
int count = 0;
for(int i=0; i<noOfElements; i++){
elementName = (String)elementNames.get(i);
elementDtdName = (String)elementDtdNames.get(i);
//apply Cardinal Constraint
failures.addAll(validateCardinalConstraint(validatee, elementName,
elementDtdName));
//apply Other Constraints
boolean isBean = validatee.isBeanElement(elementName);
if(isBean){
//Recurse if an Object
failures.addAll(recurse(elementName, validatee));
} else {
Validator validator = getValidator(xpath);
if(null != validator){
failures.addAll(validator.validate(elementName,
elementDtdName, validatee));
} else {
///String format = BundleReader.getValue(
/// "MSG_No_definition_for"); //NOI18N
///Object[] arguments =
/// new Object[]{"Validator", xpath}; //NOI18N
///System.out.println(
/// MessageFormat.format(format, arguments));
}
}
}
}
return failures;
| private java.util.Collection | validateBean(java.lang.String elementName, Validatee validatee)Validates the given element of the given Validatee .
This method is called for an element that is an object.
//This method does an recursive call on the object of given element.
ArrayList failures = new ArrayList();
Object child = null;
child = validatee.getElement(elementName);
if(child != null) {
failures.addAll(validate(child));
}
return failures;
| private java.util.Collection | validateBeans(java.lang.String elementName, Validatee validatee)Validates the given element of the given Validatee .
This method is called for an element that is an array(optional array
or madatory array) of objects.
//This method does an recursive call for each of the objects in an
//array of given element.
int noOfElements = 0;
String sizeMethodName = utils.methodNameFromBeanName(elementName,
SIZE_PREFIX);
Method sizeMethod = validatee.getMethod(sizeMethodName);
noOfElements = ((Integer)validatee.invoke(sizeMethod)).intValue();
ArrayList failures = new ArrayList();
Object child = null;
for(int i=0; i<noOfElements; i++) {
child = validatee.getElement(elementName, i);
if(child != null) {
failures.addAll(validate(child));
}
}
return failures;
| private java.util.Collection | validateCardinalConstraint(Validatee validatee, java.lang.String elementName, java.lang.String elementDtdName)Validates the given element of given Validatee
for Cardinality.This method is called for each and every
element of the Validatee. You does not need to define Cardinal
cosntraints in Validation File. Cardinal constraint is implicity
applied to each and every element.
ArrayList failures = new ArrayList();
int cardinal = validatee.getElementCardinal(elementName);
CardinalConstraint constraint = getCardinalConstraint(cardinal);
switch(cardinal){
case Constants.MANDATORY_ARRAY :
case Constants.OPTIONAL_ARRAY: {
Object[] elements =
(Object [])validatee.getElements(elementName);
String name = validatee.getIndexedXPath() +
Constants.XPATH_DELIMITER + elementDtdName;
///String name = validatee.getXPath() +
///Constants.XPATH_DELIMITER + elementDtdName;
failures.addAll(constraint.match(elements, name));
break;
}
case Constants.OPTIONAL_ELEMENT : {
Object element = validatee.getElement(elementName);
break;
}
case Constants.MANDATORY_ELEMENT :
default : {
Object element = validatee.getElement(elementName);
String name = validatee.getIndexedXPath() +
Constants.XPATH_DELIMITER + elementDtdName;
///String name = validatee.getXPath() +
///Constants.XPATH_DELIMITER + elementDtdName;
failures.addAll(constraint.match(element, name));
}
break;
}
return failures;
| public java.util.Collection | validateIndividualProperty(java.lang.String property, java.lang.String absoluteDtdName, java.lang.String fieldName)Validates the given Element.
ArrayList failures = new ArrayList();
String xpath = utils.getParentName(absoluteDtdName,
Constants.XPATH_DELIMITER_CHAR);
Validator validator = getValidator(xpath);
if(null != validator){
failures.addAll(validator.validateIndividualProperty(
property, absoluteDtdName, fieldName));
} else {
///String format = BundleReader.getValue(
/// "MSG_No_definition_for"); //NOI18N
///Object[] arguments =
/// new Object[]{"Validator", xpath}; //NOI18N
///System.out.println(
/// MessageFormat.format(format, arguments));
}
return failures;
|
|