01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.woody.validation.impl;
18:
19: import java.util.Map;
20:
21: import org.apache.avalon.framework.CascadingRuntimeException;
22: import org.apache.avalon.framework.context.Context;
23: import org.apache.cocoon.components.ContextHelper;
24: import org.apache.cocoon.woody.FormContext;
25: import org.apache.cocoon.woody.formmodel.Widget;
26: import org.apache.cocoon.woody.util.JavaScriptHelper;
27: import org.apache.cocoon.woody.validation.WidgetValidator;
28: import org.mozilla.javascript.Function;
29:
30: /**
31: * A {@link org.apache.cocoon.woody.validation.WidgetValidator} implemented as a JavaScript snippet.
32: * This snippet must return a boolean value. If it returns <code>false</code>, it <strong>must</strong> have
33: * set a validation error on the validated widget or one of its children.
34: * <p>
35: * The JavaScript snippet has the "this" variable set to the validated widget, and, if the form is used in a
36: * flowscript, can use the flow's global values and fonctions and the <code>cocoon</code> object.
37: *
38: * @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
39: * @version CVS $Id: JavaScriptValidator.java 433543 2006-08-22 06:22:54Z crossley $
40: */
41: public class JavaScriptValidator implements WidgetValidator {
42:
43: private final Function function;
44: private final Context avalonContext;
45:
46: public JavaScriptValidator(Context context, Function function) {
47: this .function = function;
48: this .avalonContext = context;
49: }
50:
51: /* (non-Javadoc)
52: * @see org.apache.cocoon.woody.validation.Validator#validate(org.apache.cocoon.woody.formmodel.Widget, org.apache.cocoon.woody.FormContext)
53: */
54: public final boolean validate(Widget widget, FormContext context) {
55:
56: Map objectModel = ContextHelper
57: .getObjectModel(this .avalonContext);
58:
59: Object result;
60:
61: try {
62: result = JavaScriptHelper.callFunction(this .function,
63: widget, new Object[] { widget }, objectModel);
64: } catch (RuntimeException re) {
65: throw re; // rethrow
66: } catch (Exception e) {
67: throw new CascadingRuntimeException(
68: "Error invoking JavaScript event handler", e);
69: }
70:
71: if (result == null) {
72: throw new RuntimeException(
73: "Validation script did not return a value");
74:
75: } else if (result instanceof Boolean) {
76: return ((Boolean) result).booleanValue();
77:
78: } else {
79: throw new RuntimeException(
80: "Validation script returned an unexpected value of type "
81: + result.getClass());
82: }
83: }
84: }
|