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.woody.binding;
18:
19: import org.apache.cocoon.woody.formmodel.Widget;
20: import org.apache.commons.jxpath.JXPathContext;
21: import org.apache.commons.jxpath.Pointer;
22:
23: /**
24: * ContextJXPathBinding provides an implementation of a {@link Binding}
25: * that narrows the binding scope to some xpath-context on the target
26: * objectModel to load and save from.
27: *
28: * @version CVS $Id: ContextJXPathBinding.java 433543 2006-08-22 06:22:54Z crossley $
29: */
30: public class ContextJXPathBinding extends ComposedJXPathBindingBase {
31:
32: /**
33: * the relative contextPath for the sub-bindings of this context
34: */
35: private final String xpath;
36:
37: /**
38: * Constructs ContextJXPathBinding for the specified xpath sub-context
39: */
40: public ContextJXPathBinding(
41: JXPathBindingBuilderBase.CommonAttributes commonAtts,
42: String contextPath, JXPathBindingBase[] childBindings) {
43: super (commonAtts, childBindings);
44: this .xpath = contextPath;
45: }
46:
47: /**
48: * Actively performs the binding from the ObjectModel wrapped in a jxpath
49: * context to the Woody-form.
50: */
51: public void doLoad(Widget frmModel, JXPathContext jxpc)
52: throws BindingException {
53: Pointer ptr = jxpc.getPointer(this .xpath);
54: if (ptr.getNode() != null) {
55: JXPathContext subContext = jxpc.getRelativeContext(ptr);
56: super .doLoad(frmModel, subContext);
57: if (getLogger().isDebugEnabled())
58: getLogger().debug("done loading " + toString());
59: } else {
60: if (getLogger().isDebugEnabled()) {
61: getLogger().debug(
62: "non-existent path: skipping " + toString());
63: }
64: }
65: }
66:
67: /**
68: * Actively performs the binding from the Woody-form to the ObjectModel
69: * wrapped in a jxpath context.
70: */
71: public void doSave(Widget frmModel, JXPathContext jxpc)
72: throws BindingException {
73: Pointer ptr = jxpc.getPointer(this .xpath);
74: if (ptr.getNode() == null) {
75: jxpc.createPath(this .xpath);
76: // Need to recreate the pointer after creating the path
77: ptr = jxpc.getPointer(this .xpath);
78: }
79: JXPathContext subContext = jxpc.getRelativeContext(ptr);
80: super .doSave(frmModel, subContext);
81: if (getLogger().isDebugEnabled()) {
82: getLogger().debug("done saving " + toString());
83: }
84: }
85:
86: public String toString() {
87: return "ContextJXPathBinding [xpath=" + this .xpath + "]";
88: }
89: }
|