| 1 |
|
package yarfraw.utils; |
| 2 |
|
|
| 3 |
|
import java.net.URI; |
| 4 |
|
import java.net.URISyntaxException; |
| 5 |
|
import java.util.regex.Matcher; |
| 6 |
|
import java.util.regex.Pattern; |
| 7 |
|
|
| 8 |
|
import org.apache.commons.lang.ArrayUtils; |
| 9 |
|
|
| 10 |
|
import yarfraw.core.datamodel.ValidationException; |
| 11 |
|
|
| 12 |
|
public class ValidationUtils{ |
| 13 |
0 |
private ValidationUtils(){} |
| 14 |
15 |
private static final Pattern EMAIL = Pattern.compile( |
| 15 |
|
"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$", |
| 16 |
|
Pattern.CASE_INSENSITIVE); |
| 17 |
|
public static void validateNotNull(String message, Object... o) throws ValidationException{ |
| 18 |
156 |
if(!ArrayUtils.isEmpty(o)){ |
| 19 |
369 |
for(Object oo : o){ |
| 20 |
216 |
if(oo== null){ |
| 21 |
3 |
throw new ValidationException(message); |
| 22 |
|
} |
| 23 |
|
} |
| 24 |
|
} |
| 25 |
153 |
} |
| 26 |
|
|
| 27 |
|
public static void validateUri(String message, String... uri) throws ValidationException{ |
| 28 |
51 |
if(!ArrayUtils.isEmpty(uri)){ |
| 29 |
99 |
for(String s : uri){ |
| 30 |
54 |
if(s != null){ |
| 31 |
|
try { |
| 32 |
|
@SuppressWarnings("unused") |
| 33 |
51 |
URI u = new URI(s); |
| 34 |
6 |
} catch (URISyntaxException e) { |
| 35 |
6 |
throw new ValidationException(message, e); |
| 36 |
45 |
} |
| 37 |
|
} |
| 38 |
|
} |
| 39 |
|
} |
| 40 |
45 |
} |
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
public static void validateEmails(String message, String... emails) throws ValidationException{ |
| 49 |
3 |
if(!ArrayUtils.isEmpty(emails)){ |
| 50 |
3 |
for(String email : emails){ |
| 51 |
3 |
if(email != null){ |
| 52 |
3 |
Matcher m = EMAIL.matcher(email); |
| 53 |
3 |
if(!m.matches()){ |
| 54 |
3 |
throw new ValidationException(message); |
| 55 |
|
} |
| 56 |
|
} |
| 57 |
|
} |
| 58 |
|
} |
| 59 |
0 |
} |
| 60 |
|
} |