KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.binding;
17
18 import org.apache.cocoon.woody.formmodel.Widget;
19 import org.apache.commons.jxpath.JXPathContext;
20 import org.apache.commons.jxpath.Pointer;
21
22 /**
23  * ContextJXPathBinding provides an implementation of a {@link Binding}
24  * that narrows the binding scope to some xpath-context on the target
25  * objectModel to load and save from.
26  *
27  * @version CVS $Id: ContextJXPathBinding.java 30932 2004-07-29 17:35:38Z vgritsenko $
28  */

29 public class ContextJXPathBinding extends ComposedJXPathBindingBase {
30
31     /**
32      * the relative contextPath for the sub-bindings of this context
33      */

34     private final String JavaDoc xpath;
35
36     /**
37      * Constructs ContextJXPathBinding for the specified xpath sub-context
38      */

39     public ContextJXPathBinding(JXPathBindingBuilderBase.CommonAttributes commonAtts, String JavaDoc contextPath, JXPathBindingBase[] childBindings) {
40         super(commonAtts, childBindings);
41         this.xpath = contextPath;
42     }
43
44     /**
45      * Actively performs the binding from the ObjectModel wrapped in a jxpath
46      * context to the Woody-form.
47      */

48     public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
49         Pointer ptr = jxpc.getPointer(this.xpath);
50         if (ptr.getNode() != null) {
51             JXPathContext subContext = jxpc.getRelativeContext(ptr);
52             super.doLoad(frmModel, subContext);
53             if (getLogger().isDebugEnabled())
54                 getLogger().debug("done loading " + toString());
55         } else {
56             if (getLogger().isDebugEnabled()) {
57                 getLogger().debug("non-existent path: skipping " + toString());
58             }
59         }
60     }
61
62     /**
63      * Actively performs the binding from the Woody-form to the ObjectModel
64      * wrapped in a jxpath context.
65      */

66     public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
67         Pointer ptr = jxpc.getPointer(this.xpath);
68         if (ptr.getNode() == null) {
69             jxpc.createPath(this.xpath);
70             // Need to recreate the pointer after creating the path
71
ptr = jxpc.getPointer(this.xpath);
72         }
73         JXPathContext subContext = jxpc.getRelativeContext(ptr);
74         super.doSave(frmModel, subContext);
75         if (getLogger().isDebugEnabled()) {
76             getLogger().debug("done saving " + toString());
77         }
78     }
79
80     public String JavaDoc toString() {
81         return "ContextJXPathBinding [xpath=" + this.xpath + "]";
82     }
83 }
84
Popular Tags