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: * CaseJXPathBinding 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: * case of a union.</li>
030: * </ol>
031: *
032: * @author Timothy Larson
033: * @version CVS $Id: CaseJXPathBinding.java 433543 2006-08-22 06:22:54Z crossley $
034: */
035: public class CaseJXPathBinding extends ComposedJXPathBindingBase {
036:
037: private final String xpath;
038:
039: private final String widgetId;
040:
041: /**
042: * Constructs CaseJXPathBinding
043: * @param commonAtts
044: * @param widgetId
045: * @param xpath
046: * @param childBindings
047: */
048: public CaseJXPathBinding(
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: Union unionWidget = (Union) frmModel;
065: if (widgetId.equals(unionWidget.getValue())) {
066: // JXPathContext subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
067: Binding[] subBindings = getChildBindings();
068: if (subBindings != null) {
069: int size = subBindings.length;
070: for (int i = 0; i < size; i++) {
071: subBindings[i].loadFormFromModel(unionWidget, jxpc);
072: }
073: }
074: if (getLogger().isDebugEnabled()) {
075: getLogger().debug("done loading " + toString());
076: }
077: }
078: }
079:
080: /**
081: * Narrows the scope on the form-model to the member widget-field, and
082: * narrows the scope on the object-model to the member xpath-context
083: * before continuing the binding over the child-bindings.
084: */
085: public void doSave(Widget frmModel, JXPathContext jxpc)
086: throws BindingException {
087: Union unionWidget = (Union) frmModel;
088: if (widgetId.equals(unionWidget.getValue())) {
089: // JXPathContext subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
090: Binding[] subBindings = getChildBindings();
091: if (subBindings != null) {
092: int size = subBindings.length;
093: for (int i = 0; i < size; i++) {
094: subBindings[i].saveFormToModel(unionWidget, jxpc);
095: }
096: }
097: if (getLogger().isDebugEnabled()) {
098: getLogger().debug("done saving " + toString());
099: }
100: }
101: }
102:
103: public String toString() {
104: return "CaseJXPathBinding [widget=" + this .widgetId
105: + ", xpath=" + this .xpath + "]";
106: }
107: }
|