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