This is an automated email from the ASF dual-hosted git repository. jsorel pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
The following commit(s) were added to refs/heads/geoapi-4.0 by this push: new e9bb6fad89 CQL : support date, period and duration literals e9bb6fad89 is described below commit e9bb6fad89c5dd142626621980eca1231ff6c1aa Author: jsorel <johann.so...@geomatys.com> AuthorDate: Mon May 9 15:07:11 2022 +0200 CQL : support date, period and duration literals --- .../main/antlr4/org/apache/sis/internal/cql/CQL.g4 | 2 +- .../src/main/java/org/apache/sis/cql/CQL.java | 6 +++++ .../org/apache/sis/cql/ExpressionReadingTest.java | 29 ++++++++++++++-------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/core/sis-cql/src/main/antlr4/org/apache/sis/internal/cql/CQL.g4 b/core/sis-cql/src/main/antlr4/org/apache/sis/internal/cql/CQL.g4 index 62fd82f74f..c2e3fcf3b5 100644 --- a/core/sis-cql/src/main/antlr4/org/apache/sis/internal/cql/CQL.g4 +++ b/core/sis-cql/src/main/antlr4/org/apache/sis/internal/cql/CQL.g4 @@ -136,7 +136,7 @@ WITHIN : W I T H I N ; DATE : DIGIT DIGIT DIGIT DIGIT '-' DIGIT DIGIT '-' DIGIT DIGIT 'T' DIGIT DIGIT ':' DIGIT DIGIT ':' DIGIT DIGIT ('.' DIGIT+)? 'Z'; DURATION_P : P (INT 'Y')? (INT 'M')? (INT 'D')? (INT 'H')? (INT 'M')? (INT 'S')?; -DURATION_T : T (INT 'H')? (INT 'M')? (INT 'S')?; +DURATION_T : P T (INT 'H')? (INT 'M')? (INT 'S')?; AFTER : A F T E R ; ANYINTERACTS : A N Y I N T E R A C T S ; diff --git a/core/sis-cql/src/main/java/org/apache/sis/cql/CQL.java b/core/sis-cql/src/main/java/org/apache/sis/cql/CQL.java index 3c8a1fe02f..83cb251721 100644 --- a/core/sis-cql/src/main/java/org/apache/sis/cql/CQL.java +++ b/core/sis-cql/src/main/java/org/apache/sis/cql/CQL.java @@ -16,6 +16,8 @@ */ package org.apache.sis.cql; +import java.time.Duration; +import java.time.Period; import java.util.List; import java.util.ArrayList; import java.time.temporal.TemporalAccessor; @@ -296,6 +298,10 @@ public final class CQL { text = text.replaceAll("\\\\'", "'"); return ff.literal(text.substring(1, text.length() - 1)); } + case DURATION_P : + return ff.literal(Period.parse(tree.getText())); + case DURATION_T : + return ff.literal(Duration.parse(tree.getText())); } } else if (tree instanceof ExpressionGeometryContext) { //: POINT ( EMPTY | coordinateSerie ) diff --git a/core/sis-cql/src/test/java/org/apache/sis/cql/ExpressionReadingTest.java b/core/sis-cql/src/test/java/org/apache/sis/cql/ExpressionReadingTest.java index 6d3928f2a6..51012f513b 100644 --- a/core/sis-cql/src/test/java/org/apache/sis/cql/ExpressionReadingTest.java +++ b/core/sis-cql/src/test/java/org/apache/sis/cql/ExpressionReadingTest.java @@ -18,6 +18,11 @@ package org.apache.sis.cql; import java.time.Instant; import java.text.ParseException; +import java.time.Duration; +import java.time.Period; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAccessor; +import java.time.temporal.TemporalUnit; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.GeometryCollection; @@ -147,36 +152,40 @@ public final strictfp class ExpressionReadingTest extends CQLTestCase { } @Test - @Ignore("Incomplete use if java.time in CQL parser.") public void testDate() throws CQLException, ParseException{ //dates are expected to be formated in ISO 8601 : yyyy-MM-dd'T'HH:mm:ss'Z' final String cql = "2012-03-21T05:42:36Z"; final Object obj = CQL.parseExpression(cql); assertTrue(obj instanceof Literal); final Literal expression = (Literal) obj; - assertEquals(Instant.parse("2012-03-21T05:42:36Z"), expression.getValue()); + Object value = expression.getValue(); + assertTrue(value instanceof TemporalAccessor); + TemporalAccessor acc = (TemporalAccessor) value; + assertEquals(Instant.parse("2012-03-21T05:42:36Z"), Instant.from(acc)); } @Test - @Ignore("Unreconized expression.") public void testDuration() throws CQLException, ParseException{ - final String cql = "P7Y6M5D4H3M2S"; + final String cql = "P7Y6M5D"; final Object obj = CQL.parseExpression(cql); assertTrue(obj instanceof Literal); final Literal expression = (Literal) obj; - final long duration = (Long) expression.getValue(); - assertEquals(236966582000l, duration); + assertTrue(expression.getValue() instanceof Period); + final Period period = (Period) expression.getValue(); + assertEquals(7, period.getYears()); + assertEquals(6, period.getMonths()); + assertEquals(5, period.getDays()); } @Test - @Ignore("Unreconized expression.") public void testDuration2() throws CQLException, ParseException{ - final String cql = "T4H3M2S"; + final String cql = "PT4H3M2S"; final Object obj = CQL.parseExpression(cql); assertTrue(obj instanceof Literal); final Literal expression = (Literal) obj; - final long duration = (Long) expression.getValue(); - assertEquals(14582000,duration); + assertTrue(expression.getValue() instanceof Duration); + final Duration duration = (Duration) expression.getValue(); + assertEquals(14582, duration.get(ChronoUnit.SECONDS)); } @Test