KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > binding > InsertBeanJXPathBinding


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 java.lang.reflect.Method JavaDoc;
19
20 import org.apache.avalon.framework.CascadingRuntimeException;
21 import org.apache.cocoon.woody.formmodel.Widget;
22 import org.apache.commons.jxpath.JXPathContext;
23
24 /**
25  * InsertBeanJXPathBinding provides an implementation of a {@link Binding}
26  * that inserts a new instance of the specified bean (classname) 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  * </ol>
32  *
33  * @version CVS $Id: InsertBeanJXPathBinding.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 public class InsertBeanJXPathBinding extends JXPathBindingBase {
36
37     private final String JavaDoc className;
38     private final String JavaDoc addMethodName;
39
40     /**
41      * Constructs InsertBeanJXPathBinding
42      */

43     public InsertBeanJXPathBinding(JXPathBindingBuilderBase.CommonAttributes commonAtts, String JavaDoc className, String JavaDoc addMethod) {
44         super(commonAtts);
45         this.className = className;
46         this.addMethodName = addMethod;
47     }
48
49     /**
50      * Do-nothing implementation of the interface.
51      */

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

62     public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
63         try {
64             Object JavaDoc parent = jxpc.getContextBean();
65             Object JavaDoc[] args = new Object JavaDoc[1];
66             Class JavaDoc[] argTypes = new Class JavaDoc[1];
67
68             // instantiate the new object
69
argTypes[0] = Class.forName(this.className);
70             args[0] = argTypes[0].newInstance();
71
72             // lookup the named method on the parent
73
Method JavaDoc addMethod =
74                 parent.getClass().getMethod(this.addMethodName, argTypes);
75
76             // invoke this method with this new beast.
77
addMethod.invoke(parent, args);
78
79             if (getLogger().isDebugEnabled())
80                 getLogger().debug("InsertBean performed.");
81         } catch (Exception JavaDoc e) {
82             throw new CascadingRuntimeException("InsertBean failed.", e);
83         }
84
85         // jxpc.setFactory(new AbstractFactory() {
86
// public boolean createObject(JXPathContext context, Pointer pointer,
87
// Object parent, String name, int index) {
88
// try {
89
// Object[] args = new Object[1];
90
// Class[] argTypes = new Class[1];
91
//
92
// // instantiate the new object
93
// argTypes[0] = Class.forName(InsertBeanJXPathBinding.this.className);
94
// args[0] = argTypes[0].newInstance();
95
// // lookup the named method on the parent
96
//
97
// Method addMethod =
98
// parent.getClass().getMethod(InsertBeanJXPathBinding.this.addMethodName, argTypes);
99
// // invoke this method with this new beast.
100
//
101
// addMethod.invoke(parent, args);
102
//
103
// if (getLogger().isDebugEnabled())
104
// getLogger().debug("InsertBean jxpath factory executed for index " + index);
105
// return true;
106
// } catch (Exception e) {
107
// throw new CascadingRuntimeException("InsertBean jxpath factory failed.", e);
108
// }
109
// }
110
// });
111
//
112
// if (getLogger().isDebugEnabled())
113
// getLogger().debug("done registered factory for inserting node -- " + toString());
114
}
115
116     public String JavaDoc toString() {
117         return "InsertBeanJXPathBinding [for class " + this.className + " to addMethod " + this.addMethodName + "]";
118     }
119
120 }
121
Popular Tags