KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > binding > JavaScriptJXPathBindingBuilder


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 org.apache.cocoon.woody.binding.JXPathBindingManager.Assistant;
19 import org.apache.cocoon.woody.util.DomHelper;
20 import org.apache.cocoon.woody.util.JavaScriptHelper;
21 import org.mozilla.javascript.Script;
22 import org.w3c.dom.Element JavaDoc;
23
24 /**
25  * Builds a {@link Binding} based on two JavaScript snippets, respectively for loading and saving the form.
26  * <p>
27  * The syntax for this binding is as follows :
28  * <pre>
29  * &lt;wb:javascript id="foo" path="@foo"&gt;
30  * &lt;wb:load-form&gt;
31  * var appValue = jxpathPointer.getValue();
32  * var formValue = doLoadConversion(appValue);
33  * widget.setValue(formValue);
34  * &lt;/wb:load-form&gt;
35  * &lt;wb:save-form&gt;
36  * var formValue = widget.getValue();
37  * var appValue = doSaveConversion(formValue);
38  * jxpathPointer.setValue(appValue);
39  * &lt;/wb:save-form&gt;
40  * &lt;/wb:javascript&gt;
41  * </pre>
42  * This example is rather trivial and could be replaced by a simple &lt;wb:value&gt;, but
43  * it shows the available variables in the script:
44  * <ul>
45  * <li><code>widget</code>: the widget identified by the "id" attribute,
46  * <li><code>jxpathPointer</code>: the JXPath pointer corresponding to the "path" attribute,
47  * <li><code>jxpathContext</code> (not shown): the JXPath context corresponding to the "path" attribute
48  * </ul>
49  * <b>Notes:</b><ul>
50  * <li>The &lt;wb:save-form&gt; snippet should be ommitted if the "direction" attribute is set to "load".</li>
51  * <li>The &lt;wb:load-form&gt; snippet should be ommitted if the "direction" attribute is set to "save".</li>
52  * </ul>
53  *
54  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
55  * @version CVS $Id: JavaScriptJXPathBindingBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
56  */

57 public class JavaScriptJXPathBindingBuilder extends JXPathBindingBuilderBase {
58
59     public JXPathBindingBase buildBinding(Element JavaDoc element, Assistant assistant) throws BindingException {
60         try {
61             CommonAttributes commonAtts = JXPathBindingBuilderBase.getCommonAttributes(element);
62
63             String JavaDoc id = DomHelper.getAttribute(element, "id");
64             String JavaDoc path = DomHelper.getAttribute(element, "path");
65
66             Script loadScript = null;
67             if (commonAtts.loadEnabled) {
68                 Element JavaDoc loadElem = DomHelper.getChildElement(element, BindingManager.NAMESPACE, "load-form");
69                 if (loadElem == null) {
70                     throw new BindingException("Element \"load-form\" is missing (" +
71                         DomHelper.getLocation(element) + ")");
72                 }
73                 loadScript = JavaScriptHelper.buildScript(loadElem);
74             }
75
76             Script saveScript = null;
77             if (commonAtts.saveEnabled) {
78                 Element JavaDoc saveElem = DomHelper.getChildElement(element, BindingManager.NAMESPACE, "save-form");
79                 if (saveElem == null) {
80                     throw new BindingException("Element \"save-form\" is missing (" +
81                         DomHelper.getLocation(element) + ")");
82                 }
83                 saveScript = JavaScriptHelper.buildScript(saveElem);
84             }
85
86             return new JavaScriptJXPathBinding(commonAtts, id, path, loadScript, saveScript);
87
88         } catch(Exception JavaDoc e) {
89             throw new BindingException("Cannot build binding at " + DomHelper.getLocation(element), e);
90         }
91     }
92 }
93
Popular Tags