1 16 17 package org.springframework.beans.factory.xml; 18 19 import org.w3c.dom.Attr ; 20 import org.w3c.dom.Element ; 21 import org.w3c.dom.Node ; 22 23 import org.springframework.beans.MutablePropertyValues; 24 import org.springframework.beans.factory.config.BeanDefinition; 25 import org.springframework.beans.factory.config.BeanDefinitionHolder; 26 import org.springframework.beans.factory.config.RuntimeBeanReference; 27 import org.springframework.core.Conventions; 28 29 46 public class SimplePropertyNamespaceHandler implements NamespaceHandler { 47 48 private static final String REF_SUFFIX = "-ref"; 49 50 51 public void init() { 52 } 53 54 public BeanDefinition parse(Element element, ParserContext parserContext) { 55 parserContext.getReaderContext().error( 56 "Class [" + getClass().getName() + "] does not support custom elements.", element); 57 return null; 58 } 59 60 public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) { 61 if (node instanceof Attr ) { 62 Attr attr = (Attr ) node; 63 String propertyName = attr.getLocalName(); 64 String propertyValue = attr.getValue(); 65 MutablePropertyValues pvs = definition.getBeanDefinition().getPropertyValues(); 66 if (pvs.contains(propertyName)) { 67 parserContext.getReaderContext().error("Property '" + propertyName + "' is already defined using " + 68 "both <property> and inline syntax. Only one approach may be used per property.", attr); 69 } 70 if (propertyName.endsWith(REF_SUFFIX)) { 71 propertyName = propertyName.substring(0, propertyName.length() - REF_SUFFIX.length()); 72 pvs.addPropertyValue(Conventions.attributeNameToPropertyName(propertyName), new RuntimeBeanReference(propertyValue)); 73 } 74 else { 75 pvs.addPropertyValue(Conventions.attributeNameToPropertyName(propertyName), propertyValue); 76 } 77 } 78 return definition; 79 } 80 81 } 82 | Popular Tags |