Coverage Report - org.apache.commons.betwixt.digester.ClassRule

Classes in this File Line Coverage Branch Coverage Complexity
ClassRule
86% 
100% 
2.333

 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  175
 public class ClassRule extends RuleSupport {
 32  
     /** Logger. */
 33  175
     private static final Log log = LogFactory.getLog(ClassRule.class);
 34  
 
 35  
     /** Base constructor. */
 36  415
     public ClassRule() {
 37  415
     }
 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  1519
         String className = attributes.getValue("name");
 49  1519
         if (className == null || "".equals(className)) {
 50  0
             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  1519
             Class beanClass = Class.forName(className);
 57  1519
             XMLBeanInfo xmlBeanInfo = new XMLBeanInfo(beanClass);
 58  1519
             XMLBeanInfoDigester xmlBeanInfoDigester = (XMLBeanInfoDigester) getDigester();
 59  1519
             xmlBeanInfoDigester.setBeanClass(beanClass);
 60  1519
             xmlBeanInfoDigester.push(xmlBeanInfo);
 61  
             
 62  0
         } catch (ClassNotFoundException e) {
 63  0
             throw new SAXException("Invalid 'class' element.  Unable to find class: " + className, e);
 64  
         }
 65  1519
     }
 66  
     
 67  
     /**
 68  
      * Process the end of this element.
 69  
      */
 70  
     public void end() {
 71  1519
         XMLBeanInfo xmlBeanInfo = (XMLBeanInfo) getDigester().pop();
 72  1519
         MultiMappingBeanInfoDigester digester = (MultiMappingBeanInfoDigester) getDigester();
 73  1519
         Map xmlBeanInfoMapping = digester.getBeanInfoMap();
 74  1519
         xmlBeanInfoMapping.put(xmlBeanInfo.getBeanClass(), xmlBeanInfo);
 75  1519
         digester.setBeanClass(null);
 76  1519
     }
 77  
   }
 78