]> git.mxchange.org Git - jcoreee.git/blob - src/org/mxchange/jsfcore/validator/string/BaseStringValidator.java
Some changes for internationalization + jcore.jar updated
[jcoreee.git] / src / org / mxchange / jsfcore / 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.jsfcore.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.jsfcore.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         /**
36          * Pre-validation of value, e.g. not null
37          *
38          * @param context FacesContext instance
39          * @param component UIComponent instance
40          * @param value Value to check
41          * @param requiredFields Array of required field names (ending with)
42          * @throws ValidatorException If something more horrible went wrong
43          */
44         @Override
45         protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
46                 // Trace message
47                 this.getLogger().trace(MessageFormat.format("context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
48
49                 // Pre-validate (e.g. on null)
50                 super.preValidate(context, component, value, requiredFields);
51
52                 // Get client id and init message + key
53                 String clientId = component.getClientId();
54                 FacesMessage facesMessage = null;
55                 String errKey;
56
57                 // So far all fine!
58                 for (final String field : requiredFields) {
59                         // Debug message
60                         this.getLogger().debug(MessageFormat.format("field={0},clientId={1}", field, clientId)); //NOI18N
61
62                         // Is it the same?
63                         if (clientId.endsWith(field)) {
64                                 if (!(value instanceof String)) {
65                                         // Value is empty
66                                         errKey = String.format("error.%s.is_not_string", field); //NOI18N
67
68                                         facesMessage = new FacesMessage(this.getMessageStringFromKey(errKey));
69                                 }
70
71                                 // Cast to string
72                                 String str = (String) value;
73
74                                 // Is it empty?
75                                 if (str.isEmpty()) {
76                                         // Value is empty
77                                         errKey = String.format("error.%s.is_empty", field); //NOI18N
78
79                                         facesMessage = new FacesMessage(this.getMessageStringFromKey(errKey));
80                                 }
81                         }
82                 }
83
84                 // Debug message
85                 this.getLogger().debug(MessageFormat.format("facesMessage={0}", facesMessage)); //NOI18N
86
87                 // Is it not null?
88                 if (null != facesMessage) {
89                         throw new ValidatorException(facesMessage);
90                 }
91
92                 // Trace message
93                 this.getLogger().trace("EXIT!"); //NOI18N
94         }
95 }