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.CSVFormat;
|
||||||
import org.apache.commons.csv.CSVRecord;
|
import org.apache.commons.csv.CSVRecord;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FileReader;
|
import java.nio.file.Files;
|
||||||
import java.io.Reader;
|
import java.nio.file.Paths;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class userVerify
|
public class userVerify
|
||||||
@ -62,12 +62,16 @@ public class userVerify
|
|||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
Boolean failedOnException = false;
|
||||||
Reader in = new FileReader(varFile);
|
Boolean parsedCorrectly = false;
|
||||||
String varHeader = fileProcessor.readFirstLine(thisFile);
|
long lineNum = 0;
|
||||||
|
|
||||||
if (varHeader.length() == 0)
|
while (!parsedCorrectly && !failedOnException) {
|
||||||
|
try (final Reader in = new InputStreamReader(new FileInputStream(varFile.getPath()), "UTF-8"))
|
||||||
{
|
{
|
||||||
|
String varHeader = new String();
|
||||||
|
varHeader = fileProcessor.readFirstLine(thisFile);
|
||||||
|
if (varHeader.length() == 0) {
|
||||||
System.out.println(thisFile + " : appears to be missing a valid header!");
|
System.out.println(thisFile + " : appears to be missing a valid header!");
|
||||||
return finalArray;
|
return finalArray;
|
||||||
}
|
}
|
||||||
@ -83,45 +87,40 @@ public class userVerify
|
|||||||
int headerLen = csvFormat.getHeader().length;
|
int headerLen = csvFormat.getHeader().length;
|
||||||
Iterable<CSVRecord> records = csvFormat.parse(in);
|
Iterable<CSVRecord> records = csvFormat.parse(in);
|
||||||
|
|
||||||
if (fromUser)
|
debugProcessor.writer(fromUser, "CSV token count : " + headerLen);
|
||||||
{
|
debugProcessor.writer(fromUser, "CSV token list : " + varHeader);
|
||||||
System.out.println("CSV token count : " + headerLen);
|
debugProcessor.writer(fromUser, "");
|
||||||
System.out.println("CSV token list : " + varHeader);
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (CSVRecord record : records)
|
for (CSVRecord record : records) {
|
||||||
{
|
|
||||||
readNum++;
|
readNum++;
|
||||||
long lineNum = record.getParser().getCurrentLineNumber();
|
lineNum = record.getParser().getCurrentLineNumber();
|
||||||
boolean isValid = record.isConsistent();
|
boolean isValid = record.isConsistent();
|
||||||
|
|
||||||
if (isValid) {
|
if (isValid)
|
||||||
finalArray.add(record.toMap());
|
|
||||||
if (fromUser)
|
|
||||||
{
|
{
|
||||||
System.out.println("PASSED : CSV line " + lineNum);
|
finalArray.add(record.toMap());
|
||||||
}
|
debugProcessor.writer(fromUser, "## " + lineNum + " [passed]");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
invalidNum++;
|
invalidNum++;
|
||||||
if (fromUser)
|
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 (IOException | IllegalArgumentException e)
|
||||||
{
|
{
|
||||||
System.out.println("FAILED : CSV line " + lineNum);
|
failedOnException = true;
|
||||||
System.out.println("FAILED : Contents " + record.toMap().toString());
|
break;
|
||||||
}
|
}
|
||||||
}
|
parsedCorrectly = true;
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fromUser)
|
debugProcessor.writer(fromUser,"");
|
||||||
{
|
debugProcessor.writer(fromUser, "CSV lines passed : " + String.valueOf(readNum - invalidNum));
|
||||||
System.out.println();
|
debugProcessor.writer(fromUser, "CSV lines failed : " + invalidNum);
|
||||||
System.out.println("CSV lines passed : " + String.valueOf(readNum - invalidNum));
|
|
||||||
System.out.println("CSV lines failed : " + invalidNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (invalidNum > 0)
|
if (invalidNum > 0)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user