diff --git a/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/TabularModule.java b/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/TabularModule.java index 4e5e4f21..60b73c6d 100644 --- a/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/TabularModule.java +++ b/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/TabularModule.java @@ -17,6 +17,8 @@ import cz.cvut.spipes.modules.exception.SheetIsNotSpecifiedException; import cz.cvut.spipes.modules.exception.SpecificationNonComplianceException; import cz.cvut.spipes.modules.model.*; +import cz.cvut.spipes.modules.tabular.CSVReader; +import cz.cvut.spipes.modules.tabular.TabularReader; import cz.cvut.spipes.modules.util.*; import cz.cvut.spipes.registry.StreamResource; import cz.cvut.spipes.registry.StreamResourceRegistry; @@ -250,12 +252,14 @@ ExecutionContext executeSelf() { try { ICsvListReader listReader = getCsvListReader(csvPreference); - if (listReader == null) { + if (listReader == null) { // TODO we need to detect this situation without need to create list reader logMissingQuoteError(); return getExecutionContext(inputModel, outputModel); } - String[] header = listReader.getHeader(true); // skip the header (can't be used with CsvListReader) + TabularReader tabularReader = new CSVReader(listReader); + + List header = tabularReader.getHeader(); if (header == null) { LOG.warn("Input stream resource {} to provide tabular data is empty.", this.sourceResource.getUri()); @@ -276,7 +280,7 @@ ExecutionContext executeSelf() { em.close(); em.getEntityManagerFactory().close(); - outputColumns = new ArrayList<>(header.length); + outputColumns = new ArrayList<>(header.size()); for (String columnTitle : header) { String columnName = normalize(columnTitle); @@ -321,10 +325,10 @@ ExecutionContext executeSelf() { // an initial subject, the non-core annotation as property, and the // value of the non-core annotation as value. - for (int i = 0; i < header.length; i++) { + for (int i = 0; i < header.size(); i++) { // 4.6.8.1 Column column = outputColumns.get(i); - String cellValue = getValueFromRow(row, i, header.length, rowNumber); + String cellValue = getValueFromRow(row, i, header.size(), rowNumber); if (cellValue != null) rowStatements.add(createRowResource(cellValue, rowNumber, column)); // 4.6.8.2 r.setDescribes(tableSchema.createAboutUrl(rowNumber)); @@ -678,7 +682,8 @@ public void processTableAtIndex(int sheetNumber) { this.processTableAtIndex = sheetNumber; } - private String[] getHeaderFromSchema(Model inputModel, String[] header, boolean hasInputSchema) { + private List getHeaderFromSchema(Model inputModel, final List header, boolean hasInputSchema) { + List headerToReturn = null; if (hasInputSchema) { List orderList = new ArrayList<>(); Resource tableSchemaResource = inputModel.getResource(tableSchema.getUri().toString()); @@ -690,22 +695,22 @@ private String[] getHeaderFromSchema(Model inputModel, String[] header, boolean rdfList.iterator().forEach(rdfNode -> orderList.add(String.valueOf(rdfNode))); tableSchema.setOrderList(orderList); - header = createHeaders(header.length, tableSchema.sortColumns(orderList)); + headerToReturn = createHeader(header.size(), tableSchema.sortColumns(orderList)); } else LOG.info("Order of columns was not provided in the schema."); } else { - header = createHeaders(header.length, new ArrayList<>()); + headerToReturn = createHeader(header.size(), new ArrayList<>()); } - return header; + return headerToReturn; } - private String[] createHeaders(int size, List columns) { - String[] headers = new String[size]; + private List createHeader(int size, List columns) { + List headers = new ArrayList<>(size); for (int i = 0; i < size; i++) { if (!columns.isEmpty()) { - headers[i] = columns.get(i).getName(); - } else headers[i] = "column_" + (i + 1); + headers.set(i, columns.get(i).getName()); + } else headers.set(i, "column_" + (i + 1)); } return headers; } diff --git a/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/tabular/CSVReader.java b/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/tabular/CSVReader.java new file mode 100644 index 00000000..b94cd6ed --- /dev/null +++ b/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/tabular/CSVReader.java @@ -0,0 +1,21 @@ +package cz.cvut.spipes.modules.tabular; + +import org.supercsv.io.ICsvListReader; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +public class CSVReader implements TabularReader { + + ICsvListReader listReader; + + public CSVReader(ICsvListReader listReader) { + this.listReader = listReader; + } + + @Override + public List getHeader() throws IOException { + return Arrays.asList((listReader.getHeader(true))); // skip the header (can't be used with CsvListReader); + } +} diff --git a/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/tabular/ExcelReader.java b/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/tabular/ExcelReader.java new file mode 100644 index 00000000..b589b974 --- /dev/null +++ b/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/tabular/ExcelReader.java @@ -0,0 +1,4 @@ +package cz.cvut.spipes.modules.tabular; + +public class ExcelReader { +} diff --git a/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/tabular/HtmlReader.java b/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/tabular/HtmlReader.java new file mode 100644 index 00000000..8c773b06 --- /dev/null +++ b/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/tabular/HtmlReader.java @@ -0,0 +1,4 @@ +package cz.cvut.spipes.modules.tabular; + +public class HtmlReader { +} diff --git a/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/tabular/TabularReader.java b/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/tabular/TabularReader.java new file mode 100644 index 00000000..920f0688 --- /dev/null +++ b/s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/modules/tabular/TabularReader.java @@ -0,0 +1,8 @@ +package cz.cvut.spipes.modules.tabular; + +import java.io.IOException; +import java.util.List; + +public interface TabularReader { + List getHeader() throws IOException; +}