KickJava   Java API By Example, From Geeks To Geeks.

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


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.Union;
19 import org.apache.cocoon.forms.formmodel.Widget;
20 import org.apache.commons.jxpath.JXPathContext;
21
22 /**
23  * UnionJXPathBinding provides an implementation of a {@link Binding}
24  * that narrows the context towards provided childbindings.
25  * <p>
26  * NOTES: <ol>
27  * <li>This Binding assumes that the provided widget-id points to a
28  * union widget.</li>
29  * </ol>
30  *
31  * @version $Id: UnionJXPathBinding.java 289538 2005-09-16 13:46:22Z sylvain $
32  */

33 public class UnionJXPathBinding extends ComposedJXPathBindingBase {
34
35     private final String JavaDoc xpath;
36
37     private final String JavaDoc widgetId;
38
39     /**
40      * Constructs UnionJXPathBinding
41      * @param commonAtts
42      * @param widgetId
43      * @param xpath
44      * @param childBindings
45      */

46     public UnionJXPathBinding(JXPathBindingBuilderBase.CommonAttributes commonAtts, String JavaDoc widgetId, String JavaDoc xpath, JXPathBindingBase[] childBindings) {
47         super(commonAtts, childBindings);
48         this.widgetId = widgetId;
49         this.xpath = xpath;
50     }
51     
52     public String JavaDoc getXPath() { return xpath; }
53     public String JavaDoc getId() { return widgetId; }
54
55     /**
56      * Narrows the scope on the form-model to the member widget-field, and
57      * narrows the scope on the object-model to the member xpath-context
58      * before continuing the binding over the child-bindings.
59      */

60     public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
61         Widget widget = selectWidget(frmModel, this.widgetId);
62         JXPathContext subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
63         if (!(widget instanceof Union))
64             throw new RuntimeException JavaDoc("Binding: Expected Union widget, but received class: \"" +
65                     widget.getClass().getName() + "\".");
66         Union unionWidget = (Union)widget;
67         Binding[] subBindings = getChildBindings();
68         if (subBindings != null) {
69             int size = subBindings.length;
70             for (int i = 0; i < size; i++) {
71                 subBindings[i].loadFormFromModel(unionWidget, subContext);
72             }
73         }
74         if (getLogger().isDebugEnabled()) {
75             getLogger().debug("done loading " + toString());
76         }
77     }
78
79     /**
80      * Narrows the scope on the form-model to the member widget-field, and
81      * narrows the scope on the object-model to the member xpath-context
82      * before continuing the binding over the child-bindings.
83      */

84     public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
85         Union unionWidget = (Union)selectWidget(frmModel, this.widgetId);
86         JXPathContext subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
87         Binding[] subBindings = getChildBindings();
88         if (subBindings != null) {
89             int size = subBindings.length;
90             for (int i = 0; i < size; i++) {
91                 subBindings[i].saveFormToModel(unionWidget, subContext);
92             }
93         }
94         if (getLogger().isDebugEnabled()) {
95             getLogger().debug("done saving " + toString());
96         }
97     }
98
99     public String JavaDoc toString() {
100         return "UnionJXPathBinding [widget=" + this.widgetId + ", xpath=" + this.xpath + "]";
101     }
102 }
103
Popular Tags