KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > binding > ValueJXPathBinding


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.avalon.framework.logger.Logger;
19 import org.apache.cocoon.woody.formmodel.Widget;
20 import org.apache.cocoon.woody.datatype.convertor.Convertor;
21 import org.apache.commons.jxpath.JXPathContext;
22 import org.apache.commons.jxpath.JXPathException;
23
24 import java.util.Locale JavaDoc;
25
26 /**
27  * ValueJXPathBinding provides an implementation of a {@link Binding}
28  * that loads and saves the information behind a specific xpath expresion
29  * (pointing to an attribute or text-node) to and from a specific Woody
30  * widget as identified by its id.
31  *
32  * @version CVS $Id: ValueJXPathBinding.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

34 public class ValueJXPathBinding extends JXPathBindingBase {
35
36     /**
37      * The xpath expression to the objectModel property
38      */

39     private final String JavaDoc xpath;
40
41     /**
42      * The id of the Woody form-widget
43      */

44     private final String JavaDoc fieldId;
45
46     /**
47      * Flag indicating if the objectModel-property can be altered or not
48      */

49     private final JXPathBindingBase updateBinding;
50
51     /**
52      * Optional convertor to convert values to and from strings when setting or reading
53      * the from the model. Especially used in combination with XML models where everything
54      * are strings.
55      */

56     private final Convertor convertor;
57
58     /**
59      * The locale to pass to the {@link #convertor}.
60      */

61     private final Locale JavaDoc convertorLocale;
62
63     /**
64      * Constructs FieldJXPathBinding.
65      *
66      * @param convertor may be null
67      */

68     public ValueJXPathBinding(JXPathBindingBuilderBase.CommonAttributes commonAtts, String JavaDoc widgetId, String JavaDoc xpath, JXPathBindingBase[] updateBindings,
69                               Convertor convertor, Locale JavaDoc convertorLocale) {
70         super(commonAtts);
71         this.fieldId = widgetId;
72         this.xpath = xpath;
73         this.updateBinding = new ComposedJXPathBindingBase(JXPathBindingBuilderBase.CommonAttributes.DEFAULT, updateBindings);
74         this.convertor = convertor;
75         this.convertorLocale = convertorLocale;
76     }
77
78     /**
79      * Actively performs the binding from the ObjectModel wrapped in a jxpath
80      * context to the Woody-form-widget specified in this object.
81      */

82     public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
83         Widget widget = frmModel.getWidget(this.fieldId);
84         if (widget == null) {
85             throw new BindingException("The widget with the ID [" + this.fieldId
86                     + "] referenced in the binding does not exist in the form definition.");
87         }
88
89         Object JavaDoc value = jxpc.getValue(this.xpath);
90         if (value != null && convertor != null) {
91             if (value instanceof String JavaDoc) {
92                 value = convertor.convertFromString((String JavaDoc)value, convertorLocale, null);
93             } else {
94                 getLogger().warn("Convertor ignored on backend-value which isn't of type String.");
95             }
96         }
97
98         widget.setValue(value);
99         if (getLogger().isDebugEnabled()) {
100             getLogger().debug("Done loading " + toString() + " -- value= " + value);
101         }
102     }
103
104     /**
105      * Actively performs the binding from the Woody-form to the ObjectModel
106      * wrapped in a jxpath context
107      */

108     public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
109         Widget widget = frmModel.getWidget(this.fieldId);
110         Object JavaDoc value = widget.getValue();
111         if (value != null && convertor != null) {
112             value = convertor.convertToString(value, convertorLocale, null);
113         }
114
115         Object JavaDoc oldValue = jxpc.getValue(this.xpath);
116         if (getLogger().isDebugEnabled()) {
117             getLogger().debug("value= " + value + "-- oldvalue=" + oldValue);
118         }
119
120         boolean update = false;
121
122         if ((value == null && oldValue != null) || value != null && !value.equals(oldValue)) {
123             // first update the value itself
124
jxpc.createPathAndSetValue(this.xpath, value);
125
126             // now perform any other bindings that need to be performed when the value is updated
127
JXPathContext subContext = null;
128             try {
129                 subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
130             } catch (JXPathException e) {
131                 // if the value has been set to null and the underlying model is a bean, then
132
// JXPath will not be able to create a relative context
133
if (getLogger().isDebugEnabled()) {
134                     getLogger().debug("(Ignorable) problem binding field " + widget.getFullyQualifiedId(), e);
135                 }
136             }
137             if (subContext != null) {
138                 this.updateBinding.saveFormToModel(frmModel, subContext);
139             }
140
141             update = true;
142         }
143
144         if (getLogger().isDebugEnabled()) {
145             getLogger().debug("done saving " + toString() + " -- value= " + value + " -- on-update == " + update);
146         }
147     }
148
149     public String JavaDoc toString() {
150         return "ValueJXPathBinding [widget=" + this.fieldId + ", xpath=" + this.xpath + "]";
151     }
152
153     public void enableLogging(Logger logger) {
154         super.enableLogging(logger);
155         this.updateBinding.enableLogging(logger);
156     }
157 }
158
Popular Tags