KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.binding;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
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 /**
30  *
31  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
32  * @version CVS $Id: JavaScriptJXPathBinding.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

34 public class JavaScriptJXPathBinding extends JXPathBindingBase {
35
36     private final String JavaDoc id;
37     private final String JavaDoc path;
38     private final Script loadScript;
39     private final Script saveScript;
40
41     public JavaScriptJXPathBinding(
42             JXPathBindingBuilderBase.CommonAttributes commonAtts, String JavaDoc id,
43             String JavaDoc 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             // Move to widget context
56
Pointer pointer = jctx.getPointer(this.path);
57     
58             // FIXME: remove this ugly hack and get the request from the
59
// Avalon context once binding builder are real components
60
Map JavaDoc objectModel = CocoonComponentManager.getCurrentEnvironment().getObjectModel();
61
62             try {
63                 Map JavaDoc values = new HashMap JavaDoc(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 JavaDoc re) {
73                 // rethrow
74
throw re;
75             } catch(Exception JavaDoc 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             // Move to widget context and create the path if needed
90
Pointer pointer = jctx.createPath(this.path);
91             JXPathContext widgetCtx = jctx.getRelativeContext(pointer);
92             try {
93                 // FIXME: remove this ugly hack and get the request from the Avalon context once
94
// binding builder are real components
95
Map JavaDoc objectModel = CocoonComponentManager.getCurrentEnvironment().getObjectModel();
96
97                 Map JavaDoc values = new HashMap JavaDoc();
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 JavaDoc re) {
105                 // rethrow
106
throw re;
107             } catch(Exception JavaDoc 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