KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: SetPropertyRule.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 import org.xml.sax.Attributes JavaDoc;
29
30
31 /**
32  * Rule implementation that sets an individual property on the object at the
33  * top of the stack, based on attributes with specified names.
34  */

35
36 public class SetPropertyRule extends Rule {
37
38
39     // ----------------------------------------------------------- Constructors
40

41
42     /**
43      * Construct a "set property" rule with the specified name and value
44      * attributes.
45      *
46      * @param digester The digester with which this rule is associated
47      * @param name Name of the attribute that will contain the name of the
48      * property to be set
49      * @param value Name of the attribute that will contain the value to which
50      * the property should be set
51      *
52      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
53      * Use {@link #SetPropertyRule(String name, String value)} instead.
54      */

55     public SetPropertyRule(Digester digester, String JavaDoc name, String JavaDoc value) {
56
57         this(name, value);
58
59     }
60
61     /**
62      * Construct a "set property" rule with the specified name and value
63      * attributes.
64      *
65      * @param name Name of the attribute that will contain the name of the
66      * property to be set
67      * @param value Name of the attribute that will contain the value to which
68      * the property should be set
69      */

70     public SetPropertyRule(String JavaDoc name, String JavaDoc value) {
71
72         this.name = name;
73         this.value = value;
74
75     }
76
77     // ----------------------------------------------------- Instance Variables
78

79
80     /**
81      * The attribute that will contain the property name.
82      */

83     protected String JavaDoc name = null;
84
85
86     /**
87      * The attribute that will contain the property value.
88      */

89     protected String JavaDoc value = null;
90
91
92     // --------------------------------------------------------- Public Methods
93

94
95     /**
96      * Process the beginning of this element.
97      *
98      * @param attributes The attribute list of this element
99      *
100      * @exception NoSuchMethodException if the bean does not
101      * have a writeable property of the specified name
102      */

103     public void begin(Attributes JavaDoc attributes) throws Exception JavaDoc {
104
105         // Identify the actual property name and value to be used
106
String JavaDoc actualName = null;
107         String JavaDoc actualValue = null;
108         for (int i = 0; i < attributes.getLength(); i++) {
109             String JavaDoc name = attributes.getLocalName(i);
110             if ("".equals(name)) {
111                 name = attributes.getQName(i);
112             }
113             String JavaDoc value = attributes.getValue(i);
114             if (name.equals(this.name)) {
115                 actualName = value;
116             } else if (name.equals(this.value)) {
117                 actualValue = value;
118             }
119         }
120
121         // Get a reference to the top object
122
Object JavaDoc top = digester.peek();
123
124         // Log some debugging information
125
if (digester.log.isDebugEnabled()) {
126             digester.log.debug("[SetPropertyRule]{" + digester.match +
127                     "} Set " + top.getClass().getName() + " property " +
128                     actualName + " to " + actualValue);
129         }
130
131         // Force an exception if the property does not exist
132
// (BeanUtils.setProperty() silently returns in this case)
133
//
134
// This code should probably use PropertyUtils.isWriteable(),
135
// like SetPropertiesRule does.
136
if (top instanceof DynaBean) {
137             DynaProperty desc =
138                 ((DynaBean) top).getDynaClass().getDynaProperty(actualName);
139             if (desc == null) {
140                 throw new NoSuchMethodException JavaDoc
141                     ("Bean has no property named " + actualName);
142             }
143         } else /* this is a standard JavaBean */ {
144             PropertyDescriptor JavaDoc desc =
145                 PropertyUtils.getPropertyDescriptor(top, actualName);
146             if (desc == null) {
147                 throw new NoSuchMethodException JavaDoc
148                     ("Bean has no property named " + actualName);
149             }
150         }
151
152         // Set the property (with conversion as necessary)
153
BeanUtils.setProperty(top, actualName, actualValue);
154
155     }
156
157
158     /**
159      * Render a printable version of this Rule.
160      */

161     public String JavaDoc toString() {
162
163         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("SetPropertyRule[");
164         sb.append("name=");
165         sb.append(name);
166         sb.append(", value=");
167         sb.append(value);
168         sb.append("]");
169         return (sb.toString());
170
171     }
172
173
174 }
175
Popular Tags