// Note: XMLGregorianCalendar.compare() returns compatible
// values
// (-1, 0, 1) but INDETERMINATE needs special treatment
if (compareResult == DatatypeConstants.INDETERMINATE) {
throw new ValueExprEvaluationException("Indeterminate result for date/time comparison");
}
}
else if (commonDatatype.equals(XMLSchema.STRING)) {
compareResult = leftLit.getLabel().compareTo(rightLit.getLabel());
}
}
catch (IllegalArgumentException e) {
// One of the basic-type method calls failed, try syntactic match
// before throwing an error
if (leftLit.equals(rightLit)) {
switch (operator) {
case EQ:
return true;
case NE:
return false;
}
}
throw new ValueExprEvaluationException(e);
}
}
}
if (compareResult != null) {
// Literals have compatible ordered datatypes
switch (operator) {
case LT:
return compareResult.intValue() < 0;
case LE:
return compareResult.intValue() <= 0;
case EQ:
return compareResult.intValue() == 0;
case NE:
return compareResult.intValue() != 0;
case GE:
return compareResult.intValue() >= 0;
case GT:
return compareResult.intValue() > 0;
default:
throw new IllegalArgumentException("Unknown operator: " + operator);
}
}
else {
// All other cases, e.g. literals with languages, unequal or
// unordered datatypes, etc. These arguments can only be compared
// using the operators 'EQ' and 'NE'. See SPARQL's RDFterm-equal
// operator
boolean literalsEqual = leftLit.equals(rightLit);
if (!literalsEqual) {
if (leftDatatype != null && rightDatatype != null
&& XMLDatatypeUtil.isCalendarDatatype(leftDatatype)
&& XMLDatatypeUtil.isCalendarDatatype(rightDatatype))
{
// left and right arguments have different date/time datatypes,
// these are always unequal
}
else if (leftDatatype != null && rightLit.getLanguage() == null || rightDatatype != null
&& leftLit.getLanguage() == null)
{
// For literals with unsupported datatypes we don't know if their
// values are equal
throw new ValueExprEvaluationException("Unable to compare literals with unsupported types");
}
}
switch (operator) {
case EQ:
return literalsEqual;
case NE:
return !literalsEqual;
case LT:
case LE:
case GE:
case GT:
throw new ValueExprEvaluationException(
"Only literals with compatible, ordered datatypes can be compared using <, <=, > and >= operators");
default:
throw new IllegalArgumentException("Unknown operator: " + operator);
}
}