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 12, 2024
1 parent b7c58ec commit 6c44c26
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public class CSVW {
/** csvw:name */
public static final IRI NAME;

/** csvw:null */
public static final IRI NULL;

/** csvw:propertyUrl */
public static final IRI PROPERTY_URL;

Expand All @@ -95,7 +98,7 @@ public class CSVW {

/** csvw:skipRows */
public static final IRI SKIP_ROWS;

/** csvw:tableSchema */
public static final IRI TABLE_SCHEMA;

Expand All @@ -105,6 +108,9 @@ public class CSVW {
/** csvw:title */
public static final IRI TITLE;

/** csvw:trim */
public static final IRI TRIM;

/** csvw:url */
public static final IRI URL;

Expand All @@ -130,13 +136,15 @@ public class CSVW {
HEADER_ROW_COUNT = Vocabularies.createIRI(NAMESPACE, "headerRowCount");
LANG = Vocabularies.createIRI(NAMESPACE, "lang");
NAME = Vocabularies.createIRI(NAMESPACE, "name");
NULL = Vocabularies.createIRI(NAMESPACE, "null");
PROPERTY_URL = Vocabularies.createIRI(NAMESPACE, "propertyUrl");
QUOTE_CHAR = Vocabularies.createIRI(NAMESPACE, "quoteChar");
REQUIRED = Vocabularies.createIRI(NAMESPACE, "required");
SKIP_ROWS = Vocabularies.createIRI(NAMESPACE, "skipRows");
TABLE_SCHEMA = Vocabularies.createIRI(NAMESPACE, "tableSchema");
TABLES = Vocabularies.createIRI(NAMESPACE, "tables");
TITLE = Vocabularies.createIRI(NAMESPACE, "title");
TRIM = Vocabularies.createIRI(NAMESPACE, "trim");
URL = Vocabularies.createIRI(NAMESPACE, "url");
VALUE_URL = Vocabularies.createIRI(NAMESPACE, "valueUrl");
VIRTUAL = Vocabularies.createIRI(NAMESPACE, "virtual");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private CellParser getCellParser(Model metadata, Resource column) {

Models.getPropertyString(metadata, column, CSVW.DEFAULT).ifPresent(v -> parser.setDefaultValue(v));
Models.getPropertyString(metadata, column, CSVW.REQUIRED)
.ifPresent(v -> parser.setIsRequired(Boolean.parseBoolean(v)));
.ifPresent(v -> parser.setRequired(Boolean.parseBoolean(v)));
Models.getPropertyString(metadata, column, CSVW.VIRTUAL)
.ifPresent(v -> parser.setVirtual(Boolean.parseBoolean(v)));

Expand All @@ -239,12 +239,15 @@ private CellParser getCellParser(Model metadata, Resource column) {
// mostly for date formats
getFormat(metadata, column).ifPresent(v -> parser.setFormat(v));

Models.getPropertyString(metadata, column, CSVW.VALUE_URL).ifPresent(v -> parser.setValueURL(v));
Models.getPropertyString(metadata, column, CSVW.TRIM)
.ifPresent(v -> parser.setVirtual(Boolean.parseBoolean(v)));

Models.getPropertyString(metadata, column, CSVW.VALUE_URL).ifPresent(v -> parser.setValueUrl(v));

// use a property from a vocabulary as predicate, or create a property relative to the namespace of the CSV
Optional<String> propertyURL = Models.getPropertyString(metadata, column, CSVW.PROPERTY_URL);
String s = propertyURL.isPresent() ? propertyURL.get() : "_local:" + parser.getName();
parser.setPropertyURL(metadata.getNamespaces(), s);
parser.setPropertyIRI(metadata.getNamespaces(), s);

return parser;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,66 +22,77 @@
* @author Bart Hanssens
*/
public abstract class CellParser {
protected String name;
protected IRI dataType;
protected String lang;
protected String defaultValue;
protected boolean isRequired;
protected boolean isVirtual;
protected IRI propertyIRI;
protected String valueUrl;
protected String format;
protected String decimalChar;
protected String groupChar;
protected String separator;
private String name;
private IRI dataType;
private String lang;
private String defaultValue;
private String nullValue;
private boolean required;
private boolean virtual = false;
private IRI propertyIRI;
private String valueUrl;
private String format;
private String decimalChar = ".";
private String groupChar;
private String separator;
private boolean trim = true;

public String getName() {
return name;
}

/**
* @param name
*/
public void setName(String name) {
this.name = name;
}

/**
* @return name
*/
public String getName() {
return name;
public IRI getDataType() {
return dataType;
}

/**
* @param dataType
*/
public void setDataType(IRI dataType) {
this.dataType = dataType;
}

/**
* Set language code
*
* @param lang language code
*/
public String getLang() {
return lang;
}

public void setLang(String lang) {
this.lang = lang;
}

/**
* @param defaultValue the defaultValue to set
*/
public String getDefaultValue() {
return defaultValue;
}

public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}

/**
* @param isRequired the isRequired to set
*/
public void setIsRequired(boolean isRequired) {
this.isRequired = isRequired;
public String getNullValue() {
return nullValue;
}

public void setNullValue(String nullValue) {
this.nullValue = nullValue;
}

public boolean isRequired() {
return required;
}

public void setRequired(boolean isRequired) {
this.required = isRequired;
}

public boolean isVirtual() {
return virtual;
}

public void setVirtual(boolean isVirtual) {
this.virtual = isVirtual;
}

/**
* @return the propertyUrl as IRI
*/
public IRI getPropertyIRI() {
return propertyIRI;
}
Expand All @@ -92,7 +103,7 @@ public IRI getPropertyIRI() {
* @param namespaces set of namespaces
* @param propertyUrl the propertyUrl to set
*/
public void setPropertyURL(Set<Namespace> namespaces, String propertyUrl) {
public void setPropertyIRI(Set<Namespace> namespaces, String propertyUrl) {
this.propertyIRI = Values.iri(namespaces, propertyUrl);
}

Expand All @@ -101,92 +112,76 @@ public void setPropertyURL(Set<Namespace> namespaces, String propertyUrl) {
*
* @param propertyUrl the propertyUrl to set
*/
public void setPropertyURL(String propertyUrl) {
public void setPropertyIRI(String propertyUrl) {
this.propertyIRI = Values.iri("", propertyUrl);
}

/**
* @return the valueUrl
*/
public String getValueURL() {
public String getValueUrl() {
return valueUrl;
}

/**
* @param valueUrl the valueUrl to set
*/
public void setValueURL(String valueUrl) {
public void setValueUrl(String valueUrl) {
this.valueUrl = valueUrl;
}

/**
* @return the separator
*/
public String getSeparator() {
return separator;
public String getFormat() {
return format;
}

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

/**
* @return the decimal character
*/
public String getDecimalChar() {
return decimalChar;
}

/**
* @param decimalChar the decimal character to set
*/
public void setDecimalChar(String decimalChar) {
this.decimalChar = decimalChar;
}

/**
* @return the group character
*/
public String getGroupChar() {
return groupChar;
}

/**
* @param groupChar the group character to set
*/
public void setGroupChar(String groupChar) {
this.groupChar = groupChar;
}

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

protected String getValueOrDefault(String s) {
if ((s == null || s.isEmpty()) && (defaultValue != null)) {
return defaultValue;
}
return s;
public void setSeparator(String separator) {
this.separator = separator;
}

/**
* @return true if the column is virtual
*/
public boolean isVirtual() {
return this.isVirtual;
public boolean isTrim() {
return trim;
}

public void setTrim(boolean trim) {
this.trim = trim;
}

/**
* @param isVirtual
* Get the (possibly trimmed) value or default value
*
* @param s
* @return
*/
public void setVirtual(boolean isVirtual) {
this.isVirtual = isVirtual;
protected String getValueOrDefault(String s) {
if ((s == null || s.isEmpty()) && (defaultValue != null)) {
return defaultValue;
}
if (s == null) {
return null;
}
if (s.equals(nullValue)) {
return null;
}

return trim ? s.trim() : s;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void setFormat(String format) {
public Value parse(String cell) {
String s = getValueOrDefault(cell);

return Values.literal(valueTrue.equals(s) ? "true" : "false", dataType);
return Values.literal(valueTrue.equals(s) ? "true" : "false", getDataType());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Value parse(String cell) {
s = DateTimeFormatter.ISO_DATE.format(formatter.parse(s));
}
System.err.println("date = " + s);
return Values.literal(s, dataType);
return Values.literal(s, getDataType());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public class CellParserDecimal extends CellParser {
public Value parse(String cell) {
String s = getValueOrDefault(cell);

if (s != null && groupChar != null) {
s = s.replace(groupChar, "");
if (s != null && getGroupChar() != null) {
s = s.replace(getGroupChar(), "");
}

// always use a '.' in RDF, not the European-style ','
if (s != null && !decimalChar.equals(".")) {
s = s.replace(decimalChar, ".");
if (s != null && !getDecimalChar().equals(".")) {
s = s.replace(getDecimalChar(), ".");
}

return Values.literal(s, dataType);
return Values.literal(s, getDataType());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ public class CellParserInteger extends CellParser {
public Value parse(String cell) {
String s = getValueOrDefault(cell);

if (s != null && groupChar != null) {
s = s.replace(groupChar, "");
if (s != null && getGroupChar() != null) {
s = s.replace(getGroupChar(), "");
}

return Values.literal(s, dataType);
return Values.literal(s, getDataType());
}

}
Loading

0 comments on commit 6c44c26

Please sign in to comment.