Java源码示例:org.eclipse.rdf4j.model.datatypes.XMLDatatypeUtil

示例1
/**
 * This method parses a number as matched by {@link #numberPattern} into a {@link Value}.
 *
 * @param valueString The string to be parsed into a number
 * @return The parsed value
 */
protected Value parseNumberPatternMatch(String valueString) {
	IRI dataType = null;

	if (XMLDatatypeUtil.isValidInteger(valueString)) {
		if (XMLDatatypeUtil.isValidNegativeInteger(valueString)) {
			dataType = XMLSchema.NEGATIVE_INTEGER;
		} else {
			dataType = XMLSchema.INTEGER;
		}
	} else if (XMLDatatypeUtil.isValidDecimal(valueString)) {
		dataType = XMLSchema.DECIMAL;
	} else if (XMLDatatypeUtil.isValidDouble(valueString)) {
		dataType = XMLSchema.DOUBLE;
	}

	return dataType != null ? valueFactory.createLiteral(valueString, dataType)
			: valueFactory.createLiteral(valueString);
}
 
示例2
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException(
				getXsdName() + " cast requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];
		IRI datatype = literal.getDatatype();

		if (QueryEvaluationUtil.isStringLiteral(literal)) {
			String lexicalValue = XMLDatatypeUtil.collapseWhiteSpace(literal.getLabel());
			if (isValidForDatatype(lexicalValue)) {
				return valueFactory.createLiteral(lexicalValue, getXsdDatatype());
			}
		} else if (datatype != null) {
			if (datatype.equals(getXsdDatatype())) {
				return literal;
			}
		}
		return convert(valueFactory, literal);
	} else {
		return convert(valueFactory, args[0]);
	}
}
 
示例3
/**
 * Compares two literal datatypes and indicates if one should be ordered after the other. This algorithm ensures
 * that compatible ordered datatypes (numeric and date/time) are grouped together so that
 * {@link QueryEvaluationUtil#compareLiterals(Literal, Literal, CompareOp)} is used in consecutive ordering steps.
 */
private int compareDatatypes(IRI leftDatatype, IRI rightDatatype) {
	if (XMLDatatypeUtil.isNumericDatatype(leftDatatype)) {
		if (XMLDatatypeUtil.isNumericDatatype(rightDatatype)) {
			// both are numeric datatypes
			return compareURIs(leftDatatype, rightDatatype);
		} else {
			return -1;
		}
	} else if (XMLDatatypeUtil.isNumericDatatype(rightDatatype)) {
		return 1;
	} else if (XMLDatatypeUtil.isCalendarDatatype(leftDatatype)) {
		if (XMLDatatypeUtil.isCalendarDatatype(rightDatatype)) {
			// both are calendar datatypes
			return compareURIs(leftDatatype, rightDatatype);
		} else {
			return -1;
		}
	} else if (XMLDatatypeUtil.isCalendarDatatype(rightDatatype)) {
		return 1;
	} else {
		// incompatible or unordered datatypes
		return compareURIs(leftDatatype, rightDatatype);
	}
}
 
示例4
/**
 * Computes the result of applying the supplied math operator on the supplied left and right operand.
 *
 * @param leftLit  a datatype literal
 * @param rightLit a datatype literal
 * @param op       a mathematical operator, as definied by MathExpr.MathOp.
 * @return a datatype literal
 */
public static Literal compute(Literal leftLit, Literal rightLit, MathOp op) throws ValueExprEvaluationException {
	IRI leftDatatype = leftLit.getDatatype();
	IRI rightDatatype = rightLit.getDatatype();

	if (XMLDatatypeUtil.isNumericDatatype(leftDatatype) && XMLDatatypeUtil.isNumericDatatype(rightDatatype)) {
		return MathUtil.compute(leftLit, rightLit, op);
	} else if (XMLDatatypeUtil.isDurationDatatype(leftDatatype)
			&& XMLDatatypeUtil.isDurationDatatype(rightDatatype)) {
		return operationsBetweenDurations(leftLit, rightLit, op);
	} else if (XMLDatatypeUtil.isDecimalDatatype(leftDatatype)
			&& XMLDatatypeUtil.isDurationDatatype(rightDatatype)) {
		return operationsBetweenDurationAndDecimal(rightLit, leftLit, op);
	} else if (XMLDatatypeUtil.isDurationDatatype(leftDatatype)
			&& XMLDatatypeUtil.isDecimalDatatype(rightDatatype)) {
		return operationsBetweenDurationAndDecimal(leftLit, rightLit, op);
	} else if (XMLDatatypeUtil.isCalendarDatatype(leftDatatype)
			&& XMLDatatypeUtil.isDurationDatatype(rightDatatype)) {
		return operationsBetweenCalendarAndDuration(leftLit, rightLit, op);
	} else if (XMLDatatypeUtil.isDurationDatatype(leftDatatype)
			&& XMLDatatypeUtil.isCalendarDatatype(rightDatatype)) {
		return operationsBetweenDurationAndCalendar(leftLit, rightLit, op);
	} else {
		throw new ValueExprEvaluationException("Mathematical operators are not supported on these operands");
	}
}
 
示例5
private static Literal operationsBetweenDurations(Literal leftLit, Literal rightLit, MathOp op) {
	Duration left = XMLDatatypeUtil.parseDuration(leftLit.getLabel());
	Duration right = XMLDatatypeUtil.parseDuration(rightLit.getLabel());
	try {
		switch (op) {
		case PLUS:
			// op:add-yearMonthDurations and op:add-dayTimeDurations
			return buildLiteral(left.add(right));
		case MINUS:
			// op:subtract-yearMonthDurations and op:subtract-dayTimeDurations
			return buildLiteral(left.subtract(right));
		case MULTIPLY:
			throw new ValueExprEvaluationException("Multiplication is not defined on xsd:duration.");
		case DIVIDE:
			throw new ValueExprEvaluationException("Division is not defined on xsd:duration.");
		default:
			throw new IllegalArgumentException("Unknown operator: " + op);
		}
	} catch (IllegalStateException e) {
		throw new ValueExprEvaluationException(e);
	}
}
 
示例6
private static Literal operationsBetweenDurationAndCalendar(Literal durationLit, Literal calendarLit, MathOp op) {
	Duration duration = XMLDatatypeUtil.parseDuration(durationLit.getLabel());
	XMLGregorianCalendar calendar = (XMLGregorianCalendar) calendarLit.calendarValue().clone();

	try {
		if (op == MathOp.PLUS) {
			// op:add-yearMonthDuration-to-dateTime and op:add-dayTimeDuration-to-dateTime and
			// op:add-yearMonthDuration-to-date and op:add-dayTimeDuration-to-date and
			// op:add-dayTimeDuration-to-time
			calendar.add(duration);
			return SimpleValueFactory.getInstance().createLiteral(calendar);
		} else {
			throw new ValueExprEvaluationException(
					"Only addition is defined between xsd:duration and calendar datatypes.");
		}
	} catch (IllegalStateException e) {
		throw new ValueExprEvaluationException(e);
	}
}
 
示例7
/**
 * Compares two literal datatypes and indicates if one should be ordered after the other. This algorithm ensures
 * that compatible ordered datatypes (numeric and date/time) are grouped together so that
 * {@link QueryEvaluationUtil#compareLiterals(Literal, Literal, CompareOp)} is used in consecutive ordering steps.
 */
private int compareDatatypes(IRI leftDatatype, IRI rightDatatype) {
	if (XMLDatatypeUtil.isNumericDatatype(leftDatatype)) {
		if (XMLDatatypeUtil.isNumericDatatype(rightDatatype)) {
			// both are numeric datatypes
			return compareURIs(leftDatatype, rightDatatype);
		} else {
			return -1;
		}
	} else if (XMLDatatypeUtil.isNumericDatatype(rightDatatype)) {
		return 1;
	} else if (XMLDatatypeUtil.isCalendarDatatype(leftDatatype)) {
		if (XMLDatatypeUtil.isCalendarDatatype(rightDatatype)) {
			// both are calendar datatypes
			return compareURIs(leftDatatype, rightDatatype);
		} else {
			return -1;
		}
	} else if (XMLDatatypeUtil.isCalendarDatatype(rightDatatype)) {
		return 1;
	} else {
		// incompatible or unordered datatypes
		return compareURIs(leftDatatype, rightDatatype);
	}
}
 
示例8
@Override
public Literal normalizeDatatype(String literalValue, IRI datatypeUri, ValueFactory valueFactory)
		throws LiteralUtilException {
	if (isRecognizedDatatype(datatypeUri)) {
		if (literalValue == null) {
			throw new NullPointerException("Literal value cannot be null");
		}

		try {
			return valueFactory.createLiteral(XMLDatatypeUtil.normalize(literalValue, datatypeUri), datatypeUri);
		} catch (IllegalArgumentException e) {
			throw new LiteralUtilException("Could not normalise XMLSchema literal", e);
		}
	}

	throw new LiteralUtilException("Could not normalise XMLSchema literal");
}
 
示例9
public void processAggregate(BindingSet s) throws QueryEvaluationException {
    if(this.typeError == null) {
        Value v = this.evaluate(s);
        if(this.distinctValue(v)) {
            if(v instanceof Literal) {
                Literal nextLiteral = (Literal)v;
                if(nextLiteral.getDatatype() != null && XMLDatatypeUtil.isNumericDatatype(nextLiteral.getDatatype())) {
                    this.sum = MathUtil.compute(this.sum, nextLiteral, MathExpr.MathOp.PLUS);
                } else {
                    this.typeError = new ValueExprEvaluationException("not a number: " + v);
                }

                ++this.count;
            } else if(v != null) {
                this.typeError = new ValueExprEvaluationException("not a number: " + v);
            }
        }

    }
}
 
示例10
public void processAggregate(BindingSet s) throws QueryEvaluationException {
    if(this.typeError == null) {
        Value v = this.evaluate(s);
        if(this.distinctValue(v)) {
            if(v instanceof Literal) {
                Literal nextLiteral = (Literal)v;
                if(nextLiteral.getDatatype() != null && XMLDatatypeUtil.isNumericDatatype(nextLiteral.getDatatype())) {
                    this.sum = MathUtil.compute(this.sum, nextLiteral, MathExpr.MathOp.PLUS);
                } else {
                    this.typeError = new ValueExprEvaluationException("not a number: " + v);
                }
            } else if(v != null) {
                this.typeError = new ValueExprEvaluationException("not a number: " + v);
            }
        }

    }
}
 
示例11
private void readObject(java.io.ObjectInputStream in) throws IOException {
	try {
		in.defaultReadObject();
		calendar = XMLDatatypeUtil.parseCalendar(this.getLabel());
	} catch (ClassNotFoundException e) {
		throw new IOException(e.getMessage());
	}
}
 
示例12
public LiteralComparatorFilter(PlanNode parent, Literal compareTo, Function<Integer, Boolean> function) {
	super(parent);
	this.function = function;
	this.compareTo = compareTo;
	IRI datatype = compareTo.getDatatype();
	numericDatatype = XMLDatatypeUtil.isNumericDatatype(datatype);
	calendarDatatype = XMLDatatypeUtil.isCalendarDatatype(datatype);
	durationDatatype = XMLDatatypeUtil.isDurationDatatype(datatype);
	booleanDatatype = XMLSchema.BOOLEAN.equals(datatype);
	timeDatatype = XMLSchema.TIME.equals(datatype);
	dateDatatype = XMLSchema.DATE.equals(datatype);

}
 
示例13
private boolean datatypesMatch(IRI datatype) {
	return (numericDatatype && XMLDatatypeUtil.isNumericDatatype(datatype))
			|| (calendarDatatype && XMLDatatypeUtil.isCalendarDatatype(datatype)
					&& (timeDatatype || !XMLSchema.TIME.equals(datatype)))
			|| (durationDatatype && XMLDatatypeUtil.isDurationDatatype(datatype))
			|| (booleanDatatype && XMLSchema.BOOLEAN.equals(datatype));

}
 
示例14
public static int intFromLiteral(Literal literal) throws ValueExprEvaluationException {

		IRI datatype = literal.getDatatype();

		// function accepts only numeric literals
		if (datatype != null && XMLDatatypeUtil.isNumericDatatype(datatype)) {
			if (XMLDatatypeUtil.isIntegerDatatype(datatype)) {
				return literal.intValue();
			} else {
				throw new ValueExprEvaluationException("unexpected datatype for function operand: " + literal);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + literal);
		}
	}
 
示例15
@Override
protected Literal convert(ValueFactory valueFactory, Value value) throws ValueExprEvaluationException {
	if (value instanceof Literal) {
		Literal literal = (Literal) value;
		IRI datatype = literal.getDatatype();
		Boolean booleanValue = null;
		try {
			if (datatype.equals(XMLSchema.FLOAT)) {
				float floatValue = literal.floatValue();
				booleanValue = floatValue != 0.0f && Float.isNaN(floatValue);
			} else if (datatype.equals(XMLSchema.DOUBLE)) {
				double doubleValue = literal.doubleValue();
				booleanValue = doubleValue != 0.0 && Double.isNaN(doubleValue);
			} else if (datatype.equals(XMLSchema.DECIMAL)) {
				BigDecimal decimalValue = literal.decimalValue();
				booleanValue = !decimalValue.equals(BigDecimal.ZERO);
			} else if (datatype.equals(XMLSchema.INTEGER)) {
				BigInteger integerValue = literal.integerValue();
				booleanValue = !integerValue.equals(BigInteger.ZERO);
			} else if (XMLDatatypeUtil.isIntegerDatatype(datatype)) {
				booleanValue = literal.longValue() != 0L;
			}
		} catch (NumberFormatException e) {
			throw typeError(literal, e);
		}

		if (booleanValue != null) {
			return valueFactory.createLiteral(booleanValue);
		}
	}

	throw typeError(value, null);
}
 
示例16
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException(
				getXsdName() + " cast requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];
		IRI datatype = literal.getDatatype();

		// we override because unlike most other cast functions, xsd:string should not accept a language-tagged
		// string literal.
		if (QueryEvaluationUtil.isSimpleLiteral(literal)) {
			String lexicalValue = XMLDatatypeUtil.collapseWhiteSpace(literal.getLabel());
			if (isValidForDatatype(lexicalValue)) {
				return valueFactory.createLiteral(lexicalValue, getXsdDatatype());
			}
		} else if (datatype != null) {
			if (datatype.equals(getXsdDatatype())) {
				return literal;
			}
		}
		return convert(valueFactory, literal);
	} else {
		return convert(valueFactory, args[0]);
	}
}
 
示例17
@Override
protected Literal convert(ValueFactory valueFactory, Value value) throws ValueExprEvaluationException {
	if (value instanceof IRI) {
		return valueFactory.createLiteral(value.toString(), XMLSchema.STRING);
	} else if (value instanceof Literal) {
		Literal literal = (Literal) value;
		IRI datatype = literal.getDatatype();

		if (QueryEvaluationUtil.isSimpleLiteral(literal)) {
			return valueFactory.createLiteral(literal.getLabel(), XMLSchema.STRING);
		} else if (!Literals.isLanguageLiteral(literal)) {
			if (XMLDatatypeUtil.isNumericDatatype(datatype) || datatype.equals(XMLSchema.BOOLEAN)
					|| datatype.equals(XMLSchema.DATETIME) || datatype.equals(XMLSchema.DATETIMESTAMP)) {
				// FIXME Slightly simplified wrt the spec, we just always use the
				// canonical value of the
				// source literal as the target lexical value. This is not 100%
				// compliant with handling of
				// some date-related datatypes.
				//
				// See
				// http://www.w3.org/TR/xpath-functions/#casting-from-primitive-to-primitive
				if (XMLDatatypeUtil.isValidValue(literal.getLabel(), datatype)) {
					String normalizedValue = XMLDatatypeUtil.normalize(literal.getLabel(), datatype);
					return valueFactory.createLiteral(normalizedValue, XMLSchema.STRING);
				} else {
					return valueFactory.createLiteral(literal.getLabel(), XMLSchema.STRING);
				}
			} else {
				// for unknown datatypes, just use the lexical value.
				return valueFactory.createLiteral(literal.getLabel(), XMLSchema.STRING);
			}
		}
	}

	throw typeError(value, null);
}
 
示例18
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("MINUTES requires 1 argument, got " + args.length);
	}

	Value argValue = args[0];
	if (argValue instanceof Literal) {
		Literal literal = (Literal) argValue;

		IRI datatype = literal.getDatatype();

		if (datatype != null && XMLDatatypeUtil.isCalendarDatatype(datatype)) {
			try {
				XMLGregorianCalendar calValue = literal.calendarValue();

				int minutes = calValue.getMinute();
				if (DatatypeConstants.FIELD_UNDEFINED != minutes) {
					return valueFactory.createLiteral(String.valueOf(minutes), XMLSchema.INTEGER);
				} else {
					throw new ValueExprEvaluationException("can not determine minutes from value: " + argValue);
				}
			} catch (IllegalArgumentException e) {
				throw new ValueExprEvaluationException("illegal calendar value: " + argValue);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + argValue);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}
}
 
示例19
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("TZ requires 1 argument, got " + args.length);
	}

	Value argValue = args[0];
	if (argValue instanceof Literal) {
		Literal literal = (Literal) argValue;

		IRI datatype = literal.getDatatype();

		if (datatype != null && XMLDatatypeUtil.isCalendarDatatype(datatype)) {
			String lexValue = literal.getLabel();

			Pattern pattern = Pattern.compile("Z|[+-]\\d\\d:\\d\\d");
			Matcher m = pattern.matcher(lexValue);

			String timeZone = "";
			if (m.find()) {
				timeZone = m.group();
			}

			return valueFactory.createLiteral(timeZone);
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + argValue);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}
}
 
示例20
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("SECONDS requires 1 argument, got " + args.length);
	}

	Value argValue = args[0];
	if (argValue instanceof Literal) {
		Literal literal = (Literal) argValue;

		IRI datatype = literal.getDatatype();

		if (datatype != null && XMLDatatypeUtil.isCalendarDatatype(datatype)) {
			try {
				XMLGregorianCalendar calValue = literal.calendarValue();

				int seconds = calValue.getSecond();
				if (DatatypeConstants.FIELD_UNDEFINED != seconds) {
					BigDecimal fraction = calValue.getFractionalSecond();
					String str = (fraction == null) ? String.valueOf(seconds)
							: String.valueOf(fraction.doubleValue() + seconds);

					return valueFactory.createLiteral(str, XMLSchema.DECIMAL);
				} else {
					throw new ValueExprEvaluationException("can not determine minutes from value: " + argValue);
				}
			} catch (IllegalArgumentException e) {
				throw new ValueExprEvaluationException("illegal calendar value: " + argValue);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + argValue);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}
}
 
示例21
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("MONTH requires 1 argument, got " + args.length);
	}

	Value argValue = args[0];
	if (argValue instanceof Literal) {
		Literal literal = (Literal) argValue;

		IRI datatype = literal.getDatatype();

		if (datatype != null && XMLDatatypeUtil.isCalendarDatatype(datatype)) {
			try {
				XMLGregorianCalendar calValue = literal.calendarValue();

				int month = calValue.getMonth();
				if (DatatypeConstants.FIELD_UNDEFINED != month) {
					return valueFactory.createLiteral(String.valueOf(month), XMLSchema.INTEGER);
				} else {
					throw new ValueExprEvaluationException("can not determine month from value: " + argValue);
				}
			} catch (IllegalArgumentException e) {
				throw new ValueExprEvaluationException("illegal calendar value: " + argValue);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + argValue);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}
}
 
示例22
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("DAY requires 1 argument, got " + args.length);
	}

	Value argValue = args[0];
	if (argValue instanceof Literal) {
		Literal literal = (Literal) argValue;

		IRI datatype = literal.getDatatype();

		if (datatype != null && XMLDatatypeUtil.isCalendarDatatype(datatype)) {
			try {
				XMLGregorianCalendar calValue = literal.calendarValue();

				int day = calValue.getDay();
				if (DatatypeConstants.FIELD_UNDEFINED != day) {
					return valueFactory.createLiteral(String.valueOf(day), XMLSchema.INTEGER);
				} else {
					throw new ValueExprEvaluationException("can not determine day from value: " + argValue);
				}
			} catch (IllegalArgumentException e) {
				throw new ValueExprEvaluationException("illegal calendar value: " + argValue);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + argValue);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}
}
 
示例23
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("YEAR requires 1 argument, got " + args.length);
	}

	Value argValue = args[0];
	if (argValue instanceof Literal) {
		Literal literal = (Literal) argValue;

		IRI datatype = literal.getDatatype();

		if (datatype != null && XMLDatatypeUtil.isCalendarDatatype(datatype)) {
			try {
				XMLGregorianCalendar calValue = literal.calendarValue();
				int year = calValue.getYear();
				if (DatatypeConstants.FIELD_UNDEFINED != year) {
					return valueFactory.createLiteral(String.valueOf(year), XMLSchema.INTEGER);
				} else {
					throw new ValueExprEvaluationException("can not determine year from value: " + argValue);
				}
			} catch (IllegalArgumentException e) {
				throw new ValueExprEvaluationException("illegal calendar value: " + argValue);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + argValue);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}
}
 
示例24
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("HOURS requires 1 argument, got " + args.length);
	}

	Value argValue = args[0];
	if (argValue instanceof Literal) {
		Literal literal = (Literal) argValue;

		IRI datatype = literal.getDatatype();

		if (datatype != null && XMLDatatypeUtil.isCalendarDatatype(datatype)) {
			try {
				XMLGregorianCalendar calValue = literal.calendarValue();

				int hours = calValue.getHour();
				if (DatatypeConstants.FIELD_UNDEFINED != hours) {
					return valueFactory.createLiteral(String.valueOf(hours), XMLSchema.INTEGER);
				} else {
					throw new ValueExprEvaluationException("can not determine hours from value: " + argValue);
				}
			} catch (IllegalArgumentException e) {
				throw new ValueExprEvaluationException("illegal calendar value: " + argValue);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + argValue);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}
}
 
示例25
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("ROUND requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];

		IRI datatype = literal.getDatatype();

		// function accepts only numeric literals
		if (datatype != null && XMLDatatypeUtil.isNumericDatatype(datatype)) {
			if (XMLDatatypeUtil.isIntegerDatatype(datatype)) {
				return literal;
			} else if (XMLDatatypeUtil.isDecimalDatatype(datatype)) {
				BigDecimal rounded = literal.decimalValue().setScale(0, RoundingMode.HALF_UP);
				return valueFactory.createLiteral(rounded.toPlainString(), datatype);
			} else if (XMLDatatypeUtil.isFloatingPointDatatype(datatype)) {
				double ceilingValue = Math.round(literal.doubleValue());
				return valueFactory.createLiteral(Double.toString(ceilingValue), datatype);
			} else {
				throw new ValueExprEvaluationException("unexpected datatype for function operand: " + args[0]);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}

}
 
示例26
private static Literal operationsBetweenDurationAndDecimal(Literal durationLit, Literal decimalLit, MathOp op) {
	Duration duration = XMLDatatypeUtil.parseDuration(durationLit.getLabel());

	try {
		if (op == MathOp.MULTIPLY) {
			// op:multiply-dayTimeDuration and op:multiply-yearMonthDuration
			return buildLiteral(duration.multiply(decimalLit.decimalValue()));
		} else {
			throw new ValueExprEvaluationException(
					"Only multiplication is defined between xsd:decimal and xsd:duration.");
		}
	} catch (IllegalStateException e) {
		throw new ValueExprEvaluationException(e);
	}
}
 
示例27
private static Literal operationsBetweenCalendarAndDuration(Literal calendarLit, Literal durationLit, MathOp op) {
	XMLGregorianCalendar calendar = (XMLGregorianCalendar) calendarLit.calendarValue().clone();
	Duration duration = XMLDatatypeUtil.parseDuration(durationLit.getLabel());

	try {
		switch (op) {
		case PLUS:
			// op:add-yearMonthDuration-to-dateTime and op:add-dayTimeDuration-to-dateTime and
			// op:add-yearMonthDuration-to-date and op:add-dayTimeDuration-to-date and
			// op:add-dayTimeDuration-to-time
			calendar.add(duration);
			return SimpleValueFactory.getInstance().createLiteral(calendar);
		case MINUS:
			// op:subtract-yearMonthDuration-from-dateTime and op:subtract-dayTimeDuration-from-dateTime and
			// op:subtract-yearMonthDuration-from-date and op:subtract-dayTimeDuration-from-date and
			// op:subtract-dayTimeDuration-from-time
			calendar.add(duration.negate());
			return SimpleValueFactory.getInstance().createLiteral(calendar);
		case MULTIPLY:
			throw new ValueExprEvaluationException(
					"Multiplication is not defined between xsd:duration and calendar values.");
		case DIVIDE:
			throw new ValueExprEvaluationException(
					"Division is not defined between xsd:duration and calendar values.");
		default:
			throw new IllegalArgumentException("Unknown operator: " + op);
		}
	} catch (IllegalStateException e) {
		throw new ValueExprEvaluationException(e);
	}
}
 
示例28
/**
 * Determines whether the operand (a variable) contains a numeric datatyped literal, i.e. a literal with datatype
 * xsd:float, xsd:double, xsd:decimal, or a derived datatype of xsd:decimal.
 *
 * @return <tt>true</tt> if the operand contains a numeric datatyped literal, <tt>false</tt> otherwise.
 */
public Value evaluate(IsNumeric node, BindingSet bindings)
		throws QueryEvaluationException {
	Value argValue = evaluate(node.getArg(), bindings);

	if (argValue instanceof Literal) {
		Literal lit = (Literal) argValue;
		IRI datatype = lit.getDatatype();

		return BooleanLiteral.valueOf(XMLDatatypeUtil.isNumericDatatype(datatype));
	} else {
		return BooleanLiteral.FALSE;
	}

}
 
示例29
@Test
public void testCastDateTimeLiteral() {
	String lexVal = "2000-01-01T00:00:00";
	Literal dtLit = f.createLiteral(XMLDatatypeUtil.parseCalendar(lexVal));
	try {
		Literal result = stringCast.evaluate(f, dtLit);
		assertNotNull(result);
		assertEquals(XMLSchema.STRING, result.getDatatype());
		assertFalse(result.getLanguage().isPresent());
		assertEquals(lexVal, result.getLabel());
	} catch (ValueExprEvaluationException e) {
		fail(e.getMessage());
	}
}
 
示例30
@Test
public void testCastDateTimeLiteral() {
	String lexVal = "2000-01-01T00:00:00";
	Literal dtLit = f.createLiteral(XMLDatatypeUtil.parseCalendar(lexVal));
	try {
		Literal result = dtCast.evaluate(f, dtLit);
		assertNotNull(result);
		assertEquals(XMLSchema.DATETIME, result.getDatatype());
		assertFalse(result.getLanguage().isPresent());
		assertEquals(lexVal, result.getLabel());
	} catch (ValueExprEvaluationException e) {
		fail(e.getMessage());
	}
}