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 15, 2024
1 parent 15e3321 commit 2b1d57f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public class CSVW {
/** csvw:skipRows */
public static final IRI SKIP_ROWS;

/** csvw:suppressOutput */
public static final IRI SUPPRESS_OUTPUT;

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

Expand Down Expand Up @@ -173,6 +176,7 @@ public class CSVW {
ROWNUM = Vocabularies.createIRI(NAMESPACE, "rownum");
SKIP_COLUMNS = Vocabularies.createIRI(NAMESPACE, "skipColumns");
SKIP_ROWS = Vocabularies.createIRI(NAMESPACE, "skipRows");
SUPPRESS_OUTPUT = Vocabularies.createIRI(NAMESPACE, "suppressOutput");
TABLE_SCHEMA = Vocabularies.createIRI(NAMESPACE, "tableSchema");
TABLES = Vocabularies.createIRI(NAMESPACE, "tables");
TITLE = Vocabularies.createIRI(NAMESPACE, "title");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ private CellParser getCellParser(Model metadata, Resource column) {
.ifPresent(v -> parser.setRequired(Boolean.parseBoolean(v)));
Models.getPropertyString(metadata, column, CSVW.VIRTUAL)
.ifPresent(v -> parser.setVirtual(Boolean.parseBoolean(v)));
Models.getPropertyString(metadata, column, CSVW.SUPPRESS_OUTPUT)
.ifPresent(v -> parser.setVirtual(Boolean.parseBoolean(v)));

// only useful for strings
Models.getPropertyString(metadata, column, CSVW.LANG).ifPresent(v -> parser.setLang(v));
Expand Down Expand Up @@ -417,7 +419,9 @@ private void parseCSV(Model metadata, RDFHandler handler, URI csvFile, CellParse

IRI predicate = cellParsers[i].getPropertyIRI();
val = cellParsers[i].parse(cells[i]);
handler.handleStatement(Statements.statement(subject, predicate, val, null));
if (!cellParsers[i].isSuppressed()) {
handler.handleStatement(Statements.statement(subject, predicate, val, null));
}
}
line++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public abstract class CellParser {
private String defaultValue;
private String nullValue;
private boolean required;
private String aboutUrl;
private IRI propertyIRI;
private String valueUrl;
private String format;
Expand All @@ -42,8 +43,11 @@ public abstract class CellParser {
private String separator;
private boolean trim = true;
private boolean virtual = false;
private String[] propPlaceholder;
private String[] valPlaceholder;
private boolean suppressed = false;

private String[] aboutPlaceholders;
private String[] propPlaceholders;
private String[] valPlaceholders;

public String getName() {
return name;
Expand Down Expand Up @@ -131,10 +135,6 @@ public void setVirtual(boolean virtual) {
this.virtual = virtual;
}

public IRI getPropertyIRI() {
return propertyIRI;
}

/**
* Extract placeholders (if any)
*
Expand All @@ -154,6 +154,34 @@ private String[] extractPlaceholders(String template) {
return null;
}

/**
* Get aboutURL
*
* @return
*/
public String getAboutUrl() {
return aboutUrl;
}

/**
* Set aboutUrl
*
* @param aboutUrl
*/
public void setAboutUrl(String aboutUrl) {
this.aboutUrl = aboutUrl;
this.aboutPlaceholders = extractPlaceholders(aboutUrl);
}

/**
* Get propertyUrl as IRI
*
* @return
*/
public IRI getPropertyIRI() {
return propertyIRI;
}

/**
* Set property URL (predicate IRI)
*
Expand Down Expand Up @@ -189,7 +217,7 @@ public String getValueUrl() {
*/
public void setValueUrl(String valueUrl) {
this.valueUrl = valueUrl;
this.valPlaceholder = extractPlaceholders(valueUrl);
this.valPlaceholders = extractPlaceholders(valueUrl);
}

/**
Expand Down Expand Up @@ -282,6 +310,24 @@ public void setTrim(boolean trim) {
this.trim = trim;
}

/**
* Set if output needs to be suppressed
*
* @return
*/
public boolean isSuppressed() {
return suppressed;
}

/**
* Set if output needs to be suppressed
*
* @param suppressed
*/
public void setSuppressed(boolean suppressed) {
this.suppressed = suppressed;
}

/**
* Get the (possibly trimmed) value or default value
*
Expand Down
12 changes: 9 additions & 3 deletions core/rio/csvw/src/test/resources/painters-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
{ "name": "last_name",
"propertyUrl": "schema:familyName"},
{ "name": "country_id",
"propertyUrl": "schema:nationality",
"valueUrl": "https://www.wikidata.org/wiki/{country_id}"},
"suppressOutput": "true" },
{ "name": "country_name_nl",
"lang": "nl" },
{ "name": "country_name_en",
Expand All @@ -32,7 +31,14 @@
"separator": " " },
{ "virtual": true,
"propertyUrl": "rdf:type",
"valueUrl": "schema:Person" }
"valueUrl": "schema:Person" },
{ "virtual": true,
"propertyUrl": "schema:nationality",
"valueUrl": "https://www.wikidata.org/wiki/{country_id}" },
{ "virtual": true,
"aboutUrl": "https://www.wikidata.org/wiki/{country_id}",
"propertyUrl": "rdf:type",
"valueUrl": "schema:Country" }
],
"primaryKey": "wikidata_id"
}
Expand Down

0 comments on commit 2b1d57f

Please sign in to comment.