1 16 package org.apache.cocoon.woody.binding; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import org.apache.avalon.framework.CascadingRuntimeException; 22 import org.apache.cocoon.components.CocoonComponentManager; 23 import org.apache.cocoon.woody.formmodel.Widget; 24 import org.apache.cocoon.woody.util.JavaScriptHelper; 25 import org.apache.commons.jxpath.JXPathContext; 26 import org.apache.commons.jxpath.Pointer; 27 import org.mozilla.javascript.Script; 28 29 34 public class JavaScriptJXPathBinding extends JXPathBindingBase { 35 36 private final String id; 37 private final String path; 38 private final Script loadScript; 39 private final Script saveScript; 40 41 public JavaScriptJXPathBinding( 42 JXPathBindingBuilderBase.CommonAttributes commonAtts, String id, 43 String path, Script loadScript, Script saveScript) { 44 super(commonAtts); 45 this.id = id; 46 this.path = path; 47 this.loadScript = loadScript; 48 this.saveScript = saveScript; 49 } 50 51 public void doLoad(Widget frmModel, JXPathContext jctx) { 52 if (this.loadScript != null) { 53 Widget widget = frmModel.getWidget(this.id); 54 55 Pointer pointer = jctx.getPointer(this.path); 57 58 Map objectModel = CocoonComponentManager.getCurrentEnvironment().getObjectModel(); 61 62 try { 63 Map values = new HashMap (3); 64 values.put("widget", widget); 65 values.put("jxpathPointer", pointer); 66 if (pointer.getNode() != null) { 67 values.put("jxpathContext", jctx.getRelativeContext(pointer)); 68 } 69 70 JavaScriptHelper.execScript(this.loadScript, values, objectModel); 71 72 } catch(RuntimeException re) { 73 throw re; 75 } catch(Exception e) { 76 throw new CascadingRuntimeException("Error invoking JavaScript event handler", e); 77 } 78 } else { 79 if (this.getLogger().isInfoEnabled()) { 80 this.getLogger().info("[Javascript Binding] - loadForm: No javascript code avaliable. Widget id=" + this.getId()); 81 } 82 } 83 } 84 85 public void doSave(Widget frmModel, JXPathContext jctx) throws BindingException { 86 if (this.saveScript != null) { 87 Widget widget = frmModel.getWidget(this.id); 88 89 Pointer pointer = jctx.createPath(this.path); 91 JXPathContext widgetCtx = jctx.getRelativeContext(pointer); 92 try { 93 Map objectModel = CocoonComponentManager.getCurrentEnvironment().getObjectModel(); 96 97 Map values = new HashMap (); 98 values.put("widget", widget); 99 values.put("jxpathContext", widgetCtx); 100 values.put("jxpathPointer", pointer); 101 102 JavaScriptHelper.execScript(this.saveScript, values, objectModel); 103 104 } catch(RuntimeException re) { 105 throw re; 107 } catch(Exception e) { 108 throw new CascadingRuntimeException("Error invoking JavaScript event handler", e); 109 } 110 } else { 111 if (this.getLogger().isInfoEnabled()) { 112 this.getLogger().info("[Javascript Binding] - saveForm: No javascript code avaliable. <wb:javascript id=" + this.getId() + ">"); 113 } 114 } 115 } 116 } 117 | Popular Tags |