1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.utils

File Validator.java

 

Coverage histogram

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

Code metrics

28
55
7
1
267
131
31
0.56
7.86
7
4.43

Classes

Class Line # Actions
Validator 15 55 0% 31 0
1.0100%
 

Contributing tests

This file is covered by 80 tests. .

Source view

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    * A simple RexEx Validator.<br>
10    * ### REGEX VALIDATION PATTERN<br>
11    * ### * = 0 or more quantifier<br>
12    * ### + = 1 or more quantifier<br>
13    * ### ? = 0 or 1 quantifier
14    */
 
15    public final class Validator {
16   
17    private static final Logger LOGGER = new LogbackLogger(Validator.class);
18   
19    /**
20    * Constructor.
21    */
 
22  1 toggle private Validator() {
23  1 throw new UnsupportedOperationException();
24    }
25   
26    /**
27    * Printable ASCII character: a-z A-Z.
28    */
29    public static final String ASCII_CHARACTER = "[a-zA-Z ]+";
30   
31    /**
32    * Boolean. 0/1 true/false TRUE/FALSE
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    * Digits form 0-9.
38    */
39    public static final String DIGIT = "[0-9 ]+";
40   
41    /**
42    * Floating point.
43    */
44    public static final String FLOATING_POINT = "-?[0-9 ]+(.[0-9]+)?";
45   
46    /**
47    * Letters a-z A-Z upper case and lower case. No digits, space or special
48    * characters.
49    */
50    public static final String LETTERS = "[a-zA-Z]+";
51   
52    /**
53    * RGB Color schema in HEX: #000000 to #ffffff.
54    */
55    public static final String RGB_COLOR
56    = "#[0-9a-fA-F]{3,3}([0-9a-fA-F]{3,3})?";
57   
58    /**
59    * Text.
60    */
61    public static final String TEXT
62    = "(\\w|\\s|\\d|[¬&§$@€# ?!.,;_Â$Ââ~<>:=_+*/%\\-\\(\\)\\{\\}\\[\\]])*";
63   
64    /**
65    * Time in 24 hour format: 00:00 to 23:59.
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    * IP Adress (Version 4) with optional port e. g.: 127.0.0.1:80
72    * (1-255).(0-255).(0-255).(0-255):(1-65535)
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])\\." // 1
77    + "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." // 2
78    + "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\." // 3
79    + "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]))" // 4
80    // Optinal Port
81    + "(:[1-5]?[0-9]{0,4}" // 1-59.999
82    + "|:6[0-4][0-9][0-9][0-9]" // 60.000-64.999
83    + "|:65[0-4][0-9][0-9]" // 65.000-65.499
84    + "|:655[0-2][0-9]" // 65.500-65.529
85    + "|:6553[0-5])?"; // 65.530-65.535
86   
87    /**
88    * Test if a email address in the typical format, like:<br>
89    * _a-zA-Z0-9-(.)_a-zA-Z0-9-(@)[a-zA-Z0-9-]*(.)a-zA-Z{2}.
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    * Version number based on semantic versioning. Major.Minor.Patch-SNAPSHOT :
96    * 1.0; 1.0-SNAPSHOT; 1.0.0-SNAPSHOT :: 000.000.000-ABCDEFGHIJ
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    * Check if the variable test inside the range of the borders min and max.
106    * The borders and all numbers between are allowed values for the variable.
107    *
108    * @param test as Integer
109    * @param min as Integer
110    * @param max as Integer
111    * @return true on success
112    */
 
113  5 toggle 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    * Check if is a given date after the given boundary. If the check date
126    * equal to the after boundary, the validation will fail.<br>
127    * Sample:
128    * <code>Validator.isDateAfter(new DateTime(), new DateTime(2015, 12, 31, 23, 59));</code>
129    * Validate to TRUE because: now() is after 2015
130    * <br><br>
131    * Convert java.util.Date to ZonedDateTime
132    * <code>ZonedDateTime.from(Date.toInstant();</code>
133    *
134    * @param check as DateTime
135    * @param after as DateTime
136    * @return true on success
137    */
 
138  12 toggle 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    * Check if is a given date before the given boundary. If the check date
148    * equal to the before boundary, the validation will fail.<br>
149    * Sample:
150    * <code>Validator.isDateBefore(new DateTime(2015, 12, 31, 23, 59), new DateTime());</code>
151    * Validate to TRUE because: 2015 is before now()
152    * <br><br>
153    * Convert java.util.Date to ZonedDateTime
154    * <code>ZonedDateTime.from(Date.toInstant();</code>
155    *
156    * @param check as DateTime
157    * @param before as DateTime
158    * @return true on success
159    */
 
160  13 toggle 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    * Test if a given date is inside a range between a min and max. The
170    * boundaries are inside the range.
171    * <br><br>
172    * Convert java.util.Date to ZonedDateTime
173    * <code>ZonedDateTime.from(Date.toInstant();</code>
174    *
175    * @param check as DateTime
176    * @param min as DateTime
177    * @param max as DateTime
178    * @return true on success
179    */
 
180  13 toggle 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    * Test if a 10 or 13 digit ISBN number is valid.
195    *
196    * @param isbn as String
197    * @return true on success
198    */
 
199  12 toggle 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    * Validate a String against an regular expression and return true if the
251    * String matches the RegEx.
252    *
253    * @param content as String
254    * @param regEx as String
255    * @return true on success
256    */
 
257  294 toggle 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    }