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: * CaseJXPathBinding 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: * case of a union.</li>
031: * </ol>
032: *
033: * @version $Id: CaseJXPathBinding.java 517733 2007-03-13 15:37:22Z vgritsenko $
034: */
035: public class CaseJXPathBinding extends ComposedJXPathBindingBase {
036:
037: private final String xpath;
038: private final String widgetId;
039:
040: /**
041: * Constructs CaseJXPathBinding
042: * @param commonAtts
043: * @param widgetId
044: * @param xpath
045: * @param childBindings
046: */
047: public CaseJXPathBinding(
048: JXPathBindingBuilderBase.CommonAttributes commonAtts,
049: String widgetId, String xpath,
050: JXPathBindingBase[] childBindings) {
051: super (commonAtts, childBindings);
052: this .widgetId = widgetId;
053: this .xpath = xpath;
054: }
055:
056: public String getXPath() {
057: return xpath;
058: }
059:
060: public String getId() {
061: return widgetId;
062: }
063:
064: /**
065: * Narrows the scope on the form-model to the member widget-field, and
066: * narrows the scope on the object-model to the member xpath-context
067: * before continuing the binding over the child-bindings.
068: */
069: public void doLoad(Widget frmModel, JXPathContext jxpc)
070: throws BindingException {
071: Union unionWidget = (Union) frmModel;
072: if (widgetId.equals(unionWidget.getValue())) {
073: // JXPathContext subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
074: Binding[] subBindings = getChildBindings();
075: if (subBindings != null) {
076: int size = subBindings.length;
077: for (int i = 0; i < size; i++) {
078: subBindings[i].loadFormFromModel(unionWidget, jxpc);
079: }
080: }
081: if (getLogger().isDebugEnabled()) {
082: getLogger().debug("done loading " + this );
083: }
084: }
085: }
086:
087: /**
088: * Narrows the scope on the form-model to the member widget-field, and
089: * narrows the scope on the object-model to the member xpath-context
090: * before continuing the binding over the child-bindings.
091: */
092: public void doSave(Widget frmModel, JXPathContext jxpc)
093: throws BindingException {
094: Union unionWidget = (Union) frmModel;
095: if (widgetId.equals(unionWidget.getValue())) {
096: // JXPathContext subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
097: Binding[] subBindings = getChildBindings();
098: if (subBindings != null) {
099: int size = subBindings.length;
100: for (int i = 0; i < size; i++) {
101: subBindings[i].saveFormToModel(unionWidget, jxpc);
102: }
103: }
104: if (getLogger().isDebugEnabled()) {
105: getLogger().debug("done saving " + this );
106: }
107: }
108: }
109:
110: public String toString() {
111: return "CaseJXPathBinding [widget=" + this .widgetId
112: + ", xpath=" + this .xpath + "]";
113: }
114: }
|