KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.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.woody.FormContext;
24 import org.apache.cocoon.woody.formmodel.Widget;
25 import org.apache.cocoon.woody.util.JavaScriptHelper;
26 import org.apache.cocoon.woody.validation.WidgetValidator;
27 import org.mozilla.javascript.Function;
28
29 /**
30  * A {@link org.apache.cocoon.woody.validation.WidgetValidator} implemented as a JavaScript snippet.
31  * This snippet must return a boolean value. If it returns <code>false</code>, it <strong>must</strong> have
32  * set a validation error on the validated widget or one of its children.
33  * <p>
34  * The JavaScript snippet has the "this" variable set to the validated widget, and, if the form is used in a
35  * flowscript, can use the flow's global values and fonctions and the <code>cocoon</code> object.
36  *
37  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
38  * @version CVS $Id: JavaScriptValidator.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  */

40 public class JavaScriptValidator implements WidgetValidator {
41     
42     private final Function function;
43     private final Context avalonContext;
44     
45     public JavaScriptValidator(Context context, Function function) {
46         this.function = function;
47         this.avalonContext = context;
48     }
49
50     /* (non-Javadoc)
51      * @see org.apache.cocoon.woody.validation.Validator#validate(org.apache.cocoon.woody.formmodel.Widget, org.apache.cocoon.woody.FormContext)
52      */

53     public final boolean validate(Widget widget, FormContext context) {
54
55         Map JavaDoc objectModel = ContextHelper.getObjectModel(this.avalonContext);
56
57         Object JavaDoc result;
58             
59         try {
60             result = JavaScriptHelper.callFunction(this.function, widget, new Object JavaDoc[] {widget}, objectModel);
61         } catch(RuntimeException JavaDoc re) {
62             throw re; // rethrow
63
} catch(Exception JavaDoc e) {
64             throw new CascadingRuntimeException("Error invoking JavaScript event handler", e);
65         }
66
67         if (result == null) {
68             throw new RuntimeException JavaDoc("Validation script did not return a value");
69             
70         } else if (result instanceof Boolean JavaDoc) {
71             return ((Boolean JavaDoc)result).booleanValue();
72             
73         } else {
74             throw new RuntimeException JavaDoc("Validation script returned an unexpected value of type " + result.getClass());
75         }
76     }
77 }
78
Popular Tags