KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > binding > NewJXPathBinding


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.binding;
17
18 import org.apache.cocoon.forms.formmodel.Widget;
19 import org.apache.commons.jxpath.JXPathContext;
20
21 /**
22  * NewJXPathBinding provides an implementation of a {@link Binding}
23  * that references a class of bindings.
24  * <p>
25  * NOTES: <ol>
26  * <li>This Binding assumes that the provided widget-id points to a
27  * class that contains other widgets.</li>
28  * </ol>
29  *
30  * @version $Id: NewJXPathBinding.java 289538 2005-09-16 13:46:22Z sylvain $
31  */

32 public class NewJXPathBinding extends ComposedJXPathBindingBase {
33
34     private final String JavaDoc widgetId;
35
36     private Binding classBinding;
37
38     /**
39      * Constructs NewJXPathBinding
40      * @param commonAtts
41      * @param widgetId
42      * @param childBindings
43      */

44     public NewJXPathBinding(JXPathBindingBuilderBase.CommonAttributes commonAtts,
45             String JavaDoc widgetId, JXPathBindingBase[] childBindings) {
46         super(commonAtts, childBindings);
47         this.widgetId = widgetId;
48         this.classBinding = null;
49     }
50     
51     public String JavaDoc getId() { return widgetId; }
52
53     /**
54      * Recursively resolves references.
55      */

56     private void resolve() throws BindingException {
57         classBinding = getClass(widgetId);
58         if (classBinding == null) {
59             throw new BindingException("Class \"" + widgetId + "\" does not exist");
60         }
61     }
62
63     /**
64      * Narrows the scope on the form-model to the member widget-field, and
65      * narrows the scope on the object-model to the member xpath-context
66      * before continuing the binding over the child-bindings.
67      */

68     public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
69         if (classBinding == null)
70             resolve();
71         Binding[] subBindings = ((ComposedJXPathBindingBase)classBinding).getChildBindings();
72         if (subBindings != null) {
73             int size = subBindings.length;
74             for (int i = 0; i < size; i++) {
75                 subBindings[i].loadFormFromModel(frmModel, jxpc);
76             }
77         }
78     }
79
80     /**
81      * Narrows the scope on the form-model to the member widget-field, and
82      * narrows the scope on the object-model to the member xpath-context
83      * before continuing the binding over the child-bindings.
84      */

85     public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
86         if (classBinding == null)
87             resolve();
88         Binding[] subBindings = ((ComposedJXPathBindingBase)classBinding).getChildBindings();
89         if (subBindings != null) {
90             int size = subBindings.length;
91             for (int i = 0; i < size; i++) {
92                 subBindings[i].saveFormToModel(frmModel, jxpc);
93             }
94         }
95     }
96
97     public String JavaDoc toString() {
98         return "NewJXPathBinding [widget=" + this.widgetId + "]";
99     }
100 }
101
Popular Tags