01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.binding;
18:
19: import org.apache.cocoon.forms.util.DomHelper;
20:
21: import org.w3c.dom.Element;
22:
23: /**
24: * ContextJXPathBindingBuilder provides a helper class for the Factory
25: * implemented in {@link JXPathBindingManager} that helps construct the
26: * actual {@link ContextJXPathBinding} out of the configuration in the
27: * provided configElement which looks like:
28: * <pre><code>
29: * <fb:context path="<i>xpath expression</i>">
30: * <!-- in here come the nested child bindings on the sub-context -->
31: * </fb:context>
32: * </code></pre>
33: * <p>The <code>fb:context</code> element can have an optional <code>factory</code>
34: * attribute, whose value, if present, must be the name of a class extending
35: * {@link org.apache.commons.jxpath.AbstractFactory}. If this attribute is present,
36: * an instance of the named class is registered with the JXPath context and can be used to
37: * create an object corresponding to the path of the <code>fb:context</code> element
38: * upon save, if needed.</p>
39: *
40: * @version $Id: ContextJXPathBindingBuilder.java 517733 2007-03-13 15:37:22Z vgritsenko $
41: */
42: public class ContextJXPathBindingBuilder extends
43: JXPathBindingBuilderBase {
44:
45: /**
46: * Creates an instance of ContextJXPathBinding with the configured
47: * path and nested child bindings from the declarations in the bindingElm
48: */
49: public JXPathBindingBase buildBinding(Element bindingElm,
50: JXPathBindingManager.Assistant assistant)
51: throws BindingException {
52:
53: try {
54: CommonAttributes commonAtts = JXPathBindingBuilderBase
55: .getCommonAttributes(bindingElm);
56: String xpath = DomHelper.getAttribute(bindingElm, "path",
57: null);
58: String factory = DomHelper.getAttribute(bindingElm,
59: "factory", null);
60:
61: JXPathBindingBase[] childBindings = new JXPathBindingBase[0];
62:
63: // do inheritance
64: ContextJXPathBinding otherBinding = (ContextJXPathBinding) assistant
65: .getContext().getSuperBinding();
66: if (otherBinding != null) {
67: childBindings = otherBinding.getChildBindings();
68: commonAtts = JXPathBindingBuilderBase
69: .mergeCommonAttributes(otherBinding
70: .getCommonAtts(), commonAtts);
71:
72: if (xpath == null) {
73: xpath = otherBinding.getXPath();
74: }
75: if (factory == null) {
76: factory = otherBinding.getFactoryClassName();
77: }
78:
79: }
80:
81: childBindings = assistant.makeChildBindings(bindingElm,
82: childBindings);
83:
84: return new ContextJXPathBinding(commonAtts, xpath, factory,
85: childBindings);
86: } catch (BindingException e) {
87: throw e;
88: } catch (Exception e) {
89: throw new BindingException(
90: "Error building context binding", DomHelper
91: .getLocationObject(bindingElm));
92: }
93: }
94: }
|