1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. 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 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 // Rule interface
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 = loadClass(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