* @param context the ValidationContext
*/
public void validate(Object object, ValidationContext context)
throws ValidationException {
if (object == null) {
throw new ValidationException("Cannot validate a null object.");
}
Class a = getJavaClass();
ClassLoader acl = a.getClassLoader();
Class b = object.getClass();
ClassLoader bcl = b.getClassLoader();
if (!getJavaClass().isAssignableFrom(object.getClass())) {
String err = "The given object is not an instance of the class"
+ " described by this ClassDecriptor.";
throw new ValidationException(err);
}
//-- DEBUG
//System.out.println("Validating class: " + object.getClass().getName());
//-- /DEBUG
XMLFieldDescriptor[] localElements = getElementArray();
XMLFieldDescriptor[] localAttributes = getAttributeArray();
if (_extends != null) {
//-- cascade call for validation
if (_extends instanceof XMLClassDescriptorImpl) {
((XMLClassDescriptorImpl) _extends).validate(object, context);
} else {
TypeValidator baseValidator = _extends.getValidator();
if (baseValidator != null) {
baseValidator.validate(object, context);
}
}
//-- get local element descriptors by filtering out inherited ones
XMLFieldDescriptor[] inheritedElements = _extends.getElementDescriptors();
XMLFieldDescriptor[] allElements = localElements;
localElements = new XMLFieldDescriptor[allElements.length - inheritedElements.length];
int localIdx = 0;
for (int i = 0; i < allElements.length; i++) {
XMLFieldDescriptor desc = allElements[i];
boolean isInherited = false;
for (int idx = 0; idx < inheritedElements.length; idx++) {
if (inheritedElements[idx].equals(desc)) {
isInherited = true;
break;
}
}
if (!isInherited) {
localElements[localIdx] = desc;
++localIdx;
}
}
//-- get local attribute descriptors by filtering out inherited ones
XMLFieldDescriptor[] inheritedAttributes = _extends.getAttributeDescriptors();
XMLFieldDescriptor[] allAttributes = localAttributes;
localAttributes =
new XMLFieldDescriptor[allAttributes.length - inheritedAttributes.length];
localIdx = 0;
for (int i = 0; i < allAttributes.length; i++) {
XMLFieldDescriptor desc = allAttributes[i];
boolean isInherited = false;
for (int idx = 0; idx < inheritedAttributes.length; idx++) {
if (inheritedAttributes[idx].equals(desc)) {
isInherited = true;
break;
}
}
if (!isInherited) {
localAttributes[localIdx] = desc;
++localIdx;
}
}
}
switch (_compositor) {
case CHOICE:
boolean found = false;
boolean hasLocalDescs = (localElements.length > 0);
XMLFieldDescriptor fieldDesc = null;
//-- handle elements, affected by choice
for (int i = 0; i < localElements.length; i++) {
XMLFieldDescriptor desc = localElements[i];
if (desc == null) {
continue;
}
FieldHandler handler = desc.getHandler();
if (handler.getValue(object) != null) {
//Special case if we have a Vector, an ArrayList
//or an Array --> need to check if it is not empty
if (desc.isMultivalued()) {
Object temp = handler.getValue(object);
//-- optimize this?
if (Array.getLength(temp) == 0) {
temp = null;
continue;
}
temp = null;
}
if (found) {
String err = null;
if (desc.isContainer()) {
err = "The group '" + desc.getFieldName();
err += "' cannot exist at the same time that ";
if (fieldDesc.isContainer()) {
err += "the group '" + fieldDesc.getFieldName();
} else {
err += "the element '" + fieldDesc.getXMLName();
}
err += "' also exists.";
} else {
err = "The element '" + desc.getXMLName();
err += "' cannot exist at the same time that ";
err += "element '" + fieldDesc.getXMLName() + "' also exists.";
}
throw new ValidationException(err);
}
found = true;
fieldDesc = desc;
FieldValidator fieldValidator = desc.getValidator();
if (fieldValidator != null) {
fieldValidator.validate(object, context);
}
}
}
// if all elements are mandatory, print the grammar that the choice
// must match.
if ((!found) && (hasLocalDescs)) {
StringBuffer buffer = new StringBuffer(40);
boolean existsOptionalElement = false;
buffer.append('(');
String sep = " | ";
for (int i = 0; i < localElements.length; i++) {
XMLFieldDescriptor desc = localElements[i];
if (desc == null) {
continue;
}
FieldValidator fieldValidator = desc.getValidator();
if (fieldValidator.getMinOccurs() == 0) {
existsOptionalElement = true;
break;
}
buffer.append(sep);
buffer.append(desc.getXMLName());
}
buffer.append(')');
if (!existsOptionalElement) {
String err = "In the choice contained in <" + this.getXMLName()
+ ">, at least one of these elements must appear:\n"
+ buffer.toString();
throw new ValidationException(err);
}
}
//-- handle attributes, not affected by choice
for (int i = 0; i < localAttributes.length; i++) {
validateField(object, context, localAttributes[i]);