]> git.mxchange.org Git - jcoreee.git/blob - src/org/mxchange/jsfcore/validator/string/BaseStringValidator.java
Renamed variable to make it more clear
[jcoreee.git] / src / org / mxchange / jsfcore / validator / string / BaseStringValidator.java
1 /*\r
2  * Copyright (C) 2015 Roland Haeder\r
3  *\r
4  * This program is free software: you can redistribute it and/or modify\r
5  * it under the terms of the GNU General Public License as published by\r
6  * the Free Software Foundation, either version 3 of the License, or\r
7  * (at your option) any later version.\r
8  *\r
9  * This program is distributed in the hope that it will be useful,\r
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License\r
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
16  */\r
17 package org.mxchange.jsfcore.validator.string;\r
18 \r
19 import java.text.MessageFormat;\r
20 import java.util.Arrays;\r
21 import javax.faces.application.FacesMessage;\r
22 import javax.faces.component.UIComponent;\r
23 import javax.faces.context.FacesContext;\r
24 import javax.faces.validator.ValidatorException;\r
25 import org.mxchange.jsfcore.validator.BaseObjectValidator;\r
26 \r
27 /**\r
28  * A general string validation class. You normally want to inherit from this\r
29  * class for many form fields, e.g. surname, street name, city name and such.\r
30  *\r
31  * @author Roland Haeder\r
32  */\r
33 public abstract class BaseStringValidator extends BaseObjectValidator {\r
34 \r
35         /**\r
36          * Pre-validation of value, e.g. not null\r
37          *\r
38          * @param context FacesContext instance\r
39          * @param component UIComponent instance\r
40          * @param value Value to check\r
41          * @param requiredFields Array of required field names (ending with)\r
42          * @throws ValidatorException If something more horrible went wrong\r
43          */\r
44         @Override\r
45         protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {\r
46                 // Trace message\r
47                 this.getLogger().trace(MessageFormat.format("context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields)));\r
48 \r
49                 // Pre-validate (e.g. on null)\r
50                 super.preValidate(context, component, value, requiredFields);\r
51 \r
52                 // Get client id and init message + key\r
53                 String clientId = component.getClientId();\r
54                 FacesMessage facesMessage = null;\r
55                 String errKey;\r
56 \r
57                 // So far all fine!\r
58                 for (final String field : requiredFields) {\r
59                         // Debug message\r
60                         this.getLogger().debug(MessageFormat.format("field={0},clientId={1}", field, clientId));\r
61 \r
62                         // Is it the same?\r
63                         if (clientId.endsWith(field)) {\r
64                                 if (!(value instanceof String)) {\r
65                                         // Value is empty\r
66                                         errKey = String.format("error.%s.is_not_string", field);\r
67 \r
68                                         facesMessage = new FacesMessage(this.getMessageStringFromKey(errKey));\r
69                                 }\r
70 \r
71                                 // Cast to string\r
72                                 String str = (String) value;\r
73 \r
74                                 // Is it empty?\r
75                                 if (str.isEmpty()) {\r
76                                         // Value is empty\r
77                                         errKey = String.format("error.%s.is_empty", field);\r
78 \r
79                                         facesMessage = new FacesMessage(this.getMessageStringFromKey(errKey));\r
80                                 }\r
81                         }\r
82                 }\r
83 \r
84                 // Debug message\r
85                 this.getLogger().debug(MessageFormat.format("facesMessage={0}", facesMessage));\r
86 \r
87                 // Is it not null?\r
88                 if (null != facesMessage) {\r
89                         throw new ValidatorException(facesMessage);\r
90                 }\r
91 \r
92                 // Trace message\r
93                 this.getLogger().trace("EXIT!");\r
94         }\r
95 }\r