01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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(
52: "Invalid 'class' element. "
53: + "Attribute 'name' is required but was not found but was not found.");
54: }
55:
56: try {
57:
58: Class beanClass = Class.forName(className);
59: XMLBeanInfo xmlBeanInfo = new XMLBeanInfo(beanClass);
60: XMLBeanInfoDigester xmlBeanInfoDigester = (XMLBeanInfoDigester) getDigester();
61: xmlBeanInfoDigester.setBeanClass(beanClass);
62: xmlBeanInfoDigester.push(xmlBeanInfo);
63:
64: } catch (ClassNotFoundException e) {
65: throw new SAXException(
66: "Invalid 'class' element. Unable to find class: "
67: + className, e);
68: }
69: }
70:
71: /**
72: * Process the end of this element.
73: */
74: public void end() {
75: XMLBeanInfo xmlBeanInfo = (XMLBeanInfo) getDigester().pop();
76: MultiMappingBeanInfoDigester digester = (MultiMappingBeanInfoDigester) getDigester();
77: Map xmlBeanInfoMapping = digester.getBeanInfoMap();
78: xmlBeanInfoMapping.put(xmlBeanInfo.getBeanClass(), xmlBeanInfo);
79: digester.setBeanClass(null);
80: }
81: }
|