1 16 17 package org.springframework.beans.factory.xml; 18 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 import java.util.Set ; 23 24 import org.w3c.dom.Element ; 25 26 import org.springframework.beans.factory.config.FieldRetrievingFactoryBean; 27 import org.springframework.beans.factory.config.ListFactoryBean; 28 import org.springframework.beans.factory.config.MapFactoryBean; 29 import org.springframework.beans.factory.config.PropertiesFactoryBean; 30 import org.springframework.beans.factory.config.PropertyPathFactoryBean; 31 import org.springframework.beans.factory.config.SetFactoryBean; 32 import org.springframework.beans.factory.support.AbstractBeanDefinition; 33 import org.springframework.beans.factory.support.BeanDefinitionBuilder; 34 import org.springframework.util.StringUtils; 35 36 43 public class UtilNamespaceHandler extends NamespaceHandlerSupport { 44 45 public void init() { 46 registerBeanDefinitionParser("constant", new ConstantBeanDefinitionParser()); 47 registerBeanDefinitionParser("property-path", new PropertyPathBeanDefinitionParser()); 48 registerBeanDefinitionParser("list", new ListBeanDefinitionParser()); 49 registerBeanDefinitionParser("set", new SetBeanDefinitionParser()); 50 registerBeanDefinitionParser("map", new MapBeanDefinitionParser()); 51 registerBeanDefinitionParser("properties", new PropertiesBeanDefinitionParser()); 52 } 53 54 55 private static class ConstantBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser { 56 57 protected Class getBeanClass(Element element) { 58 return FieldRetrievingFactoryBean.class; 59 } 60 61 protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) { 62 String id = super.resolveId(element, definition, parserContext); 63 if (!StringUtils.hasText(id)) { 64 id = element.getAttribute("static-field"); 65 } 66 return id; 67 } 68 } 69 70 71 private static class PropertyPathBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { 72 73 protected Class getBeanClass(Element element) { 74 return PropertyPathFactoryBean.class; 75 } 76 77 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { 78 String path = element.getAttribute("path"); 79 if (!StringUtils.hasText(path)) { 80 parserContext.getReaderContext().error("Attribute 'path' must not be empty", element); 81 return; 82 } 83 int dotIndex = path.indexOf("."); 84 if (dotIndex == -1) { 85 parserContext.getReaderContext().error( 86 "Attribute 'path' must follow pattern 'beanName.propertyName'", element); 87 return; 88 } 89 String beanName = path.substring(0, dotIndex); 90 String propertyPath = path.substring(dotIndex + 1); 91 builder.addPropertyValue("targetBeanName", beanName); 92 builder.addPropertyValue("propertyPath", propertyPath); 93 } 94 95 protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) { 96 String id = super.resolveId(element, definition, parserContext); 97 if (!StringUtils.hasText(id)) { 98 id = element.getAttribute("path"); 99 } 100 return id; 101 } 102 } 103 104 105 private static class ListBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { 106 107 protected Class getBeanClass(Element element) { 108 return ListFactoryBean.class; 109 } 110 111 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { 112 String listClass = element.getAttribute("list-class"); 113 List parsedList = parserContext.getDelegate().parseListElement(element, builder.getRawBeanDefinition()); 114 builder.addPropertyValue("sourceList", parsedList); 115 if (StringUtils.hasText(listClass)) { 116 builder.addPropertyValue("targetListClass", listClass); 117 } 118 } 119 } 120 121 122 private static class SetBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { 123 124 protected Class getBeanClass(Element element) { 125 return SetFactoryBean.class; 126 } 127 128 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { 129 String setClass = element.getAttribute("set-class"); 130 Set parsedSet = parserContext.getDelegate().parseSetElement(element, builder.getRawBeanDefinition()); 131 builder.addPropertyValue("sourceSet", parsedSet); 132 if (StringUtils.hasText(setClass)) { 133 builder.addPropertyValue("targetSetClass", setClass); 134 } 135 } 136 } 137 138 139 private static class MapBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { 140 141 protected Class getBeanClass(Element element) { 142 return MapFactoryBean.class; 143 } 144 145 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { 146 String mapClass = element.getAttribute("map-class"); 147 Map parsedMap = parserContext.getDelegate().parseMapElement(element, builder.getRawBeanDefinition()); 148 builder.addPropertyValue("sourceMap", parsedMap); 149 if (StringUtils.hasText(mapClass)) { 150 builder.addPropertyValue("targetMapClass", mapClass); 151 } 152 } 153 } 154 155 156 private static class PropertiesBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser { 157 158 protected Class getBeanClass(Element element) { 159 return PropertiesFactoryBean.class; 160 } 161 162 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { 163 super.doParse(element, parserContext, builder); 164 Properties parsedProps = parserContext.getDelegate().parsePropsElement(element); 165 builder.addPropertyValue("properties", parsedProps); 166 } 167 } 168 169 } 170 | Popular Tags |