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.forms.binding;
018:
019: import org.apache.cocoon.forms.formmodel.Union;
020: import org.apache.cocoon.forms.formmodel.Widget;
021:
022: import org.apache.commons.jxpath.JXPathContext;
023:
024: /**
025: * UnionJXPathBinding provides an implementation of a {@link Binding}
026: * that narrows the context towards provided childbindings.
027: * <p>NOTES:
028: * <ol>
029: * <li>This Binding assumes that the provided widget-id points to a
030: * union widget.</li>
031: * </ol>
032: *
033: * @version $Id: UnionJXPathBinding.java 450255 2006-09-26 23:48:43Z vgritsenko $
034: */
035: public class UnionJXPathBinding extends ComposedJXPathBindingBase {
036:
037: private final String xpath;
038:
039: private final String widgetId;
040:
041: /**
042: * Constructs UnionJXPathBinding
043: * @param commonAtts
044: * @param widgetId
045: * @param xpath
046: * @param childBindings
047: */
048: public UnionJXPathBinding(
049: JXPathBindingBuilderBase.CommonAttributes commonAtts,
050: String widgetId, String xpath,
051: JXPathBindingBase[] childBindings) {
052: super (commonAtts, childBindings);
053: this .widgetId = widgetId;
054: this .xpath = xpath;
055: }
056:
057: public String getXPath() {
058: return xpath;
059: }
060:
061: public String getId() {
062: return widgetId;
063: }
064:
065: /**
066: * Narrows the scope on the form-model to the member widget-field, and
067: * narrows the scope on the object-model to the member xpath-context
068: * before continuing the binding over the child-bindings.
069: */
070: public void doLoad(Widget frmModel, JXPathContext jxpc)
071: throws BindingException {
072: Widget widget = selectWidget(frmModel, this .widgetId);
073: JXPathContext subContext = jxpc.getRelativeContext(jxpc
074: .getPointer(this .xpath));
075: if (!(widget instanceof Union))
076: throw new RuntimeException(
077: "Binding: Expected Union widget, but received class: \""
078: + widget.getClass().getName() + "\".");
079: Union unionWidget = (Union) widget;
080: Binding[] subBindings = getChildBindings();
081: if (subBindings != null) {
082: int size = subBindings.length;
083: for (int i = 0; i < size; i++) {
084: subBindings[i].loadFormFromModel(unionWidget,
085: subContext);
086: }
087: }
088: if (getLogger().isDebugEnabled()) {
089: getLogger().debug("done loading " + toString());
090: }
091: }
092:
093: /**
094: * Narrows the scope on the form-model to the member widget-field, and
095: * narrows the scope on the object-model to the member xpath-context
096: * before continuing the binding over the child-bindings.
097: */
098: public void doSave(Widget frmModel, JXPathContext jxpc)
099: throws BindingException {
100: Union unionWidget = (Union) selectWidget(frmModel,
101: this .widgetId);
102: JXPathContext subContext = jxpc.getRelativeContext(jxpc
103: .getPointer(this .xpath));
104: Binding[] subBindings = getChildBindings();
105: if (subBindings != null) {
106: int size = subBindings.length;
107: for (int i = 0; i < size; i++) {
108: subBindings[i].saveFormToModel(unionWidget, subContext);
109: }
110: }
111: if (getLogger().isDebugEnabled()) {
112: getLogger().debug("done saving " + toString());
113: }
114: }
115:
116: public String toString() {
117: return "UnionJXPathBinding [widget=" + this .widgetId
118: + ", xpath=" + this .xpath + "]";
119: }
120: }
|