forked from uniVocity/univocity-parsers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidatedConversion.java
153 lines (131 loc) · 4.58 KB
/
ValidatedConversion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (c) 2018. Univocity Software Pty Ltd
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.univocity.parsers.conversions;
import com.univocity.parsers.annotations.helpers.*;
import com.univocity.parsers.common.*;
import java.util.*;
import java.util.regex.*;
/**
* Performs one or more validations against the values of a given record.
*/
public class ValidatedConversion implements Conversion<Object, Object> {
private final String regexToMatch;
private final boolean nullable;
private final boolean allowBlanks;
private final Set<String> oneOf;
private final Set<String> noneOf;
private final Matcher matcher;
private final Validator[] validators;
public ValidatedConversion() {
this(false, false, null, null, null);
}
public ValidatedConversion(String regexToMatch) {
this(false, false, null, null, regexToMatch);
}
public ValidatedConversion(boolean nullable, boolean allowBlanks) {
this(nullable, allowBlanks, null, null, null);
}
public ValidatedConversion(boolean nullable, boolean allowBlanks, String[] oneOf, String[] noneOf, String regexToMatch) {
this(nullable, allowBlanks, oneOf, noneOf, regexToMatch, null);
}
public ValidatedConversion(boolean nullable, boolean allowBlanks, String[] oneOf, String[] noneOf, String regexToMatch, Class[] validators) {
this.regexToMatch = regexToMatch;
this.matcher = regexToMatch == null || regexToMatch.isEmpty() ? null : Pattern.compile(regexToMatch).matcher("");
this.nullable = nullable;
this.allowBlanks = allowBlanks;
this.oneOf = oneOf == null || oneOf.length == 0 ? null : new HashSet<String>(Arrays.asList(oneOf));
this.noneOf = noneOf == null || noneOf.length == 0 ? null : new HashSet<String>(Arrays.asList(noneOf));
this.validators = validators == null || validators.length == 0 ? new Validator[0] : instantiateValidators(validators);
}
private Validator[] instantiateValidators(Class[] validators) {
Validator[] out = new Validator[validators.length];
for (int i = 0; i < validators.length; i++) {
out[i] = (Validator) AnnotationHelper.newInstance(Validator.class, validators[i], ArgumentUtils.EMPTY_STRING_ARRAY);
}
return out;
}
@Override
public Object execute(Object input) {
validate(input);
return input;
}
@Override
public Object revert(Object input) {
validate(input);
return input;
}
protected void validate(Object value) {
DataValidationException e = null;
String str = null;
if (value == null) {
if (nullable) {
if (noneOf != null && noneOf.contains(null)) {
e = new DataValidationException("Value '{value}' is not allowed.");
} else {
return;
}
} else {
if (oneOf != null && oneOf.contains(null)) {
return;
} else {
e = new DataValidationException("Null values not allowed.");
}
}
} else {
str = String.valueOf(value);
if (str.trim().isEmpty()) {
if (allowBlanks) {
if (noneOf != null && noneOf.contains(str)) {
e = new DataValidationException("Value '{value}' is not allowed.");
} else {
return;
}
} else {
if (oneOf != null && oneOf.contains(str)) {
return;
} else {
e = new DataValidationException("Blanks are not allowed. '{value}' is blank.");
}
}
}
if (matcher != null && e == null) {
boolean match;
synchronized (matcher) {
match = matcher.reset(str).matches();
}
if (!match) {
e = new DataValidationException("Value '{value}' does not match expected pattern: '" + regexToMatch + "'");
}
}
}
if (oneOf != null && !oneOf.contains(str)) {
e = new DataValidationException("Value '{value}' is not allowed. Expecting one of: " + oneOf);
}
if (e == null && noneOf != null && noneOf.contains(str)) {
e = new DataValidationException("Value '{value}' is not allowed.");
}
for (int i = 0; e == null && i < validators.length; i++) {
String error = validators[i].validate(value);
if (error != null && !error.trim().isEmpty()) {
e = new DataValidationException("Value '{value}' didn't pass validation: " + error);
}
}
if (e != null) {
e.setValue(value);
throw e;
}
}
}