2 * Copyright (C) 2016, 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.validator.url;
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;
29 * A validator for URL vallidation (only regex, but allow empty value if
32 * @author Roland Häder<roland@mxchange.org>
34 @FacesValidator ("UrlValidator")
35 public class PizzaUrlValidator extends BaseStringValidator {
40 private static final Pattern PATTERN_MATCHER = Pattern.compile(PizzaUrlValidator.URL_REGEX);
45 private static final String URL_REGEX = "(http|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?"; //NOI18N
50 private static final long serialVersionUID = 187_536_745_607_193L;
55 public PizzaUrlValidator () {
59 public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
61 String[] requiredFields = {"companyWebsiteUrl"}; //NOI18N
63 // Default is to reject empty email address fields
64 Boolean allowEmptyValue = Boolean.FALSE;
66 // Is attribute "allowEmptyValue" set?
67 if (component.getAttributes().containsKey("allowEmptyValue")) { //NOI18N
69 Object attribute = component.getAttributes().get("allowEmptyValue"); //NOI18N
71 // Make sure, it is Boolean as no String is accepted anymore
72 if (!(attribute instanceof String)) {
73 // Not valid attribute, please use "true" or "false" (default)
74 throw new IllegalArgumentException("allowEmptyValue must be of type String. Please use \"true\" or \"false\" for f:attribute value."); //NOI18N
78 allowEmptyValue = Boolean.parseBoolean((String) attribute);
81 // Pre-validation (example: not null, not a string, empty string ...)
82 super.preValidate(context, component, value, requiredFields, allowEmptyValue);
84 // Is the email address empty and allowed?
85 if (null == value && allowEmptyValue) {
86 // Then accept this here
88 } else if (null == value) {
90 throw new ValidatorException(new FacesMessage("No empty URL allowed.")); //NOI18N
93 // Get string from object ... ;-)
94 String url = String.valueOf(value).trim();
96 // Checks if the email address matches a regex ("low-level" check)
97 // @TODO Should also be done by <f:validatorRegex />)
98 boolean matches = PATTERN_MATCHER.matcher(url).matches(); //NOI18N
100 // Is the email address valid?
103 String message = MessageFormat.format("URL {0} does not match regular expression.", url); //NOI18N
106 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message));