Skip to content
Snippets Groups Projects
Commit 63ffe1e7 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Replaced read methods with wrappers for opencsv code.

parent 13c3fd1e
Branches
Tags 0.1.0
No related merge requests found
...@@ -3,6 +3,10 @@ ...@@ -3,6 +3,10 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<groupId>edu.unl.cse.csv_io</groupId> <groupId>edu.unl.cse.csv_io</groupId>
<artifactId>csv_io</artifactId> <artifactId>csv_io</artifactId>
...@@ -41,6 +45,11 @@ ...@@ -41,6 +45,11 @@
<version>4.12</version> <version>4.12</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.0</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -2,18 +2,57 @@ ...@@ -2,18 +2,57 @@
* CSV Reader/Writer, copyright (c) 2019 Christopher A. Bohn, bohn@unl.edu. * CSV Reader/Writer, copyright (c) 2019 Christopher A. Bohn, bohn@unl.edu.
*/ */
package edu.unl.cse.csv_io; package edu.unl.cse.csv_io;
import com.opencsv.CSVReaderHeaderAware;
import com.opencsv.CSVReaderHeaderAwareBuilder;
import com.opencsv.exceptions.CsvValidationException;
import java.io.*; import java.io.*;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.*;
public class CSVReaderWriter { public class CSVReaderWriter {
// this does not (yet) handle data elements that include commas and/or are surrounded by quotation marks @SuppressWarnings("WeakerAccess")
private static final String DELIMTER = ","; public static Set<Map<String, String>> readCSVasSet(String filename) {
private static final char BYTE_ORDER_MARK = '\ufeff'; Set<Map<String, String>> csvSet = null;
try (InputStreamReader inputStreamReader = new InputStreamReader(Objects.requireNonNull(
CSVReaderWriter.class.getClassLoader().getResourceAsStream("csv/" + filename)))) {
csvSet = (Set<Map<String, String>>) parseCSV(inputStreamReader, new HashSet<>());
} catch (NullPointerException nullPointerException) {
System.err.println("Check spelling of file " + filename + ".");
System.exit(1);
} catch (IOException ioException) {
System.err.println("Error loading " + filename + ". " + ioException);
System.exit(1);
} catch (CsvValidationException csvValidationException) {
System.err.println("Could not validate a line in " + filename + ". " + csvValidationException);
System.exit(1);
}
return csvSet;
}
private static Collection<Map<String, String>> parseCSV(InputStreamReader inputStreamReader,
Collection<Map<String, String>> destination)
throws IOException, CsvValidationException {
Map<String, String> line;
try {
CSVReaderHeaderAware csvReader = new CSVReaderHeaderAwareBuilder(inputStreamReader).build();
while ((line = csvReader.readMap()) != null) {
destination.add(line);
}
} catch (NullPointerException ignored) {
// CSVReaderHeaderAwareBuilder.build() throws NullPointerException if inputStreamReader is empty
}
return destination;
}
/* LEGACY METHODS */
@SuppressWarnings("WeakerAccess")
public static Set<Map<String, String>> readCSV(String filename) {
return readCSVasSet(filename);
}
/*
public static Set<Map<String, String>> readCSV(String filename) { public static Set<Map<String, String>> readCSV(String filename) {
Set<Map<String, String>> csvSet = null; Set<Map<String, String>> csvSet = null;
ClassLoader classLoader = CSVReaderWriter.class.getClassLoader(); ClassLoader classLoader = CSVReaderWriter.class.getClassLoader();
...@@ -24,6 +63,7 @@ public class CSVReaderWriter { ...@@ -24,6 +63,7 @@ public class CSVReaderWriter {
} }
return csvSet; return csvSet;
} }
*/
public static boolean writeCSV(String filename, Set<Map<String, String>> data) { public static boolean writeCSV(String filename, Set<Map<String, String>> data) {
boolean wroteFile = true; boolean wroteFile = true;
...@@ -47,6 +87,16 @@ public class CSVReaderWriter { ...@@ -47,6 +87,16 @@ public class CSVReaderWriter {
return wroteFile; return wroteFile;
} }
static Set<Map<String, String>> parseCSV(InputStream inputStream) throws IOException {
try {
return (Set<Map<String, String>>) parseCSV(new InputStreamReader(inputStream), new HashSet<>());
} catch (CsvValidationException csvValidationException) {
csvValidationException.printStackTrace();
return null;
}
}
/*
static Set<Map<String, String>> parseCSV(InputStream inputStream) throws IOException { static Set<Map<String, String>> parseCSV(InputStream inputStream) throws IOException {
Set<Map<String, String>> csvSet = new HashSet<>(); Set<Map<String, String>> csvSet = new HashSet<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
...@@ -57,7 +107,8 @@ public class CSVReaderWriter { ...@@ -57,7 +107,8 @@ public class CSVReaderWriter {
String[] elements = line.split(DELIMTER); String[] elements = line.split(DELIMTER);
if (header) { if (header) {
if (elements[0].charAt(0) == BYTE_ORDER_MARK) { if (elements[0].charAt(0) == BYTE_ORDER_MARK) {
elements[0] = elements[0].substring(1); // remove text-encoding character sometimes added by Excel elements[0] = elements[0].substring(1); // remove text-encoding character sometimes added by
// Excel
} }
fieldNames.addAll(Arrays.asList(elements)); fieldNames.addAll(Arrays.asList(elements));
header = false; header = false;
...@@ -72,6 +123,7 @@ public class CSVReaderWriter { ...@@ -72,6 +123,7 @@ public class CSVReaderWriter {
} }
return csvSet; return csvSet;
} }
*/
static void placeCSVonStream(Set<Map<String, String>> data, OutputStream outputStream) { static void placeCSVonStream(Set<Map<String, String>> data, OutputStream outputStream) {
PrintStream writer = new PrintStream(outputStream); PrintStream writer = new PrintStream(outputStream);
...@@ -103,4 +155,34 @@ public class CSVReaderWriter { ...@@ -103,4 +155,34 @@ public class CSVReaderWriter {
System.out.println(success ? "Wrote file!" : "Didn't write file"); System.out.println(success ? "Wrote file!" : "Didn't write file");
} }
*/ */
/*
public static void main(String[] args) {
Set<Map<String,String>> students = readCSV("students.csv");
for (Map<String,String> student: students) {
System.out.println(student.get("Name"));
}
}
*//*
public static void main(String[] args) {
Set<Map<String, String>> courses = readCSV("courses.csv");
for (Map<String, String> course : courses) {
String foo = course.get("URL");
if (foo == null) {
foo = "Null object";
} else if (foo.equals("")) foo = "Empty String";
System.out.println(course.get("CourseID") + " " + course.get("Section") + " " + foo + " " + course.get(
"Prerequisite1"));
}
}*/
public static void main(String[] args) {
Set<Map<String, String>> courses = readCSV("empty.csv");
for (Map<String, String> course : courses) {
String foo = course.get("URL");
if (foo == null) {
foo = "Null object";
} else if (foo.equals("")) foo = "Empty String";
System.out.println(course.get("CourseID") + " " + course.get("Section") + " " + foo + " " + course.get(
"Prerequisite1"));
}
}
} }
...@@ -10,6 +10,7 @@ import java.util.*; ...@@ -10,6 +10,7 @@ import java.util.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
@SuppressWarnings("unchecked") // pee-yew!
public class CSVReaderWriterTest { public class CSVReaderWriterTest {
private static InputStream inputStream; private static InputStream inputStream;
private static OutputStream outputStream; private static OutputStream outputStream;
...@@ -62,6 +63,7 @@ public class CSVReaderWriterTest { ...@@ -62,6 +63,7 @@ public class CSVReaderWriterTest {
expectedRow.put(headers[i], row[i]); expectedRow.put(headers[i], row[i]);
} }
// Compare // Compare
assertNotNull(result);
Map<String, String> aRow = (Map<String, String>) result.toArray()[0]; Map<String, String> aRow = (Map<String, String>) result.toArray()[0];
assertEquals(expectedKeys, aRow.keySet()); assertEquals(expectedKeys, aRow.keySet());
assertTrue(result.contains(expectedRow)); assertTrue(result.contains(expectedRow));
...@@ -86,12 +88,13 @@ public class CSVReaderWriterTest { ...@@ -86,12 +88,13 @@ public class CSVReaderWriterTest {
// Oracle -- Rows // Oracle -- Rows
Map<String, String>[] expectedRows = new Map[2]; Map<String, String>[] expectedRows = new Map[2];
for (int i = 0; i < expectedRows.length; i++) { for (int i = 0; i < expectedRows.length; i++) {
expectedRows[i] = new HashMap<String, String>(); expectedRows[i] = new HashMap<>();
for (int j = 0; j < headers.length; j++) { for (int j = 0; j < headers.length; j++) {
expectedRows[i].put(headers[j], rows[i][j]); expectedRows[i].put(headers[j], rows[i][j]);
} }
} }
// Compare // Compare
assertNotNull(result);
Map<String, String> aRow = (Map<String, String>) result.toArray()[0]; Map<String, String> aRow = (Map<String, String>) result.toArray()[0];
assertEquals(expectedKeys, aRow.keySet()); assertEquals(expectedKeys, aRow.keySet());
for (Map<String, String> expectedRow : expectedRows) { for (Map<String, String> expectedRow : expectedRows) {
...@@ -100,7 +103,7 @@ public class CSVReaderWriterTest { ...@@ -100,7 +103,7 @@ public class CSVReaderWriterTest {
} }
@Test @Test
public void testParsingCSVwithOnlyHeaderRow() { public void testParsingCSVWithOnlyHeaderRow() {
// Input // Input
String[] headers = {"header1", "header2"}; String[] headers = {"header1", "header2"};
String[][] data = {headers}; String[][] data = {headers};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment