KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > 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.forms.binding;
17
18 import java.util.Locale JavaDoc;
19
20 import org.apache.avalon.framework.logger.Logger;
21 import org.apache.cocoon.forms.datatype.convertor.Convertor;
22 import org.apache.cocoon.forms.datatype.convertor.ConversionResult;
23 import org.apache.cocoon.forms.formmodel.Widget;
24 import org.apache.commons.jxpath.JXPathContext;
25 import org.apache.commons.jxpath.JXPathException;
26
27 /**
28  * ValueJXPathBinding provides an implementation of a {@link Binding}
29  * that loads and saves the information behind a specific xpath expresion
30  * (pointing to an attribute or text-node) to and from a specific CForms
31  * widget as identified by its id.
32  *
33  * @version $Id: ValueJXPathBinding.java 289538 2005-09-16 13:46:22Z sylvain $
34  */

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

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

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

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

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

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

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

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

116     public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
117         Widget widget = selectWidget(frmModel, this.fieldId);
118         Object JavaDoc value = widget.getValue();
119         if (value != null && convertor != null) {
120             value = convertor.convertToString(value, convertorLocale, null);
121         }
122
123         Object JavaDoc oldValue = jxpc.getValue(this.xpath);
124         if (getLogger().isDebugEnabled()) {
125             getLogger().debug("value= " + value + "-- oldvalue=" + oldValue);
126         }
127
128         boolean update = false;
129
130         if ((value == null && oldValue != null) || value != null && !value.equals(oldValue)) {
131             // first update the value itself
132
jxpc.createPathAndSetValue(this.xpath, value);
133
134             // now perform any other bindings that need to be performed when the value is updated
135
JXPathContext subContext = null;
136             try {
137                 subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath));
138             } catch (JXPathException e) {
139                 // if the value has been set to null and the underlying model is a bean, then
140
// JXPath will not be able to create a relative context
141
if (getLogger().isDebugEnabled()) {
142                     getLogger().debug("(Ignorable) problem binding field " + widget.getRequestParameterName(), e);
143                 }
144             }
145             if (subContext != null) {
146                 this.updateBinding.saveFormToModel(frmModel, subContext);
147             }
148
149             update = true;
150         }
151
152         if (getLogger().isDebugEnabled()) {
153             getLogger().debug("done saving " + toString() + " -- value= " + value + " -- on-update == " + update);
154         }
155     }
156
157     public String JavaDoc toString() {
158         return "ValueJXPathBinding [widget=" + this.fieldId + ", xpath=" + this.xpath + "]";
159     }
160
161     public void enableLogging(Logger logger) {
162         super.enableLogging(logger);
163         this.updateBinding.enableLogging(logger);
164     }
165
166     public String JavaDoc getFieldId() {
167         return this.fieldId;
168     }
169
170     public String JavaDoc getXPath() {
171         return this.xpath;
172     }
173
174     public Convertor getConvertor() {
175         return this.convertor;
176     }
177
178     public Locale JavaDoc getConvertorLocale() {
179         return this.convertorLocale;
180     }
181 }
182
Popular Tags