1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.digester;
18
19 import java.util.Map;
20
21 import org.apache.commons.betwixt.XMLBeanInfo;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.xml.sax.Attributes;
25 import org.xml.sax.SAXException;
26
27 /***
28 * Digester Rule to process class elements.
29 * @since 0.7
30 * @author Brian Pugh
31 */
32 public class ClassRule extends RuleSupport {
33 /*** Logger. */
34 private static final Log log = LogFactory.getLog(ClassRule.class);
35
36 /*** Base constructor. */
37 public ClassRule() {
38 }
39
40
41
42 /***
43 * Process the beginning of this element.
44 *
45 * @param attributes The attribute list of this element
46 * @throws org.xml.sax.SAXException if the primitiveTypes attribute contains an invalid value
47 */
48 public void begin(Attributes attributes) throws SAXException {
49 String className = attributes.getValue("name");
50 if (className == null || "".equals(className)) {
51 throw new SAXException("Invalid 'class' element. "
52 + "Attribute 'name' is required but was not found but was not found.");
53 }
54
55 try {
56
57 Class beanClass = Class.forName(className);
58 XMLBeanInfo xmlBeanInfo = new XMLBeanInfo(beanClass);
59 XMLBeanInfoDigester xmlBeanInfoDigester = (XMLBeanInfoDigester) getDigester();
60 xmlBeanInfoDigester.setBeanClass(beanClass);
61 xmlBeanInfoDigester.push(xmlBeanInfo);
62
63 } catch (ClassNotFoundException e) {
64 throw new SAXException("Invalid 'class' element. Unable to find class: " + className, e);
65 }
66 }
67
68 /***
69 * Process the end of this element.
70 */
71 public void end() {
72 XMLBeanInfo xmlBeanInfo = (XMLBeanInfo) getDigester().pop();
73 MultiMappingBeanInfoDigester digester = (MultiMappingBeanInfoDigester) getDigester();
74 Map xmlBeanInfoMapping = digester.getBeanInfoMap();
75 xmlBeanInfoMapping.put(xmlBeanInfo.getBeanClass(), xmlBeanInfo);
76 digester.setBeanClass(null);
77 }
78 }
79