KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.binding;
17
18 import org.apache.cocoon.woody.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 CVS $Id: InsertNodeJXPathBinding.java 30932 2004-07-29 17:35:38Z vgritsenko $
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     /**
49      * Do-nothing implementation of the interface.
50      */

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

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