Skip to content

Commit

Permalink
Replace hand-written CSV parser with Apache Commons CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
dcbriccetti authored and lognaturel committed Sep 15, 2021
1 parent b74d9b4 commit fae6c7c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 37 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ dependencies {
// Be sure to update dependencies in pom.xml to match
implementation 'joda-time:joda-time:2.10.10'
implementation 'org.slf4j:slf4j-api:1.7.31'
// Upgrade to version higher than 1.4 when Collect minSDK >= 26
implementation 'org.apache.commons:commons-csv:1.4'
compileOnly 'net.sf.kxml:kxml2:2.3.0'
testImplementation 'ch.qos.logback:logback-classic:1.2.3'
testImplementation 'junit:junit:4.13.2'
Expand Down
Binary file modified javarosa-android-api-level-checker.zip
Binary file not shown.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.31</version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
package org.javarosa.core.model.instance;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.javarosa.core.model.data.UncastData;

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

try (BufferedReader br = new BufferedReader(new FileReader(path))) {
String csvLine = br.readLine();
final CSVParser csvParser = CSVParser.parse(new File(path), StandardCharsets.UTF_8, CSVFormat.DEFAULT.withFirstRecordAsHeader());
final String[] fieldNames = csvParser.getHeaderMap().keySet().toArray(new String[0]);
int multiplicity = 0;

if (csvLine != null) {
String[] fieldNames = csvLine.split(",");
int multiplicity = 0;
for (CSVRecord csvRecord : csvParser.getRecords()) {
TreeElement item = new TreeElement("item", multiplicity);

while ((csvLine = br.readLine()) != null) {
TreeElement item = new TreeElement("item", multiplicity);
String[] data = csvLine.split(",");
for (int i = 0; i < fieldNames.length; ++i) {
TreeElement field = new TreeElement(fieldNames[i], 0);
field.setValue(new UncastData(i < data.length ? data[i] : ""));

item.addChild(field);
}

root.addChild(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++;
}

return root;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
package org.javarosa.core.model.instance;

import org.junit.Test;

import java.io.IOException;

import static org.javarosa.test.utils.ResourcePathHelper.r;
import static org.junit.Assert.assertEquals;

import java.io.IOException;
import org.junit.Before;
import org.junit.Test;

public class CsvExternalInstanceTest {
private TreeElement root;

@Before
public void setUp() throws IOException {
root = CsvExternalInstance.parse("id", r("non_trivial.csv").toString());
}

@Test
public void heading_has_no_extra_quotes() throws IOException {
TreeElement root = CsvExternalInstance.parse("id", r("non_trivial.csv").toString());
public void heading_has_no_extra_quotes() {
assertEquals("label", root.getChildAt(0).getChildAt(0).getName());
}

@Test
public void value_has_no_extra_quotes() throws IOException {
TreeElement root = CsvExternalInstance.parse("id", r("non_trivial.csv").toString());
public void value_has_no_extra_quotes() {
assertEquals("A", root.getChildAt(0).getChildAt(0).getValue().getValue());
}

@Test
public void quoted_string_with_comma() {
assertEquals("121 Main St, NE", root.getChildAt(6).getChildAt(0).getValue().getValue());
}

@Test
public void missing_fields_replaced_with_spaces() {
for (int fieldIndex = 1; fieldIndex < 2; fieldIndex++) {
assertEquals("", root.getChildAt(5).getChildAt(fieldIndex).getValue().getValue());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
"label","name","first"
"A","a",
B,b,
C,c,
C,c
AA,aa,a
AB,ab,a
AC,ac,a
BA,ba,b
BB,bb,b
BC,bc,b
CA,ca,c
CB,cb,c
CC,cc,c
AC
"121 Main St, NE", main, m

0 comments on commit fae6c7c

Please sign in to comment.