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.Widget;
020:
021: import org.apache.commons.jxpath.JXPathContext;
022:
023: /**
024: * NewJXPathBinding provides an implementation of a {@link Binding}
025: * that references a class of bindings.
026: * <p>
027: * NOTES: <ol>
028: * <li>This Binding assumes that the provided widget-id points to a
029: * class that contains other widgets.</li>
030: * </ol>
031: *
032: * @version $Id: NewJXPathBinding.java 517733 2007-03-13 15:37:22Z vgritsenko $
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: public String getId() {
055: return widgetId;
056: }
057:
058: /**
059: * Recursively resolves references.
060: */
061: private void resolve() throws BindingException {
062: classBinding = getClass(widgetId);
063: if (classBinding == null) {
064: throw new BindingException("Class '" + widgetId
065: + "' does not exist");
066: }
067: }
068:
069: /**
070: * Narrows the scope on the form-model to the member widget-field, and
071: * narrows the scope on the object-model to the member xpath-context
072: * before continuing the binding over the child-bindings.
073: */
074: public void doLoad(Widget frmModel, JXPathContext jxpc)
075: throws BindingException {
076: if (classBinding == null) {
077: resolve();
078: }
079: Binding[] subBindings = ((ComposedJXPathBindingBase) classBinding)
080: .getChildBindings();
081: if (subBindings != null) {
082: int size = subBindings.length;
083: for (int i = 0; i < size; i++) {
084: subBindings[i].loadFormFromModel(frmModel, jxpc);
085: }
086: }
087: }
088:
089: /**
090: * Narrows the scope on the form-model to the member widget-field, and
091: * narrows the scope on the object-model to the member xpath-context
092: * before continuing the binding over the child-bindings.
093: */
094: public void doSave(Widget frmModel, JXPathContext jxpc)
095: throws BindingException {
096: if (classBinding == null) {
097: resolve();
098: }
099: Binding[] subBindings = ((ComposedJXPathBindingBase) classBinding)
100: .getChildBindings();
101: if (subBindings != null) {
102: int size = subBindings.length;
103: for (int i = 0; i < size; i++) {
104: subBindings[i].saveFormToModel(frmModel, jxpc);
105: }
106: }
107: }
108:
109: public String toString() {
110: return "NewJXPathBinding [widget=" + this .widgetId + "]";
111: }
112: }
|