1010
1111import java .io .Reader ;
1212import java .nio .charset .Charset ;
13+ import java .util .ArrayList ;
1314import java .util .List ;
1415import static uk .gov .nationalarchives .csv .validator .api .CsvValidator$ .MODULE$ ;
1516
1920 *
2021 * <p> A typical invocation sequence:</p>
2122 * <blockquote><pre>{@code
22- * Boolean failFast = false;
23- * List<Substitution> pathSubstitutions = new ArrayList<Substitution>();
23+ * Charset csvEncoding = JCharset.forName("UTF-8"); // default is UTF-8
24+ * Charset csvSchemaEncoding = JCharset.forName("UTF-8"); // default is UTF-8
25+ * boolean failFast = true; // default is false
26+ * List<Substitution> pathSubstitutions = new ArrayList<Substitution>(); // default is any empty ArrayList
27+ * boolean enforceCaseSensitivePathChecks = true; // default is false
28+ * boolean trace = false; // default is false
29+ * ProgressCallback progress; // default is null
30+ * boolean skipFileChecks = true; // default is false
31+ * int maxCharsPerCell = 8096; // default is 4096
2432 *
2533 * //add a substitution path
2634 * pathSubstitutions.add(new Substitution("file://something", "/home/xxx"));
2735 *
28- * List<FailMessage> messages = CsvValidator.validate(
29- * "/home/dev/IdeaProjects/csv/csv-validator/csv-validator-core/data.csv",
30- * "/home/dev/IdeaProjects/csv/csv-validator/csv-validator-core/data-schema.csvs",
31- * failFast,
32- * pathSubstitutions,
33- * true);
36+ * CsvValidator.ValidatorBuilder validateWithStringNames = new CsvValidator.ValidatorBuilder(
37+ * "/home/dev/IdeaProjects/csv/csv-validator/csv-validator-core/data.csv",
38+ * "/home/dev/IdeaProjects/csv/csv-validator/csv-validator-core/data-schema.csvs"
39+ * )
40+ *
41+ * // alternatively, you can pass in Readers for each file
42+ * Reader csvReader = new Reader();
43+ * Reader csvSchemaReader = new Reader();
44+ * CsvValidator.ValidatorBuilder validateWithReaders = new CsvValidator.ValidatorBuilder(
45+ * csvReader, csvSchemaReader
46+ * )
47+ *
48+ * List<FailMessage> messages = validateWithStringNames
49+ * .csvEncoding()
50+ * .csvSchemaEncoding()
51+ * .failFast(failFast)
52+ * .pathSubstitutions(pathSubstitutions)
53+ * .enforceCaseSensitivePathChecks(enforceCaseSensitivePathChecks)
54+ * .trace(trace)
55+ * .progress(progress)
56+ * .skipFileChecks(skipFileChecks)
57+ * .maxCharsPerCell(maxCharsPerCell)
3458 *
3559 * if(messages.isEmpty()) {
3660 * System.out.println("All worked OK");
@@ -63,6 +87,7 @@ public class CsvValidator {
6387 *
6488 * @return empty list of (if there are no errors), or list of error strings.
6589 */
90+ @ Deprecated
6691 public static List <FailMessage > validate (final String csvFilename , final String csvSchemaFilename , final boolean failFast , final List <Substitution > pathSubstitutions , final Boolean enforceCaseSensitivePathChecks , final Boolean trace ) {
6792 return validate (csvFilename , MODULE$ .DEFAULT_ENCODING (), csvSchemaFilename , MODULE$ .DEFAULT_ENCODING (), failFast , pathSubstitutions , enforceCaseSensitivePathChecks , trace );
6893 }
@@ -82,6 +107,7 @@ public static List<FailMessage> validate(final String csvFilename, final String
82107 *
83108 * @return empty list of (if there are no errors), or list of error strings.
84109 */
110+ @ Deprecated
85111 public static List <FailMessage > validate (final String csvFilename , final Charset csvEncoding , final String csvSchemaFilename , final Charset csvSchemaEncoding , final boolean failFast , final List <Substitution > pathSubstitutions , final Boolean enforceCaseSensitivePathChecks , final Boolean trace ) {
86112 return CsvValidatorJavaBridge .validate (csvFilename , csvEncoding , csvSchemaFilename , csvSchemaEncoding , failFast , pathSubstitutions , enforceCaseSensitivePathChecks , trace );
87113 }
@@ -104,6 +130,7 @@ public static List<FailMessage> validate(final String csvFilename, final Charset
104130 *
105131 * @return empty list of (if there are no errors), or list of error strings.
106132 */
133+ @ Deprecated
107134 public static List <FailMessage > validate (final String csvFilename , final String csvSchemaFilename , final boolean failFast , final List <Substitution > pathSubstitutions , final Boolean enforceCaseSensitivePathChecks , final Boolean trace , final ProgressCallback progress ) {
108135 return validate (csvFilename , MODULE$ .DEFAULT_ENCODING (), csvSchemaFilename , MODULE$ .DEFAULT_ENCODING (), failFast , pathSubstitutions , enforceCaseSensitivePathChecks , trace , progress );
109136 }
@@ -125,6 +152,7 @@ public static List<FailMessage> validate(final String csvFilename, final String
125152 *
126153 * @return empty list of (if there are no errors), or list of error strings.
127154 */
155+ @ Deprecated
128156 public static List <FailMessage > validate (final String csvFilename , final Charset csvEncoding , final String csvSchemaFilename , final Charset csvSchemaEncoding , final boolean failFast , final List <Substitution > pathSubstitutions , final Boolean enforceCaseSensitivePathChecks , final Boolean trace , final ProgressCallback progress ) {
129157 return CsvValidatorJavaBridge .validate (csvFilename , csvEncoding , csvSchemaFilename , csvSchemaEncoding , failFast , pathSubstitutions , enforceCaseSensitivePathChecks , trace , progress );
130158 }
@@ -142,6 +170,7 @@ public static List<FailMessage> validate(final String csvFilename, final Charset
142170 *
143171 * @return empty list of (if there are no errors), or list of error strings.
144172 */
173+ @ Deprecated
145174 public static List <FailMessage > validate (final Reader csvData , final Reader csvSchema , final boolean failFast , final List <Substitution > pathSubstitutions , final Boolean enforceCaseSensitivePathChecks , final Boolean trace ) {
146175 return CsvValidatorJavaBridge .validate (csvData , csvSchema , failFast , pathSubstitutions , enforceCaseSensitivePathChecks , trace );
147176 }
@@ -161,8 +190,97 @@ public static List<FailMessage> validate(final Reader csvData, final Reader csvS
161190 *
162191 * @return empty list of (if there are no errors), or list of error strings.
163192 */
193+ @ Deprecated
164194 public static List <FailMessage > validate (final Reader csvData , final Reader csvSchema , final boolean failFast , final List <Substitution > pathSubstitutions , final Boolean enforceCaseSensitivePathChecks , final Boolean trace , final ProgressCallback progress ) {
165195 return CsvValidatorJavaBridge .validate (csvData , csvSchema , failFast , pathSubstitutions , enforceCaseSensitivePathChecks , trace , progress );
166196 }
197+
198+ static class ValidatorBuilder {
199+ private String csvFileName ;
200+ private String csvSchemaFilename ;
201+ private Reader csvReader ;
202+ private Reader csvSchemaReader ;
203+ private Charset csvEncoding = MODULE$ .DEFAULT_ENCODING ();
204+ private boolean validateUtf8Encoding = csvEncoding .name ().equals ("UTF-8" );
205+ private Charset csvSchemaEncoding = MODULE$ .DEFAULT_ENCODING ();
206+ private boolean failFast = false ;
207+ private List <Substitution > pathSubstitutions = new ArrayList <>();
208+ private boolean enforceCaseSensitivePathChecks = false ;
209+ private boolean trace = false ;
210+ private ProgressCallback progress ;
211+ private boolean skipFileChecks = false ;
212+ private int maxCharsPerCell = 4096 ;
213+
214+ private boolean textFileValidation = false ;
215+
216+ public ValidatorBuilder (String csvFileName , String csvSchemaFilename ) {
217+ this .csvFileName = csvFileName ;
218+ this .csvSchemaFilename = csvSchemaFilename ;
219+ this .textFileValidation = true ;
220+ }
221+
222+ public ValidatorBuilder (Reader csvReader , Reader csvSchemaReader ) {
223+ this .csvReader = csvReader ;
224+ this .csvSchemaReader = csvSchemaReader ;
225+ }
226+
227+ public ValidatorBuilder usingCsvEncoding (Charset encoding , boolean validateUtf8Encoding ) throws Exception {
228+ if (!encoding .name ().equals ("UTF-8" ) && validateUtf8Encoding ){
229+ throw new Exception ("'validateUtf8Encoding' is set to 'true' but " + encoding .name () + " charset was passed in" );
230+ }
231+
232+ this .csvEncoding = encoding ;
233+ this .validateUtf8Encoding = validateUtf8Encoding ;
234+ return this ;
235+ }
236+
237+ public ValidatorBuilder usingCsvSchemaEncoding (Charset schemaEncoding ) {
238+ this .csvSchemaEncoding = schemaEncoding ;
239+ return this ;
240+ }
241+
242+ public ValidatorBuilder usingFailFast (boolean failFast ) {
243+ this .failFast = failFast ;
244+ return this ;
245+ }
246+
247+ public ValidatorBuilder usingPathSubstitutions (List <Substitution > pathSubstitutions ) {
248+ this .pathSubstitutions = pathSubstitutions ;
249+ return this ;
250+ }
251+
252+ public ValidatorBuilder usingEnforceCaseSensitivePathChecks (boolean enforceCaseSensitivePathChecks ) {
253+ this .enforceCaseSensitivePathChecks = enforceCaseSensitivePathChecks ;
254+ return this ;
255+ }
256+
257+ public ValidatorBuilder usingTrace (boolean trace ) {
258+ this .trace = trace ;
259+ return this ;
260+ }
261+
262+ public ValidatorBuilder usingProgress (ProgressCallback progress ) {
263+ this .progress = progress ;
264+ return this ;
265+ }
266+
267+ public ValidatorBuilder usingSkipFileChecks (boolean skipFileChecks ) {
268+ this .skipFileChecks = skipFileChecks ;
269+ return this ;
270+ }
271+
272+ public ValidatorBuilder usingMaxCharsPerCell (int maxCharsPerCell ) {
273+ this .maxCharsPerCell = maxCharsPerCell ;
274+ return this ;
275+ }
276+
277+ public Result runValidation () {
278+ if (textFileValidation ) {
279+ return CsvValidatorJavaBridge .validate (new CsvValidatorJavaBridge .ValidationRequest (this .csvFileName , this .csvEncoding , this .validateUtf8Encoding , this .csvSchemaFilename , this .csvSchemaEncoding , true , this .failFast , this .pathSubstitutions , this .enforceCaseSensitivePathChecks , this .trace , this .progress , this .skipFileChecks , this .maxCharsPerCell ));
280+ } else {
281+ return CsvValidatorJavaBridge .validate (new CsvValidatorJavaBridge .ReaderValidationRequest (this .csvReader , this .csvSchemaReader , this .failFast , this .pathSubstitutions , this .enforceCaseSensitivePathChecks , this .trace , this .progress , this .skipFileChecks , this .maxCharsPerCell ));
282+ }
283+ }
284+ }
167285}
168286
0 commit comments