View Javadoc

1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      // Rule interface
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