]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java
c7316a2f69df5a2c7f96e228dba9f2235c31878c
[jcore-utils.git] / src / org / mxchange / jcoreee / validator / string / BaseStringValidator.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcoreee.validator.string;
18
19 import java.text.MessageFormat;
20 import java.util.Arrays;
21 import javax.faces.application.FacesMessage;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.validator.ValidatorException;
25 import org.mxchange.jcoreee.validator.BaseObjectValidator;
26
27 /**
28  * A general string validation class. You normally want to inherit from this
29  * class for many form fields, e.g. surname, street name, city name and such.
30  *
31  * @author Roland Haeder
32  */
33 public abstract class BaseStringValidator extends BaseObjectValidator {
34
35         @Override
36         protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
37                 // Trace message
38                 this.getLogger().trace(MessageFormat.format("context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
39
40                 // Pre-validate (e.g. on null)
41                 super.preValidate(context, component, value, requiredFields);
42
43                 // Get client id and init message + key
44                 String clientId = component.getClientId();
45                 FacesMessage facesMessage = null;
46                 String errKey;
47
48                 // So far all fine, no check if the field is fine
49                 for (final String field : requiredFields) {
50                         // Debug message
51                         this.getLogger().debug(MessageFormat.format("field={0},clientId={1}", field, clientId)); //NOI18N
52
53                         // Is it the same?
54                         if (clientId.endsWith(field)) {
55                                 // Compare value's type
56                                 if (!(value instanceof String)) {
57                                         // Value is empty
58                                         errKey = String.format("error.%s.is_not_string", field); //NOI18N
59
60                                         facesMessage = new FacesMessage(this.getMessageStringFromKey(errKey));
61                                 }
62
63                                 // Cast to string
64                                 String str = (String) value;
65
66                                 // Is it empty?
67                                 if (str.isEmpty()) {
68                                         // Value is empty
69                                         errKey = String.format("error.%s.is_empty", field); //NOI18N
70
71                                         facesMessage = new FacesMessage(this.getMessageStringFromKey(errKey));
72                                 }
73                         }
74                 }
75
76                 // Debug message
77                 this.getLogger().debug(MessageFormat.format("facesMessage={0}", facesMessage)); //NOI18N
78
79                 // Is it not null?
80                 if (null != facesMessage) {
81                         throw new ValidatorException(facesMessage);
82                 }
83
84                 // Trace message
85                 this.getLogger().trace("EXIT!"); //NOI18N
86         }
87 }