1 24 package org.riotfamily.common.beans.override; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.riotfamily.common.beans.xml.GenericBeanDefinitionParser; 32 import org.riotfamily.common.beans.xml.GenericNamespaceHandlerSupport; 33 import org.springframework.beans.factory.config.BeanDefinition; 34 import org.springframework.beans.factory.support.BeanDefinitionBuilder; 35 import org.springframework.beans.factory.support.RootBeanDefinition; 36 import org.springframework.beans.factory.xml.ParserContext; 37 import org.w3c.dom.Element ; 38 39 43 public class OverrideNamespaceHandler extends GenericNamespaceHandlerSupport { 44 45 public void init() { 46 registerBeanDefinitionParser("properties", new PropertyOverrideParser()); 47 registerBeanDefinitionParser("put", new MapMergeParser()); 48 registerBeanDefinitionParser("add", new ListMergeParser()); 49 registerBeanDefinitionParser("bean", new BeanOverrideParser()); 50 } 51 52 private static class PropertyOverrideParser extends GenericBeanDefinitionParser { 53 54 public PropertyOverrideParser() { 55 super(PropertyOverrideProcessor.class); 56 } 57 58 protected void postProcess(BeanDefinitionBuilder beanDefinition, 59 ParserContext parserContext, Element element) { 60 61 BeanDefinition bd = new RootBeanDefinition(); 62 parserContext.getDelegate().parsePropertyElements(element, bd); 63 beanDefinition.addPropertyValue("propertyValues", bd.getPropertyValues()); 64 } 65 } 66 67 private static class MapMergeParser extends GenericBeanDefinitionParser { 68 69 public MapMergeParser() { 70 super(MapMergeProcessor.class); 71 } 72 73 protected void postProcess(BeanDefinitionBuilder beanDefinition, 74 ParserContext parserContext, Element element) { 75 76 Map entries = parserContext.getDelegate().parseMapElement( 77 element, beanDefinition.getBeanDefinition()); 78 79 beanDefinition.addPropertyValue("entries", new HashMap (entries)); 83 } 84 } 85 86 private static class ListMergeParser extends GenericBeanDefinitionParser { 87 88 public ListMergeParser() { 89 super(ListMergeProcessor.class); 90 } 91 92 protected void postProcess(BeanDefinitionBuilder beanDefinition, 93 ParserContext parserContext, Element element) { 94 95 List values = parserContext.getDelegate().parseListElement( 96 element, beanDefinition.getBeanDefinition()); 97 98 beanDefinition.addPropertyValue("values", new ArrayList (values)); 102 } 103 } 104 105 private static class BeanOverrideParser extends GenericBeanDefinitionParser { 106 107 public BeanOverrideParser() { 108 super(BeanOverrideProcessor.class); 109 } 110 111 protected boolean isEligibleAttribute(String attributeName) { 112 return attributeName.equals("ref") 113 || attributeName.equals("merge") 114 || attributeName.equals("order"); 115 } 116 117 protected void postProcess(BeanDefinitionBuilder builder, 118 ParserContext parserContext, Element element) { 119 120 BeanReplacement replacement = new BeanReplacement( 121 parserContext.getDelegate().parseBeanDefinitionElement( 122 element, null, builder.getBeanDefinition())); 123 124 builder.addPropertyValue("beanReplacement", replacement); 125 builder.getBeanDefinition().getPropertyValues().setConverted(); 126 } 127 } 128 129 static class BeanReplacement { 130 131 private BeanDefinition beanDefinition; 132 133 public BeanReplacement(BeanDefinition beanDefinition) { 134 this.beanDefinition = beanDefinition; 135 } 136 137 public BeanDefinition getBeanDefinition() { 138 return this.beanDefinition; 139 } 140 141 } 142 143 } 144 | Popular Tags |