1 |
|
package org.europa.together.utils; |
2 |
|
|
3 |
|
import java.time.ZonedDateTime; |
4 |
|
import org.europa.together.application.LogbackLogger; |
5 |
|
import org.europa.together.business.Logger; |
6 |
|
import org.europa.together.domain.LogLevel; |
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (90) |
Complexity: 31 |
Complexity Density: 0.56 |
|
15 |
|
public final class Validator { |
16 |
|
|
17 |
|
private static final Logger LOGGER = new LogbackLogger(Validator.class); |
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
22 |
1 |
private Validator() {... |
23 |
1 |
throw new UnsupportedOperationException(); |
24 |
|
} |
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
public static final String ASCII_CHARACTER = "[a-zA-Z ]+"; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
public static final String BOOLEAN = "(0|1)|(t|T)(r|R)(u|U)(e|E)|(f|F)(a|A)(l|L)(s|S)(e|E)"; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
public static final String DIGIT = "[0-9 ]+"; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
public static final String FLOATING_POINT = "-?[0-9 ]+(.[0-9]+)?"; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
public static final String LETTERS = "[a-zA-Z]+"; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
public static final String RGB_COLOR |
56 |
|
= "#[0-9a-fA-F]{3,3}([0-9a-fA-F]{3,3})?"; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
public static final String TEXT |
62 |
|
= "(\\w|\\s|\\d|[¬&§$@€# ?!.,;_Â$Ââ~<>:=_+*/%\\-\\(\\)\\{\\}\\[\\]])*"; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
public static final String TIME_24H |
68 |
|
= "((0[0-9])|(1[0-9])|(2[0-3])):((0[0-9])|([1-5][0-9]))"; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
public static final String IP4_ADDRESS |
75 |
|
= "(localhost|" |
76 |
|
+ "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." |
77 |
|
+ "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." |
78 |
|
+ "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." |
79 |
|
+ "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]))" |
80 |
|
|
81 |
|
+ "(:[1-5]?[0-9]{0,4}" |
82 |
|
+ "|:6[0-4][0-9][0-9][0-9]" |
83 |
|
+ "|:65[0-4][0-9][0-9]" |
84 |
|
+ "|:655[0-2][0-9]" |
85 |
|
+ "|:6553[0-5])?"; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
public static final String E_MAIL_ADDRESS = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" |
92 |
|
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
public static final String SEMANTIC_VERSION_NUMBER |
99 |
|
= "[1-9][0-9]*" |
100 |
|
+ "(\\.[0-9]|\\.[1-9][0-9]*)?" |
101 |
|
+ "(\\.[0-9]|\\.[1-9][0-9]*)?" |
102 |
|
+ "(-?[A-Za-z]){0,10}"; |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
@param |
109 |
|
@param |
110 |
|
@param |
111 |
|
@return |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
113 |
5 |
public static boolean isIntegerInRange(final int test, final int min, final int max) {... |
114 |
5 |
boolean check = false; |
115 |
5 |
if (test >= min && test <= max) { |
116 |
3 |
check = true; |
117 |
|
} else { |
118 |
2 |
LOGGER.log("Integer: " + test + " is not between [" + min + "," + max + "]", |
119 |
|
LogLevel.WARN); |
120 |
|
} |
121 |
5 |
return check; |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
@param |
135 |
|
@param |
136 |
|
@return |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
138 |
12 |
public static boolean isDateAfter(final ZonedDateTime check, final ZonedDateTime after) {... |
139 |
12 |
boolean success = false; |
140 |
12 |
if (check != null && after != null) { |
141 |
9 |
success = check.isAfter(after); |
142 |
|
} |
143 |
12 |
return success; |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
@param |
157 |
|
@param |
158 |
|
@return |
159 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
160 |
13 |
public static boolean isDateBefore(final ZonedDateTime check, final ZonedDateTime before) {... |
161 |
13 |
boolean success = false; |
162 |
13 |
if (check != null && before != null) { |
163 |
10 |
success = check.isBefore(before); |
164 |
|
} |
165 |
13 |
return success; |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
@param |
176 |
|
@param |
177 |
|
@param |
178 |
|
@return |
179 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 6 |
Complexity Density: 1 |
|
180 |
13 |
public static boolean isDateInRange(... |
181 |
|
final ZonedDateTime check, final ZonedDateTime min, final ZonedDateTime max) { |
182 |
13 |
boolean success = false; |
183 |
13 |
if (check != null && min != null && max != null) { |
184 |
6 |
if (isDateBefore(check, max) && isDateAfter(check, min)) { |
185 |
1 |
success = true; |
186 |
|
} else { |
187 |
5 |
LOGGER.log("Date: " + check.toString() + " is not in range.", LogLevel.WARN); |
188 |
|
} |
189 |
|
} |
190 |
13 |
return success; |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
@param |
197 |
|
@return |
198 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (45) |
Complexity: 13 |
Complexity Density: 0.45 |
|
199 |
12 |
public static boolean isIsbn(final String isbn)... |
200 |
|
throws NumberFormatException { |
201 |
12 |
boolean success = false; |
202 |
12 |
final int value09 = 9; |
203 |
12 |
final int value10 = 10; |
204 |
12 |
final int value11 = 11; |
205 |
12 |
final int value13 = 13; |
206 |
12 |
int tmp = 0; |
207 |
12 |
String check = isbn.replaceAll("-", ""); |
208 |
12 |
int size = check.length(); |
209 |
12 |
if (StringUtils.isEmpty(isbn) || size != value10 && size != value13) { |
210 |
3 |
throw new NumberFormatException( |
211 |
|
"The format has not the correct lenght of a valid ISBN."); |
212 |
|
} |
213 |
|
|
214 |
98 |
for (int i = 0; i < size; i++) { |
215 |
90 |
String element = check.substring(i, i + 1); |
216 |
|
|
217 |
90 |
int digit; |
218 |
90 |
if (size == value10 && i == value09 && element.equalsIgnoreCase("x")) { |
219 |
1 |
digit = value10; |
220 |
|
} else { |
221 |
89 |
digit = Integer.parseInt(element); |
222 |
|
} |
223 |
|
|
224 |
89 |
if (size == value10) { |
225 |
50 |
tmp += digit * (value10 - i); |
226 |
50 |
LOGGER.log("(ISBN-10 SUM: " + tmp, LogLevel.DEBUG); |
227 |
|
} else { |
228 |
39 |
if (i % 2 == 0) { |
229 |
21 |
tmp += digit; |
230 |
|
} else { |
231 |
18 |
tmp += digit * 3; |
232 |
|
} |
233 |
39 |
LOGGER.log("(ISBN-13 SUM: " + tmp, LogLevel.DEBUG); |
234 |
|
} |
235 |
|
} |
236 |
|
|
237 |
8 |
if (size == value10) { |
238 |
5 |
if (tmp % value11 == 0) { |
239 |
3 |
success = true; |
240 |
|
} |
241 |
|
} else { |
242 |
3 |
if (tmp % value10 == 0) { |
243 |
2 |
success = true; |
244 |
|
} |
245 |
|
} |
246 |
8 |
return success; |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
@param |
254 |
|
@param |
255 |
|
@return |
256 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
257 |
294 |
public static boolean validate(final String content, final String regEx) {... |
258 |
294 |
boolean test = false; |
259 |
293 |
if (content.matches(regEx)) { |
260 |
214 |
test = true; |
261 |
|
} else { |
262 |
79 |
String msg = "validate('" + regEx + "') did not match " + content; |
263 |
79 |
LOGGER.log(msg, LogLevel.WARN); |
264 |
|
} |
265 |
293 |
return test; |
266 |
|
} |
267 |
|
} |