Java Code Examples for org.eclipse.rdf4j.model.Literal#calendarValue()

The following examples show how to use org.eclipse.rdf4j.model.Literal#calendarValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Minutes.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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]);
	}
}
 
Example 2
Source File: Seconds.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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]);
	}
}
 
Example 3
Source File: Month.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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]);
	}
}
 
Example 4
Source File: Day.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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]);
	}
}
 
Example 5
Source File: Year.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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]);
	}
}
 
Example 6
Source File: Hours.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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]);
	}
}
 
Example 7
Source File: QueryResults.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static boolean bindingSetsMatch(BindingSet bs1, BindingSet bs2, Map<BNode, BNode> bNodeMapping) {

		if (bs1.size() != bs2.size()) {
			return false;
		}

		for (Binding binding1 : bs1) {
			Value value1 = binding1.getValue();
			Value value2 = bs2.getValue(binding1.getName());

			if (value1 instanceof BNode && value2 instanceof BNode) {
				BNode mappedBNode = bNodeMapping.get(value1);

				if (mappedBNode != null) {
					// bNode 'value1' was already mapped to some other bNode
					if (!value2.equals(mappedBNode)) {
						// 'value1' and 'value2' do not match
						return false;
					}
				} else {
					// 'value1' was not yet mapped, we need to check if 'value2' is a
					// possible mapping candidate
					if (bNodeMapping.containsValue(value2)) {
						// 'value2' is already mapped to some other value.
						return false;
					}
				}
			} else {
				// values are not (both) bNodes
				if (value1 instanceof Literal && value2 instanceof Literal) {
					// do literal value-based comparison for supported datatypes
					Literal leftLit = (Literal) value1;
					Literal rightLit = (Literal) value2;

					IRI dt1 = leftLit.getDatatype();
					IRI dt2 = rightLit.getDatatype();

					if (dt1 != null && dt2 != null && dt1.equals(dt2)
							&& XMLDatatypeUtil.isValidValue(leftLit.getLabel(), dt1)
							&& XMLDatatypeUtil.isValidValue(rightLit.getLabel(), dt2)) {
						Integer compareResult = null;
						if (dt1.equals(XMLSchema.DOUBLE)) {
							compareResult = Double.compare(leftLit.doubleValue(), rightLit.doubleValue());
						} else if (dt1.equals(XMLSchema.FLOAT)) {
							compareResult = Float.compare(leftLit.floatValue(), rightLit.floatValue());
						} else if (dt1.equals(XMLSchema.DECIMAL)) {
							compareResult = leftLit.decimalValue().compareTo(rightLit.decimalValue());
						} else if (XMLDatatypeUtil.isIntegerDatatype(dt1)) {
							compareResult = leftLit.integerValue().compareTo(rightLit.integerValue());
						} else if (dt1.equals(XMLSchema.BOOLEAN)) {
							Boolean leftBool = leftLit.booleanValue();
							Boolean rightBool = rightLit.booleanValue();
							compareResult = leftBool.compareTo(rightBool);
						} else if (XMLDatatypeUtil.isCalendarDatatype(dt1)) {
							XMLGregorianCalendar left = leftLit.calendarValue();
							XMLGregorianCalendar right = rightLit.calendarValue();

							compareResult = left.compare(right);
						}

						if (compareResult != null) {
							if (compareResult.intValue() != 0) {
								return false;
							}
						} else if (!value1.equals(value2)) {
							return false;
						}
					} else if (!value1.equals(value2)) {
						return false;
					}
				} else if (!value1.equals(value2)) {
					return false;
				}
			}
		}

		return true;
	}
 
Example 8
Source File: DateTimeCast.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected Literal convert(ValueFactory vf, Value value) throws ValueExprEvaluationException {
	if (value instanceof Literal) {
		Literal literal = (Literal) value;
		IRI datatype = literal.getDatatype();

		if (datatype.equals(XMLSchema.DATE)) {
			// If ST is xs:date, then let SYR be eg:convertYearToString(
			// fn:year-from-date( SV )), let SMO be eg:convertTo2CharString(
			// fn:month-from-date( SV )), let SDA be eg:convertTo2CharString(
			// fn:day-from-date( SV )) and let STZ be eg:convertTZtoString(
			// fn:timezone-from-date( SV )); TV is xs:dateTime( fn:concat(
			// SYR , '-', SMO , '-', SDA , 'T00:00:00 ', STZ ) ).
			try {
				XMLGregorianCalendar calValue = literal.calendarValue();

				int year = calValue.getYear();
				int month = calValue.getMonth();
				int day = calValue.getDay();
				int timezoneOffset = calValue.getTimezone();

				if (DatatypeConstants.FIELD_UNDEFINED != year && DatatypeConstants.FIELD_UNDEFINED != month
						&& DatatypeConstants.FIELD_UNDEFINED != day) {
					StringBuilder dtBuilder = new StringBuilder();
					dtBuilder.append(year);
					dtBuilder.append("-");
					if (month < 10) {
						dtBuilder.append("0");
					}
					dtBuilder.append(month);
					dtBuilder.append("-");
					if (day < 10) {
						dtBuilder.append("0");
					}
					dtBuilder.append(day);
					dtBuilder.append("T00:00:00");
					if (DatatypeConstants.FIELD_UNDEFINED != timezoneOffset) {
						int minutes = Math.abs(timezoneOffset);
						int hours = minutes / 60;
						minutes = minutes - (hours * 60);
						if (timezoneOffset > 0) {
							dtBuilder.append("+");
						} else {
							dtBuilder.append("-");
						}
						if (hours < 10) {
							dtBuilder.append("0");
						}
						dtBuilder.append(hours);
						dtBuilder.append(":");
						if (minutes < 10) {
							dtBuilder.append("0");
						}
						dtBuilder.append(minutes);
					}

					return vf.createLiteral(dtBuilder.toString(), XMLSchema.DATETIME);
				} else {
					throw typeError(literal, null);
				}
			} catch (IllegalArgumentException e) {
				throw typeError(literal, e);
			}
		}
	}
	throw typeError(value, null);
}
 
Example 9
Source File: Timezone.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("TIMEZONE 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 timezoneOffset = calValue.getTimezone();

				if (DatatypeConstants.FIELD_UNDEFINED != timezoneOffset) {
					// TODO creating xsd:dayTimeDuration lexical representation
					// manually. Surely there is a better way to do this?
					int minutes = Math.abs(timezoneOffset);
					int hours = minutes / 60;
					minutes = minutes - (hours * 60);

					StringBuilder tzDuration = new StringBuilder();
					if (timezoneOffset < 0) {
						tzDuration.append("-");
					}
					tzDuration.append("PT");
					if (hours > 0) {
						tzDuration.append(hours + "H");
					}
					if (minutes > 0) {
						tzDuration.append(minutes + "M");
					}
					if (timezoneOffset == 0) {
						tzDuration.append("0S");
					}
					return valueFactory.createLiteral(tzDuration.toString(), XMLSchema.DAYTIMEDURATION);
				} else {
					throw new ValueExprEvaluationException("can not determine timezone 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]);
	}
}
 
Example 10
Source File: Literals.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Gets the calendar value of the supplied literal. The fallback value is returned in case
 * {@link Literal#calendarValue()} throws a {@link NumberFormatException}.
 *
 * @param l        The literal to get the calendar value for.
 * @param fallback The value to fall back to in case no calendar value could gotten from the literal.
 * @return Either the literal's calendar value, or the fallback value.
 */
public static XMLGregorianCalendar getCalendarValue(Literal l, XMLGregorianCalendar fallback) {
	try {
		return l.calendarValue();
	} catch (IllegalArgumentException e) {
		return fallback;
	}
}