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

Expose secondary instance CSV parsing #791

Merged
merged 2 commits into from
Aug 16, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
package org.javarosa.core.model.instance;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.input.BOMInputStream;
import org.javarosa.core.model.data.UncastData;
import org.javarosa.xform.parse.ExternalInstanceParser;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

public class CsvExternalInstance implements ExternalInstanceParser.FileInstanceParser {

public TreeElement parse(@NotNull String instanceId, @NotNull String path) throws IOException {
final TreeElement root = new TreeElement("root", 0);
root.setInstanceName(instanceId);

final CSVFormat csvFormat = CSVFormat.DEFAULT
.withDelimiter(getDelimiter(path))
.withFirstRecordAsHeader();
Reader reader = new InputStreamReader(new BOMInputStream(new FileInputStream(path)));
final CSVParser csvParser = new CSVParser(reader, csvFormat);
final String[] fieldNames = csvParser.getHeaderMap().keySet().toArray(new String[0]);
int multiplicity = 0;

for (CSVRecord csvRecord : csvParser.getRecords()) {
TreeElement item = new TreeElement("item", multiplicity);

for (int i = 0; i < fieldNames.length; ++i) {
TreeElement field = new TreeElement(fieldNames[i], 0);
field.setValue(new UncastData(i < csvRecord.size() ? csvRecord.get(i) : ""));
item.addChild(field);
try (
final CSVParser csvParser = new SecondaryInstanceCSVParserBuilder()
.path(path)
.build()
) {
final String[] fieldNames = csvParser.getHeaderMap().keySet().toArray(new String[0]);
int multiplicity = 0;

for (CSVRecord csvRecord : csvParser) {
TreeElement item = new TreeElement("item", multiplicity);

for (int i = 0; i < fieldNames.length; ++i) {
TreeElement field = new TreeElement(fieldNames[i], 0);
field.setValue(new UncastData(i < csvRecord.size() ? csvRecord.get(i) : ""));
item.addChild(field);
}

root.addChild(item);
multiplicity++;
}

root.addChild(item);
multiplicity++;
return root;
}

return root;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.javarosa.core.model.instance;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.io.input.BOMInputStream;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

public class SecondaryInstanceCSVParserBuilder {

private String path;

public SecondaryInstanceCSVParserBuilder path(String path) {
this.path = path;
return this;
}

public CSVParser build() throws IOException {
final CSVFormat csvFormat = CSVFormat.DEFAULT
.withDelimiter(getDelimiter(path))
.withFirstRecordAsHeader();
Reader reader = new InputStreamReader(new BOMInputStream(new FileInputStream(path)));
return new CSVParser(reader, csvFormat);
}

private static char getDelimiter(String path) throws IOException {
char delimiter = ',';
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
String header = reader.readLine();

if (header.contains(";")) {
delimiter = ';';
}
}
return delimiter;
}
}