]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/validator/url/FinancialsUrlValidator.java
Don't cherry-pick:
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / validator / url / FinancialsUrlValidator.java
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jfinancials.validator.url;
18
19 import java.text.MessageFormat;
20 import java.util.regex.Pattern;
21 import javax.faces.application.FacesMessage;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.validator.FacesValidator;
25 import javax.faces.validator.ValidatorException;
26 import org.mxchange.jcoreee.validator.string.BaseStringValidator;
27
28 /**
29  * A validator for URL vallidation (only regex, but allow empty value if
30  * allowed).
31  * <p>
32  * @author Roland Häder<roland@mxchange.org>
33  */
34 @FacesValidator ("UrlValidator")
35 public class FinancialsUrlValidator extends BaseStringValidator {
36
37         /**
38          * Pattern matcher
39          */
40         private static final Pattern PATTERN_MATCHER = Pattern.compile(FinancialsUrlValidator.URL_REGEX);
41
42         /**
43          * Email pattern
44          */
45         private static final String URL_REGEX = "(http|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?"; //NOI18N
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 187_536_745_607_193L;
51
52         /**
53          * Whether empty data is allowed
54          */
55         private Boolean allowEmptyRequiredData;
56
57         /**
58          * Default constructor
59          */
60         public FinancialsUrlValidator () {
61                 this.allowEmptyRequiredData = Boolean.FALSE;
62         }
63
64         /**
65          * Setter for allowEmptyRequiredData flag
66          * <p>
67          * @param allowEmptyRequiredData Whether empty values are allowed
68          */
69         public void setAllowEmptyRequiredData (final Boolean allowEmptyRequiredData) {
70                 this.allowEmptyRequiredData = allowEmptyRequiredData;
71         }
72
73         @Override
74         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
75                 // The required field
76                 final String[] requiredFields = {"companyWebsiteUrl"}; //NOI18N
77
78                 // Pre-validation (example: not null, not a string, empty string ...)
79                 super.preValidate(context, component, value, requiredFields, this.allowEmptyRequiredData);
80
81                 // Is the email address empty and allowed?
82                 if (null == value && this.allowEmptyRequiredData) {
83                         // Then accept this here
84                         return;
85                 } else if (null == value) {
86                         // Abort here
87                         throw new ValidatorException(new FacesMessage("No empty URL allowed.")); //NOI18N
88                 }
89
90                 // Get string from object ... ;-)
91                 final String url = String.valueOf(value).trim();
92
93                 // Checks if the email address matches a regex ("low-level" check)
94                 // @TODO Should also be done by <f:validatorRegex />)
95                 final boolean matches = PATTERN_MATCHER.matcher(url).matches(); //NOI18N
96
97                 // Is the email address valid?
98                 if (!matches) {
99                         // Generate message
100                         String message = MessageFormat.format("URL {0} does not match regular expression.", url); //NOI18N
101
102                         // Not matching
103                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message));
104                 }
105         }
106
107 }