Skip to content

Commit 1c3dfd7

Browse files
authored
Merge pull request #199 from neo4j-labs/4.0
4.0
2 parents 9346783 + e78f6dc commit 1c3dfd7

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

src/main/java/n10s/RDFToLPGStatementProcessor.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import n10s.graphconfig.GraphConfig;
44
import n10s.graphconfig.RDFParserConfig;
5+
import n10s.utils.DateUtils;
56
import n10s.utils.InvalidNamespacePrefixDefinitionInDB;
67
import n10s.utils.NsPrefixMap;
78
import org.eclipse.rdf4j.model.*;
@@ -14,11 +15,14 @@
1415
import org.neo4j.internal.helpers.collection.Iterables;
1516
import org.neo4j.logging.Log;
1617

18+
import java.time.DateTimeException;
1719
import java.time.LocalDate;
1820
import java.time.LocalDateTime;
1921
import java.time.format.DateTimeParseException;
2022
import java.util.*;
2123

24+
import javax.xml.bind.DatatypeConverter;
25+
2226
import static n10s.graphconfig.GraphConfig.*;
2327
import static n10s.graphconfig.Params.CUSTOM_DATA_TYPE_SEPERATOR;
2428
import static n10s.graphconfig.Params.PREFIX_SEPARATOR;
@@ -110,15 +114,15 @@ protected Object getObjectValue(IRI propertyIRI, Literal object) {
110114
return object.booleanValue();
111115
} else if (datatype.equals(XMLSchema.DATETIME)) {
112116
try {
113-
return LocalDateTime.parse(object.stringValue());
114-
} catch (DateTimeParseException e) {
117+
return DateUtils.parseDateTime(object.stringValue());
118+
} catch (IllegalArgumentException e) {
115119
//if date cannot be parsed we return string value
116120
return object.stringValue();
117121
}
118122
} else if (datatype.equals(XMLSchema.DATE)) {
119123
try {
120-
return LocalDate.parse(object.stringValue());
121-
} catch (DateTimeParseException e) {
124+
return DateUtils.parseDateTime(object.stringValue());
125+
} catch (IllegalArgumentException e) {
122126
//if date cannot be parsed we return string value
123127
return object.stringValue();
124128
}
@@ -140,6 +144,8 @@ protected Object getObjectValue(IRI propertyIRI, Literal object) {
140144
// default
141145
return object.stringValue();
142146
}
147+
148+
143149

144150
protected String getValueWithDatatype(IRI datatype, String value) {
145151
StringBuilder result = new StringBuilder(value);

src/main/java/n10s/graphconfig/GraphConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public Map<String, Object> serialiseConfig() {
313313
configAsMap.put("_subClassOfRel", this.subClassOfRelName);
314314
configAsMap.put("_dataTypePropertyLabel", this.dataTypePropertyLabelName);
315315
configAsMap.put("_objectPropertyLabel", this.objectPropertyLabelName);
316-
configAsMap.put("_subPropertyOfRell", this.subPropertyOfRelName);
316+
configAsMap.put("_subPropertyOfRel", this.subPropertyOfRelName);
317317
configAsMap.put("_domainRel", this.domainRelName);
318318
configAsMap.put("_rangeRel", this.rangeRelName);
319319

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package n10s.utils;
2+
3+
import java.time.DateTimeException;
4+
import java.time.LocalDateTime;
5+
import java.time.format.DateTimeParseException;
6+
import java.util.Calendar;
7+
8+
import javax.xml.bind.DatatypeConverter;
9+
10+
public class DateUtils {
11+
/**
12+
* Convert a String-formatted date into a LocalDateTime object by using
13+
* LocalDateTime.parse first, and DatatypeConverter.parseDateTime if the
14+
* first one fails.
15+
* @param stringDateTime
16+
* The string-formatted date.
17+
* @return LocalDateTime object.
18+
* @throws IllegalArgumentException if the string is not parseable to a date.
19+
*/
20+
public static LocalDateTime parseDateTime(String stringDateTime) {
21+
boolean dateParsed = false;
22+
LocalDateTime localDateTime = null;
23+
StringBuilder parserErrors = new StringBuilder("Error parsing ").append(stringDateTime).append(":\n");
24+
25+
/* Try date parsing with LocalDateTime.parse */
26+
try {
27+
localDateTime = LocalDateTime.parse(stringDateTime);
28+
dateParsed = true;
29+
} catch (DateTimeParseException e) {
30+
dateParsed = false;
31+
parserErrors.append(e.getMessage()).append("\n");
32+
}
33+
34+
/* If date is not parsed */
35+
if (!dateParsed) {
36+
/* Try with DatatypeConverter.parseDateTime */
37+
try {
38+
Calendar calendar = DatatypeConverter.parseDateTime(stringDateTime);
39+
localDateTime = LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId());
40+
dateParsed = true;
41+
} catch (IllegalArgumentException | DateTimeException e) {
42+
dateParsed = false;
43+
parserErrors.append(e.getMessage()).append("\n");
44+
}
45+
}
46+
47+
/* If date is not parsed, throw exception */
48+
if (!dateParsed) {
49+
throw new IllegalArgumentException(parserErrors.toString());
50+
} else {
51+
return localDateTime;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)