1
2
3
4 package org.rcfaces.core.internal.tools;
5
6 import java.util.Iterator;
7 import java.util.Locale;
8 import java.util.TimeZone;
9
10 import javax.faces.FacesException;
11 import javax.faces.component.UIComponent;
12 import javax.faces.component.ValueHolder;
13 import javax.faces.context.FacesContext;
14 import javax.faces.convert.Converter;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.rcfaces.core.component.capability.IClientValidationCapability;
19 import org.rcfaces.core.internal.RcfacesContext;
20 import org.rcfaces.core.internal.manager.ITransientAttributesManager;
21 import org.rcfaces.core.internal.util.CommandParserIterator;
22 import org.rcfaces.core.internal.util.CommandParserIterator.ICommand;
23 import org.rcfaces.core.internal.validator.IClientValidatorDescriptor;
24 import org.rcfaces.core.internal.validator.IClientValidatorsRegistry;
25 import org.rcfaces.core.internal.validator.IServerConverter;
26
27
28
29
30
31
32 public class ClientValidatorTools {
33 private static final String REVISION = "$Revision$";
34
35 private static final Log LOG = LogFactory.getLog(CheckTools.class);
36
37 private static final String CLIENT_VALIDATION_CONTEXT_PROPERTY = "org.rcfaces.core.CLIENT_VALIDATOR_CONTEXT";
38
39 public static void setClientValidator(FacesContext facesContext,
40 IClientValidationCapability clientValidationCapability) {
41
42 if (facesContext == null) {
43 facesContext = FacesContext.getCurrentInstance();
44 }
45
46 if ((clientValidationCapability instanceof ValueHolder) == false) {
47 throw new FacesException("Invalid clientValidationCapability '"
48 + clientValidationCapability + "'.");
49 }
50
51 IClientValidationContext clientValidationContext = getClientValidationContext(
52 facesContext, clientValidationCapability);
53
54 if (LOG.isDebugEnabled()) {
55 LOG.debug("clientValidationContext='" + clientValidationContext
56 + "' for component='" + clientValidationCapability + "'.");
57 }
58
59 if (clientValidationContext == null) {
60 return;
61 }
62
63 ValueHolder valueHolder = (ValueHolder) clientValidationCapability;
64
65 if (valueHolder.getConverter() != null) {
66
67 if (LOG.isDebugEnabled()) {
68 LOG
69 .debug("Converter of valueHolder is already setted for component='"
70 + clientValidationCapability + "'.");
71 }
72 return;
73 }
74
75 IServerConverter serverConverter = clientValidationContext
76 .getClientValidatorDescriptor().getServerConverter();
77
78 if (serverConverter != null) {
79 Converter converter = serverConverter.getInstance(facesContext,
80 (UIComponent) valueHolder);
81
82 if (converter != null) {
83 if (LOG.isDebugEnabled()) {
84 LOG.debug("Set Converter '" + converter
85 + "' to component='" + clientValidationCapability
86 + "'.");
87 }
88
89 valueHolder.setConverter(converter);
90 }
91 }
92 }
93
94 public static IClientValidationContext getClientValidationContext(
95 FacesContext facesContext,
96 IClientValidationCapability clientValidationCapability) {
97
98 ITransientAttributesManager transientAttributesManager = null;
99
100 if (clientValidationCapability instanceof ITransientAttributesManager) {
101 transientAttributesManager = (ITransientAttributesManager) clientValidationCapability;
102
103 Object object = transientAttributesManager
104 .getTransientAttribute(CLIENT_VALIDATION_CONTEXT_PROPERTY);
105
106 if (object instanceof IClientValidationContext) {
107 return (IClientValidationContext) object;
108 }
109 if (object != null) {
110 return null;
111 }
112 }
113
114 String validator = clientValidationCapability.getClientValidator();
115
116 if (validator == null) {
117
118
119 if (transientAttributesManager != null) {
120 transientAttributesManager.setTransientAttribute(
121 CLIENT_VALIDATION_CONTEXT_PROPERTY, Boolean.FALSE);
122 }
123
124 return null;
125 }
126
127 Iterator it = new CommandParserIterator(validator);
128 if (it.hasNext() == false) {
129 if (transientAttributesManager != null) {
130 transientAttributesManager.setTransientAttribute(
131 CLIENT_VALIDATION_CONTEXT_PROPERTY, Boolean.FALSE);
132 }
133 return null;
134 }
135
136 final CommandParserIterator.ICommand command = (ICommand) it.next();
137
138 if (it.hasNext()) {
139 throw new FacesException(
140 "Validator does not support multiple expression.");
141 }
142
143 if (facesContext == null) {
144 facesContext = FacesContext.getCurrentInstance();
145 }
146
147 IClientValidatorsRegistry clientValidatorManager = RcfacesContext
148 .getInstance(facesContext).getClientValidatorsRegistry();
149 if (clientValidatorManager == null) {
150
151
152
153
154 if (transientAttributesManager != null) {
155 transientAttributesManager.setTransientAttribute(
156 CLIENT_VALIDATION_CONTEXT_PROPERTY, Boolean.FALSE);
157 }
158 return null;
159 }
160
161 Locale locale = ContextTools.getUserLocale(facesContext);
162 TimeZone timeZone = ContextTools.getUserTimeZone(facesContext);
163
164 final IClientValidatorDescriptor validatorDescriptor = clientValidatorManager
165 .getClientValidatorById(facesContext, command.getName(),
166 locale, timeZone);
167 if (validatorDescriptor == null) {
168 throw new FacesException("Can not find validator '"
169 + command.getName() + "' for component '"
170 + ((UIComponent) clientValidationCapability).getId()
171 + "' !");
172 }
173
174 IClientValidationContext clientValidatorContext = new IClientValidationContext() {
175 private static final String REVISION = "$Revision$";
176
177 public ICommand getClientValidatorCommand() {
178 return command;
179 }
180
181 public IClientValidatorDescriptor getClientValidatorDescriptor() {
182 return validatorDescriptor;
183 }
184 };
185
186 if (transientAttributesManager != null) {
187 transientAttributesManager.setTransientAttribute(
188 CLIENT_VALIDATION_CONTEXT_PROPERTY, clientValidatorContext);
189 }
190
191 return clientValidatorContext;
192 }
193
194
195
196
197
198
199 public static interface IClientValidationContext {
200 IClientValidatorDescriptor getClientValidatorDescriptor();
201
202 CommandParserIterator.ICommand getClientValidatorCommand();
203 }
204 }