/** * Validates the given Object * * @param object the Object to validate * @param resolver the ClassDescriptorResolver to load ClassDescriptors for use during validation. */ public void validate(Object object, ClassDescriptorResolver resolver) throws ValidationException { if (object == null) { throw new ValidationException("Cannot validate a null Object."); } if (resolver == null) { resolver = new ClassDescriptorResolverImpl(); } XMLClassDescriptor classDesc = resolver.resolve(object.getClass()); // -- we cannot validate an object if ClassDescriptor is null if (classDesc == null) return; TypeValidator validator = classDesc.getValidator(); XMLFieldDescriptor fieldDesc = null; try { if (validator != null) { if (validator instanceof ClassValidator) ((ClassValidator) validator).validate(object, resolver); else validator.validate(object); } // -- default validation else { // -- just validate each field FieldDescriptor[] fields = classDesc.getFields(); if (fields != null) { for (int i = 0; i < fields.length; i++) { fieldDesc = (XMLFieldDescriptor) fields[i]; if (fieldDesc == null) continue; FieldValidator fieldValidator = fieldDesc.getValidator(); if (fieldValidator != null) fieldValidator.validate(object, resolver); } } } } catch (ValidationException vx) { // -- add location information XPathLocation loc = (XPathLocation) vx.getLocation(); if (loc == null) { loc = new XPathLocation(); vx.setLocation(loc); if (fieldDesc != null) { if (fieldDesc.getNodeType() == NodeType.Attribute) loc.addAttribute(fieldDesc.getXMLName()); else loc.addChild(fieldDesc.getXMLName()); } } loc.addParent(classDesc.getXMLName()); throw vx; } } // -- validate