1 16 package org.apache.cocoon.woody.validation.impl; 17 18 import java.util.Map ; 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 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 53 public final boolean validate(Widget widget, FormContext context) { 54 55 Map objectModel = ContextHelper.getObjectModel(this.avalonContext); 56 57 Object result; 58 59 try { 60 result = JavaScriptHelper.callFunction(this.function, widget, new Object [] {widget}, objectModel); 61 } catch(RuntimeException re) { 62 throw re; } catch(Exception e) { 64 throw new CascadingRuntimeException("Error invoking JavaScript event handler", e); 65 } 66 67 if (result == null) { 68 throw new RuntimeException ("Validation script did not return a value"); 69 70 } else if (result instanceof Boolean ) { 71 return ((Boolean )result).booleanValue(); 72 73 } else { 74 throw new RuntimeException ("Validation script returned an unexpected value of type " + result.getClass()); 75 } 76 } 77 } 78 | Popular Tags |