forked from eclipse-rdf4j/rdf4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/develop' into develop
- Loading branch information
Showing
16 changed files
with
491 additions
and
118 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
...ation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/function/xsd/DateCast.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Eclipse RDF4J contributors. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Distribution License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*******************************************************************************/ | ||
package org.eclipse.rdf4j.query.algebra.evaluation.function.xsd; | ||
|
||
import static javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED; | ||
|
||
import static org.eclipse.rdf4j.model.datatypes.XMLDatatypeUtil.isValidDate; | ||
import static org.eclipse.rdf4j.model.datatypes.XMLDatatypeUtil.isValidDateTime; | ||
import static org.eclipse.rdf4j.model.vocabulary.XSD.DATE; | ||
import static org.eclipse.rdf4j.model.vocabulary.XSD.DATETIME; | ||
import static org.eclipse.rdf4j.model.vocabulary.XSD.STRING; | ||
|
||
import javax.xml.datatype.XMLGregorianCalendar; | ||
|
||
import org.eclipse.rdf4j.model.IRI; | ||
import org.eclipse.rdf4j.model.Literal; | ||
import org.eclipse.rdf4j.model.Value; | ||
import org.eclipse.rdf4j.model.ValueFactory; | ||
import org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException; | ||
|
||
/** | ||
* A {@link org.eclipse.rdf4j.query.algebra.evaluation.function.Function} that tries to cast its argument to an | ||
* <var>xsd:date</var>. | ||
*/ | ||
public class DateCast extends CastFunction { | ||
|
||
private static final String ZERO = "0"; | ||
|
||
@Override | ||
protected IRI getXsdDatatype() { | ||
return DATE; | ||
} | ||
|
||
@Override | ||
protected boolean isValidForDatatype(String lexicalValue) { | ||
return isValidDate(lexicalValue); | ||
} | ||
|
||
@Override | ||
protected Literal convert(ValueFactory vf, Value value) throws ValueExprEvaluationException { | ||
if (value instanceof Literal) { | ||
Literal literal = (Literal) value; | ||
IRI datatype = literal.getDatatype(); | ||
|
||
if (STRING.equals(datatype) || DATETIME.equals(datatype)) { | ||
try { | ||
XMLGregorianCalendar calValue = literal.calendarValue(); | ||
int year = calValue.getYear(); | ||
int month = calValue.getMonth(); | ||
int day = calValue.getDay(); | ||
int timezoneOffset = calValue.getTimezone(); | ||
|
||
if (FIELD_UNDEFINED != year && FIELD_UNDEFINED != month && FIELD_UNDEFINED != day) { | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append(year).append("-"); | ||
addZeroIfNeeded(month, builder); | ||
builder.append(month).append("-"); | ||
addZeroIfNeeded(day, builder); | ||
builder.append(day); | ||
|
||
if (FIELD_UNDEFINED != timezoneOffset) { | ||
int minutes = Math.abs(timezoneOffset); | ||
int hours = minutes / 60; | ||
minutes = minutes - (hours * 60); | ||
builder.append(timezoneOffset > 0 ? "+" : "-"); | ||
addZeroIfNeeded(hours, builder); | ||
builder.append(hours); | ||
builder.append(":"); | ||
addZeroIfNeeded(minutes, builder); | ||
builder.append(minutes); | ||
} | ||
|
||
return vf.createLiteral(builder.toString(), DATE); | ||
} else { | ||
throw typeError(literal, null); | ||
} | ||
} catch (IllegalArgumentException e) { | ||
throw typeError(literal, e); | ||
} | ||
} | ||
} | ||
throw typeError(value, null); | ||
} | ||
|
||
private static void addZeroIfNeeded(int value, StringBuilder builder) { | ||
if (value < 10) { | ||
builder.append(ZERO); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...n/src/test/java/org/eclipse/rdf4j/query/algebra/evaluation/function/xsd/TestDateCast.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Eclipse RDF4J contributors. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Distribution License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*******************************************************************************/ | ||
package org.eclipse.rdf4j.query.algebra.evaluation.function.xsd; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
import static org.eclipse.rdf4j.model.datatypes.XMLDatatypeUtil.parseCalendar; | ||
import static org.eclipse.rdf4j.model.vocabulary.XSD.DATE; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.fail; | ||
|
||
import org.eclipse.rdf4j.model.Literal; | ||
import org.eclipse.rdf4j.model.ValueFactory; | ||
import org.eclipse.rdf4j.model.impl.SimpleValueFactory; | ||
import org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class TestDateCast { | ||
|
||
private DateCast dateCast; | ||
private final ValueFactory vf = SimpleValueFactory.getInstance(); | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
dateCast = new DateCast(); | ||
} | ||
|
||
@Test | ||
public void testCastPlainLiteral_date() { | ||
testDateCast(vf.createLiteral("1999-09-09"), "1999-09-09"); | ||
} | ||
|
||
@Test | ||
public void testCastPlainLiteral_date_withTimeZone_utc() { | ||
testDateCast(vf.createLiteral("1999-09-09Z"), "1999-09-09Z"); | ||
} | ||
|
||
@Test | ||
public void testCastPlainLiteral_date_withTimeZone_offset() { | ||
testDateCast(vf.createLiteral("1999-09-09-06:00"), "1999-09-09-06:00"); | ||
} | ||
|
||
@Test | ||
public void testCastPlainLiteral_date_invalid() { | ||
// Arrange | ||
Literal plainLit = vf.createLiteral("1999-09-xx"); | ||
|
||
// Act & Assert | ||
assertThatExceptionOfType(ValueExprEvaluationException.class).isThrownBy(() -> dateCast.evaluate(vf, plainLit)); | ||
} | ||
|
||
@Test | ||
public void testCastPlainLiteral_dateTime() { | ||
testDateCast(vf.createLiteral("1999-09-09T14:45:13"), "1999-09-09"); | ||
} | ||
|
||
@Test | ||
public void testCastPlainLiteral_dateTime_withTimeZone_utc() { | ||
testDateCast(vf.createLiteral("1999-09-09T14:45:13Z"), "1999-09-09-00:00"); | ||
} | ||
|
||
@Test | ||
public void testCastPlainLiteral_dateTime_withTimeZone_offset() { | ||
testDateCast(vf.createLiteral("1999-09-09T14:45:13-06:00"), "1999-09-09-06:00"); | ||
} | ||
|
||
@Test | ||
public void testCastPlainLiteral_dateTime_invalid() { | ||
// Arrange | ||
Literal plainLit = vf.createLiteral("1999-09-09T14:45:xx"); | ||
|
||
// Act & Assert | ||
assertThatExceptionOfType(ValueExprEvaluationException.class).isThrownBy(() -> dateCast.evaluate(vf, plainLit)); | ||
} | ||
|
||
@Test | ||
public void testCastDateLiteral() { | ||
testDateCast(vf.createLiteral("2022-11-01Z", DATE), "2022-11-01Z"); | ||
} | ||
|
||
@Test | ||
public void testCastDateTimeLiteral() { | ||
testDateCast(vf.createLiteral(parseCalendar("1999-09-09T14:45:13")), "1999-09-09"); | ||
} | ||
|
||
@Test | ||
public void testCastDateTimeLiteral_withTimeZone_utc() { | ||
testDateCast(vf.createLiteral(parseCalendar("1999-09-09T14:45:13Z")), "1999-09-09-00:00"); | ||
} | ||
|
||
@Test | ||
public void testCastDateTimeLiteral_withTimeZone_offset() { | ||
testDateCast(vf.createLiteral(parseCalendar("1999-09-09T14:45:13+03:00")), "1999-09-09+03:00"); | ||
} | ||
|
||
private void testDateCast(Literal literal, String expected) { | ||
// Arrange | ||
Literal result = null; | ||
|
||
// Act | ||
try { | ||
result = dateCast.evaluate(vf, literal); | ||
} catch (ValueExprEvaluationException e) { | ||
fail(e.getMessage()); | ||
} | ||
|
||
// Assert | ||
assertNotNull(result); | ||
assertThat(result.getLabel()).isEqualTo(expected); | ||
assertThat(result.getDatatype()).isEqualTo(DATE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.