|
8 | 8 | import uk.gov.nationalarchives.csv.validator.api.java.*; |
9 | 9 |
|
10 | 10 | import java.nio.charset.Charset; |
| 11 | +import java.nio.charset.StandardCharsets; |
11 | 12 | import java.nio.file.Path; |
12 | 13 | import java.util.ArrayList; |
| 14 | +import java.util.Collections; |
13 | 15 | import java.util.List; |
14 | 16 |
|
15 | 17 | import static uk.gov.nationalarchives.csv.validator.api.CsvValidator$.MODULE$; |
|
19 | 21 | public class CsvValidationService { |
20 | 22 |
|
21 | 23 | private static final Logger LOG = LoggerFactory.getLogger(CsvValidationService.class); |
22 | | - private static final int DEFAULT_MAX_CHARS_PER_CELL = 8096; |
23 | 24 |
|
24 | 25 | @Inject |
25 | 26 | private SchemaService schemaService; |
26 | 27 |
|
27 | 28 | public ValidationResult validateCsvFile(Path csvFilePath, String schemaId) { |
| 29 | + String schemaFilePath = String.valueOf(schemaService.getSchemaFilePath(schemaId)); |
| 30 | + Charset csvEncoding = StandardCharsets.UTF_8; |
| 31 | + boolean validateUtf8Encoding = true; |
| 32 | + Charset csvSchemaEncoding = StandardCharsets.UTF_8; |
| 33 | + boolean failFast = false; |
| 34 | + List<Substitution> pathSubstitutions = Collections.emptyList(); |
| 35 | + boolean enforceCaseSensitivePathChecks = false; |
| 36 | + boolean trace = false; |
| 37 | + boolean skipFileChecks = false; |
| 38 | + |
| 39 | + CsvValidatorJavaBridge.ValidationRequest validationRequest = new CsvValidatorJavaBridge.ValidationRequest( |
| 40 | + csvFilePath.toString(), csvEncoding, validateUtf8Encoding, schemaFilePath, |
| 41 | + csvSchemaEncoding, true, failFast, pathSubstitutions, |
| 42 | + enforceCaseSensitivePathChecks, trace, null, skipFileChecks, -1); |
| 43 | + |
28 | 44 | long startTime = System.currentTimeMillis(); |
29 | | - try { |
30 | | - String schemaFilePath = String.valueOf(schemaService.getSchemaFilePath(schemaId)); |
31 | | - Charset csvEncoding = MODULE$.DEFAULT_ENCODING(); |
32 | | - boolean validateUtf8Encoding = csvEncoding.name().equals("UTF-8"); |
33 | | - Charset csvSchemaEncoding = MODULE$.DEFAULT_ENCODING(); |
34 | | - boolean failFast = false; |
35 | | - List<Substitution> pathSubstitutions = new ArrayList<>(); |
36 | | - boolean enforceCaseSensitivePathChecks = false; |
37 | | - boolean trace = false; |
38 | | - boolean skipFileChecks = false; |
39 | | - |
40 | | - CsvValidatorJavaBridge.ValidationRequest validationRequest = new CsvValidatorJavaBridge.ValidationRequest( |
41 | | - csvFilePath.toString(), csvEncoding, validateUtf8Encoding, schemaFilePath, |
42 | | - csvSchemaEncoding, true, failFast, pathSubstitutions, |
43 | | - enforceCaseSensitivePathChecks, trace, null, skipFileChecks, DEFAULT_MAX_CHARS_PER_CELL); |
44 | | - |
45 | | - CsvValidatorJavaBridge.ValidationResult result = CsvValidatorJavaBridge.validate(validationRequest); |
46 | | - List<FailMessage> errors = result.errors(); |
47 | | - long executionTime = System.currentTimeMillis() - startTime; |
48 | | - return processValidationMessages(errors, executionTime); |
49 | | - |
50 | | - } catch (Exception e) { |
51 | | - long executionTime = System.currentTimeMillis() - startTime; |
52 | | - LOG.error("Error validating CSV file: {}", csvFilePath, e); |
53 | | - return ValidationResult.error("Validation failed: " + e.getMessage(), executionTime); |
54 | | - } |
| 45 | + CsvValidatorJavaBridge.ValidationResult result = CsvValidatorJavaBridge.validate(validationRequest); |
| 46 | + List<FailMessage> errors = result.errors(); |
| 47 | + long executionTime = System.currentTimeMillis() - startTime; |
| 48 | + return processValidationMessages(errors, executionTime); |
55 | 49 | } |
56 | 50 |
|
57 | 51 |
|
|
0 commit comments