KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
19
20 import org.apache.cocoon.forms.binding.JXPathBindingManager.Assistant;
21 import org.apache.cocoon.forms.util.DomHelper;
22 import org.w3c.dom.Element JavaDoc;
23
24 /**
25  * CustomJXPathBindingBuilder provides a helper class for the Factory
26  * implemented in {@link JXPathBindingManager} that helps construct the
27  * actual {@link CustomJXPathBinding} out of the configuration in the
28  * provided configElement which looks like one of the following:
29  *
30  * <p> 1. No additional configuration requirements:
31  * <pre><code>
32  * &lt;fb:custom id="<i>widget-id</i>" path="<i>xpath expression</i>"
33  * class="your.package.CustomBindingX" /&gt;
34  * </code></pre>
35  *
36  * <p> 2. With custom configuration requirements:
37  * <pre><code>
38  * &lt;fb:custom id="<i>widget-id</i>" path="<i>xpath expression</i>"
39  * builderclass="your.package.CustomBindingXBuilder"
40  * factorymethod="makeBinding"
41  * &gt;
42  * &lt;fb:config custom-atts="someValue"&gt;
43  * &lt;!-- in here come the nested custom elements (recommended in own namespace)
44  * that make up the custom config --&gt;
45  * &lt;/fb:config&gt;
46  * &lt;/fb:context&gt;
47  * </code></pre>
48  *
49  * @version $Id: CustomJXPathBindingBuilder.java 289538 2005-09-16 13:46:22Z sylvain $
50  */

51 public class CustomJXPathBindingBuilder extends JXPathBindingBuilderBase {
52     private static final Class JavaDoc[] DOMELEMENT_METHODARGS;
53     private static final Class JavaDoc[] EMPTY_METHODARGS;
54     
55     static {
56         DOMELEMENT_METHODARGS = new Class JavaDoc[1];
57         DOMELEMENT_METHODARGS[0] = Element JavaDoc.class;
58         EMPTY_METHODARGS = null;
59     }
60
61     /**
62      * Builds the custom Binding class and wraps it into a CustomJXPathBinding
63      *
64      * @param bindingElm configuration element describing the binding to build
65      * @param assistant helper-class for building possible nested bindings
66      * @return the freshly built binding based on the configuration element
67      * @throws BindingException
68      */

69     public JXPathBindingBase buildBinding(Element JavaDoc bindingElm,
70         Assistant assistant) throws BindingException {
71
72         try {
73             CommonAttributes commonAtts =
74                 JXPathBindingBuilderBase.getCommonAttributes(bindingElm);
75             String JavaDoc xpath = DomHelper.getAttribute(bindingElm, "path", ".");
76             String JavaDoc widgetId = DomHelper.getAttribute(bindingElm, "id", null);
77
78             Object JavaDoc bindingInstance = null;
79             
80             String JavaDoc className = DomHelper.getAttribute(bindingElm, "class", null);
81             if(className != null) {
82                 Class JavaDoc clazz = Class.forName(className);
83                 bindingInstance = clazz.newInstance();
84                 
85             } else {
86                 String JavaDoc builderClassName =
87                     DomHelper.getAttribute(bindingElm, "builderclass");
88                 String JavaDoc factoryMethodName =
89                     DomHelper.getAttribute(bindingElm, "factorymethod");
90                 Element JavaDoc configNode =
91                     DomHelper.getChildElement(bindingElm, BindingManager.NAMESPACE, "config");
92                 
93                 // only do it if attributes exist
94
if(! (builderClassName == null || factoryMethodName == null) ) {
95                     Class JavaDoc builderClass = Class.forName(builderClassName);
96                     Method JavaDoc factoryMethod = null;
97                     Object JavaDoc[] args = null;
98                     try {
99                         factoryMethod = builderClass.getMethod(factoryMethodName, DOMELEMENT_METHODARGS);
100                         args = new Object JavaDoc[1];
101                         args[0] = configNode;
102                     } catch (NoSuchMethodException JavaDoc e) {
103                         factoryMethod = null;
104                     }
105                     
106                     if (factoryMethod == null) {
107                         factoryMethod = builderClass.getMethod(factoryMethodName, EMPTY_METHODARGS);
108                         args = null;
109                     }
110                       
111                     // we pass null to indicate that the method should be static
112
bindingInstance = factoryMethod.invoke(null, args);
113                 }
114             }
115             
116 // do inheritance
117
CustomJXPathBinding otherBinding = (CustomJXPathBinding)assistant.getContext().getSuperBinding();
118             if(otherBinding!=null) {
119                 commonAtts = JXPathBindingBuilderBase.mergeCommonAttributes(otherBinding.getCommonAtts(),commonAtts);
120                 
121                 if(xpath==null)
122                     xpath = otherBinding.getXPath();
123                 if(widgetId==null)
124                     widgetId = otherBinding.getId();
125                 if(bindingInstance==null)
126                     bindingInstance = otherBinding.getWrappedBinding();
127             }
128
129             CustomJXPathBinding customBinding =
130                 new CustomJXPathBinding(commonAtts, widgetId, xpath, (AbstractCustomBinding)bindingInstance);
131             return customBinding;
132         } catch (BindingException e) {
133             throw e;
134         } catch (Exception JavaDoc e) {
135             throw new BindingException("Error building custom binding defined at " + DomHelper.getLocation(bindingElm), e);
136         }
137     }
138 }
139
Popular Tags