KickJava   Java API By Example, From Geeks To Geeks.

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


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.cocoon.forms.formmodel.Widget;
19 import org.apache.commons.jxpath.JXPathContext;
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.DocumentFragment JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23
24 /**
25  * InsertNodeJXPathBinding provides an implementation of a {@link Binding}
26  * that inserts a clone of some 'template document-fragment' into the target
27  * back-end model upon save.
28  * <p>
29  * NOTES: <ol>
30  * <li>This Binding does not perform any actions when loading.</li>
31  * <li>This expects the back-end model to be an XML file.</li>
32  * </ol>
33  *
34  * @version $Id: InsertNodeJXPathBinding.java 289538 2005-09-16 13:46:22Z sylvain $
35  */

36 public class InsertNodeJXPathBinding extends JXPathBindingBase {
37
38     private final DocumentFragment JavaDoc template;
39
40     /**
41      * Constructs InsertNodeJXPathBinding
42      */

43     public InsertNodeJXPathBinding(JXPathBindingBuilderBase.CommonAttributes commonAtts, DocumentFragment JavaDoc domTemplate) {
44         super(commonAtts);
45         this.template = domTemplate;
46     }
47     
48     public DocumentFragment JavaDoc getTemplate() { return template; }
49
50     /**
51      * Do-nothing implementation of the interface.
52      */

53     public void doLoad(Widget frmModel, JXPathContext jxpc) {
54         // doesn't do a thing when loading.
55
}
56
57     /**
58      * Registers a JXPath Factory on the JXPath Context.
59      * <p>
60      * The factory will inserts a clone of the 'template' DocumentFragment
61      * inside this object into the target objectmodel.
62      */

63     public void doSave(Widget frmModel, JXPathContext jxpc) {
64
65         Node JavaDoc parentNode = (Node JavaDoc)jxpc.getContextBean();
66         Document JavaDoc targetDoc = parentNode.getOwnerDocument();
67         Node JavaDoc toInsert = targetDoc.importNode(this.template, true);
68         parentNode.appendChild(toInsert);
69
70         if (getLogger().isDebugEnabled())
71             getLogger().debug("InsertNode executed.");
72
73         // jxpc.setFactory(new AbstractFactory() {
74
// public boolean createObject(JXPathContext context, Pointer pointer,
75
// Object parent, String name, int index) {
76
//
77
// Node parentNode = (Node) parent;
78
// Document targetDoc = parentNode.getOwnerDocument();
79
// Node toInsert = targetDoc.importNode(InsertNodeJXPathBinding.this.template, true);
80
// parentNode.appendChild(toInsert);
81
//
82
// if (getLogger().isDebugEnabled())
83
// getLogger().debug("InsertNode jxpath factory executed for index." + index);
84
// return true;
85
// }
86
// });
87
//
88
// if (getLogger().isDebugEnabled())
89
// getLogger().debug("done registered factory for inserting node -- " + toString());
90
}
91
92     public String JavaDoc toString() {
93         return "InsertNodeJXPathBinding [for nested template]";
94     }
95 }
96
Popular Tags