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.w3c.dom.Document;
22: import org.w3c.dom.DocumentFragment;
23: import org.w3c.dom.Node;
24:
25: /**
26: * InsertNodeJXPathBinding provides an implementation of a {@link Binding}
27: * that inserts a clone of some 'template document-fragment' into the target
28: * back-end model upon save.
29: * <p>
30: * NOTES: <ol>
31: * <li>This Binding does not perform any actions when loading.</li>
32: * <li>This expects the back-end model to be an XML file.</li>
33: * </ol>
34: *
35: * @version CVS $Id: InsertNodeJXPathBinding.java 433543 2006-08-22 06:22:54Z crossley $
36: */
37: public class InsertNodeJXPathBinding extends JXPathBindingBase {
38:
39: private final DocumentFragment template;
40:
41: /**
42: * Constructs InsertNodeJXPathBinding
43: */
44: public InsertNodeJXPathBinding(
45: JXPathBindingBuilderBase.CommonAttributes commonAtts,
46: DocumentFragment domTemplate) {
47: super (commonAtts);
48: this .template = domTemplate;
49: }
50:
51: /**
52: * Do-nothing implementation of the interface.
53: */
54: public void doLoad(Widget frmModel, JXPathContext jxpc) {
55: // doesn't do a thing when loading.
56: }
57:
58: /**
59: * Registers a JXPath Factory on the JXPath Context.
60: * <p>
61: * The factory will inserts a clone of the 'template' DocumentFragment
62: * inside this object into the target objectmodel.
63: */
64: public void doSave(Widget frmModel, JXPathContext jxpc) {
65:
66: Node parentNode = (Node) jxpc.getContextBean();
67: Document targetDoc = parentNode.getOwnerDocument();
68: Node toInsert = targetDoc.importNode(this .template, true);
69: parentNode.appendChild(toInsert);
70:
71: if (getLogger().isDebugEnabled())
72: getLogger().debug("InsertNode executed.");
73:
74: // jxpc.setFactory(new AbstractFactory() {
75: // public boolean createObject(JXPathContext context, Pointer pointer,
76: // Object parent, String name, int index) {
77: //
78: // Node parentNode = (Node) parent;
79: // Document targetDoc = parentNode.getOwnerDocument();
80: // Node toInsert = targetDoc.importNode(InsertNodeJXPathBinding.this.template, true);
81: // parentNode.appendChild(toInsert);
82: //
83: // if (getLogger().isDebugEnabled())
84: // getLogger().debug("InsertNode jxpath factory executed for index." + index);
85: // return true;
86: // }
87: // });
88: //
89: // if (getLogger().isDebugEnabled())
90: // getLogger().debug("done registered factory for inserting node -- " + toString());
91: }
92:
93: public String toString() {
94: return "InsertNodeJXPathBinding [for nested template]";
95: }
96: }
|