Skip to content

Commit

Permalink
eclipse-rdf4jGH-5058: additional parser code (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
barthanssens committed Jul 9, 2024
1 parent 8a7544d commit 26b6826
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class CSVW {
/** csvw:dialect */
public static final IRI DIALECT;

/** csvw:format */
public static final IRI FORMAT;

/** csvw:header */
public static final IRI HEADER;

Expand Down Expand Up @@ -94,6 +97,7 @@ public class CSVW {
DATATYPE = Vocabularies.createIRI(NAMESPACE, "datatype");
DEFAULT = Vocabularies.createIRI(NAMESPACE, "default");
DIALECT = Vocabularies.createIRI(NAMESPACE, "dialect");
FORMAT = Vocabularies.createIRI(NAMESPACE, "format");
HEADER = Vocabularies.createIRI(NAMESPACE, "header");
LANG = Vocabularies.createIRI(NAMESPACE, "lang");
NAME = Vocabularies.createIRI(NAMESPACE, "name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.rdf4j.rio.csvw.parsers;

import java.time.format.DateTimeFormatter;
import java.util.Set;

import org.eclipse.rdf4j.model.IRI;
Expand All @@ -25,12 +26,12 @@
*/
public class CellParser {
private String name;
private IRI dataType;
private String defaultValue;
protected IRI dataType;
protected String defaultValue;
private boolean isRequired;
private String format;
private IRI propertyIRI;
private String valueUrl;
private String format;
private String separator;

/**
Expand Down Expand Up @@ -68,13 +69,6 @@ public void setIsRequired(boolean isRequired) {
this.isRequired = isRequired;
}

/**
* @param format the format to set
*/
public void setFormat(String format) {
this.format = format;
}

/**
* @return the propertyUrl as IRI
*/
Expand Down Expand Up @@ -129,6 +123,13 @@ public void setSeparator(String separator) {
this.separator = separator;
}

/**
* @param format
*/
public void setFormat(String format) {
this.format = format;
}

/**
* Get the value from a cell
*
Expand All @@ -143,7 +144,8 @@ public Value parse(String cell) {
if (valueUrl != null && s != null) {
return Values.iri(valueUrl.replace("{" + name + "}", s));
}

System.err.println(s);
System.err.println(dataType);
return Values.literal(s, dataType);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
* Copyright (c) 2024 Eclipse RDF4J contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/
package org.eclipse.rdf4j.rio.csvw.parsers;

import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Set;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Namespace;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.util.Literals;
import org.eclipse.rdf4j.model.util.Values;
import org.eclipse.rdf4j.rio.RDFParseException;

/**
*
* @author Bart Hanssens
*/
public class CellParserDate extends CellParser {
private DateTimeFormatter formatter;

/**
* @param format
*/
@Override
public void setFormat(String format) {
super.setFormat(format);
formatter = DateTimeFormatter.ofPattern(format);
}

/**
* Get the value from a cell
*
* @param cell
* @return
*/
public Value parse(String cell) {
String s = cell;
if ((s == null || s.isEmpty()) && (defaultValue != null)) {
s = defaultValue;
}
if (formatter != null) {
s = DateTimeFormatter.BASIC_ISO_DATE.format(formatter.parse(s));
}
return Values.literal(s, dataType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.rdf4j.rio.csvw.parsers;

import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.base.CoreDatatype.XSD;

/**
*
Expand All @@ -19,11 +20,18 @@
public class CellParserFactory {
/**
* Create a new CellParser based on datatype
*
* @param datatype
* @return
* @return
*/
public static CellParser create(IRI datatype) {
CellParser p = new CellParser();
CellParser p;

if (datatype.equals(XSD.DATE.getIri())) {
p = new CellParserDate();
} else {
p = new CellParser();
}
p.setDataType(datatype);
return p;
}
Expand Down

0 comments on commit 26b6826

Please sign in to comment.