| 1 |
package org.wikidata.wdtk.rdf.values; |
| 2 |
|
| 3 |
/* |
| 4 |
* #%L |
| 5 |
* Wikidata Toolkit RDF |
| 6 |
* %% |
| 7 |
* Copyright (C) 2014 Wikidata Toolkit Developers |
| 8 |
* %% |
| 9 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 |
* you may not use this file except in compliance with the License. |
| 11 |
* You may obtain a copy of the License at |
| 12 |
* |
| 13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
* |
| 15 |
* Unless required by applicable law or agreed to in writing, software |
| 16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 |
* See the License for the specific language governing permissions and |
| 19 |
* limitations under the License. |
| 20 |
* #L% |
| 21 |
*/ |
| 22 |
|
| 23 |
import org.eclipse.rdf4j.model.Literal; |
| 24 |
import org.eclipse.rdf4j.model.Resource; |
| 25 |
import org.eclipse.rdf4j.model.IRI; |
| 26 |
import org.eclipse.rdf4j.model.Value; |
| 27 |
import org.eclipse.rdf4j.rio.RDFHandlerException; |
| 28 |
import org.wikidata.wdtk.datamodel.interfaces.DatatypeIdValue; |
| 29 |
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; |
| 30 |
import org.wikidata.wdtk.datamodel.interfaces.TimeValue; |
| 31 |
import org.wikidata.wdtk.rdf.OwlDeclarationBuffer; |
| 32 |
import org.wikidata.wdtk.rdf.PropertyRegister; |
| 33 |
import org.wikidata.wdtk.rdf.RdfWriter; |
| 34 |
import org.wikidata.wdtk.rdf.Vocabulary; |
| 35 |
|
| 36 |
import java.time.Month; |
| 37 |
|
| 38 |
public class TimeValueConverter extends BufferedValueConverter<TimeValue> { |
| 39 |
|
| 40 |
public TimeValueConverter(RdfWriter rdfWriter, |
| 41 |
PropertyRegister PropertyRegister, |
| 42 |
OwlDeclarationBuffer rdfConversionBuffer) { |
| 43 |
super(rdfWriter, PropertyRegister, rdfConversionBuffer); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public Value getRdfValue(TimeValue value, PropertyIdValue propertyIdValue, |
| 48 |
boolean simple) { |
| 49 |
|
| 50 |
String datatype = this.propertyRegister.setPropertyTypeFromTimeValue( |
| 51 |
propertyIdValue, value); |
| 52 |
|
| 53 |
switch (datatype) { |
| 54 |
case DatatypeIdValue.DT_TIME: |
| 55 |
if (simple) { |
| 56 |
this.rdfConversionBuffer.addDatatypeProperty(propertyIdValue); |
| 57 |
return TimeValueConverter.getTimeLiteral(value, this.rdfWriter); |
| 58 |
} else { |
| 59 |
IRI valueUri = this.rdfWriter.getUri(Vocabulary.getTimeValueUri(value)); |
| 60 |
this.rdfConversionBuffer.addObjectProperty(propertyIdValue); |
| 61 |
addValue(value, valueUri); |
| 62 |
|
| 63 |
return valueUri; |
| 64 |
} |
| 65 |
default: |
| 66 |
logIncompatibleValueError(propertyIdValue, datatype, "time"); |
| 67 |
return null; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Write the auxiliary RDF data for encoding the given value. |
| 73 |
* |
| 74 |
* @param value |
| 75 |
* the value to write |
| 76 |
* @param resource |
| 77 |
* the (subject) URI to use to represent this value in RDF |
| 78 |
* @throws RDFHandlerException |
| 79 |
*/ |
| 80 |
@Override |
| 81 |
public void writeValue(TimeValue value, Resource resource) |
| 82 |
throws RDFHandlerException { |
| 83 |
this.rdfWriter.writeTripleValueObject(resource, RdfWriter.RDF_TYPE, |
| 84 |
RdfWriter.WB_TIME_VALUE); |
| 85 |
|
| 86 |
this.rdfWriter.writeTripleValueObject(resource, RdfWriter.WB_TIME, |
| 87 |
TimeValueConverter.getTimeLiteral(value, this.rdfWriter)); |
| 88 |
|
| 89 |
this.rdfWriter.writeTripleIntegerObject(resource, |
| 90 |
RdfWriter.WB_TIME_PRECISION, value.getPrecision()); |
| 91 |
this.rdfWriter.writeTripleIntegerObject(resource, |
| 92 |
RdfWriter.WB_TIME_TIMEZONE, |
| 93 |
value.getTimezoneOffset()); |
| 94 |
this.rdfWriter.writeTripleUriObject(resource, |
| 95 |
RdfWriter.WB_TIME_CALENDAR_MODEL, |
| 96 |
value.getPreferredCalendarModel()); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns the RDF literal to encode the time component of a given time |
| 101 |
* value. |
| 102 |
* <p> |
| 103 |
* Times with limited precision are encoded using limited-precision XML |
| 104 |
* Schema datatypes, such as gYear, if available. Wikidata encodes the year |
| 105 |
* 1BCE as 0000, while XML Schema, even in version 2, does not allow 0000 |
| 106 |
* and interprets -0001 as 1BCE. Thus all negative years must be shifted by |
| 107 |
* 1, but we only do this if the year is precise. |
| 108 |
* |
| 109 |
* @param value |
| 110 |
* the value to convert |
| 111 |
* @param rdfWriter |
| 112 |
* the object to use for creating the literal |
| 113 |
* @return the RDF literal |
| 114 |
*/ |
| 115 |
private static Literal getTimeLiteral(TimeValue value, RdfWriter rdfWriter) { |
| 116 |
/* we need to check for year zero before julian date conversion, |
| 117 |
since that can change the year (if the date is 1 Jan 1 for example) |
| 118 |
*/ |
| 119 |
boolean yearZero = value.getYear() == 0; |
| 120 |
|
| 121 |
TimeValue gregorian = value.toGregorian(); |
| 122 |
if(gregorian != null) { |
| 123 |
value = gregorian; |
| 124 |
} |
| 125 |
|
| 126 |
long year = value.getYear(); |
| 127 |
|
| 128 |
/* https://www.mediawiki.org/wiki/Wikibase/DataModel/JSON#time says the following about the JSON mapping: |
| 129 |
|
| 130 |
The format used for Gregorian and Julian dates use a notation resembling ISO 8601. E.g. ā+1994-01-01T00:00:00Zā. |
| 131 |
The year is represented by at least four digits, zeros are added on the left side as needed. |
| 132 |
Years BCE are represented as negative numbers, using the historical numbering, in which year 0 is undefined, |
| 133 |
and the year 1 BCE is represented as -0001, the year 44 BCE is represented as -0044, etc., |
| 134 |
like XSD 1.0 (ISO 8601:1988) does. |
| 135 |
In contrast, the RDF mapping relies on XSD 1.1 (ISO 8601:2004) dates that use the proleptic Gregorian calendar |
| 136 |
and astronomical year numbering, where the year 1 BCE is represented as +0000 and the year 44 BCE |
| 137 |
is represented as -0043. |
| 138 |
*/ |
| 139 |
// map negative dates from historical numbering to XSD 1.1 |
| 140 |
if (year < 0 && value.getPrecision() >= TimeValue.PREC_YEAR) { |
| 141 |
year++; |
| 142 |
} |
| 143 |
|
| 144 |
byte month = value.getMonth(); |
| 145 |
byte day = value.getDay(); |
| 146 |
|
| 147 |
if ((value.getPrecision() < TimeValue.PREC_MONTH || month == 0) && !yearZero) { |
| 148 |
month = 1; |
| 149 |
} |
| 150 |
|
| 151 |
if ((value.getPrecision() < TimeValue.PREC_DAY || day == 0) && !yearZero) { |
| 152 |
day = 1; |
| 153 |
} |
| 154 |
|
| 155 |
if (value.getPrecision() >= TimeValue.PREC_DAY && !yearZero) { |
| 156 |
int maxDays = Byte.MAX_VALUE; |
| 157 |
if (month > 0 && month < 13) { |
| 158 |
boolean leap = (year % 4L) == 0L && (year % 100L != 0L || year % 400L == 0L); |
| 159 |
maxDays = Month.of(month).length(leap); |
| 160 |
} |
| 161 |
if (day > maxDays) { |
| 162 |
day = (byte)maxDays; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
String minus = year < 0 ? "-" : ""; |
| 167 |
String timestamp = String.format("%s%04d-%02d-%02dT%02d:%02d:%02dZ", |
| 168 |
minus, Math.abs(year), month, day, |
| 169 |
value.getHour(), value.getMinute(), value.getSecond()); |
| 170 |
if (yearZero) { |
| 171 |
return rdfWriter.getLiteral("+" + timestamp); |
| 172 |
} |
| 173 |
return rdfWriter.getLiteral(timestamp, RdfWriter.XSD_DATETIME); |
| 174 |
} |
| 175 |
|
| 176 |
} |