KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.DomHelper;
19 import org.w3c.dom.Element JavaDoc;
20
21 /**
22  * ContextJXPathBindingBuilder provides a helper class for the Factory
23  * implemented in {@link JXPathBindingManager} that helps construct the
24  * actual {@link ContextJXPathBinding} out of the configuration in the
25  * provided configElement which looks like:
26  * <pre><code>
27  * &lt;fb:context path="<i>xpath expression</i>"&gt;
28  * &lt;!-- in here come the nested child bindings on the sub-context --&gt;
29  * &lt;/fb:context&gt;
30  * </code></pre>
31  * <p>The <code>fb:context</code> element can have an optional <code>factory</code>
32  * attribute, whose value, if present, must be the name of a class extending
33  * {@link org.apache.commons.jxpath.AbstractFactory}. If this attribute is present,
34  * an instance of the named class is registered with the JXPath context and can be used to
35  * create an object corresponding to the path of the <code>fb:context</code> element
36  * upon save, if needed.</p>
37  *
38  * @version $Id: ContextJXPathBindingBuilder.java 289538 2005-09-16 13:46:22Z sylvain $
39  */

40 public class ContextJXPathBindingBuilder extends JXPathBindingBuilderBase {
41
42     /**
43      * Creates an instance of ContextJXPathBinding with the configured
44      * path and nested child bindings from the declarations in the bindingElm
45      */

46     public JXPathBindingBase buildBinding(Element JavaDoc bindingElm,
47         JXPathBindingManager.Assistant assistant) throws BindingException {
48
49         try {
50             CommonAttributes commonAtts = JXPathBindingBuilderBase.getCommonAttributes(bindingElm);
51             String JavaDoc xpath = DomHelper.getAttribute(bindingElm, "path", null);
52             String JavaDoc factory = DomHelper.getAttribute(bindingElm, "factory", null);
53
54             JXPathBindingBase[] childBindings = new JXPathBindingBase[0];
55             
56             // do inheritance
57
ContextJXPathBinding otherBinding = (ContextJXPathBinding)assistant.getContext().getSuperBinding();
58             if(otherBinding!=null) {
59                 childBindings = otherBinding.getChildBindings();
60                 commonAtts = JXPathBindingBuilderBase.mergeCommonAttributes(otherBinding.getCommonAtts(),commonAtts);
61                 
62                 if(xpath==null)
63                     xpath = otherBinding.getXPath();
64              if (factory == null)
65                  factory = otherBinding.getFactoryClassName();
66             
67             }
68             
69             childBindings = assistant.makeChildBindings(bindingElm,childBindings);
70             
71             ContextJXPathBinding contextBinding = new ContextJXPathBinding(commonAtts, xpath, factory, childBindings);
72             return contextBinding;
73         } catch (BindingException e) {
74             throw e;
75         } catch (Exception JavaDoc e) {
76             throw new BindingException("Error building context binding defined at " + DomHelper.getLocation(bindingElm), e);
77         }
78     }
79 }
80
Popular Tags