1 package org.apache.commons.digester3;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static java.lang.String.format;
23 import static org.apache.commons.beanutils.BeanUtils.setProperty;
24 import static org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptor;
25
26 import java.beans.PropertyDescriptor;
27
28 import org.apache.commons.beanutils.DynaBean;
29 import org.apache.commons.beanutils.DynaProperty;
30 import org.xml.sax.Attributes;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class BeanPropertySetterRule
49 extends Rule
50 {
51
52
53
54
55
56
57
58
59
60
61 public BeanPropertySetterRule( String propertyName )
62 {
63 this.propertyName = propertyName;
64 }
65
66
67
68
69
70
71
72 public BeanPropertySetterRule()
73 {
74 this( null );
75 }
76
77
78
79
80
81
82 private String propertyName;
83
84
85
86
87 private String propertyNameFromAttribute;
88
89
90
91
92 private String bodyText = null;
93
94
95
96
97
98
99
100
101 public String getPropertyName()
102 {
103 return propertyName;
104 }
105
106
107
108
109
110
111
112 public void setPropertyNameFromAttribute( String propertyNameFromAttribute )
113 {
114 this.propertyNameFromAttribute = propertyNameFromAttribute;
115 }
116
117
118
119
120
121
122 protected String getBodyText()
123 {
124 return bodyText;
125 }
126
127
128
129
130 @Override
131 public void begin( String namespace, String name, Attributes attributes )
132 throws Exception
133 {
134 if ( propertyNameFromAttribute != null )
135 {
136 propertyName = attributes.getValue( propertyNameFromAttribute );
137
138 getDigester().getLogger().warn( format( "[BeanPropertySetterRule]{%s} Attribute '%s' not found in matching element '%s'",
139 getDigester().getMatch(), propertyNameFromAttribute, name ) );
140 }
141 }
142
143
144
145
146 @Override
147 public void body( String namespace, String name, String text )
148 throws Exception
149 {
150
151 if ( getDigester().getLogger().isDebugEnabled() )
152 {
153 getDigester().getLogger().debug( format( "[BeanPropertySetterRule]{%s} Called with text '%s'",
154 getDigester().getMatch(),
155 text ) );
156 }
157
158 bodyText = text.trim();
159 }
160
161
162
163
164 @Override
165 public void end( String namespace, String name )
166 throws Exception
167 {
168 String property = propertyName;
169
170 if ( property == null )
171 {
172
173
174 property = name;
175 }
176
177
178 Object top = getDigester().peek();
179
180
181 if ( getDigester().getLogger().isDebugEnabled() )
182 {
183 getDigester().getLogger().debug( format( "[BeanPropertySetterRule]{%s} Set %s property %s with text %s",
184 getDigester().getMatch(),
185 top.getClass().getName(),
186 property,
187 bodyText ) );
188 }
189
190
191
192 if ( top instanceof DynaBean )
193 {
194 DynaProperty desc = ( (DynaBean) top ).getDynaClass().getDynaProperty( property );
195 if ( desc == null )
196 {
197 throw new NoSuchMethodException( "Bean has no property named " + property );
198 }
199 }
200 else
201
202 {
203 PropertyDescriptor desc = getPropertyDescriptor( top, property );
204 if ( desc == null )
205 {
206 throw new NoSuchMethodException( "Bean has no property named " + property );
207 }
208 }
209
210
211 setProperty( top, property, bodyText );
212 }
213
214
215
216
217 @Override
218 public void finish()
219 throws Exception
220 {
221 bodyText = null;
222 }
223
224
225
226
227 @Override
228 public String toString()
229 {
230 return format( "BeanPropertySetterRule[propertyName=%s]", propertyName );
231 }
232
233 }