diff --git a/src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java b/src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java index 8caef8977e8999383e5dac1444ff77494d1e4552..593cc946488fc60acee28698abab89e1411bc763 100644 --- a/src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java +++ b/src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java @@ -14,6 +14,7 @@ import java.util.*; @SuppressWarnings("unused") public class CSVReaderWriter { + @SuppressWarnings("WeakerAccess") public static Set<Map<String, String>> readCSVasSet(String filename) { Set<Map<String, String>> csvSet = null; @@ -22,13 +23,8 @@ public class CSVReaderWriter { 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; } @@ -40,20 +36,14 @@ public class CSVReaderWriter { csvList = (List<Map<String, String>>) parseCSV(inputStreamReader, new LinkedList<>()); } 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 csvList; } private static Collection<Map<String, String>> parseCSV(InputStreamReader inputStreamReader, - Collection<Map<String, String>> destination) - throws IOException, CsvValidationException { + Collection<Map<String, String>> destination) { Map<String, String> line; try { CSVReaderHeaderAware csvReader = new CSVReaderHeaderAwareBuilder(inputStreamReader).build(); @@ -62,32 +52,17 @@ public class CSVReaderWriter { } } 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) { - Set<Map<String, String>> csvSet = null; - ClassLoader classLoader = CSVReaderWriter.class.getClassLoader(); - try (InputStream inputStream = classLoader.getResourceAsStream("csv/" + filename)) { - csvSet = parseCSV(inputStream); } catch (IOException ioException) { - System.err.println("Error loading " + filename + ". " + ioException); + System.err.println("Error reading CSV file. " + ioException); + } catch (CsvValidationException csvValidationException) { + System.err.println("Could not validate a line in CSV file. " + csvValidationException); } - return csvSet; + return destination; } -*/ // I'd like to replace this with something that uses openCSV, but it works, and openCSV doesn't seem to have // writers that take map<string,string> - @SuppressWarnings("WeakerAccess") - public static boolean writeCSV(String filename, Iterable<Map<String, String>> data) { + public static boolean writeCSV(String filename, Collection<Map<String, String>> data) { boolean wroteFile = true; ClassLoader classLoader = CSVReaderWriter.class.getClassLoader(); URL resource = classLoader.getResource("csv/" + filename); @@ -109,45 +84,7 @@ public class CSVReaderWriter { 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 { - Set<Map<String, String>> csvSet = new HashSet<>(); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - String line; - List<String> fieldNames = new ArrayList<>(); - boolean header = true; - while ((line = reader.readLine()) != null) { - String[] elements = line.split(DELIMTER); - if (header) { - if (elements[0].charAt(0) == BYTE_ORDER_MARK) { - elements[0] = elements[0].substring(1); // remove text-encoding character sometimes added by - // Excel - } - fieldNames.addAll(Arrays.asList(elements)); - header = false; - } else { - Map<String, String> row = new HashMap<>(); - int column = 0; - for (String element : elements) { - row.put(fieldNames.get(column++), element); - } - csvSet.add(row); - } - } - return csvSet; - } -*/ - - static void placeCSVonStream(Iterable<Map<String, String>> data, OutputStream outputStream) { + static void placeCSVonStream(Collection<Map<String, String>> data, OutputStream outputStream) { PrintStream writer = new PrintStream(outputStream); Set<String> fieldNames = null; int number_of_fields = 0; @@ -169,10 +106,21 @@ public class CSVReaderWriter { } } } + + /* LEGACY METHODS */ + public static Set<Map<String, String>> readCSV(String filename) { + return readCSVasSet(filename); + } + + static Set<Map<String, String>> parseCSV(InputStream inputStream) { + return (Set<Map<String, String>>) parseCSV(new InputStreamReader(inputStream), new HashSet<>()); + } +/* public static void main(String[] args) { Set<Map<String, String>> demo = readCSV("demo.csv"); boolean success = writeCSV("out.csv", demo); System.out.println(success ? "Wrote file!" : "Didn't write file"); } +*/ } diff --git a/src/main/resources/csv/demo.csv b/src/main/resources/csv/demo.csv index c0e808529d06101dc0e15eaa9c56c14c07593309..923ffd82ccd82e180419c3b42bac945b710d87e6 100644 --- a/src/main/resources/csv/demo.csv +++ b/src/main/resources/csv/demo.csv @@ -1,6 +1,2 @@ -A,B,C,D +A,B,C,D larry,curly,moe, -one,two,,three -four,,five,six -7,8,9, -,alpha,beta,gamma \ No newline at end of file diff --git a/src/test/java/edu/unl/cse/csv_io/CSVReaderWriterTest.java b/src/test/java/edu/unl/cse/csv_io/CSVReaderWriterTest.java index 12e27b653371350ea56e1a19523203106f8f567e..3e6d78f68aa4c797dc56d8a582cae3a2d81bc735 100644 --- a/src/test/java/edu/unl/cse/csv_io/CSVReaderWriterTest.java +++ b/src/test/java/edu/unl/cse/csv_io/CSVReaderWriterTest.java @@ -49,12 +49,8 @@ public class CSVReaderWriterTest { String[][] data = {headers, row}; // Output placeCSVStringOnInputStream(data); - Set<Map<String, String>> result = null; - try { - result = CSVReaderWriter.parseCSV(inputStream); - } catch (IOException ignored) { - fail(); - } + Set<Map<String, String>> result; + result = CSVReaderWriter.parseCSV(inputStream); // Oracle -- Header Set<String> expectedKeys = new HashSet<>(Arrays.asList(headers)); // Oracle -- Rows @@ -77,12 +73,8 @@ public class CSVReaderWriterTest { String[][] data = {headers, rows[0], rows[1]}; // Output placeCSVStringOnInputStream(data); - Set<Map<String, String>> result = null; - try { - result = CSVReaderWriter.parseCSV(inputStream); - } catch (IOException ignored) { - fail(); - } + Set<Map<String, String>> result; + result = CSVReaderWriter.parseCSV(inputStream); // Oracle -- Header Set<String> expectedKeys = new HashSet<>(Arrays.asList(headers)); // Oracle -- Rows @@ -109,12 +101,8 @@ public class CSVReaderWriterTest { String[][] data = {headers}; // Output placeCSVStringOnInputStream(data); - Set<Map<String, String>> result = null; - try { - result = CSVReaderWriter.parseCSV(inputStream); - } catch (IOException ignored) { - fail(); - } + Set<Map<String, String>> result; + result = CSVReaderWriter.parseCSV(inputStream); // Oracle int expectedResultSize = 0; // Compare @@ -128,12 +116,8 @@ public class CSVReaderWriterTest { String[][] data = {}; // Output placeCSVStringOnInputStream(data); - Set<Map<String, String>> result = null; - try { - result = CSVReaderWriter.parseCSV(inputStream); - } catch (IOException ignored) { - fail(); - } + Set<Map<String, String>> result; + result = CSVReaderWriter.parseCSV(inputStream); // Oracle int expectedResultSize = 0; // Compare