KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > xml > UtilNamespaceHandler


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.factory.xml;
18
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.w3c.dom.Element JavaDoc;
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 /**
37  * {@link NamespaceHandler} for the <code>util</code> namespace.
38  *
39  * @author Rob Harrop
40  * @author Juergen Hoeller
41  * @since 2.0
42  */

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 JavaDoc getBeanClass(Element JavaDoc element) {
58             return FieldRetrievingFactoryBean.class;
59         }
60
61         protected String JavaDoc resolveId(Element JavaDoc element, AbstractBeanDefinition definition, ParserContext parserContext) {
62             String JavaDoc 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 JavaDoc getBeanClass(Element JavaDoc element) {
74             return PropertyPathFactoryBean.class;
75         }
76
77         protected void doParse(Element JavaDoc element, ParserContext parserContext, BeanDefinitionBuilder builder) {
78             String JavaDoc 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 JavaDoc beanName = path.substring(0, dotIndex);
90             String JavaDoc propertyPath = path.substring(dotIndex + 1);
91             builder.addPropertyValue("targetBeanName", beanName);
92             builder.addPropertyValue("propertyPath", propertyPath);
93         }
94
95         protected String JavaDoc resolveId(Element JavaDoc element, AbstractBeanDefinition definition, ParserContext parserContext) {
96             String JavaDoc 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 JavaDoc getBeanClass(Element JavaDoc element) {
108             return ListFactoryBean.class;
109         }
110
111         protected void doParse(Element JavaDoc element, ParserContext parserContext, BeanDefinitionBuilder builder) {
112             String JavaDoc listClass = element.getAttribute("list-class");
113             List JavaDoc 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 JavaDoc getBeanClass(Element JavaDoc element) {
125             return SetFactoryBean.class;
126         }
127
128         protected void doParse(Element JavaDoc element, ParserContext parserContext, BeanDefinitionBuilder builder) {
129             String JavaDoc setClass = element.getAttribute("set-class");
130             Set JavaDoc 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 JavaDoc getBeanClass(Element JavaDoc element) {
142             return MapFactoryBean.class;
143         }
144
145         protected void doParse(Element JavaDoc element, ParserContext parserContext, BeanDefinitionBuilder builder) {
146             String JavaDoc mapClass = element.getAttribute("map-class");
147             Map JavaDoc 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 JavaDoc getBeanClass(Element JavaDoc element) {
159             return PropertiesFactoryBean.class;
160         }
161
162         protected void doParse(Element JavaDoc element, ParserContext parserContext, BeanDefinitionBuilder builder) {
163             super.doParse(element, parserContext, builder);
164             Properties JavaDoc parsedProps = parserContext.getDelegate().parsePropsElement(element);
165             builder.addPropertyValue("properties", parsedProps);
166         }
167     }
168
169 }
170
Popular Tags