KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.binding;
17
18 import org.apache.cocoon.woody.formmodel.Union;
19 import org.apache.cocoon.woody.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  * @author Timothy Larson
32  * @version CVS $Id: UnionJXPathBinding.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

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

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

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

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