001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.xml;
018:
019: import java.util.List;
020: import java.util.Map;
021: import java.util.Properties;
022: import java.util.Set;
023:
024: import org.w3c.dom.Element;
025:
026: import org.springframework.beans.factory.config.FieldRetrievingFactoryBean;
027: import org.springframework.beans.factory.config.ListFactoryBean;
028: import org.springframework.beans.factory.config.MapFactoryBean;
029: import org.springframework.beans.factory.config.PropertiesFactoryBean;
030: import org.springframework.beans.factory.config.PropertyPathFactoryBean;
031: import org.springframework.beans.factory.config.SetFactoryBean;
032: import org.springframework.beans.factory.support.AbstractBeanDefinition;
033: import org.springframework.beans.factory.support.BeanDefinitionBuilder;
034: import org.springframework.util.StringUtils;
035:
036: /**
037: * {@link NamespaceHandler} for the <code>util</code> namespace.
038: *
039: * @author Rob Harrop
040: * @author Juergen Hoeller
041: * @since 2.0
042: */
043: public class UtilNamespaceHandler extends NamespaceHandlerSupport {
044:
045: private static final String SCOPE_ATTRIBUTE = "scope";
046:
047: public void init() {
048: registerBeanDefinitionParser("constant",
049: new ConstantBeanDefinitionParser());
050: registerBeanDefinitionParser("property-path",
051: new PropertyPathBeanDefinitionParser());
052: registerBeanDefinitionParser("list",
053: new ListBeanDefinitionParser());
054: registerBeanDefinitionParser("set",
055: new SetBeanDefinitionParser());
056: registerBeanDefinitionParser("map",
057: new MapBeanDefinitionParser());
058: registerBeanDefinitionParser("properties",
059: new PropertiesBeanDefinitionParser());
060: }
061:
062: private static class ConstantBeanDefinitionParser extends
063: AbstractSimpleBeanDefinitionParser {
064:
065: protected Class getBeanClass(Element element) {
066: return FieldRetrievingFactoryBean.class;
067: }
068:
069: protected String resolveId(Element element,
070: AbstractBeanDefinition definition,
071: ParserContext parserContext) {
072: String id = super .resolveId(element, definition,
073: parserContext);
074: if (!StringUtils.hasText(id)) {
075: id = element.getAttribute("static-field");
076: }
077: return id;
078: }
079: }
080:
081: private static class PropertyPathBeanDefinitionParser extends
082: AbstractSingleBeanDefinitionParser {
083:
084: protected Class getBeanClass(Element element) {
085: return PropertyPathFactoryBean.class;
086: }
087:
088: protected void doParse(Element element,
089: ParserContext parserContext,
090: BeanDefinitionBuilder builder) {
091: String path = element.getAttribute("path");
092: if (!StringUtils.hasText(path)) {
093: parserContext.getReaderContext().error(
094: "Attribute 'path' must not be empty", element);
095: return;
096: }
097: int dotIndex = path.indexOf(".");
098: if (dotIndex == -1) {
099: parserContext
100: .getReaderContext()
101: .error(
102: "Attribute 'path' must follow pattern 'beanName.propertyName'",
103: element);
104: return;
105: }
106: String beanName = path.substring(0, dotIndex);
107: String propertyPath = path.substring(dotIndex + 1);
108: builder.addPropertyValue("targetBeanName", beanName);
109: builder.addPropertyValue("propertyPath", propertyPath);
110: }
111:
112: protected String resolveId(Element element,
113: AbstractBeanDefinition definition,
114: ParserContext parserContext) {
115: String id = super .resolveId(element, definition,
116: parserContext);
117: if (!StringUtils.hasText(id)) {
118: id = element.getAttribute("path");
119: }
120: return id;
121: }
122: }
123:
124: private static class ListBeanDefinitionParser extends
125: AbstractSingleBeanDefinitionParser {
126:
127: protected Class getBeanClass(Element element) {
128: return ListFactoryBean.class;
129: }
130:
131: protected void doParse(Element element,
132: ParserContext parserContext,
133: BeanDefinitionBuilder builder) {
134: String listClass = element.getAttribute("list-class");
135: List parsedList = parserContext.getDelegate()
136: .parseListElement(element,
137: builder.getRawBeanDefinition());
138: builder.addPropertyValue("sourceList", parsedList);
139: if (StringUtils.hasText(listClass)) {
140: builder.addPropertyValue("targetListClass", listClass);
141: }
142: String scope = element.getAttribute(SCOPE_ATTRIBUTE);
143: if (StringUtils.hasLength(scope)) {
144: builder.setScope(scope);
145: }
146: }
147: }
148:
149: private static class SetBeanDefinitionParser extends
150: AbstractSingleBeanDefinitionParser {
151:
152: protected Class getBeanClass(Element element) {
153: return SetFactoryBean.class;
154: }
155:
156: protected void doParse(Element element,
157: ParserContext parserContext,
158: BeanDefinitionBuilder builder) {
159: String setClass = element.getAttribute("set-class");
160: Set parsedSet = parserContext.getDelegate()
161: .parseSetElement(element,
162: builder.getRawBeanDefinition());
163: builder.addPropertyValue("sourceSet", parsedSet);
164: if (StringUtils.hasText(setClass)) {
165: builder.addPropertyValue("targetSetClass", setClass);
166: }
167: String scope = element.getAttribute(SCOPE_ATTRIBUTE);
168: if (StringUtils.hasLength(scope)) {
169: builder.setScope(scope);
170: }
171: }
172: }
173:
174: private static class MapBeanDefinitionParser extends
175: AbstractSingleBeanDefinitionParser {
176:
177: protected Class getBeanClass(Element element) {
178: return MapFactoryBean.class;
179: }
180:
181: protected void doParse(Element element,
182: ParserContext parserContext,
183: BeanDefinitionBuilder builder) {
184: String mapClass = element.getAttribute("map-class");
185: Map parsedMap = parserContext.getDelegate()
186: .parseMapElement(element,
187: builder.getRawBeanDefinition());
188: builder.addPropertyValue("sourceMap", parsedMap);
189: if (StringUtils.hasText(mapClass)) {
190: builder.addPropertyValue("targetMapClass", mapClass);
191: }
192: String scope = element.getAttribute(SCOPE_ATTRIBUTE);
193: if (StringUtils.hasLength(scope)) {
194: builder.setScope(scope);
195: }
196: }
197: }
198:
199: private static class PropertiesBeanDefinitionParser extends
200: AbstractSimpleBeanDefinitionParser {
201:
202: protected Class getBeanClass(Element element) {
203: return PropertiesFactoryBean.class;
204: }
205:
206: protected boolean isEligibleAttribute(String attributeName) {
207: return super .isEligibleAttribute(attributeName)
208: && !SCOPE_ATTRIBUTE.equals(attributeName);
209: }
210:
211: protected void doParse(Element element,
212: ParserContext parserContext,
213: BeanDefinitionBuilder builder) {
214: super .doParse(element, parserContext, builder);
215: Properties parsedProps = parserContext.getDelegate()
216: .parsePropsElement(element);
217: builder.addPropertyValue("properties", parsedProps);
218: String scope = element.getAttribute(SCOPE_ATTRIBUTE);
219: if (StringUtils.hasLength(scope)) {
220: builder.setScope(scope);
221: }
222: }
223: }
224:
225: }
|