KickJava   Java API By Example, From Geeks To Geeks.

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


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.avalon.framework.CascadingRuntimeException;
19 import org.apache.cocoon.forms.binding.JXPathBindingBuilderBase.CommonAttributes;
20 import org.apache.cocoon.forms.formmodel.Widget;
21 import org.apache.cocoon.util.ClassUtils;
22 import org.apache.commons.jxpath.AbstractFactory;
23 import org.apache.commons.jxpath.JXPathContext;
24 import org.apache.commons.jxpath.Pointer;
25
26 /**
27  * ContextJXPathBinding provides an implementation of a {@link Binding}
28  * that narrows the binding scope to some xpath-context on the target
29  * objectModel to load and save from.
30  *
31  * @version $Id: ContextJXPathBinding.java 289538 2005-09-16 13:46:22Z sylvain $
32  */

33 public class ContextJXPathBinding extends ComposedJXPathBindingBase {
34
35     /**
36      * the relative contextPath for the sub-bindings of this context
37      */

38     private final String JavaDoc xpath;
39     
40     /**
41      * The name of a factory class for building intermediate elements. Must implement
42      * {@link org.apache.commons.jxpath.AbstractFactory}.
43      */

44     private AbstractFactory factory;
45
46     /**
47      * Constructs ContextJXPathBinding for the specified xpath sub-context
48      */

49     public ContextJXPathBinding(CommonAttributes commonAtts, String JavaDoc contextPath, JXPathBindingBase[] childBindings) {
50         super(commonAtts, childBindings);
51         this.xpath = contextPath;
52     }
53
54     /**
55      * Constructs ContextJXPathBinding for the specified xpath sub-context and optional JXPath factory class.
56      */

57     public ContextJXPathBinding(JXPathBindingBuilderBase.CommonAttributes commonAtts, String JavaDoc contextPath, String JavaDoc factoryClassName, JXPathBindingBase[] childBindings) {
58         super(commonAtts, childBindings);
59         this.xpath = contextPath;
60         if (factoryClassName != null) {
61             try {
62                 this.factory = (AbstractFactory) ClassUtils.newInstance(factoryClassName);
63             } catch (Exception JavaDoc e) {
64                 throw new CascadingRuntimeException("Cannot create an instance of " + factoryClassName, e);
65             }
66         }
67     }
68     
69     public String JavaDoc getFactoryClassName() {
70         return this.factory == null ? null : this.factory.getClass().getName();
71     }
72
73     /**
74      * Actively performs the binding from the ObjectModel wrapped in a jxpath
75      * context to the CocoonForm.
76      */

77     public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
78         Pointer ptr = jxpc.getPointer(this.xpath);
79         if (ptr.getNode() != null) {
80             JXPathContext subContext = jxpc.getRelativeContext(ptr);
81             super.doLoad(frmModel, subContext);
82             if (getLogger().isDebugEnabled())
83                 getLogger().debug("done loading " + toString());
84         } else {
85             if (getLogger().isDebugEnabled()) {
86                 getLogger().debug("non-existent path: skipping " + toString());
87             }
88         }
89     }
90
91     /**
92      * Actively performs the binding from the CocoonForm to the ObjectModel
93      * wrapped in a jxpath context.
94      */

95     public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
96         if (this.factory != null) {
97             jxpc.setFactory(this.factory);
98         }
99         Pointer ptr = jxpc.getPointer(this.xpath);
100         if (ptr.getNode() == null) {
101             jxpc.createPath(this.xpath);
102             // Need to recreate the pointer after creating the path
103
ptr = jxpc.getPointer(this.xpath);
104         }
105         JXPathContext subContext = jxpc.getRelativeContext(ptr);
106         super.doSave(frmModel, subContext);
107         if (getLogger().isDebugEnabled()) {
108             getLogger().debug("done saving " + toString());
109         }
110     }
111     
112     /** To allow child classes to know which path they bind to */
113     public String JavaDoc getXPath() {
114         return this.xpath;
115     }
116
117     public String JavaDoc toString() {
118         return "ContextJXPathBinding [xpath=" + this.xpath + "]";
119     }
120 }
121
Popular Tags