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.formmodel.Union;
020: import org.apache.cocoon.woody.formmodel.Widget;
021: import org.apache.commons.jxpath.JXPathContext;
022:
023: /**
024: * UnionJXPathBinding provides an implementation of a {@link Binding}
025: * that narrows the context towards provided childbindings.
026: * <p>
027: * NOTES: <ol>
028: * <li>This Binding assumes that the provided widget-id points to a
029: * union widget.</li>
030: * </ol>
031: *
032: * @author Timothy Larson
033: * @version CVS $Id: UnionJXPathBinding.java 433543 2006-08-22 06:22:54Z crossley $
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: /**
058: * Narrows the scope on the form-model to the member widget-field, and
059: * narrows the scope on the object-model to the member xpath-context
060: * before continuing the binding over the child-bindings.
061: */
062: public void doLoad(Widget frmModel, JXPathContext jxpc)
063: throws BindingException {
064: Widget widget = frmModel.getWidget(this .widgetId);
065: JXPathContext subContext = jxpc.getRelativeContext(jxpc
066: .getPointer(this .xpath));
067: if (!(widget instanceof Union))
068: throw new RuntimeException(
069: "Binding: Expected Union widget, but received class: \""
070: + widget.getClass().getName() + "\".");
071: Union unionWidget = (Union) widget;
072: Binding[] subBindings = getChildBindings();
073: if (subBindings != null) {
074: int size = subBindings.length;
075: for (int i = 0; i < size; i++) {
076: subBindings[i].loadFormFromModel(unionWidget,
077: subContext);
078: }
079: }
080: if (getLogger().isDebugEnabled()) {
081: getLogger().debug("done loading " + toString());
082: }
083: }
084:
085: /**
086: * Narrows the scope on the form-model to the member widget-field, and
087: * narrows the scope on the object-model to the member xpath-context
088: * before continuing the binding over the child-bindings.
089: */
090: public void doSave(Widget frmModel, JXPathContext jxpc)
091: throws BindingException {
092: Union unionWidget = (Union) frmModel.getWidget(this .widgetId);
093: JXPathContext subContext = jxpc.getRelativeContext(jxpc
094: .getPointer(this .xpath));
095: Binding[] subBindings = getChildBindings();
096: if (subBindings != null) {
097: int size = subBindings.length;
098: for (int i = 0; i < size; i++) {
099: subBindings[i].saveFormToModel(unionWidget, subContext);
100: }
101: }
102: if (getLogger().isDebugEnabled()) {
103: getLogger().debug("done saving " + toString());
104: }
105: }
106:
107: public String toString() {
108: return "UnionJXPathBinding [widget=" + this .widgetId
109: + ", xpath=" + this .xpath + "]";
110: }
111: }
|