in xmlschema-walker/src/main/java/org/apache/ws/commons/schema/docpath/XmlSchemaElementValidator.java [78:155]
static void validateAttributes(XmlSchemaStateMachineNode state, Attributes attrs,
NamespaceContext nsContext) throws ValidationException {
if ((state == null) || (attrs == null) || (nsContext == null)
|| !state.getNodeType().equals(XmlSchemaStateMachineNode.Type.ELEMENT)) {
throw new ValidationException("None of state, attrs, or nsContext can be null, and state must"
+ " be of an SchemaStateMachineNode.Type.ELEMENT node, not "
+ ((state == null) ? null : state.getNodeType()));
}
final List<XmlSchemaAttrInfo> attributes = state.getAttributes();
if ((attributes == null) || attributes.isEmpty()) {
// Nothing to validate.
return;
}
final QName elemQName = state.getElement().getQName();
for (XmlSchemaAttrInfo attribute : attributes) {
final XmlSchemaAttribute xmlSchemaAttr = attribute.getAttribute();
final QName attrQName = xmlSchemaAttr.getQName();
final XmlSchemaUse use = xmlSchemaAttr.getUse();
String value = attrs.getValue(attrQName.getNamespaceURI(), attrQName.getLocalPart());
if (value == null) {
// A namespace is not always available.
value = attrs.getValue("", attrQName.getLocalPart());
}
if (value != null) {
value = value.trim();
}
// Confirm the attribute is used correctly.
switch (use) {
case OPTIONAL:
break;
case PROHIBITED:
if ((value != null) && (value.length() > 0)) {
throw new ValidationException("Attribute " + attrQName + " was declared 'prohibited' by "
+ elemQName + " and cannot have a value.");
}
break;
case REQUIRED:
if ((value == null) || (value.length() == 0)) {
throw new ValidationException("Attribute " + attrQName + " was declared 'required' by "
+ elemQName + " and must have a value.");
}
break;
case NONE:
/*
* An attribute with no usage is optional, which was already
* taken care of by XmlSchemaWalker.
*/
default:
throw new ValidationException("Attribute " + attrQName + " of element " + elemQName
+ " has an unrecognized usage of " + use + ".");
}
/*
* If the value is null or empty there is no further validation we
* can perform here.
*/
if ((value == null) || (value.length() == 0)) {
continue;
}
if (attribute.getType().getType().equals(XmlSchemaTypeInfo.Type.COMPLEX)) {
throw new ValidationException("Attribute " + attrQName + " of element " + elemQName
+ " cannot have a COMPLEX type.");
}
validateType("Attribute " + attrQName + " of " + elemQName, value, attribute.getType(), nsContext);
}
}