-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
47 additions
and
10 deletions.
There are no files selected for viewing
15 changes: 5 additions & 10 deletions
15
src/main/java/org/javarosa/core/model/instance/CsvExternalInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/org/javarosa/core/model/instance/SecondaryInstanceCSVParserBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |