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.Widget;
020: import org.apache.commons.jxpath.JXPathContext;
021:
022: /**
023: * NewJXPathBinding provides an implementation of a {@link Binding}
024: * that references a class of bindings.
025: * <p>
026: * NOTES: <ol>
027: * <li>This Binding assumes that the provided widget-id points to a
028: * class that contains other widgets.</li>
029: * </ol>
030: *
031: * @author Timothy Larson
032: * @version CVS $Id: NewJXPathBinding.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034: public class NewJXPathBinding extends ComposedJXPathBindingBase {
035:
036: private final String widgetId;
037:
038: private Binding classBinding;
039:
040: /**
041: * Constructs NewJXPathBinding
042: * @param commonAtts
043: * @param widgetId
044: * @param childBindings
045: */
046: public NewJXPathBinding(
047: JXPathBindingBuilderBase.CommonAttributes commonAtts,
048: String widgetId, JXPathBindingBase[] childBindings) {
049: super (commonAtts, childBindings);
050: this .widgetId = widgetId;
051: this .classBinding = null;
052: }
053:
054: private void resolve() {
055: classBinding = getClass(widgetId);
056: }
057:
058: /**
059: * Narrows the scope on the form-model to the member widget-field, and
060: * narrows the scope on the object-model to the member xpath-context
061: * before continuing the binding over the child-bindings.
062: */
063: public void doLoad(Widget frmModel, JXPathContext jxpc)
064: throws BindingException {
065: if (classBinding == null)
066: resolve();
067: if (classBinding instanceof ClassJXPathBinding) {
068: Binding[] subBindings = ((ComposedJXPathBindingBase) classBinding)
069: .getChildBindings();
070: if (subBindings != null) {
071: int size = subBindings.length;
072: for (int i = 0; i < size; i++) {
073: subBindings[i].loadFormFromModel(frmModel, jxpc);
074: }
075: }
076: } else {
077: classBinding.loadFormFromModel(frmModel, jxpc);
078: }
079: }
080:
081: /**
082: * Narrows the scope on the form-model to the member widget-field, and
083: * narrows the scope on the object-model to the member xpath-context
084: * before continuing the binding over the child-bindings.
085: */
086: public void doSave(Widget frmModel, JXPathContext jxpc)
087: throws BindingException {
088: if (classBinding == null)
089: resolve();
090: if (classBinding instanceof ClassJXPathBinding) {
091: Binding[] subBindings = ((ComposedJXPathBindingBase) classBinding)
092: .getChildBindings();
093: if (subBindings != null) {
094: int size = subBindings.length;
095: for (int i = 0; i < size; i++) {
096: subBindings[i].saveFormToModel(frmModel, jxpc);
097: }
098: }
099: } else {
100: classBinding.saveFormToModel(frmModel, jxpc);
101: }
102: }
103:
104: public String toString() {
105: return "NewJXPathBinding [widget=" + this .widgetId + "]";
106: }
107: }
|