KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > BeanPropertySetterRule


1 /* $Id: BeanPropertySetterRule.java 155412 2005-02-26 12:58:36Z dirkv $
2  *
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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
18
19 package org.apache.commons.digester;
20
21
22 import java.beans.PropertyDescriptor JavaDoc;
23
24 import org.apache.commons.beanutils.BeanUtils;
25 import org.apache.commons.beanutils.DynaBean;
26 import org.apache.commons.beanutils.DynaProperty;
27 import org.apache.commons.beanutils.PropertyUtils;
28
29
30 /**
31  * <p> Rule implements sets a bean property on the top object
32  * to the body text.</p>
33  *
34  * <p> The property set:</p>
35  * <ul><li>can be specified when the rule is created</li>
36  * <li>or can match the current element when the rule is called.</li></ul>
37  *
38  * <p> Using the second method and the {@link ExtendedBaseRules} child match
39  * pattern, all the child elements can be automatically mapped to properties
40  * on the parent object.</p>
41  */

42
43 public class BeanPropertySetterRule extends Rule {
44
45
46     // ----------------------------------------------------------- Constructors
47

48
49     /**
50      * <p>Construct rule that sets the given property from the body text.</p>
51      *
52      * @param digester associated <code>Digester</code>
53      * @param propertyName name of property to set
54      *
55      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
56      * Use {@link #BeanPropertySetterRule(String propertyName)} instead.
57      */

58     public BeanPropertySetterRule(Digester digester, String JavaDoc propertyName) {
59
60         this(propertyName);
61
62     }
63
64     /**
65      * <p>Construct rule that automatically sets a property from the body text.
66      *
67      * <p> This construct creates a rule that sets the property
68      * on the top object named the same as the current element.
69      *
70      * @param digester associated <code>Digester</code>
71      *
72      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
73      * Use {@link #BeanPropertySetterRule()} instead.
74      */

75     public BeanPropertySetterRule(Digester digester) {
76
77         this();
78
79     }
80
81     /**
82      * <p>Construct rule that sets the given property from the body text.</p>
83      *
84      * @param propertyName name of property to set
85      */

86     public BeanPropertySetterRule(String JavaDoc propertyName) {
87
88         this.propertyName = propertyName;
89
90     }
91
92     /**
93      * <p>Construct rule that automatically sets a property from the body text.
94      *
95      * <p> This construct creates a rule that sets the property
96      * on the top object named the same as the current element.
97      */

98     public BeanPropertySetterRule() {
99
100         this((String JavaDoc)null);
101
102     }
103     
104     // ----------------------------------------------------- Instance Variables
105

106
107     /**
108      * Set this property on the top object.
109      */

110     protected String JavaDoc propertyName = null;
111
112
113     /**
114      * The body text used to set the property.
115      */

116     protected String JavaDoc bodyText = null;
117
118
119     // --------------------------------------------------------- Public Methods
120

121
122     /**
123      * Process the body text of this element.
124      *
125      * @param namespace the namespace URI of the matching element, or an
126      * empty string if the parser is not namespace aware or the element has
127      * no namespace
128      * @param name the local name if the parser is namespace aware, or just
129      * the element name otherwise
130      * @param text The text of the body of this element
131      */

132     public void body(String JavaDoc namespace, String JavaDoc name, String JavaDoc text)
133         throws Exception JavaDoc {
134
135         // log some debugging information
136
if (digester.log.isDebugEnabled()) {
137             digester.log.debug("[BeanPropertySetterRule]{" +
138                     digester.match + "} Called with text '" + text + "'");
139         }
140
141         bodyText = text.trim();
142
143     }
144
145
146     /**
147      * Process the end of this element.
148      *
149      * @param namespace the namespace URI of the matching element, or an
150      * empty string if the parser is not namespace aware or the element has
151      * no namespace
152      * @param name the local name if the parser is namespace aware, or just
153      * the element name otherwise
154      *
155      * @exception NoSuchMethodException if the bean does not
156      * have a writeable property of the specified name
157      */

158     public void end(String JavaDoc namespace, String JavaDoc name) throws Exception JavaDoc {
159
160         String JavaDoc property = propertyName;
161
162         if (property == null) {
163             // If we don't have a specific property name,
164
// use the element name.
165
property = name;
166         }
167
168         // Get a reference to the top object
169
Object JavaDoc top = digester.peek();
170
171         // log some debugging information
172
if (digester.log.isDebugEnabled()) {
173             digester.log.debug("[BeanPropertySetterRule]{" + digester.match +
174                     "} Set " + top.getClass().getName() + " property " +
175                                property + " with text " + bodyText);
176         }
177
178         // Force an exception if the property does not exist
179
// (BeanUtils.setProperty() silently returns in this case)
180
if (top instanceof DynaBean) {
181             DynaProperty desc =
182                 ((DynaBean) top).getDynaClass().getDynaProperty(property);
183             if (desc == null) {
184                 throw new NoSuchMethodException JavaDoc
185                     ("Bean has no property named " + property);
186             }
187         } else /* this is a standard JavaBean */ {
188             PropertyDescriptor JavaDoc desc =
189                 PropertyUtils.getPropertyDescriptor(top, property);
190             if (desc == null) {
191                 throw new NoSuchMethodException JavaDoc
192                     ("Bean has no property named " + property);
193             }
194         }
195
196         // Set the property (with conversion as necessary)
197
BeanUtils.setProperty(top, property, bodyText);
198
199     }
200
201
202     /**
203      * Clean up after parsing is complete.
204      */

205     public void finish() throws Exception JavaDoc {
206
207         bodyText = null;
208
209     }
210
211
212     /**
213      * Render a printable version of this Rule.
214      */

215     public String JavaDoc toString() {
216
217         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("BeanPropertySetterRule[");
218         sb.append("propertyName=");
219         sb.append(propertyName);
220         sb.append("]");
221         return (sb.toString());
222
223     }
224
225 }
226
Popular Tags