KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > validation > impl > JavaScriptValidator


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.validation.impl;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.CascadingRuntimeException;
21 import org.apache.avalon.framework.context.Context;
22 import org.apache.cocoon.components.ContextHelper;
23 import org.apache.cocoon.forms.formmodel.Widget;
24 import org.apache.cocoon.forms.util.JavaScriptHelper;
25 import org.apache.cocoon.forms.validation.ValidationError;
26 import org.apache.cocoon.forms.validation.ValidationErrorAware;
27 import org.apache.cocoon.forms.validation.WidgetValidator;
28 import org.apache.excalibur.xml.sax.XMLizable;
29 import org.mozilla.javascript.Function;
30
31 /**
32  * A {@link org.apache.cocoon.forms.validation.WidgetValidator} implemented as a JavaScript snippet.
33  * <p>
34  * This snippet must return a value which can be of different types. The only way to indicate
35  * successfull validation is to return a boolean <code>true</code> value.
36  * <p>
37  * To indicate validation error, a number of result types are possible:
38  * <ul>
39  * <li>A boolean <code>false</code>: the validator <strong>must</strong> then have
40  * set a validation error on the validated widget or one of its children.</li>
41  * <li>A {@link ValidationError}: this error is then set on the validated widget.</li>
42  * <li>A <code>String</code>: a validation error using that string as a non-i18nized message is
43  * then set on the validated widget</li>
44  * <li>An <code>XMLizable</code> such as {@link org.apache.cocoon.forms.util.I18nMessage}: this
45  * xmlizable is used to build a validation error that is set on the validated widget.</li>
46  * </ul>
47  * <p>
48  * The JavaScript snippet has the "this" and "widget" variables set to the validated widget, and, if the form
49  * is used in a flowscript, can use the flow's global values and fonctions and the <code>cocoon</code> object.
50  *
51  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
52  * @version $Id: JavaScriptValidator.java 328856 2005-10-27 11:33:59Z sylvain $
53  */

54 public class JavaScriptValidator implements WidgetValidator {
55     
56     private final Function function;
57     private final Context avalonContext;
58     
59     public JavaScriptValidator(Context context, Function function) {
60         this.function = function;
61         this.avalonContext = context;
62     }
63
64     public final boolean validate(Widget widget) {
65
66         Map JavaDoc objectModel = ContextHelper.getObjectModel(this.avalonContext);
67
68         Object JavaDoc result;
69             
70         try {
71             result = JavaScriptHelper.callFunction(this.function, widget, new Object JavaDoc[] {widget}, objectModel);
72         } catch(RuntimeException JavaDoc re) {
73             throw re; // rethrow
74
} catch(Exception JavaDoc e) {
75             throw new CascadingRuntimeException("Error invoking JavaScript event handler", e);
76         }
77
78         if (result == null) {
79             throw new RuntimeException JavaDoc("Validation script did not return a value");
80         }
81
82         if (result instanceof Boolean JavaDoc) {
83             return ((Boolean JavaDoc)result).booleanValue();
84         }
85         
86         if (result instanceof ValidationError) {
87             // Set the validation error on the widget
88
((ValidationErrorAware)widget).setValidationError((ValidationError)result);
89             return false;
90         }
91
92         if (result instanceof String JavaDoc) {
93             // Set a non-i18n error on the current widget
94
((ValidationErrorAware)widget).setValidationError(new ValidationError((String JavaDoc)result, false));
95             return false;
96         }
97
98         if (result instanceof XMLizable) {
99             // Set a xmlizable error (e.g. I18nMessage) on the current widget
100
((ValidationErrorAware)widget).setValidationError(new ValidationError((XMLizable)result));
101             return false;
102         }
103
104         throw new RuntimeException JavaDoc("Validation script returned an unexpected value of type " + result.getClass());
105     }
106 }
107
Popular Tags