Handles CSV exceptions correctly
Changed the code so that any exception thrown by the CSVParser is handled correctly
This commit is contained in:
parent
067e878d57
commit
4054a1c84d
@ -3,9 +3,9 @@ package com.belkast.soap;
|
||||
import org.apache.commons.csv.CSVFormat;
|
||||
import org.apache.commons.csv.CSVRecord;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.Reader;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
|
||||
public class userVerify
|
||||
@ -62,66 +62,65 @@ public class userVerify
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
try {
|
||||
Reader in = new FileReader(varFile);
|
||||
String varHeader = fileProcessor.readFirstLine(thisFile);
|
||||
Boolean failedOnException = false;
|
||||
Boolean parsedCorrectly = false;
|
||||
long lineNum = 0;
|
||||
|
||||
if (varHeader.length() == 0)
|
||||
while (!parsedCorrectly && !failedOnException) {
|
||||
try (final Reader in = new InputStreamReader(new FileInputStream(varFile.getPath()), "UTF-8"))
|
||||
{
|
||||
System.out.println(thisFile + " : appears to be missing a valid header!");
|
||||
return finalArray;
|
||||
}
|
||||
|
||||
CSVFormat csvFormat = CSVFormat.RFC4180.builder()
|
||||
.setHeader(varHeader.split(","))
|
||||
.setSkipHeaderRecord(true)
|
||||
.setIgnoreEmptyLines(true)
|
||||
.setIgnoreSurroundingSpaces(true)
|
||||
.setTrim(true)
|
||||
.build();
|
||||
|
||||
int headerLen = csvFormat.getHeader().length;
|
||||
Iterable<CSVRecord> records = csvFormat.parse(in);
|
||||
|
||||
if (fromUser)
|
||||
{
|
||||
System.out.println("CSV token count : " + headerLen);
|
||||
System.out.println("CSV token list : " + varHeader);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
for (CSVRecord record : records)
|
||||
{
|
||||
readNum++;
|
||||
long lineNum = record.getParser().getCurrentLineNumber();
|
||||
boolean isValid = record.isConsistent();
|
||||
|
||||
if (isValid) {
|
||||
finalArray.add(record.toMap());
|
||||
if (fromUser)
|
||||
{
|
||||
System.out.println("PASSED : CSV line " + lineNum);
|
||||
}
|
||||
String varHeader = new String();
|
||||
varHeader = fileProcessor.readFirstLine(thisFile);
|
||||
if (varHeader.length() == 0) {
|
||||
System.out.println(thisFile + " : appears to be missing a valid header!");
|
||||
return finalArray;
|
||||
}
|
||||
else
|
||||
{
|
||||
invalidNum++;
|
||||
if (fromUser)
|
||||
|
||||
CSVFormat csvFormat = CSVFormat.RFC4180.builder()
|
||||
.setHeader(varHeader.split(","))
|
||||
.setSkipHeaderRecord(true)
|
||||
.setIgnoreEmptyLines(true)
|
||||
.setIgnoreSurroundingSpaces(true)
|
||||
.setTrim(true)
|
||||
.build();
|
||||
|
||||
int headerLen = csvFormat.getHeader().length;
|
||||
Iterable<CSVRecord> records = csvFormat.parse(in);
|
||||
|
||||
debugProcessor.writer(fromUser, "CSV token count : " + headerLen);
|
||||
debugProcessor.writer(fromUser, "CSV token list : " + varHeader);
|
||||
debugProcessor.writer(fromUser, "");
|
||||
|
||||
for (CSVRecord record : records) {
|
||||
readNum++;
|
||||
lineNum = record.getParser().getCurrentLineNumber();
|
||||
boolean isValid = record.isConsistent();
|
||||
|
||||
if (isValid)
|
||||
{
|
||||
System.out.println("FAILED : CSV line " + lineNum);
|
||||
System.out.println("FAILED : Contents " + record.toMap().toString());
|
||||
finalArray.add(record.toMap());
|
||||
debugProcessor.writer(fromUser, "## " + lineNum + " [passed]");
|
||||
}
|
||||
else
|
||||
{
|
||||
invalidNum++;
|
||||
String thisLine = Files.readAllLines(Paths.get(thisFile)).get(Math.toIntExact(lineNum - 1));
|
||||
debugProcessor.writer(fromUser, "!! " + lineNum + " [FAILED] : " + thisLine);
|
||||
debugProcessor.writer(fromUser, "!! " + lineNum + " [FAILED] : " + record.toMap().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
catch (IOException | IllegalArgumentException e)
|
||||
{
|
||||
failedOnException = true;
|
||||
break;
|
||||
}
|
||||
parsedCorrectly = true;
|
||||
}
|
||||
|
||||
if (fromUser)
|
||||
{
|
||||
System.out.println();
|
||||
System.out.println("CSV lines passed : " + String.valueOf(readNum - invalidNum));
|
||||
System.out.println("CSV lines failed : " + invalidNum);
|
||||
}
|
||||
debugProcessor.writer(fromUser,"");
|
||||
debugProcessor.writer(fromUser, "CSV lines passed : " + String.valueOf(readNum - invalidNum));
|
||||
debugProcessor.writer(fromUser, "CSV lines failed : " + invalidNum);
|
||||
|
||||
if (invalidNum > 0)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user