public boolean evaluate(QName element, XMLAttributes attributes) throws Exception {
int type1 = child1.getType();
int type2 = child2.getType();
Object obj1, obj2;
XSSimpleTypeDecl simpleType;
if (type1 == TYPE_UNTYPED && type2 == TYPE_DOUBLE) {
// attribute and numeral
String attrValue = child1.getValue(attributes).toString();
simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType("double");
//cast the attribute value into double as per the XPath 2.0 general comparison rules
obj1 = simpleType.validate(attrValue, null, null);
obj2 = child2.getValue(attributes);
return DataMatcher.compareActualValues(obj1, obj2, comp, simpleType);
} else if (type1 == TYPE_UNTYPED && type2 == TYPE_STRING) {
// attribute and string
String attrValue = child1.getValue(attributes).toString();
simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType("string");
//cast the attribute value into string as per the XPath 2.0 general comparison rules
obj1 = simpleType.validate(attrValue, null, null);
obj2 = child2.getValue(attributes);
return DataMatcher.compareActualValues(obj1, obj2, comp, simpleType);
} else if (type1 == TYPE_DOUBLE && type2 == TYPE_UNTYPED) {
// numeral and attribute
String attrValue = child2.getValue(attributes).toString();
simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType("double");
obj1 = child1.getValue(attributes);
//cast the attribute value into double as per the XPath 2.0 general comparison rules
obj2 = simpleType.validate(attrValue, null, null);
return DataMatcher.compareActualValues(obj1, obj2, comp, simpleType);
} else if (type1 == TYPE_STRING && type2 == TYPE_UNTYPED) {
// string and attribute
String attrValue = child2.getValue(attributes).toString();
simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType("string");
obj1 = child1.getValue(attributes);
//cast the attribute value into string as per the XPath 2.0 general comparison rules
obj2 = simpleType.validate(attrValue, null, null);
return DataMatcher.compareActualValues(obj1, obj2, comp, simpleType);
} else if (type1 == TYPE_UNTYPED && type2 == TYPE_UNTYPED) {
// attr and attr
String attrVal1 = child1.getValue(attributes).toString();
String attrVal2 = child2.getValue(attributes).toString();
simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType("string");
//cast the both attribute values into string as per the XPath 2.0 general comparison rules
obj1 = simpleType.validate(attrVal1, null, null);
obj2 = simpleType.validate(attrVal2, null, null);
return DataMatcher.compareActualValues(obj1, obj2, comp, simpleType);
} else if (type1 == TYPE_UNTYPED && type2 == TYPE_OTHER) {
// attr and cast expr
String type = child2.getTypeName();
String attrVal = child1.getValue(attributes).toString();
simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType(type);
if (simpleType == null) {
throw new XPathException("Casted type is not a built-in type");
}
//try to cast the attribute value into the type of the cast expression
obj1 = simpleType.validate(attrVal, null, null);
obj2 = child2.getValue(attributes);
return DataMatcher.compareActualValues(obj1, obj2, comp, simpleType);
} else if (type1 == TYPE_OTHER && type2 == TYPE_UNTYPED) {
// cast expr and attr
String type = child1.getTypeName();
String attrVal = child2.getValue(attributes).toString();
simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType(type);
if (simpleType == null) {
throw new XPathException("Casted type is not a built-in type");
}
obj1 = child1.getValue(attributes);
//try to cast the attribute value into the type of the cast expression
obj2 = simpleType.validate(attrVal, null, null);
return DataMatcher.compareActualValues(obj1, obj2, comp, simpleType);
} else if (type1 == TYPE_OTHER && type2 == TYPE_OTHER) {
//cast expr and cast expr
String typeName1 = child1.getTypeName();
String typeName2 = child2.getTypeName();
simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType(typeName1);
if (simpleType == null) {
throw new XPathException("Casted type is not a built-in type");
}
short dt1 = simpleType.getBuiltInKind();
simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType(typeName2);
if (simpleType == null) {
throw new XPathException("Casted type is not a built-in type");
}
short dt2 = simpleType.getBuiltInKind();
// check whether the two types are comparable
if (DataMatcher.isComparable(dt1, dt2, null, null)) {
obj1 = simpleType.validate(child1.getValue(attributes), null, null);
obj2 = child2.getValue(attributes);
return DataMatcher.compareActualValues(obj1, obj2, comp, simpleType);
} else {
throw new XPathException("Invalid comparison between incompatible types");
}