1 /*
2 * $Id$
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.struts.faces.component;
23
24 import jakarta.el.ValueExpression;
25 import jakarta.faces.component.UIOutput;
26 import jakarta.faces.context.FacesContext;
27
28 /**
29 * {@code JavascriptValidatorComponent} is a specialized
30 * subclass of {@code UIOutput} that supports automatic creation of
31 * JavaScript for client side validation based on the validation
32 * rules loaded by the {@code ValidatorPlugIn} defined in the
33 * {@code struts-config.xml} file. This is based on the code in
34 * the corresponding class of the Struts HTML tag library, modified
35 * as needed to reflect differences in the way JavaServer Faces
36 * renders field identifiers.
37 *
38 * @version $Rev$ $Date$
39 */
40 public class JavascriptValidatorComponent extends UIOutput {
41
42
43 // ------------------------------------------------------------ Constructors
44
45
46 /**
47 * Create a new {@link JavascriptValidatorComponent} with default properties.
48 */
49 public JavascriptValidatorComponent() {
50
51 super();
52 setRendererType("org.apache.struts.faces.JavascriptValidator");
53
54 }
55
56
57 // ------------------------------------------------------ Instance Variables
58
59
60 /**
61 * MessageResources attribute key to use for message lookup.
62 */
63 private String bundle = null;
64
65 /**
66 * The name of the form that corresponds with the action name
67 * in struts-config.xml. Specifying a form name places a
68 * <script> </script> around the JavaScript.
69 */
70 private String formName = null;
71
72 /**
73 * The current page number of a multi-part form.
74 * Only valid when the {@code formName} attribute is set.
75 */
76 private int page = 0;
77 private boolean pageSet = false;
78
79 /**
80 * This will be used as is for the JavaScript validation method name if it
81 * has a value. This is the method name of the main JavaScript method that
82 * the form calls to perform validations.
83 */
84 private String method = null;
85
86 /**
87 * The static JavaScript methods will only be printed if this is set to
88 * {@code true}.
89 */
90 private boolean staticJavascript = true;
91 private boolean staticJavascriptSet = false;
92
93 /**
94 * The dynamic JavaScript objects will only be generated if this is set to
95 * {@code true}.
96 */
97 private boolean dynamicJavascript = true;
98 private boolean dynamicJavascriptSet = false;
99
100 /**
101 * The {@code src} attribute for html script element (used to include an
102 * external script resource). The {@code src} attribute is only recognized
103 * when the {@code formName} attribute is specified.
104 */
105 private String src = null;
106
107 /**
108 * The JavaScript methods will enclosed with html comments if this is set to
109 * {@code true}.
110 */
111 private boolean htmlComment = true;
112 private boolean htmlCommentSet = false;
113
114 /**
115 * Hide JavaScript methods in a CDATA section for XHTML when {@code true}.
116 */
117 private boolean cdata = true;
118 private boolean cdataSet = false;
119
120
121 // ---------------------------------------------------- Component Properties
122
123
124 /**
125 * Return the MessageResources key.
126 */
127 public String getBundle() {
128
129 if (bundle != null) {
130 return bundle;
131 }
132 ValueExpression vb = getValueExpression("bundle");
133 if (vb != null) {
134 return (String) vb.getValue(getFacesContext().getELContext());
135 } else {
136 return null;
137 }
138
139 }
140
141
142 /**
143 * Set the MessageResources key.
144 *
145 * @param bundle The new key
146 */
147 public void setBundle(String bundle) {
148
149 this.bundle = bundle;
150
151 }
152
153 /**
154 * Return the component family to which this component belongs.
155 */
156 public String getFamily() {
157
158 return "org.apache.struts.faces.JavascriptValidator";
159
160 }
161
162 /**
163 * Gets the key (form name) that will be used
164 * to retrieve a set of validation rules to be
165 * performed on the bean passed in for validation.
166 */
167 public String getFormName() {
168
169 if (formName != null) {
170 return formName;
171 }
172 ValueExpression vb = getValueExpression("formName");
173 if (vb != null) {
174 return (String) vb.getValue(getFacesContext().getELContext());
175 } else {
176 return null;
177 }
178
179 }
180
181 /**
182 * Sets the key (form name) that will be used
183 * to retrieve a set of validation rules to be
184 * performed on the bean passed in for validation.
185 * Specifying a form name places a
186 * <script> </script> tag around the javascript.
187 */
188 public void setFormName(String formName) {
189
190 this.formName = formName;
191
192 }
193
194 /**
195 * Gets the current page number of a multi-part form.
196 * Only field validations with a matching page numer
197 * will be generated that match the current page number.
198 * Only valid when the formName attribute is set.
199 */
200 public int getPage() {
201
202 if (pageSet) {
203 return page;
204 }
205 ValueExpression vb = getValueExpression("page");
206 if (vb != null) {
207 return (Integer) vb.getValue(getFacesContext().getELContext());
208 } else {
209 return 0;
210 }
211
212 }
213
214 /**
215 * Sets the current page number of a multi-part form.
216 * Only field validations with a matching page numer
217 * will be generated that match the current page number.
218 * Only valid when the formName attribute is set.
219 */
220 public void setPage(int page) {
221
222 this.page = page;
223 this.pageSet = true;
224
225 }
226
227 /**
228 * Gets the method name that will be used for the JavaScript
229 * validation method name if it has a value. This overrides
230 * the auto-generated method name based on the key (form name)
231 * passed in.
232 */
233 public String getMethod() {
234
235 if (method != null) {
236 return method;
237 }
238 ValueExpression vb = getValueExpression("method");
239 if (vb != null) {
240 return (String) vb.getValue(getFacesContext().getELContext());
241 } else {
242 return null;
243 }
244
245 }
246
247 /**
248 * Sets the method name that will be used for the JavaScript
249 * validation method name if it has a value. This overrides
250 * the auto-generated method name based on the key (form name)
251 * passed in.
252 */
253 public void setMethod(String method) {
254
255 this.method = method;
256
257 }
258
259 /**
260 * Gets whether or not to generate the static
261 * JavaScript. If this is set to 'true', which
262 * is the default, the static JavaScript will be generated.
263 */
264 public boolean isStaticJavascript() {
265
266 if (staticJavascriptSet) {
267 return staticJavascript;
268 }
269 ValueExpression vb = getValueExpression("staticJavascript");
270 if (vb != null) {
271 return (Boolean) vb.getValue(getFacesContext().getELContext());
272 } else {
273 return true;
274 }
275
276 }
277
278 /**
279 * Sets whether or not to generate the static
280 * JavaScript. If this is set to 'true', which
281 * is the default, the static JavaScript will be generated.
282 */
283 public void setStaticJavascript(boolean staticJavascript) {
284
285 this.staticJavascript = staticJavascript;
286 this.staticJavascriptSet = true;
287
288 }
289
290 /**
291 * Gets whether or not to generate the dynamic
292 * JavaScript. If this is set to 'true', which
293 * is the default, the dynamic JavaScript will be generated.
294 */
295 public boolean isDynamicJavascript() {
296
297 if (dynamicJavascriptSet) {
298 return dynamicJavascript;
299 }
300 ValueExpression vb = getValueExpression("dynamicJavascript");
301 if (vb != null) {
302 return (Boolean) vb.getValue(getFacesContext().getELContext());
303 } else {
304 return true;
305 }
306
307 }
308
309 /**
310 * Sets whether or not to generate the dynamic
311 * JavaScript. If this is set to 'true', which
312 * is the default, the dynamic JavaScript will be generated.
313 */
314 public void setDynamicJavascript(boolean dynamicJavascript) {
315
316 this.dynamicJavascript = dynamicJavascript;
317 this.dynamicJavascriptSet = true;
318
319 }
320
321 /**
322 * Gets the src attribute's value when defining
323 * the html script element.
324 */
325 public String getSrc() {
326
327 if (src != null) {
328 return src;
329 }
330 ValueExpression vb = getValueExpression("src");
331 if (vb != null) {
332 return (String) vb.getValue(getFacesContext().getELContext());
333 } else {
334 return null;
335 }
336
337 }
338
339 /**
340 * Sets the src attribute's value when defining
341 * the html script element. The src attribute is only recognized
342 * when the formName attribute is specified.
343 */
344 public void setSrc(String src) {
345
346 this.src = src;
347
348 }
349
350 /**
351 * Gets whether or not to delimit the JavaScript with HTML comments.
352 * If this is set to 'true', which is the default, the HTML-
353 * Comment will be surround the JavaScript.
354 */
355 public boolean isHtmlComment() {
356
357 if (htmlCommentSet) {
358 return htmlComment;
359 }
360 ValueExpression vb = getValueExpression("htmlComment");
361 if (vb != null) {
362 return (Boolean) vb.getValue(getFacesContext().getELContext());
363 } else {
364 return true;
365 }
366
367 }
368
369 /**
370 * Sets whether or not to delimit the JavaScript with HTML comments.
371 * If this is set to 'true', which is the default, the HTML-
372 * Comment will be surround the JavaScript.
373 */
374 public void setHtmlComment(boolean htmlComment) {
375
376 this.htmlComment = htmlComment;
377 this.htmlCommentSet = true;
378
379 }
380
381 /**
382 * Returns the cdata setting "true" or "false".
383 * @return String - "true" if JavaScript will be hidden in a CDATA section
384 */
385 public boolean isCdata() {
386
387 if (cdataSet) {
388 return cdata;
389 }
390 ValueExpression vb = getValueExpression("cdata");
391 if (vb != null) {
392 return (Boolean) vb.getValue(getFacesContext().getELContext());
393 } else {
394 return true;
395 }
396
397 }
398
399 /**
400 * Sets the CDATA status.
401 * @param cdata The CDATA to set
402 */
403 public void setCdata(boolean cdata) {
404
405 this.cdata = cdata;
406 this.cdataSet = true;
407
408 }
409
410
411 // ---------------------------------------------------------- UIForm Methods
412
413
414 /**
415 * <p>Restore our state from the specified object.</p>
416 *
417 * @param context <code>FacesContext</code> for the current request
418 * @param state Object containing our saved state
419 */
420 public void restoreState(FacesContext context, Object state) {
421
422 Object values[] = (Object[]) state;
423 super.restoreState(context, values[0]);
424 bundle = (String) values[1];
425 formName = (String) values[2];
426 page = (Integer) values[3];
427 pageSet = (Boolean) values[4];
428 method = (String) values[5];
429 staticJavascript = (Boolean) values[6];
430 staticJavascriptSet = (Boolean) values[7];
431 dynamicJavascript = (Boolean) values[8];
432 dynamicJavascriptSet = (Boolean) values[9];
433 src = (String) values[10];
434 htmlComment = (Boolean) values[11];
435 htmlCommentSet = (Boolean) values[12];
436 cdata = (Boolean) values[13];
437 cdataSet = (Boolean) values[14];
438
439 }
440
441
442 /**
443 * <p>Create and return an object representing our state to be saved.</p>
444 *
445 * @param context <code>FacesContext</code> for the current request
446 */
447 public Object saveState(FacesContext context) {
448
449 Object values[] = new Object[15];
450 values[0] = super.saveState(context);
451 values[1] = bundle;
452 values[2] = formName;
453 values[3] = page;
454 values[4] = pageSet;
455 values[5] = method;
456 values[6] = staticJavascript;
457 values[7] = staticJavascriptSet;
458 values[8] = dynamicJavascript;
459 values[9] = dynamicJavascriptSet;
460 values[10] = src;
461 values[11] = htmlComment;
462 values[12] = htmlCommentSet;
463 values[13] = cdata;
464 values[14] = cdataSet;
465 return values;
466
467 }
468
469
470 }