Skip to content

Commit e78f6dc

Browse files
authored
Merge pull request #183 from fanavarro/4.0
Fix typo in _subPropertyOfRel map key and new date parser added
2 parents e195592 + fb24252 commit e78f6dc

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;
@@ -113,15 +117,15 @@ protected Object getObjectValue(IRI propertyIRI, Literal object) {
113117
return object.booleanValue();
114118
} else if (datatype.equals(XMLSchema.DATETIME)) {
115119
try {
116-
return LocalDateTime.parse(object.stringValue());
117-
} catch (DateTimeParseException e) {
120+
return DateUtils.parseDateTime(object.stringValue());
121+
} catch (IllegalArgumentException e) {
118122
//if date cannot be parsed we return string value
119123
return object.stringValue();
120124
}
121125
} else if (datatype.equals(XMLSchema.DATE)) {
122126
try {
123-
return LocalDate.parse(object.stringValue());
124-
} catch (DateTimeParseException e) {
127+
return DateUtils.parseDateTime(object.stringValue());
128+
} catch (IllegalArgumentException e) {
125129
//if date cannot be parsed we return string value
126130
return object.stringValue();
127131
}
@@ -143,6 +147,8 @@ protected Object getObjectValue(IRI propertyIRI, Literal object) {
143147
// default
144148
return object.stringValue();
145149
}
150+
151+
146152

147153
protected String getValueWithDatatype(IRI datatype, String value) {
148154
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)