1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.io;
19
20 import org.apache.commons.betwixt.BindingConfiguration;
21 import org.apache.commons.betwixt.ElementDescriptor;
22 import org.apache.commons.betwixt.XMLIntrospector;
23 import org.apache.commons.betwixt.expression.Context;
24 import org.apache.commons.betwixt.io.read.BeanBindAction;
25 import org.apache.commons.betwixt.io.read.MappingAction;
26 import org.apache.commons.betwixt.io.read.ReadConfiguration;
27 import org.apache.commons.betwixt.io.read.ReadContext;
28 import org.apache.commons.digester.Digester;
29 import org.apache.commons.digester.Rule;
30 import org.apache.commons.digester.RuleSet;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.xml.sax.Attributes;
34
35
36
37
38
39
40
41 public class BeanRuleSet implements RuleSet {
42
43
44 private static Log log = LogFactory.getLog(BeanRuleSet.class);
45
46
47
48
49
50 public static void setLog(Log aLog) {
51 log = aLog;
52 }
53
54
55 private String basePath;
56
57 private ElementDescriptor baseElementDescriptor;
58
59
60 private DigesterReadContext context;
61
62 private String classNameAttribute = "className";
63
64
65
66
67
68
69
70
71
72
73
74 public BeanRuleSet(
75 XMLIntrospector introspector,
76 String basePath,
77 ElementDescriptor baseElementDescriptor,
78 Class baseBeanClass,
79 boolean matchIDs) {
80 this.basePath = basePath;
81 this.baseElementDescriptor = baseElementDescriptor;
82 BindingConfiguration bindingConfiguration = new BindingConfiguration();
83 bindingConfiguration.setMapIDs(matchIDs);
84 context =
85 new DigesterReadContext(
86 log,
87 bindingConfiguration,
88 new ReadConfiguration());
89 context.setRootClass(baseBeanClass);
90 context.setXMLIntrospector(introspector);
91 }
92
93
94
95
96
97
98
99
100
101
102
103 public BeanRuleSet(
104 XMLIntrospector introspector,
105 String basePath,
106 ElementDescriptor baseElementDescriptor,
107 Context context) {
108
109 this.basePath = basePath;
110 this.baseElementDescriptor = baseElementDescriptor;
111 this.context =
112 new DigesterReadContext(context, new ReadConfiguration());
113 this.context.setRootClass(
114 baseElementDescriptor.getSingularPropertyType());
115 this.context.setXMLIntrospector(introspector);
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129 public BeanRuleSet(
130 XMLIntrospector introspector,
131 String basePath,
132 ElementDescriptor baseElementDescriptor,
133 Class baseBeanClass,
134 Context context) {
135 this(
136 introspector,
137 basePath,
138 baseElementDescriptor,
139 baseBeanClass,
140 new ReadContext( context, new ReadConfiguration() ));
141 }
142
143
144
145
146
147
148
149
150
151
152
153 public BeanRuleSet(
154 XMLIntrospector introspector,
155 String basePath,
156 ElementDescriptor baseElementDescriptor,
157 Class baseBeanClass,
158 ReadContext baseContext) {
159 this.basePath = basePath;
160 this.baseElementDescriptor = baseElementDescriptor;
161 this.context = new DigesterReadContext(baseContext);
162 this.context.setRootClass(baseBeanClass);
163 this.context.setXMLIntrospector(introspector);
164 }
165
166
167
168
169
170
171
172
173
174 public String getClassNameAttribute() {
175 return context.getClassNameAttribute();
176 }
177
178
179
180
181
182
183
184
185
186
187
188 public void setClassNameAttribute(String classNameAttribute) {
189 context.setClassNameAttribute(classNameAttribute);
190 }
191
192
193
194
195
196
197
198
199
200
201 public String getNamespaceURI() {
202 return null;
203 }
204
205
206
207
208
209
210 public void addRuleInstances(Digester digester) {
211 if (log.isTraceEnabled()) {
212 log.trace("Adding rules to:" + digester);
213 }
214
215 context.setDigester(digester);
216
217
218 if (context.getClassLoader() == null) {
219 context.setClassLoader(digester.getClassLoader());
220 }
221
222
223
224 digester.addRule("!" + basePath + "/*", new ActionMappingRule());
225 }
226
227
228
229
230
231
232 private final class ActionMappingRule extends Rule {
233
234
235
236
237
238
239 public void begin(String namespace, String name, Attributes attributes)
240 throws Exception {
241
242 if (log.isTraceEnabled()) {
243 int attributesLength = attributes.getLength();
244 if (attributesLength > 0) {
245 log.trace("Attributes:");
246 }
247 for (int i = 0, size = attributesLength; i < size; i++) {
248 log.trace("Local:" + attributes.getLocalName(i));
249 log.trace("URI:" + attributes.getURI(i));
250 log.trace("QName:" + attributes.getQName(i));
251 }
252 }
253
254 context.pushElement(name);
255
256 MappingAction nextAction =
257 nextAction(namespace, name, attributes, context);
258
259 context.pushMappingAction(nextAction);
260 }
261
262
263
264
265
266
267
268
269
270
271 private MappingAction nextAction(
272 String namespace,
273 String name,
274 Attributes attributes,
275 ReadContext context)
276 throws Exception {
277
278 MappingAction result = null;
279 MappingAction lastAction = context.currentMappingAction();
280 if (lastAction == null)
281 {
282 result = BeanBindAction.INSTANCE;
283 } else {
284
285 result = lastAction.next(namespace, name, attributes, context);
286 }
287 return result.begin(namespace, name, attributes, context);
288 }
289
290
291
292
293
294
295
296
297 public void body(String namespace, String name, String text)
298 throws Exception {
299
300 if (log.isTraceEnabled()) log.trace("[BRS] Body with text " + text);
301 if (digester.getCount() > 0) {
302 MappingAction action = context.currentMappingAction();
303 action.body(text, context);
304 } else {
305 log.trace("[BRS] ZERO COUNT");
306 }
307 }
308
309
310
311
312
313 public void end(String namespace, String name) throws Exception {
314
315 MappingAction action = context.popMappingAction();
316 action.end(context);
317 }
318
319
320
321
322 public void finish() {
323
324
325
326 context.clearBeans();
327 }
328
329 }
330
331
332
333
334
335
336 private static class DigesterReadContext extends ReadContext {
337
338 private Digester digester;
339
340
341
342
343
344 public DigesterReadContext(
345 Context context,
346 ReadConfiguration readConfiguration) {
347 super(context, readConfiguration);
348
349 }
350
351
352
353
354
355 public DigesterReadContext(
356 BindingConfiguration bindingConfiguration,
357 ReadConfiguration readConfiguration) {
358 super(bindingConfiguration, readConfiguration);
359 }
360
361
362
363
364
365
366 public DigesterReadContext(
367 Log log,
368 BindingConfiguration bindingConfiguration,
369 ReadConfiguration readConfiguration) {
370 super(log, bindingConfiguration, readConfiguration);
371 }
372
373
374
375
376
377
378 public DigesterReadContext(ReadContext readContext) {
379 super(readContext);
380 }
381
382 public Digester getDigester() {
383
384 return digester;
385 }
386
387 public void setDigester(Digester digester) {
388
389 this.digester = digester;
390 }
391
392
393
394
395 public void pushBean(Object bean) {
396 super.pushBean(bean);
397 digester.push(bean);
398 }
399
400
401
402
403 public Object popBean() {
404 Object bean = super.popBean();
405
406 if (digester.getCount() > 0) {
407 digester.pop();
408 }
409 return bean;
410 }
411 }
412
413 }