in xmlschema-walker/src/main/java/org/apache/ws/commons/schema/docpath/XmlSchemaElementValidator.java [238:309]
private static void validateType(String name, String value, XmlSchemaTypeInfo typeInfo,
NamespaceContext nsContext) throws ValidationException {
if ((value == null) || (value.length() == 0)) {
throw new ValidationException(name + " cannot have a null or empty value!");
}
final HashMap<XmlSchemaRestriction.Type, List<XmlSchemaRestriction>> facets = typeInfo.getFacets();
switch (typeInfo.getType()) {
case ATOMIC:
validateAtomicType(name, value, typeInfo, nsContext);
break;
case LIST: {
/*
* A list is a whitespace-separated series of elements. Split the
* list and perform a type-check on the items.
*/
final String[] values = value.split(" ");
for (String item : values) {
validateType(name + " item value \"" + item + "\"", item, typeInfo.getChildTypes().get(0),
nsContext);
}
listLengthChecks(name, values, facets);
break;
}
case UNION: {
/*
* We just want to confirm that the value we are given validates
* against at least one of the types; we do not care which one.
*/
boolean foundValidType = false;
for (XmlSchemaTypeInfo unionType : typeInfo.getChildTypes()) {
try {
validateType(name, value, unionType, nsContext);
foundValidType = true;
break;
} catch (ValidationException e) {
// The type did not validate; try another.
}
}
if (!foundValidType) {
StringBuilder errMsg = new StringBuilder(name);
errMsg.append(" does not validate against any of its union of");
errMsg.append(" types. The value is \"").append(value);
errMsg.append("\" and the union types are: ");
for (int childIndex = 0; childIndex < typeInfo.getChildTypes().size() - 1; ++childIndex) {
errMsg.append(typeInfo.getChildTypes().get(childIndex).getBaseType());
errMsg.append(", ");
}
errMsg
.append(typeInfo.getChildTypes().get(typeInfo.getChildTypes().size() - 1).getBaseType());
errMsg.append('.');
throw new ValidationException(errMsg.toString());
}
break;
}
case COMPLEX:
// This only validates if the type is mixed.
if (!typeInfo.isMixed()) {
throw new ValidationException(name + " has a value of \"" + value
+ "\" but it represents a non-mixed complex type.");
}
break;
default:
throw new ValidationException(name + " has an unrecognized type of " + typeInfo.getType());
}
}