1 16 package org.apache.cocoon.forms.binding; 17 18 import java.util.Iterator ; 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.components.flow.javascript.ScriptableMap; 25 import org.apache.cocoon.forms.formmodel.Widget; 26 import org.apache.cocoon.forms.util.JavaScriptHelper; 27 import org.apache.commons.jxpath.JXPathContext; 28 import org.apache.commons.jxpath.Pointer; 29 import org.mozilla.javascript.Function; 30 import org.mozilla.javascript.Scriptable; 31 32 37 public class JavaScriptJXPathBinding extends JXPathBindingBase { 38 39 private final Context avalonContext; 40 private final String id; 41 private final String path; 42 private final Function loadScript; 43 private final Function saveScript; 44 private final Scriptable childBindings; 45 private final Map childBindingsMap; 46 47 final static String [] LOAD_PARAMS = { "widget", "jxpathPointer", "jxpathContext", "childBindings" }; 48 final static String [] SAVE_PARAMS = { "widget", "jxpathPointer", "jxpathContext", "childBindings" }; 49 50 public JavaScriptJXPathBinding( 51 Context context, JXPathBindingBuilderBase.CommonAttributes commonAtts, String id, 52 String path, Function loadScript, Function saveScript, Map childBindings) { 53 super(commonAtts); 54 this.id = id; 55 this.path = path; 56 this.loadScript = loadScript; 57 this.saveScript = saveScript; 58 this.avalonContext = context; 59 60 for(Iterator iter = childBindings.values().iterator(); iter.hasNext(); ) { 62 ((Binding)iter.next()).setParent(this); 63 } 64 65 this.childBindingsMap = childBindings; 66 this.childBindings = new ScriptableMap(childBindings); 67 } 68 69 public String getPath() { return path; } 70 public String getId() { return id; } 71 public Context getContext() { return avalonContext; } 72 public Function getLoadScript() { return loadScript; } 73 public Function getSaveScript() { return saveScript; } 74 public Map getChildBindingsMap() { return childBindingsMap; } 75 76 public void doLoad(Widget frmModel, JXPathContext jctx) { 77 if (this.loadScript != null) { 78 Widget widget = selectWidget(frmModel,this.id); 79 80 Pointer pointer = jctx.getPointer(this.path); 82 83 Map objectModel = ContextHelper.getObjectModel(this.avalonContext); 84 85 try { 86 JXPathContext newCtx = pointer.getNode() == null ? null : 87 jctx.getRelativeContext(pointer); 88 89 JavaScriptHelper.callFunction(this.loadScript, widget, 90 new Object [] {widget, pointer, newCtx, this.childBindings}, objectModel); 91 92 } catch(RuntimeException re) { 93 throw re; 95 } catch(Exception e) { 96 throw new CascadingRuntimeException("Error invoking JavaScript event handler", e); 97 } 98 } else { 99 if (this.getLogger().isInfoEnabled()) { 100 this.getLogger().info("[Javascript Binding] - loadForm: No javascript code avaliable. Widget id=" + this.getId()); 101 } 102 } 103 } 104 105 public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException { 106 if (this.saveScript != null) { 107 Widget widget = selectWidget(frmModel,this.id); 108 109 Pointer pointer = jctx.createPath(this.path); 111 JXPathContext widgetCtx = jctx.getRelativeContext(pointer); 112 try { 113 Map objectModel = ContextHelper.getObjectModel(this.avalonContext); 114 115 JavaScriptHelper.callFunction(this.saveScript, widget, 116 new Object [] {widget, pointer, widgetCtx, this.childBindings}, objectModel); 117 118 } catch(RuntimeException re) { 119 throw re; 121 } catch(Exception e) { 122 throw new CascadingRuntimeException("Error invoking JavaScript event handler", e); 123 } 124 } else { 125 if (this.getLogger().isInfoEnabled()) { 126 this.getLogger().info("[Javascript Binding] - saveForm: No code avaliable on the javascript binding with id \"" + this.getId() + "\""); 127 } 128 } 129 } 130 } 131 | Popular Tags |