KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > 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.forms.binding;
17
18 import java.lang.reflect.Method JavaDoc;
19
20 import org.apache.avalon.framework.CascadingRuntimeException;
21 import org.apache.cocoon.forms.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) or a new instance
27  * created by the model itself into the target 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 a Java Bean model.</li>
32  * </ol>
33  *
34  * @version $Id: InsertBeanJXPathBinding.java 289538 2005-09-16 13:46:22Z sylvain $
35  */

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

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

56     public void doLoad(Widget frmModel, JXPathContext jxpc) {
57         // doesn't do a thing when loading.
58
}
59
60     /**
61      * Registers a JXPath Factory on the JXPath Context.
62      * <p>
63      * The factory will insert a new instance of the specified bean (classname)
64      * inside this object into the target objectmodel or it will just call the
65      * add method if classname is null.
66      */

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