Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
blcham committed Jan 6, 2025
1 parent 047fec9 commit 7ad0f2d
Showing 1 changed file with 50 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import cz.cvut.spipes.exception.ResourceNotUniqueException;
import cz.cvut.spipes.exception.SPipesException;
import cz.cvut.spipes.modules.annotations.SPipesModule;
import cz.cvut.spipes.modules.exception.SheetDoesntExistsException;
import cz.cvut.spipes.modules.exception.SheetIsNotSpecifiedException;
import cz.cvut.spipes.modules.exception.SpecificationNonComplianceException;
import cz.cvut.spipes.modules.handlers.ModeHandler;
Expand All @@ -31,6 +30,7 @@
import org.supercsv.io.ICsvListReader;
import org.supercsv.prefs.CsvPreference;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
Expand Down Expand Up @@ -113,12 +113,12 @@ public class TabularModule extends AnnotatedAbstractModule {
private final Property P_PROCESS_TABLE_AT_INDEX = getSpecificParameter("process-table-at-index");

@Parameter(iri = SML.replace, comment = "Specifies whether a module should overwrite triples" +
" from its predecessors. When set to true (default is false), it prevents" +
" passing through triples from the predecessors.")
" from its predecessors. When set to true (default is false), it prevents" +
" passing through triples from the predecessors.")
private boolean isReplace = false;

@Parameter(iri = PARAM_URL_PREFIX + "source-resource-uri", comment = "URI of resource" +
" that represent tabular data (e.g. resource representing CSV file).")
" that represent tabular data (e.g. resource representing CSV file).")
private StreamResource sourceResource;

@Parameter(iri = PARAM_URL_PREFIX + "delimiter", comment = "Column delimiter. Default value is comma ','.")
Expand Down Expand Up @@ -281,9 +281,9 @@ ExecutionContext executeSelf() {

tableSchema.setAboutUrl(schemaColumn, sourceResource.getUri());
schemaColumn.setProperty(
dataPrefix,
sourceResource.getUri(),
hasInputSchema ? tableSchema.getColumn(columnName) : null);
dataPrefix,
sourceResource.getUri(),
hasInputSchema ? tableSchema.getColumn(columnName) : null);
schemaColumn.setTitle(columnTitle);
if (isDuplicate) throwNotUniqueException(schemaColumn, columnTitle, columnName);
}
Expand Down Expand Up @@ -337,24 +337,25 @@ ExecutionContext executeSelf() {
tableSchema.adjustProperties(hasInputSchema, outputColumns, sourceResource.getUri());
tableSchema.setColumnsSet(new HashSet<>(outputColumns));

em = JopaPersistenceUtils.getEntityManager("cz.cvut.spipes.modules.model", outputModel);
em.getTransaction().begin();
em.persist(tableGroup);
em.merge(tableSchema);
em = JopaPersistenceUtils.getEntityManager("cz.cvut.spipes.modules.model", outputModel);
em.getTransaction().begin();
em.persist(tableGroup);
em.merge(tableSchema);

if (tsvConvertor != null) {
List<Region> regions = tsvConvertor.getMergedRegions(originalSourceResource);

int cellsNum = 1;
for (Region region : regions) {
int firstCellInRegionNum = cellsNum;
for(int i = region.getFirstRow();i <= region.getLastRow();i++){
for(int j = region.getFirstColumn();j <= region.getLastColumn();j++) {
Cell cell = new Cell(sourceResource.getUri()+"#cell"+(cellsNum));
for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) {
for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
Cell cell = new Cell(sourceResource.getUri() + "#cell" + cellsNum);
cell.setRow(tableSchema.createAboutUrl(i));
cell.setColumn(outputColumns.get(j).getUri().toString());
if(cellsNum != firstCellInRegionNum)
cell.setSameValueAsCell(sourceResource.getUri()+"#cell"+(firstCellInRegionNum));
if (cellsNum != firstCellInRegionNum) {
cell.setSameValueAsCell(sourceResource.getUri() + "#cell" + firstCellInRegionNum);
}
em.merge(cell);
cellsNum++;
}
Expand All @@ -381,19 +382,19 @@ private String getValueFromRow(List<String> row, int index, int expectedRowLengt
StringBuilder record = new StringBuilder(recordDelimiter);
for (int i = 0; i < row.size(); i++) {
record
.append(i)
.append(":")
.append(row.get(i))
.append(recordDelimiter);
.append(i)
.append(":")
.append(row.get(i))
.append(recordDelimiter);
}
LOG.error("Reading input file failed when reading record #{} (may not reflect the line #).\n" +
" It was expected that the current record contains {} values" +
", but {}. element was not retrieved before whole record was processed.\n" +
"The problematic record: {}",
currentRecordNumber,
expectedRowLength,
index+1,
record
" It was expected that the current record contains {} values" +
", but {}. element was not retrieved before whole record was processed.\n" +
"The problematic record: {}",
currentRecordNumber,
expectedRowLength,
index+1,
record
);
throw new SPipesException("Reading input file failed.", e);
}
Expand All @@ -413,9 +414,9 @@ private Statement createRowResource(String cellValue, int rowNumber, Column colu
Resource rowResource = ResourceFactory.createResource(tableSchema.createAboutUrl(rowNumber));

return ResourceFactory.createStatement(
rowResource,
ResourceFactory.createProperty(column.getPropertyUrl()),
ResourceFactory.createPlainLiteral(cellValue));
rowResource,
ResourceFactory.createProperty(column.getPropertyUrl()),
ResourceFactory.createPlainLiteral(cellValue));
}

private boolean hasInputSchema(TableSchema inputTableSchema) {
Expand All @@ -429,11 +430,11 @@ private boolean hasInputSchema(TableSchema inputTableSchema) {

private TableSchema getTableSchema(EntityManager em) {
TypedQuery<TableSchema> query = em.createNativeQuery(
"PREFIX csvw: <http://www.w3.org/ns/csvw#>\n" +
"SELECT ?t WHERE { \n" +
"?t a csvw:TableSchema. \n" +
"}",
TableSchema.class
"PREFIX csvw: <http://www.w3.org/ns/csvw#>\n" +
"SELECT ?t WHERE { \n" +
"?t a csvw:TableSchema. \n" +
"}",
TableSchema.class
);

int tableSchemaCount = query.getResultList().size();
Expand All @@ -452,14 +453,14 @@ private TableSchema getTableSchema(EntityManager em) {

private void throwNotUniqueException(Column column, String columnTitle, String columnName) {
throw new ResourceNotUniqueException(
String.format("Unable to create value of property %s due to collision. " +
"Both column titles '%s' and '%s' are normalized to '%s' " +
"and thus would refer to the same property url <%s>.",
CSVW.propertyUrl,
columnTitle,
column.getTitle(),
columnName,
column.getPropertyUrl()));
String.format("Unable to create value of property %s due to collision. " +
"Both column titles '%s' and '%s' are normalized to '%s' " +
"and thus would refer to the same property url <%s>.",
CSVW.propertyUrl,
columnTitle,
column.getTitle(),
columnName,
column.getPropertyUrl()));
}

private ExecutionContext getExecutionContext(Model inputModel, Model outputModel) {
Expand All @@ -473,12 +474,12 @@ private ExecutionContext getExecutionContext(Model inputModel, Model outputModel
@Override
public void loadManualConfiguration() {
sourceResourceFormat = ResourceFormat.fromString(
getPropertyValue(P_SOURCE_RESOURCE_FORMAT, ResourceFormat.PLAIN.getValue())
getPropertyValue(P_SOURCE_RESOURCE_FORMAT, ResourceFormat.PLAIN.getValue())
);
delimiter = getPropertyValue(P_DELIMITER, getDefaultDelimiterSupplier(sourceResourceFormat));
quoteCharacter = getPropertyValue(P_QUOTE_CHARACTER, getDefaultQuoteCharacterSupplier(sourceResourceFormat));
outputMode = Mode.fromResource(
getPropertyValue(P_OUTPUT_MODE, Mode.STANDARD.getResource())
getPropertyValue(P_OUTPUT_MODE, Mode.STANDARD.getResource())
);
setInputCharset(delimiter);
}
Expand Down Expand Up @@ -514,7 +515,7 @@ private Supplier<Character> getDefaultQuoteCharacterSupplier(ResourceFormat sour
if (sourceResourceFormat == ResourceFormat.CSV) {
return () -> {
LOG.debug("Quote character not specified, using double-quote as default value" +
" to be compliant with RFC 4180 (CSV)");
" to be compliant with RFC 4180 (CSV)");
return '"';
};
}
Expand All @@ -524,8 +525,8 @@ private Supplier<Character> getDefaultQuoteCharacterSupplier(ResourceFormat sour
private char getPropertyValue(Property property,
Supplier<Character> defaultValueSupplier) {
return Optional.ofNullable(getPropertyValue(property))
.map(n -> n.asLiteral().getChar())
.orElseGet(defaultValueSupplier);
.map(n -> n.asLiteral().getChar())
.orElseGet(defaultValueSupplier);
}

@Override
Expand Down Expand Up @@ -624,7 +625,7 @@ public int getDelimiter() {

public void setDelimiter(int delimiter) {
if ((sourceResourceFormat == ResourceFormat.CSV && delimiter != ',') ||
(sourceResourceFormat == ResourceFormat.TSV && delimiter != '\t')) {
(sourceResourceFormat == ResourceFormat.TSV && delimiter != '\t')) {
throw new SpecificationNonComplianceException(sourceResourceFormat, delimiter);
}
this.delimiter = delimiter;
Expand Down

0 comments on commit 7ad0f2d

Please sign in to comment.