1 25 package org.riotfamily.common.beans.xml; 26 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 30 import org.springframework.beans.factory.config.RuntimeBeanReference; 31 import org.springframework.beans.factory.support.AbstractBeanDefinition; 32 import org.springframework.beans.factory.support.BeanDefinitionBuilder; 33 import org.springframework.beans.factory.xml.ParserContext; 34 import org.springframework.core.Conventions; 35 import org.springframework.util.Assert; 36 import org.springframework.util.StringUtils; 37 import org.w3c.dom.Attr ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.NamedNodeMap ; 40 41 45 public class GenericBeanDefinitionParser extends AbstractGenericBeanDefinitionParser { 46 47 public static final String NAME_ATTRIBUTE = "name"; 48 49 private HashMap translations = new HashMap (); 50 51 private HashSet references = new HashSet (); 52 53 public GenericBeanDefinitionParser(Class beanClass) { 54 super(beanClass); 55 } 56 57 public GenericBeanDefinitionParser addTranslation(String attributeName, 58 String property) { 59 60 translations.put(attributeName, property); 61 return this; 62 } 63 64 public GenericBeanDefinitionParser addReference(String attributeName) { 65 references.add(extractPropertyName(attributeName)); 66 return this; 67 } 68 69 protected String resolveAlias(Element element, AbstractBeanDefinition definition, ParserContext parserContext) { 70 return element.getAttribute(NAME_ATTRIBUTE); 71 } 72 73 90 protected final void doParse(Element element, 91 ParserContext parserContext, BeanDefinitionBuilder builder) { 92 93 NamedNodeMap attributes = element.getAttributes(); 94 for (int x = 0; x < attributes.getLength(); x++) { 95 Attr attribute = (Attr ) attributes.item(x); 96 String name = attribute.getLocalName(); 97 if (isEligibleAttribute(name)) { 98 String propertyName = extractPropertyName(name); 99 Assert.state(StringUtils.hasText(propertyName), 100 "Illegal property name returned from 'extractPropertyName(String)': cannot be null or empty."); 101 102 Object value; 103 if (references.contains(propertyName)) { 104 value = new RuntimeBeanReference(attribute.getValue()); 105 } 106 else { 107 value = attribute.getValue(); 108 } 109 builder.addPropertyValue(propertyName, value); 110 } 111 } 112 postProcess(builder, parserContext, element); 113 } 114 115 123 protected boolean isEligibleAttribute(String attributeName) { 124 return !ID_ATTRIBUTE.equals(attributeName) && !NAME_ATTRIBUTE.equals(attributeName); 125 } 126 127 141 protected String extractPropertyName(String attributeName) { 142 String property = (String ) translations.get(attributeName); 143 if (property == null) { 144 property = Conventions.attributeNameToPropertyName(attributeName); 145 } 146 return property; 147 } 148 149 158 protected void postProcess(BeanDefinitionBuilder beanDefinition, 159 ParserContext parserContext, Element element) { 160 161 postProcess(beanDefinition, element); 162 } 163 164 171 protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) { 172 } 173 174 175 } 176 | Popular Tags |