1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.utils

File Validator.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

34
75
8
1
326
172
35
0.47
9.38
8
4.38

Classes

Class Line # Actions
Validator 16 75 0% 35 0
1.0100%
 

Contributing tests

This file is covered by 271 tests. .

Source view

1    package org.europa.together.utils;
2   
3    import java.time.ZonedDateTime;
4    import java.util.List;
5    import org.europa.together.application.LogbackLogger;
6    import org.europa.together.business.Logger;
7    import org.europa.together.domain.LogLevel;
8   
9    /**
10    * A simple RexEx Validator.<br>
11    * ### REGEX VALIDATION PATTERN<br>
12    * ### * = 0 or more quantifier<br>
13    * ### + = 1 or more quantifier<br>
14    * ### ? = 0 or 1 quantifier
15    */
 
16    public final class Validator {
17   
18    private static final Logger LOGGER = new LogbackLogger(Validator.class);
19   
20    private static final int INT_10 = 10;
21   
22    /**
23    * Constructor.
24    */
 
25  1 toggle private Validator() {
26  1 throw new UnsupportedOperationException();
27    }
28   
29    /**
30    * Printable ASCII character: a-z A-Z.
31    */
32    public static final String ASCII_CHARACTER = "[a-zA-Z ]+";
33   
34    /**
35    * Boolean. 0/1 true/false TRUE/FALSE
36    */
37    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)";
38   
39    /**
40    * Digits form 0-9.
41    */
42    public static final String DIGIT = "[0-9 ]+";
43   
44    /**
45    * Floating point.
46    */
47    public static final String FLOATING_POINT = "-?[0-9 ]+(.[0-9]+)?";
48   
49    /**
50    * Letters a-z A-Z upper case and lower case. No digits, space or special
51    * characters.
52    */
53    public static final String LETTERS = "[a-zA-Z]+";
54   
55    /**
56    * RGB Color schema in HEX: #000000 to #ffffff.
57    */
58    public static final String RGB_COLOR
59    = "#[0-9a-fA-F]{3,3}([0-9a-fA-F]{3,3})?";
60   
61    /**
62    * Text.
63    */
64    public static final String TEXT
65    = "(\\w|\\s|\\d|[¬&§$@€# ?!.,;_Â$Ââ~<>:=_+*/%\\-\\(\\)\\{\\}\\[\\]])*";
66   
67    /**
68    * Time in 24 hour format: 00:00 to 23:59.
69    */
70    public static final String TIME_24H
71    = "((0[0-9])|(1[0-9])|(2[0-3])):((0[0-9])|([1-5][0-9]))";
72   
73    /**
74    * IP Adress (Version 4) with optional port e. g.: 127.0.0.1:80
75    * (1-255).(0-255).(0-255).(0-255):(1-65535)
76    */
77    public static final String IP4_ADDRESS
78    = "(localhost|"
79    + "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." // 1
80    + "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." // 2
81    + "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." // 3
82    + "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]))" // 4
83    // Optinal Port
84    + "(:[1-5]?[0-9]{0,4}" // 1-59.999
85    + "|:6[0-4][0-9][0-9][0-9]" // 60.000-64.999
86    + "|:65[0-4][0-9][0-9]" // 65.000-65.499
87    + "|:655[0-2][0-9]" // 65.500-65.529
88    + "|:6553[0-5])?"; // 65.530-65.535
89   
90    /**
91    * General E-Mail Regex (RFC 5322 Official Standard.
92    */
93    public static final String E_MAIL_ADDRESS = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]"
94    + "+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:"
95    + "[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]"
96    + "|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")"
97    + "@"
98    + "(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)"
99    + "+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]"
100    + "|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]"
101    + "|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:"
102    + "(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]"
103    + "|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
104   
105    /**
106    * Version number based on semantic versioning. Major.Minor.Patch-SNAPSHOT :
107    * 1.0; 1.0-SNAPSHOT; 1.0.0-SNAPSHOT :: 000.000.000-ABCDEFGHIJ
108    */
109    public static final String SEMANTIC_VERSION_NUMBER
110    = "[1-9][0-9]*"
111    + "(\\.[0-9]|\\.[1-9][0-9]*)?"
112    + "(\\.[0-9]|\\.[1-9][0-9]*)?"
113    + "(-?[A-Za-z]){0,10}";
114   
115    /**
116    * Check if the variable test inside the range of the borders min and max.
117    * The borders and all numbers between are allowed values for the variable.
118    *
119    * @param test as Integer
120    * @param min as Integer
121    * @param max as Integer
122    * @return true on success
123    */
 
124  5 toggle public static boolean isIntegerInRange(final int test, final int min, final int max) {
125  5 boolean check = false;
126  5 if (test >= min && test <= max) {
127  3 check = true;
128    } else {
129  2 LOGGER.log("Integer: " + test + " is not between [" + min + "," + max + "]",
130    LogLevel.WARN);
131    }
132  5 return check;
133    }
134   
135    /**
136    * Check if is a given date after the given boundary. If the check date
137    * equal to the after boundary, the validation will fail.<br>
138    * Sample:
139    * <code>Validator.isDateAfter(new DateTime(), new DateTime(2015, 12, 31, 23, 59));</code>
140    * Validate to TRUE because: now() is after 2015
141    * <br><br>
142    * Convert java.util.Date to ZonedDateTime
143    * <code>ZonedDateTime.from(Date.toInstant();</code>
144    *
145    * @param check as DateTime
146    * @param after as DateTime
147    * @return true on success
148    */
 
149  12 toggle public static boolean isDateAfter(final ZonedDateTime check, final ZonedDateTime after) {
150  12 boolean success = false;
151  12 if (check != null && after != null) {
152  9 success = check.isAfter(after);
153    }
154  12 return success;
155    }
156   
157    /**
158    * Check if is a given date before the given boundary. If the check date
159    * equal to the before boundary, the validation will fail.<br>
160    * Sample:
161    * <code>Validator.isDateBefore(new DateTime(2015, 12, 31, 23, 59), new DateTime());</code>
162    * Validate to TRUE because: 2015 is before now()
163    * <br><br>
164    * Convert java.util.Date to ZonedDateTime
165    * <code>ZonedDateTime.from(Date.toInstant();</code>
166    *
167    * @param check as DateTime
168    * @param before as DateTime
169    * @return true on success
170    */
 
171  13 toggle public static boolean isDateBefore(final ZonedDateTime check, final ZonedDateTime before) {
172  13 boolean success = false;
173  13 if (check != null && before != null) {
174  10 success = check.isBefore(before);
175    }
176  13 return success;
177    }
178   
179    /**
180    * Test if a given date is inside a range between a min and max. The
181    * boundaries are inside the range.
182    * <br><br>
183    * Convert java.util.Date to ZonedDateTime
184    * <code>ZonedDateTime.from(Date.toInstant();</code>
185    *
186    * @param check as DateTime
187    * @param min as DateTime
188    * @param max as DateTime
189    * @return true on success
190    */
 
191  13 toggle public static boolean isDateInRange(
192    final ZonedDateTime check, final ZonedDateTime min, final ZonedDateTime max) {
193  13 boolean success = false;
194  13 if (check != null && min != null && max != null) {
195  6 if (isDateBefore(check, max) && isDateAfter(check, min)) {
196  1 success = true;
197    } else {
198  5 LOGGER.log("Date: " + check.toString() + " is not in range.", LogLevel.WARN);
199    }
200    }
201  13 return success;
202    }
203   
204    /**
205    * Test if a 10 or 13 digit ISBN number is valid.
206    *
207    * @param isbn as String
208    * @return true on success
209    */
 
210  13 toggle public static boolean isIsbn(final String isbn)
211    throws NumberFormatException {
212  13 boolean success = false;
213  13 final int value09 = 9;
214  13 final int value10 = 10;
215  13 final int value11 = 11;
216  13 final int value13 = 13;
217  13 int tmp = 0;
218  13 String check = isbn.replaceAll("-", "");
219  13 int size = check.length();
220  13 if (StringUtils.isEmpty(isbn) || size != value10 && size != value13) {
221  4 throw new NumberFormatException(
222    "The format has not the correct lenght of a valid ISBN.");
223    }
224   
225  98 for (int i = 0; i < size; i++) {
226  90 String element = check.substring(i, i + 1);
227   
228  90 int digit;
229  90 if (size == value10 && i == value09 && element.equalsIgnoreCase("x")) {
230  1 digit = value10;
231    } else {
232  89 digit = Integer.parseInt(element);
233    }
234   
235  89 if (size == value10) {
236  50 tmp += digit * (value10 - i);
237  50 LOGGER.log("(ISBN-10 SUM: " + tmp, LogLevel.DEBUG);
238    } else {
239  39 if (i % 2 == 0) {
240  21 tmp += digit;
241    } else {
242  18 tmp += digit * 3;
243    }
244  39 LOGGER.log("(ISBN-13 SUM: " + tmp, LogLevel.DEBUG);
245    }
246    }
247   
248  8 if (size == value10) {
249  5 if (tmp % value11 == 0) {
250  3 success = true;
251    }
252    } else {
253  3 if (tmp % value10 == 0) {
254  2 success = true;
255    }
256    }
257  8 return success;
258    }
259   
260    /**
261    * Luhns Algortihm is secure way to find for an number it's corrsponding
262    * checksum (validation key). This algorithm is used by Visa, Mastercard and
263    * others to verify the credit card number.This mechanism can also used to
264    * verify gift card codes.<br>
265    * You need to place the number wich have verified and it's corresponding
266    * checksum. If the validation succeed by the correct checksum the method
267    * return tue.
268    *
269    * @param numbers as Integer
270    * @return true on success
271    */
 
272  2 toggle public static boolean luhnNumberChecksum(final String numbers) {
273  2 boolean success = false;
274  2 List<Integer> digits = StringUtils.splitDigitStringToIntegerList(numbers);
275   
276  2 int sum = 0;
277  2 int counter = 1;
278  2 for (Integer digit : digits) {
279   
280  27 if (counter % 2 == 0) {
281  13 sum += digit;
282   
283    } else {
284  14 int result = digit * 2;
285  14 if (result >= INT_10) {
286  7 List<Integer> stack = StringUtils.splitDigitStringToIntegerList(
287    String.valueOf(result));
288  7 int temporal = 0;
289  7 for (Integer element : stack) {
290  14 temporal += element;
291    }
292  7 sum += temporal;
293   
294    } else {
295  7 sum += result;
296    }
297    }
298  27 LOGGER.log("CNT: " + counter + "\tDigit: " + digit + "\tSUM: " + sum, LogLevel.DEBUG);
299  27 counter++;
300    }
301   
302  2 if (sum % INT_10 == 0) {
303  1 success = true;
304    }
305  2 return success;
306    }
307   
308    /**
309    * Validate a String against an regular expression and return true if the
310    * String matches the RegEx.
311    *
312    * @param content as String
313    * @param regEx as String
314    * @return true on success
315    */
 
316  310 toggle public static boolean validate(final String content, final String regEx) {
317  310 boolean test = false;
318  309 if (content.matches(regEx)) {
319  229 test = true;
320    } else {
321  80 String msg = "validate('" + regEx + "') did not match " + content;
322  80 LOGGER.log(msg, LogLevel.WARN);
323    }
324  309 return test;
325    }
326    }