KickJava   Java API By Example, From Geeks To Geeks.

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


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.Widget;
19 import org.apache.cocoon.woody.datatype.convertor.Convertor;
20 import org.apache.commons.jxpath.JXPathContext;
21
22 import java.util.Locale JavaDoc;
23
24 /**
25  * UniqueFieldJXPathBinding provides an implementation of a {@link Binding}
26  * that that allows the specification of a uniquefields defined inside a repeater.
27  * <p>
28  * NOTES: <ol>
29  * <li>This Binding uses the provided widget-id of a defined field in the repeater.</li>
30  * </ol>
31  *
32  * @version CVS $Id: UniqueFieldJXPathBinding.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

34 public class UniqueFieldJXPathBinding 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      * Optional convertor to convert values to and from strings when setting or reading
48      * the from the model. Especially used in combination with XML models where everything
49      * are strings.
50      */

51     private final Convertor convertor;
52
53     /**
54      * The locale to pass to the {@link #convertor}.
55      */

56     private final Locale JavaDoc convertorLocale;
57
58     /**
59      * Constructs UniqueFieldJXPathBinding.
60      *
61      * @param convertor may be null
62      */

63     public UniqueFieldJXPathBinding(JXPathBindingBuilderBase.CommonAttributes commonAtts, String JavaDoc widgetId, String JavaDoc xpath,
64                               Convertor convertor, Locale JavaDoc convertorLocale) {
65         super(commonAtts);
66         this.fieldId = widgetId;
67         this.xpath = xpath;
68         this.convertor = convertor;
69         this.convertorLocale = convertorLocale;
70     }
71
72     /**
73      * Actively performs the binding from the ObjectModel wrapped in a jxpath
74      * context to the Woody-form-widget specified in this object.
75      */

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

102     public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
103         // Do nothing
104
}
105
106     public String JavaDoc toString() {
107         return "UniqueFieldJXPathBinding [widget=" + this.fieldId + ", xpath=" + this.xpath + "]";
108     }
109
110     /*public void enableLogging(Logger logger) {
111         super.enableLogging(logger);
112     }*/

113     /**
114      * @return Returns the convertor.
115      */

116     public Convertor getConvertor() {
117         return convertor;
118     }
119     /**
120      * @return Returns the convertorLocale.
121      */

122     public Locale JavaDoc getConvertorLocale() {
123         return convertorLocale;
124     }
125     /**
126      * @return Returns the fieldId.
127      */

128     public String JavaDoc getFieldId() {
129         return fieldId;
130     }
131     /**
132      * @return Returns the xpath.
133      */

134     public String JavaDoc getXpath() {
135         return xpath;
136     }
137 }
138
Popular Tags