1 25 package org.riotfamily.common.beans.xml; 26 27 import org.springframework.beans.factory.BeanDefinitionStoreException; 28 import org.springframework.beans.factory.config.BeanDefinition; 29 import org.springframework.beans.factory.config.BeanDefinitionHolder; 30 import org.springframework.beans.factory.parsing.BeanComponentDefinition; 31 import org.springframework.beans.factory.support.AbstractBeanDefinition; 32 import org.springframework.beans.factory.support.BeanDefinitionBuilder; 33 import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; 34 import org.springframework.beans.factory.support.BeanDefinitionRegistry; 35 import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; 36 import org.springframework.beans.factory.xml.BeanDefinitionParser; 37 import org.springframework.beans.factory.xml.ParserContext; 38 import org.springframework.util.Assert; 39 import org.springframework.util.StringUtils; 40 import org.w3c.dom.Element ; 41 42 50 public abstract class AbstractGenericBeanDefinitionParser implements BeanDefinitionParser { 51 52 53 public static final String ID_ATTRIBUTE = "id"; 54 55 private Class beanClass; 56 57 private boolean decorate = true; 58 59 public AbstractGenericBeanDefinitionParser(Class beanClass) { 60 Assert.notNull(beanClass, "The beanClass must not be null"); 61 this.beanClass = beanClass; 62 } 63 64 67 public void setDecorate(boolean decorate) { 68 this.decorate = decorate; 69 } 70 71 public final BeanDefinition parse(Element element, ParserContext parserContext) { 72 AbstractBeanDefinition definition = parseInternal(element, parserContext); 73 if (!parserContext.isNested()) { 74 try { 75 String id = resolveId(element, definition, parserContext); 76 if (!StringUtils.hasText(id)) { 77 parserContext.getReaderContext().error( 78 "Id is required for element '" + element.getLocalName() + "' when used as a top-level tag", element); 79 } 80 String [] aliases = resolveAliases(element, definition, parserContext); 81 BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, id, aliases); 82 if (decorate) { 83 parserContext.getDelegate().decorateBeanDefinitionIfRequired(element, holder); 84 } 85 registerBeanDefinition(holder, parserContext.getRegistry()); 86 if (shouldFireEvents()) { 87 BeanComponentDefinition componentDefinition = new BeanComponentDefinition(holder); 88 postProcessComponentDefinition(componentDefinition); 89 parserContext.registerComponent(componentDefinition); 90 } 91 } 92 catch (BeanDefinitionStoreException ex) { 93 parserContext.getReaderContext().error(ex.getMessage(), element); 94 return null; 95 } 96 } 97 else if (decorate) { 98 BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, ""); 99 parserContext.getDelegate().decorateBeanDefinitionIfRequired(element, holder); 100 } 101 return definition; 102 } 103 104 115 protected final AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { 116 BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(beanClass); 117 builder.setSource(parserContext.extractSource(element)); 118 if (parserContext.isNested()) { 119 builder.setSingleton(parserContext.getContainingBeanDefinition().isSingleton()); 121 } 122 if (parserContext.isDefaultLazyInit()) { 123 builder.setLazyInit(true); 125 } 126 doParse(element, parserContext, builder); 127 return builder.getBeanDefinition(); 128 } 129 130 143 protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) 144 throws BeanDefinitionStoreException { 145 146 if (shouldGenerateId()) { 147 return parserContext.getReaderContext().generateBeanName(definition); 148 } 149 else { 150 String id = element.getAttribute(ID_ATTRIBUTE); 151 if (!StringUtils.hasText(id) && shouldGenerateIdAsFallback()) { 152 id = parserContext.getReaderContext().generateBeanName(definition); 153 } 154 return id; 155 } 156 } 157 158 164 protected String [] resolveAliases(Element element, 165 AbstractBeanDefinition definition, ParserContext parserContext) { 166 167 String alias = resolveAlias(element, definition, parserContext); 168 return StringUtils.tokenizeToStringArray(alias, ",; "); 169 } 170 171 176 protected String resolveAlias(Element element, 177 AbstractBeanDefinition definition, ParserContext parserContext) { 178 179 return null; 180 } 181 182 196 protected void registerBeanDefinition(BeanDefinitionHolder definition, BeanDefinitionRegistry registry) { 197 BeanDefinitionReaderUtils.registerBeanDefinition(definition, registry); 198 } 199 200 207 protected boolean shouldGenerateId() { 208 return false; 209 } 210 211 219 protected boolean shouldGenerateIdAsFallback() { 220 return true; 221 } 222 223 235 protected boolean shouldFireEvents() { 236 return true; 237 } 238 239 249 protected void postProcessComponentDefinition(BeanComponentDefinition componentDefinition) { 250 } 251 252 262 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { 263 doParse(element, builder); 264 } 265 266 273 protected void doParse(Element element, BeanDefinitionBuilder builder) { 274 } 275 276 } 277 | Popular Tags |