Repository: jena Updated Branches: refs/heads/master d72568333 -> ec5b5dad8
JENA-886 : Add datatypes from XSD 1.1 xsd:dateTimeStamp, xsd:yearMonthDuration, xsd:dayTimeDuration Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/03dc77f4 Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/03dc77f4 Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/03dc77f4 Branch: refs/heads/master Commit: 03dc77f49562d96d658a02886fd3ea285b8bf980 Parents: d725683 Author: Andy Seaborne <[email protected]> Authored: Sun Mar 8 17:13:19 2015 +0000 Committer: Andy Seaborne <[email protected]> Committed: Sun Mar 8 17:13:19 2015 +0000 ---------------------------------------------------------------------- .../hp/hpl/jena/datatypes/xsd/XSDDatatype.java | 12 ++ .../xsd/impl/XSDDateTimeStampType.java | 57 +++++++ .../xsd/impl/XSDDayTimeDurationType.java | 60 +++++++ .../xsd/impl/XSDYearMonthDurationType.java | 37 +++++ .../java/com/hp/hpl/jena/vocabulary/XSD.java | 12 ++ .../hp/hpl/jena/datatypes/TestDatatypes.java | 160 +++++++++++++++++++ .../com/hp/hpl/jena/datatypes/TestPackage.java | 40 +++++ .../java/com/hp/hpl/jena/test/TestPackage.java | 1 + 8 files changed, 379 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/03dc77f4/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDDatatype.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDDatatype.java b/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDDatatype.java index aee2bcf..e4f2d72 100644 --- a/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDDatatype.java +++ b/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/XSDDatatype.java @@ -176,9 +176,18 @@ public class XSDDatatype extends BaseDatatype { /** Datatype representing xsd:dateTime */ public static final XSDDatatype XSDdateTime = new XSDDateTimeType("dateTime"); + /** Datatype representing xsd:dateTime */ + public static final XSDDatatype XSDdateTimeStamp = new XSDDateTimeStampType("dateTimeStamp"); + /** Datatype representing xsd:duration */ public static final XSDDatatype XSDduration = new XSDDurationType(); + /** Datatype representing xsd:dayTimeDration */ + public static final XSDDatatype XSDdayTimeDuration = new XSDDayTimeDurationType(); + + /** Datatype representing xsd:yearMonthDuration */ + public static final XSDDatatype XSDyearMonthDuration = new XSDYearMonthDurationType(); + /** Datatype representing xsd:gDay */ public static final XSDDatatype XSDgDay = new XSDDayType("gDay"); @@ -570,7 +579,10 @@ public class XSDDatatype extends BaseDatatype { tm.registerDatatype(XSDdate); tm.registerDatatype(XSDtime); tm.registerDatatype(XSDdateTime); + tm.registerDatatype(XSDdateTimeStamp); tm.registerDatatype(XSDduration); + tm.registerDatatype(XSDyearMonthDuration); + tm.registerDatatype(XSDdayTimeDuration) ; tm.registerDatatype(XSDgYearMonth); tm.registerDatatype(XSDgMonthDay); tm.registerDatatype(XSDgMonth); http://git-wip-us.apache.org/repos/asf/jena/blob/03dc77f4/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/impl/XSDDateTimeStampType.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/impl/XSDDateTimeStampType.java b/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/impl/XSDDateTimeStampType.java new file mode 100644 index 0000000..7040697 --- /dev/null +++ b/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/impl/XSDDateTimeStampType.java @@ -0,0 +1,57 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hp.hpl.jena.datatypes.xsd.impl; + +import com.hp.hpl.jena.datatypes.DatatypeFormatException ; +import com.hp.hpl.jena.datatypes.xsd.impl.XSDDateTimeType ; + +public class XSDDateTimeStampType extends XSDDateTimeType { + public XSDDateTimeStampType(String name) { + // Pretend to to be dateTime then tweak the uri. + super("dateTime") ; + super.javaClass = null ; + super.uri = XSD + "#"+name ; + } + + @Override + public Object parse(String lex) { + Object obj = super.parse(lex) ; + // At this point it is OK as an xsd:dateTime - check it has a timezone. + // timezoneFrag ::= 'Z' | ('+' | '-') (('0' digit | '1' [0-3]) ':' minuteFrag | '14:00') + if ( lex.indexOf('Z') != -1 ) + return obj ; + // Check a legal xsd:dateTime ends with timezoneFrag + // Z or a +/- at length-6 + + // Avoid regex! + int n = lex.length() ; + char z = lex.charAt(n-6) ; + if ( z != '+' && z != '-' ) + throw new DatatypeFormatException("Not valid as xsd:dateTimeStamp: "+lex) ; +// || ! RiotChars.isDigit(lex.charAt(n-5)) +// || ! RiotChars.isDigit(lex.charAt(n-4)) +// || lex.charAt(n-3) != ':' +// || ! RiotChars.isDigit(lex.charAt(n-2)) +// || ! RiotChars.isDigit(lex.charAt(n-1)) ) + return obj ; + // Alternative: + //static Pattern p = Pattern.compile("\\d\\d:\\d\\d$") ; + //return p.matcher(x).find() ; + } + } http://git-wip-us.apache.org/repos/asf/jena/blob/03dc77f4/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/impl/XSDDayTimeDurationType.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/impl/XSDDayTimeDurationType.java b/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/impl/XSDDayTimeDurationType.java new file mode 100644 index 0000000..4166a7c --- /dev/null +++ b/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/impl/XSDDayTimeDurationType.java @@ -0,0 +1,60 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hp.hpl.jena.datatypes.xsd.impl; + +import com.hp.hpl.jena.datatypes.DatatypeFormatException ; +import com.hp.hpl.jena.datatypes.xsd.impl.XSDAbstractDateTimeType ; + +// Xerces 2.11.0 does not have xsd:dayTimeDuration. +// Treat as duration with checking. +public class XSDDayTimeDurationType extends XSDAbstractDateTimeType { + public XSDDayTimeDurationType() { + super("duration") ; + super.uri = XSD + "#dayTimeDuration" ; + } + + @Override + public Object parse(String lex) { + Object obj = super.parse(lex) ; + // Must not have Y + if ( lex.indexOf('Y') != -1 ) + // has year. + throw new DatatypeFormatException("Not valid as xsd:dayTimeDuration: "+lex) ; + + int idx_T = lex.indexOf('T') ; + int idx_M = lex.indexOf('M') ; + + if ( idx_T == -1 ) { + // There is no T ; must have D and no M. + if ( lex.indexOf('D') == -1 ) + throw new DatatypeFormatException("Not valid as xsd:dayTimeDuration: "+lex) ; + if ( idx_M != -1 ) + // M, no T -> month + throw new DatatypeFormatException("Not valid as xsd:dayTimeDuration: "+lex) ; + return obj ; + } + + // Has T + // Must not have a M before T + if ( idx_M != -1 && idx_M < idx_T ) + // M before T => month. + throw new DatatypeFormatException("Not valid as xsd:dayTimeDuration: "+lex) ; + return obj ; + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/03dc77f4/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/impl/XSDYearMonthDurationType.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/impl/XSDYearMonthDurationType.java b/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/impl/XSDYearMonthDurationType.java new file mode 100644 index 0000000..bd58b94 --- /dev/null +++ b/jena-core/src/main/java/com/hp/hpl/jena/datatypes/xsd/impl/XSDYearMonthDurationType.java @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hp.hpl.jena.datatypes.xsd.impl; + +import com.hp.hpl.jena.datatypes.DatatypeFormatException ; +import com.hp.hpl.jena.datatypes.xsd.impl.XSDAbstractDateTimeType ; + +public class XSDYearMonthDurationType extends XSDAbstractDateTimeType { + public XSDYearMonthDurationType() { + super("duration") ; + super.uri = XSD + "#yearMonthDuration" ; + } + + @Override + public Object parse(String lex) { + Object obj = super.parse(lex) ; + if ( lex.indexOf('D') != -1 || lex.indexOf('T') != -1 ) + throw new DatatypeFormatException("Not valid as xsd:yearMonthDuration: "+lex) ; + return obj ; + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/03dc77f4/jena-core/src/main/java/com/hp/hpl/jena/vocabulary/XSD.java ---------------------------------------------------------------------- diff --git a/jena-core/src/main/java/com/hp/hpl/jena/vocabulary/XSD.java b/jena-core/src/main/java/com/hp/hpl/jena/vocabulary/XSD.java index c31d9b3..c141a25 100644 --- a/jena-core/src/main/java/com/hp/hpl/jena/vocabulary/XSD.java +++ b/jena-core/src/main/java/com/hp/hpl/jena/vocabulary/XSD.java @@ -147,9 +147,18 @@ public class XSD { /** Resource URI for xsd:dateTime */ public static Resource dateTime; + /** Resource URI for xsd:dateTimeStamp */ + public static Resource dateTimeStamp; + /** Resource URI for xsd:duration */ public static Resource duration; + /** Resource URI for xsd:yearMonthDuration */ + public static Resource yearMonthDuration; + + /** Resource URI for xsd:dayTimeDuration */ + public static Resource dayTimeDuration; + /** Resource URI for xsd:gDay */ public static Resource gDay; @@ -202,7 +211,10 @@ public class XSD { date = ResourceFactory.createResource(XSDDatatype.XSDdate.getURI()); time = ResourceFactory.createResource(XSDDatatype.XSDtime.getURI()); dateTime = ResourceFactory.createResource(XSDDatatype.XSDdateTime.getURI()); + dateTimeStamp = ResourceFactory.createResource(XSDDatatype.XSDdateTimeStamp.getURI()); duration = ResourceFactory.createResource(XSDDatatype.XSDduration.getURI()); + yearMonthDuration = ResourceFactory.createResource(XSDDatatype.XSDyearMonthDuration.getURI()); + dayTimeDuration = ResourceFactory.createResource(XSDDatatype.XSDdayTimeDuration.getURI()); gDay = ResourceFactory.createResource(XSDDatatype.XSDgDay.getURI()); gMonth = ResourceFactory.createResource(XSDDatatype.XSDgMonth.getURI()); gYear = ResourceFactory.createResource(XSDDatatype.XSDgYear.getURI()); http://git-wip-us.apache.org/repos/asf/jena/blob/03dc77f4/jena-core/src/test/java/com/hp/hpl/jena/datatypes/TestDatatypes.java ---------------------------------------------------------------------- diff --git a/jena-core/src/test/java/com/hp/hpl/jena/datatypes/TestDatatypes.java b/jena-core/src/test/java/com/hp/hpl/jena/datatypes/TestDatatypes.java new file mode 100644 index 0000000..389be78 --- /dev/null +++ b/jena-core/src/test/java/com/hp/hpl/jena/datatypes/TestDatatypes.java @@ -0,0 +1,160 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hp.hpl.jena.datatypes; + +import static org.junit.Assert.assertEquals ; +import static org.junit.Assert.assertFalse ; +import static org.junit.Assert.assertNotNull ; +import static org.junit.Assert.assertTrue ; +import org.junit.Test ; + +import com.hp.hpl.jena.datatypes.xsd.XSDDatatype ; +import com.hp.hpl.jena.graph.NodeFactory ; +import com.hp.hpl.jena.rdf.model.Resource ; +import com.hp.hpl.jena.vocabulary.XSD ; + +public class TestDatatypes { + + public XSDDatatype xsdDateTime = XSDDatatype.XSDdateTime ; + public XSDDatatype xsdDateTimeStamp = XSDDatatype.XSDdateTimeStamp ; + public XSDDatatype xsdDuration = XSDDatatype.XSDduration ; + public XSDDatatype xsdYearMonthDuration = XSDDatatype.XSDyearMonthDuration ; + public XSDDatatype xsdDayTimeDuration = XSDDatatype.XSDdayTimeDuration ; + + @Test public void registration_01() { checkRegistration1("dateTime", XSD.dateTime); } + @Test public void registration_02() { checkRegistration1("dateTimeStamp", XSD.dateTimeStamp); } + @Test public void registration_03() { checkRegistration1("duration", XSD.duration); } + @Test public void registration_04() { checkRegistration1("yearMonthDuration", XSD.yearMonthDuration); } + @Test public void registration_05() { checkRegistration1("dayTimeDuration", XSD.dayTimeDuration); } + + // xsd:dateTimeStamp + + @Test public void dateTimeStamp_01() { + valid(xsdDateTime, "2015-02-23T15:21:18Z") ; + valid(xsdDateTimeStamp, "2015-02-23T15:21:18Z") ; + } + + @Test public void dateTimeStamp_02() { + valid(xsdDateTime, "2015-02-23T15:21:18") ; + invalid(xsdDateTimeStamp, "2015-02-23T15:21:18") ; + } + + @Test public void dateTimeStamp_03() { + invalid(xsdDateTime, "2015-02-23Z") ; + invalid(xsdDateTimeStamp, "2015-02-23Z") ; + } + + @Test public void dateTimeStamp_04() { + valid(xsdDateTime, "2015-02-23T15:21:18.665Z") ; + valid(xsdDateTimeStamp, "2015-02-23T15:21:18.665Z") ; + } + + @Test public void dateTimeStamp_05() { + valid(xsdDateTime, "2015-02-23T15:21:18.665+00:00") ; + valid(xsdDateTimeStamp, "2015-02-23T15:21:18.665+00:00") ; + } + + @Test public void dateTimeStamp_06() { + invalid(xsdDateTime, "2015-02-23T15:21:18.665+15:00") ; + invalid(xsdDateTimeStamp, "2015-02-23T15:21:18.665+15:00") ; + } + + // xsd:yearMonthDuration + @Test public void yearMonthDuration_01() { + valid(xsdDuration, "P1Y") ; + valid(xsdYearMonthDuration, "P1Y") ; + } + + @Test public void yearMonthDuration_02() { + valid(xsdDuration, "-P1M") ; + valid(xsdYearMonthDuration, "-P1M") ; + } + + @Test public void yearMonthDuration_03() { + valid(xsdYearMonthDuration, "P9Y10M") ; + valid(xsdYearMonthDuration, "P9Y10M") ; + } + + @Test public void yearMonthDuration_04() { + valid(xsdDuration, "P1Y1D") ; + invalid(xsdYearMonthDuration, "P1Y1D") ; + } + + @Test public void yearMonthDuration_05() { + valid(xsdDuration, "P1YT1M") ; + invalid(xsdYearMonthDuration, "P1YT1M") ; + } + + @Test public void yearMonthDuration_06() { + valid(xsdDuration, "P1D") ; + invalid(xsdYearMonthDuration, "P1D") ; + } + + // xsd:dayTimeDuration + @Test public void dayTimeDuration_01() { + valid(xsdDuration, "PT0S") ; + valid(xsdDayTimeDuration, "PT0S") ; + } + + @Test public void dayTimeDuration_02() { + invalid(xsdDuration, "PT") ; + invalid(xsdDayTimeDuration, "PT") ; + } + + @Test public void dayTimeDuration_03() { + valid(xsdDuration, "P1D") ; + valid(xsdDayTimeDuration, "P1D") ; + } + + @Test public void dayTimeDuration_04() { + valid(xsdDuration, "PT1M") ; + valid(xsdDayTimeDuration, "PT1M") ; + } + + @Test public void dayTimeDuration_05() { + valid(xsdDuration, "PT1S") ; + valid(xsdDayTimeDuration, "PT1S") ; + } + + @Test public void dayTimeDuration_06() { + valid(xsdDuration, "PT1M") ; + invalid(xsdDayTimeDuration, "P1M") ; + } + + @Test public void dayTimeDuration_07() { + invalid(xsdDuration, "P1DT") ; + invalid(xsdDayTimeDuration, "P1DT") ; + } + + private void valid(XSDDatatype xsddatatype, String string) { + assertTrue("Expected valid: "+string, xsddatatype.isValid(string)) ; + } + + private void invalid(XSDDatatype xsddatatype, String string) { + assertFalse("Expected invalid: "+string, xsddatatype.isValid(string)) ; + } + + private void checkRegistration1(String localName, Resource r) { + XSDDatatype _xsd = (XSDDatatype)NodeFactory.getType(XSD.getURI() + localName) ; + assertNotNull(_xsd) ; + assertEquals(r.getURI(), _xsd.getURI()) ; + } + +} + http://git-wip-us.apache.org/repos/asf/jena/blob/03dc77f4/jena-core/src/test/java/com/hp/hpl/jena/datatypes/TestPackage.java ---------------------------------------------------------------------- diff --git a/jena-core/src/test/java/com/hp/hpl/jena/datatypes/TestPackage.java b/jena-core/src/test/java/com/hp/hpl/jena/datatypes/TestPackage.java new file mode 100644 index 0000000..b470fc4 --- /dev/null +++ b/jena-core/src/test/java/com/hp/hpl/jena/datatypes/TestPackage.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hp.hpl.jena.datatypes; + +import junit.framework.JUnit4TestAdapter ; +import junit.framework.TestSuite ; + +/** + Collected test suite for the .datatype package. + (many other tests are elsewhere) +*/ + +public class TestPackage extends TestSuite { + + static public TestSuite suite() { + return new TestPackage(); + } + + /** Creates new TestPackage */ + private TestPackage() { + super("datatypes"); + addTest(new JUnit4TestAdapter(TestDatatypes.class)) ; + } +} http://git-wip-us.apache.org/repos/asf/jena/blob/03dc77f4/jena-core/src/test/java/com/hp/hpl/jena/test/TestPackage.java ---------------------------------------------------------------------- diff --git a/jena-core/src/test/java/com/hp/hpl/jena/test/TestPackage.java b/jena-core/src/test/java/com/hp/hpl/jena/test/TestPackage.java index b75da5b..7008367 100644 --- a/jena-core/src/test/java/com/hp/hpl/jena/test/TestPackage.java +++ b/jena-core/src/test/java/com/hp/hpl/jena/test/TestPackage.java @@ -41,6 +41,7 @@ public class TestPackage extends TestCase { ts.setName("Jena") ; addTest(ts, "System setup", TestSystemSetup.suite()); addTest(ts, "Enhanced", com.hp.hpl.jena.enhanced.test.TestPackage.suite()); + addTest(ts, "Datatypes", com.hp.hpl.jena.datatypes.TestPackage.suite()) ; addTest(ts, "Graph", com.hp.hpl.jena.graph.test.TestPackage.suite()); addTest(ts, "Mem", com.hp.hpl.jena.mem.test.TestMemPackage.suite() ); addTest(ts, "Mem2", com.hp.hpl.jena.mem.test.TestGraphMemPackage.suite() );
