001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.woody.binding;
018:
019: import org.apache.cocoon.woody.binding.JXPathBindingManager.Assistant;
020: import org.apache.cocoon.woody.util.DomHelper;
021: import org.apache.cocoon.woody.util.JavaScriptHelper;
022: import org.mozilla.javascript.Script;
023: import org.w3c.dom.Element;
024:
025: /**
026: * Builds a {@link Binding} based on two JavaScript snippets, respectively for loading and saving the form.
027: * <p>
028: * The syntax for this binding is as follows :
029: * <pre>
030: * <wb:javascript id="foo" path="@foo">
031: * <wb:load-form>
032: * var appValue = jxpathPointer.getValue();
033: * var formValue = doLoadConversion(appValue);
034: * widget.setValue(formValue);
035: * </wb:load-form>
036: * <wb:save-form>
037: * var formValue = widget.getValue();
038: * var appValue = doSaveConversion(formValue);
039: * jxpathPointer.setValue(appValue);
040: * </wb:save-form>
041: * </wb:javascript>
042: * </pre>
043: * This example is rather trivial and could be replaced by a simple <wb:value>, but
044: * it shows the available variables in the script:
045: * <ul>
046: * <li><code>widget</code>: the widget identified by the "id" attribute,
047: * <li><code>jxpathPointer</code>: the JXPath pointer corresponding to the "path" attribute,
048: * <li><code>jxpathContext</code> (not shown): the JXPath context corresponding to the "path" attribute
049: * </ul>
050: * <b>Notes:</b><ul>
051: * <li>The <wb:save-form> snippet should be ommitted if the "direction" attribute is set to "load".</li>
052: * <li>The <wb:load-form> snippet should be ommitted if the "direction" attribute is set to "save".</li>
053: * </ul>
054: *
055: * @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
056: * @version CVS $Id: JavaScriptJXPathBindingBuilder.java 433543 2006-08-22 06:22:54Z crossley $
057: */
058: public class JavaScriptJXPathBindingBuilder extends
059: JXPathBindingBuilderBase {
060:
061: public JXPathBindingBase buildBinding(Element element,
062: Assistant assistant) throws BindingException {
063: try {
064: CommonAttributes commonAtts = JXPathBindingBuilderBase
065: .getCommonAttributes(element);
066:
067: String id = DomHelper.getAttribute(element, "id");
068: String path = DomHelper.getAttribute(element, "path");
069:
070: Script loadScript = null;
071: if (commonAtts.loadEnabled) {
072: Element loadElem = DomHelper.getChildElement(element,
073: BindingManager.NAMESPACE, "load-form");
074: if (loadElem == null) {
075: throw new BindingException(
076: "Element \"load-form\" is missing ("
077: + DomHelper.getLocation(element)
078: + ")");
079: }
080: loadScript = JavaScriptHelper.buildScript(loadElem);
081: }
082:
083: Script saveScript = null;
084: if (commonAtts.saveEnabled) {
085: Element saveElem = DomHelper.getChildElement(element,
086: BindingManager.NAMESPACE, "save-form");
087: if (saveElem == null) {
088: throw new BindingException(
089: "Element \"save-form\" is missing ("
090: + DomHelper.getLocation(element)
091: + ")");
092: }
093: saveScript = JavaScriptHelper.buildScript(saveElem);
094: }
095:
096: return new JavaScriptJXPathBinding(commonAtts, id, path,
097: loadScript, saveScript);
098:
099: } catch (Exception e) {
100: throw new BindingException("Cannot build binding at "
101: + DomHelper.getLocation(element), e);
102: }
103: }
104: }
|