001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.betwixt.digester; 018 019 import java.util.Map; 020 021 import org.apache.commons.betwixt.XMLBeanInfo; 022 import org.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 import org.xml.sax.Attributes; 025 import org.xml.sax.SAXException; 026 027 /** 028 * Digester Rule to process class elements. 029 * @since 0.7 030 * @author Brian Pugh 031 */ 032 public class ClassRule extends RuleSupport { 033 /** Logger. */ 034 private static final Log log = LogFactory.getLog(ClassRule.class); 035 036 /** Base constructor. */ 037 public ClassRule() { 038 } 039 040 // Rule interface 041 //------------------------------------------------------------------------- 042 /** 043 * Process the beginning of this element. 044 * 045 * @param attributes The attribute list of this element 046 * @throws org.xml.sax.SAXException if the primitiveTypes attribute contains an invalid value 047 */ 048 public void begin(Attributes attributes) throws SAXException { 049 String className = attributes.getValue("name"); 050 if (className == null || "".equals(className)) { 051 throw new SAXException("Invalid 'class' element. " 052 + "Attribute 'name' is required but was not found but was not found."); 053 } 054 055 try { 056 057 Class beanClass = loadClass(className); 058 XMLBeanInfo xmlBeanInfo = new XMLBeanInfo(beanClass); 059 XMLBeanInfoDigester xmlBeanInfoDigester = (XMLBeanInfoDigester) getDigester(); 060 xmlBeanInfoDigester.setBeanClass(beanClass); 061 xmlBeanInfoDigester.push(xmlBeanInfo); 062 063 } catch (ClassNotFoundException e) { 064 throw new SAXException("Invalid 'class' element. Unable to find class: " + className, e); 065 } 066 } 067 068 /** 069 * Process the end of this element. 070 */ 071 public void end() { 072 XMLBeanInfo xmlBeanInfo = (XMLBeanInfo) getDigester().pop(); 073 MultiMappingBeanInfoDigester digester = (MultiMappingBeanInfoDigester) getDigester(); 074 Map xmlBeanInfoMapping = digester.getBeanInfoMap(); 075 xmlBeanInfoMapping.put(xmlBeanInfo.getBeanClass(), xmlBeanInfo); 076 digester.setBeanClass(null); 077 } 078 } 079