Skip to content

Commit

Permalink
[Upd] Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
blcham committed Oct 25, 2023
1 parent 0300908 commit a989317
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 33 deletions.
9 changes: 6 additions & 3 deletions s-pipes-core/src/main/resources/config-core.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
audit.resourcesPath=/scripts/.spipes
audit.resourcesPath=/home/blcha/projects/19kodi/sforms/.spipes
audit.enable=true
contexts.scriptPaths=/scripts
contextsLoader.data.keepUpdated=false
contexts.scriptPaths=/home/blcha/projects/kbss/git/s-pipes-modules;/home/blcha/projects/23ava/git/ava-model/data;
contextsLoader.data.keepUpdated=true
execution.exitOnError=false
execution.checkValidationConstraints=true
execution.configUrl=
execution.environment=development
execution.developmentServiceUrl=http://localhost:8080/s-pipes
compatibility.loadSparqlMotionFiles=false
#execution.tempDirectoryPath=
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
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.util.BNodesTransformer;
import cz.cvut.spipes.modules.util.HTML2TSVConvertor;
import cz.cvut.spipes.modules.util.JopaPersistenceUtils;
import cz.cvut.spipes.modules.util.XLS2TSVConvertor;
import cz.cvut.spipes.modules.util.*;
import cz.cvut.spipes.registry.StreamResource;
import cz.cvut.spipes.registry.StreamResourceRegistry;
import cz.cvut.spipes.util.JenaUtils;
Expand Down Expand Up @@ -197,11 +194,12 @@ ExecutionContext executeSelf() {
table = onTable(null);

StreamResource originalSourceResource = sourceResource;
TSVConvertor tsvConvertor = null;

switch (sourceResourceFormat) {
case HTML:
HTML2TSVConvertor htmlConvertor = new HTML2TSVConvertor();
setSourceResource(htmlConvertor.convertToTSV(sourceResource));
tsvConvertor = new HTML2TSVConvertor();
setSourceResource(tsvConvertor.convertToTSV(sourceResource));
setDelimiter('\t');
break;
case XLS:
Expand All @@ -210,18 +208,18 @@ ExecutionContext executeSelf() {
if (processSpecificSheetInXLSFile == 0) {
throw new SheetIsNotSpecifiedException("Source resource format is set to XLS file but no specific sheet is set for processing.");
}
XLS2TSVConvertor xlsConvertor = new XLS2TSVConvertor();
int numberOfSheets = xlsConvertor.getNumberOfSheets(sourceResource, sourceResourceFormat);
table.setLabel(xlsConvertor.getSheetName(sourceResource, processSpecificSheetInXLSFile, sourceResourceFormat));
LOG.debug("Number of sheets:{}", numberOfSheets);
tsvConvertor = new XLS2TSVConvertor(processSpecificSheetInXLSFile, sourceResourceFormat);
int numberOfSheets = tsvConvertor.getNumberTables(sourceResource);
table.setLabel(tsvConvertor.getTableName(sourceResource));
LOG.debug("Number of sheets: {}", numberOfSheets);
if ((processSpecificSheetInXLSFile > numberOfSheets) || (processSpecificSheetInXLSFile < 1)) {
LOG.error("Requested sheet doesn't exist, number of sheets in the doc: {}, requested sheet: {}",
numberOfSheets,
processSpecificSheetInXLSFile
);
throw new SheetDoesntExistsException("Requested sheet doesn't exists");
throw new SheetDoesntExistsException("Requested sheet doesn't exists.");
}
setSourceResource(xlsConvertor.convertToTSV(sourceResource, processSpecificSheetInXLSFile, sourceResourceFormat));
setSourceResource(tsvConvertor.convertToTSV(sourceResource));
setDelimiter('\t');
break;
}
Expand Down Expand Up @@ -343,18 +341,9 @@ ExecutionContext executeSelf() {
em.persist(tableGroup);
em.merge(tableSchema);

if(sourceResourceFormat == ResourceFormat.XLS || sourceResourceFormat == ResourceFormat.XLSM || sourceResourceFormat == ResourceFormat.XLSX || sourceResourceFormat == ResourceFormat.HTML) {
List<Region> regions = new ArrayList<>();
switch (sourceResourceFormat) {
case HTML:
HTML2TSVConvertor html2TSVConvertor = new HTML2TSVConvertor();
regions = html2TSVConvertor.getMergedRegions(originalSourceResource);
break;
default:
XLS2TSVConvertor xls2TSVConvertor = new XLS2TSVConvertor();
regions = xls2TSVConvertor.getMergedRegions(originalSourceResource, processSpecificSheetInXLSFile, sourceResourceFormat);
break;
}
if (tsvConvertor != null) {
List<Region> regions = tsvConvertor.getMergedRegions(originalSourceResource);

int cellsNum = 1;
for (Region region : regions) {
int firstCellInRegionNum = cellsNum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
* </tr>
* </table
*/
public class HTML2TSVConvertor {
public class HTML2TSVConvertor implements TSVConvertor {

@Override
public StringStreamResource convertToTSV(StreamResource streamResource) {
StringBuilder tsvStringBuilder = new StringBuilder();

Expand Down Expand Up @@ -61,6 +62,7 @@ private void processTag(Element row, StringBuilder sb, String tag) {
}
}

@Override
public List<Region> getMergedRegions(StreamResource streamResource){
List<Region> list = new ArrayList<>();
Document doc = Jsoup.parseBodyFragment(new String(streamResource.getContent()));
Expand Down Expand Up @@ -91,6 +93,16 @@ public List<Region> getMergedRegions(StreamResource streamResource){
return list;
}

@Override
public int getNumberTables(StreamResource streamResource) {
throw new UnsupportedOperationException("Not implemented yet.");
}

@Override
public String getTableName(StreamResource streamResource) {
throw new UnsupportedOperationException("Not implemented yet.");
}

int parseInt(String s,int defaultValue){
int res = 0;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cz.cvut.spipes.modules.util;

import cz.cvut.spipes.modules.model.Region;
import cz.cvut.spipes.registry.StreamResource;
import cz.cvut.spipes.registry.StringStreamResource;

import java.util.List;

public interface TSVConvertor {

StringStreamResource convertToTSV(StreamResource streamResource);

List<Region> getMergedRegions(StreamResource streamResource);

int getNumberTables(StreamResource streamResource);

String getTableName(StreamResource streamResource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@
/**
* Module for converting tabular data from XLS to TSV. Converts specific sheet of the xls file.
*/
public class XLS2TSVConvertor {
public class XLS2TSVConvertor implements TSVConvertor {

private static final Logger LOG = LoggerFactory.getLogger(XLS2TSVConvertor.class);
private int sheetNumber;
private ResourceFormat format;

public StringStreamResource convertToTSV(StreamResource streamResource,int sheetNumber, ResourceFormat format){
public XLS2TSVConvertor(int sheetNumber, ResourceFormat format) {
this.sheetNumber = sheetNumber;
this.format = format;
}

@Override
public StringStreamResource convertToTSV(StreamResource streamResource){
try {
Workbook workbook;
if(format == ResourceFormat.XLS)workbook = new HSSFWorkbook(new ByteArrayInputStream(streamResource.getContent()));
Expand All @@ -53,7 +61,8 @@ public StringStreamResource convertToTSV(StreamResource streamResource,int sheet
}
}

public List<Region> getMergedRegions(StreamResource streamResource, int sheetNumber, ResourceFormat format){
@Override
public List<Region> getMergedRegions(StreamResource streamResource){
Workbook workbook;
List<Region> list = new ArrayList<>();
try {
Expand All @@ -76,7 +85,8 @@ public List<Region> getMergedRegions(StreamResource streamResource, int sheetNum
return list;
}

public int getNumberOfSheets(StreamResource streamResource, ResourceFormat format){
@Override
public int getNumberTables(StreamResource streamResource){
try {
if(format == ResourceFormat.XLS)return new HSSFWorkbook(new ByteArrayInputStream(streamResource.getContent())).getNumberOfSheets();
else return new XSSFWorkbook(new ByteArrayInputStream(streamResource.getContent())).getNumberOfSheets();
Expand All @@ -85,7 +95,8 @@ public int getNumberOfSheets(StreamResource streamResource, ResourceFormat forma
}
}

public String getSheetName(StreamResource streamResource,int sheetNumber, ResourceFormat format){
@Override
public String getTableName(StreamResource streamResource){
try {
Workbook workbook;
if(format == ResourceFormat.XLS)workbook = new HSSFWorkbook(new ByteArrayInputStream(streamResource.getContent()));
Expand Down

0 comments on commit a989317

Please sign in to comment.