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 517733 2007-03-13 15:37:22Z 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: }
080: Union unionWidget = (Union) widget;
081: Binding[] subBindings = getChildBindings();
082: if (subBindings != null) {
083: int size = subBindings.length;
084: for (int i = 0; i < size; i++) {
085: subBindings[i].loadFormFromModel(unionWidget,
086: subContext);
087: }
088: }
089: if (getLogger().isDebugEnabled()) {
090: getLogger().debug("done loading " + this );
091: }
092: }
093:
094: /**
095: * Narrows the scope on the form-model to the member widget-field, and
096: * narrows the scope on the object-model to the member xpath-context
097: * before continuing the binding over the child-bindings.
098: */
099: public void doSave(Widget frmModel, JXPathContext jxpc)
100: throws BindingException {
101: Union unionWidget = (Union) selectWidget(frmModel,
102: this .widgetId);
103: JXPathContext subContext = jxpc.getRelativeContext(jxpc
104: .getPointer(this .xpath));
105: Binding[] subBindings = getChildBindings();
106: if (subBindings != null) {
107: int size = subBindings.length;
108: for (int i = 0; i < size; i++) {
109: subBindings[i].saveFormToModel(unionWidget, subContext);
110: }
111: }
112: if (getLogger().isDebugEnabled()) {
113: getLogger().debug("done saving " + this );
114: }
115: }
116:
117: public String toString() {
118: return "UnionJXPathBinding [widget=" + this .widgetId
119: + ", xpath=" + this .xpath + "]";
120: }
121: }
|