KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > binding > JavaScriptJXPathBinding


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.forms.binding;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
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 /**
33  *
34  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
35  * @version $Id: JavaScriptJXPathBinding.java 327146 2005-10-21 10:38:37Z sylvain $
36  */

37 public class JavaScriptJXPathBinding extends JXPathBindingBase {
38
39     private final Context avalonContext;
40     private final String JavaDoc id;
41     private final String JavaDoc path;
42     private final Function loadScript;
43     private final Function saveScript;
44     private final Scriptable childBindings;
45     private final Map JavaDoc childBindingsMap;
46     
47     final static String JavaDoc[] LOAD_PARAMS = { "widget", "jxpathPointer", "jxpathContext", "childBindings" };
48     final static String JavaDoc[] SAVE_PARAMS = { "widget", "jxpathPointer", "jxpathContext", "childBindings" };
49
50     public JavaScriptJXPathBinding(
51             Context context, JXPathBindingBuilderBase.CommonAttributes commonAtts, String JavaDoc id,
52             String JavaDoc path, Function loadScript, Function saveScript, Map JavaDoc 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         // Set parent on child bindings
61
for(Iterator JavaDoc 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 JavaDoc getPath() { return path; }
70     public String JavaDoc getId() { return id; }
71     public Context getContext() { return avalonContext; }
72     public Function getLoadScript() { return loadScript; }
73     public Function getSaveScript() { return saveScript; }
74     public Map JavaDoc 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             // Move to widget context
81
Pointer pointer = jctx.getPointer(this.path);
82     
83             Map JavaDoc 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 JavaDoc[] {widget, pointer, newCtx, this.childBindings}, objectModel);
91     
92             } catch(RuntimeException JavaDoc re) {
93                 // rethrow
94
throw re;
95             } catch(Exception JavaDoc 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             // Move to widget context and create the path if needed
110
Pointer pointer = jctx.createPath(this.path);
111             JXPathContext widgetCtx = jctx.getRelativeContext(pointer);
112             try {
113                 Map JavaDoc objectModel = ContextHelper.getObjectModel(this.avalonContext);
114
115                 JavaScriptHelper.callFunction(this.saveScript, widget,
116                         new Object JavaDoc[] {widget, pointer, widgetCtx, this.childBindings}, objectModel);
117
118             } catch(RuntimeException JavaDoc re) {
119                 // rethrow
120
throw re;
121             } catch(Exception JavaDoc 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