KickJava   Java API By Example, From Geeks To Geeks.

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


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.FormsConstants;
19 import org.apache.cocoon.forms.datatype.convertor.Convertor;
20 import org.apache.cocoon.forms.util.DomHelper;
21 import org.apache.cocoon.i18n.I18nUtils;
22 import org.w3c.dom.Element JavaDoc;
23
24 import java.util.Locale JavaDoc;
25
26 /**
27  * ValueJXPathBindingBuilder provides a helper class for the Factory
28  * implemented in {@link JXPathBindingManager} that helps construct the
29  * actual {@link ValueJXPathBinding} out of the configuration in the
30  * provided configElement which looks like:
31  * <pre><code>
32  * &lt;fb:value id="<i>widget-id</i>" path="<i>xpath-expression</i>"&gt;
33  * &lt;!-- optional child binding to be executed upon 'save' of changed value --&gt;
34  * &lt;fb:on-update&gt;
35  * &lt;!-- any childbinding --&gt;
36  * &lt;/fb:on-update&gt;
37  * &lt;/fb:value&gt;
38  * </code></pre>
39  *
40  * @version $Id: ValueJXPathBindingBuilder.java 326838 2005-10-20 06:26:53Z sylvain $
41  */

42 public class ValueJXPathBindingBuilder extends JXPathBindingBuilderBase {
43
44     /**
45      * Creates an instance of {@link ValueJXPathBinding} based on the attributes
46      * and nested configuration of the provided bindingElm.
47      */

48     public JXPathBindingBase buildBinding(Element JavaDoc bindingElm, JXPathBindingManager.Assistant assistant) throws BindingException {
49
50         try {
51             CommonAttributes commonAtts = JXPathBindingBuilderBase.getCommonAttributes(bindingElm);
52             String JavaDoc xpath = DomHelper.getAttribute(bindingElm, "path", null);
53             String JavaDoc widgetId = DomHelper.getAttribute(bindingElm, "id", null);
54
55             Convertor convertor = null;
56             Locale JavaDoc convertorLocale = Locale.US;
57             Element JavaDoc convertorEl = DomHelper.getChildElement(bindingElm, FormsConstants.DEFINITION_NS, "convertor");
58             if (convertorEl != null) {
59                 String JavaDoc datatype = DomHelper.getAttribute(convertorEl, "datatype");
60                 String JavaDoc localeStr = DomHelper.getAttribute(convertorEl, "locale", null);
61                 if (localeStr != null) {
62                     convertorLocale = I18nUtils.parseLocale(localeStr);
63                 }
64
65                 convertor = assistant.getDatatypeManager().createConvertor(datatype, convertorEl);
66             }
67             
68             // do inheritance
69
ValueJXPathBinding otherBinding = (ValueJXPathBinding)assistant.getContext().getSuperBinding();
70             JXPathBindingBase[] existingUpdateBindings = null;
71             if(otherBinding!=null) {
72                 commonAtts = JXPathBindingBuilderBase.mergeCommonAttributes(otherBinding.getCommonAtts(),commonAtts);
73                 
74                 if(xpath==null)
75                     xpath = otherBinding.getXPath();
76                 if(widgetId==null)
77                     widgetId = otherBinding.getId();
78                 if(convertor==null)
79                     convertor = otherBinding.getConvertor();
80                 if(convertorLocale==null)
81                     convertorLocale = otherBinding.getConvertorLocale();
82                 if(convertorLocale==null)
83                     convertorLocale = otherBinding.getConvertorLocale();
84                 
85                 existingUpdateBindings = otherBinding.getUpdateBinding().getChildBindings();
86             }
87             
88             Element JavaDoc updateWrapElement =
89                 DomHelper.getChildElement(bindingElm, BindingManager.NAMESPACE, "on-update");
90             JXPathBindingBase[] updateBindings = assistant.makeChildBindings(updateWrapElement,existingUpdateBindings);
91
92             ValueJXPathBinding fieldBinding =
93                     new ValueJXPathBinding(commonAtts,
94                             widgetId, xpath, updateBindings, convertor, convertorLocale);
95
96             return fieldBinding;
97         } catch (BindingException e) {
98             throw e;
99         } catch (Exception JavaDoc e) {
100             throw new BindingException("Error building binding defined at " + DomHelper.getLocation(bindingElm), e);
101         }
102     }
103 }
104
Popular Tags