Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified RecordsFactory to allow passing source data as InputStream, … #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/be/ugent/rml/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ public static boolean isRemoteFile(String location) {
return location.startsWith("https://") || location.startsWith("http://");
}

public static boolean isLabeledFile(String location) {
return location.startsWith("label://");
}

public static List<Term> getSubjectsFromQuads(List<Quad> quads) {
ArrayList<Term> subjects = new ArrayList<>();

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/be/ugent/rml/records/CSVW.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ private String getContentType() {
this.is = Utils.getInputStreamFromLocation(path, new File(cwd), getContentType());
}

CSVW(InputStream inputStream) throws IOException {
this.is = inputStream;
}

private String helper(String term) {
List<Term> terms = Utils.getObjectsFromQuads(this.rmlStore.getQuads(this.dialect, new NamedNode(NAMESPACES.CSVW + term), null));
if (!terms.isEmpty()) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/be/ugent/rml/records/IteratorFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public List<Record> get(String location, String iterator, String cwd) throws IOE
return _get(stream, iterator);
}

public List<Record> get(InputStream stream, String iterator) throws IOException {
return _get(stream, iterator);
}

abstract List<Record> _get(InputStream stream, String iterator) throws IOException;

abstract String getContentType();
Expand Down
44 changes: 38 additions & 6 deletions src/main/java/be/ugent/rml/records/RecordsFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.commons.lang.NotImplementedException;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class RecordsFactory {
Expand All @@ -21,16 +22,32 @@ public class RecordsFactory {
private Map<String, Map<String, List<Record>>> allXMLRecords;
private Map<String, Map<Integer, List<Record>>> allRDBsRecords;
private Map<String, Map<Integer, List<Record>>> allSPARQLRecords;
private Map<String, InputStream> labeledFilesMap;

public RecordsFactory(DataFetcher dataFetcher) {
this.dataFetcher = dataFetcher;
private void Init() {
allCSVRecords = new HashMap<>();
allJSONRecords = new HashMap<>();
allXMLRecords = new HashMap<>();
allRDBsRecords = new HashMap<>();
allSPARQLRecords = new HashMap<>();
}

public RecordsFactory(DataFetcher dataFetcher) {
this.dataFetcher = dataFetcher;
this.Init();
}

public RecordsFactory(Map<String, InputStream> labeledFilesMap) {
this.labeledFilesMap = labeledFilesMap;
this.Init();
}

public RecordsFactory(DataFetcher dataFetcher, Map<String, InputStream> streamsMap) {
this.dataFetcher = dataFetcher;
this.labeledFilesMap = streamsMap;
this.Init();
}

public List<Record> createRecords(Term triplesMap, QuadStore rmlStore) throws IOException {
//get logical source
List<Term> logicalSources = Utils.getObjectsFromQuads(rmlStore.getQuads(triplesMap, new NamedNode(NAMESPACES.RML + "logicalSource"), null));
Expand Down Expand Up @@ -120,7 +137,12 @@ private List<Record> getCSVRecords(String source) throws IOException {
return allCSVRecords.get(source);
} else {
try {
CSVW CSVW = new CSVW(source, dataFetcher.getCwd());
CSVW CSVW;
if(Utils.isLabeledFile(source)) {
CSVW = new CSVW(this.labeledFilesMap.get(source));
} else {
CSVW = new CSVW(source, dataFetcher.getCwd());
}
allCSVRecords.put(source, CSVW.getRecords());
} catch (IOException e) {
throw e;
Expand Down Expand Up @@ -185,7 +207,12 @@ private List<Record> getXMLRecords(String source, List<Term> iterators, Term tri
} else {
try {
XML xml = new XML();
List<Record> records = xml.get(source, iterator, dataFetcher.getCwd());
List<Record> records = new ArrayList<>();
if(Utils.isLabeledFile(source)) {
records = xml.get(this.labeledFilesMap.get(source), iterator);
} else {
records = xml.get(source, iterator, dataFetcher.getCwd());
}

if (allXMLRecords.containsKey(source)) {
allXMLRecords.get(source).put(iterator, records);
Expand Down Expand Up @@ -213,8 +240,13 @@ private List<Record> getJSONRecords(String source, List<Term> iterators, Term tr
return allJSONRecords.get(source).get(iterator);
} else {
try {
JSON json = new JSON();
List<Record> records = json.get(source, iterator, dataFetcher.getCwd());
JSON json = new JSON();
List<Record> records = new ArrayList<>();
if(Utils.isLabeledFile(source)) {
records = json.get(this.labeledFilesMap.get(source), iterator);
} else {
records = json.get(source, iterator, dataFetcher.getCwd());
}

if (allJSONRecords.containsKey(source)) {
allJSONRecords.get(source).put(iterator, records);
Expand Down