KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.w3c.dom.Element JavaDoc;
20
21 import org.springframework.beans.factory.support.AbstractBeanDefinition;
22 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
23 import org.springframework.util.Assert;
24
25 /**
26  * Base class for those {@link BeanDefinitionParser} implementations that
27  * need to parse and define just a <i>single</i> <code>BeanDefinition</code>.
28  *
29  * <p>Extend this parser class when you want to create a single bean definition
30  * from an arbitrarily complex XML element. You may wish to consider extending
31  * the {@link AbstractSimpleBeanDefinitionParser} when you want to create a
32  * single bean definition from a relatively simple custom XML element.
33  *
34  * <p>The resulting <code>BeanDefinition</code> will be automatically registered
35  * with the {@link org.springframework.beans.factory.support.BeanDefinitionRegistry}.
36  * Your job simply is to {@link #doParse parse} the custom XML {@link Element}
37  * into a single <code>BeanDefinition</code>.
38  *
39  * @author Rob Harrop
40  * @author Juergen Hoeller
41  * @author Rick Evans
42  * @since 2.0
43  */

44 public abstract class AbstractSingleBeanDefinitionParser extends AbstractBeanDefinitionParser {
45
46     /**
47      * Creates a {@link BeanDefinitionBuilder} instance for the
48      * {@link #getBeanClass bean Class} and passes it to the
49      * {@link #doParse} strategy method.
50      * @param element the element that is to be parsed into a single BeanDefinition
51      * @param parserContext the object encapsulating the current state of the parsing process
52      * @return the BeanDefinition resulting from the parsing of the supplied {@link Element}
53      * @throws IllegalStateException if the bean {@link Class} returned from
54      * {@link #getBeanClass(org.w3c.dom.Element)} is <code>null</code>
55      * @see #doParse
56      */

57     protected final AbstractBeanDefinition parseInternal(Element JavaDoc element, ParserContext parserContext) {
58         Class JavaDoc beanClass = getBeanClass(element);
59         Assert.state(beanClass != null, "Class returned from getBeanClass(Element) must not be null");
60         BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(beanClass);
61         builder.setSource(parserContext.extractSource(element));
62         if (parserContext.isNested()) {
63             // Inner bean definition must receive same singleton status as containing bean.
64
builder.setSingleton(parserContext.getContainingBeanDefinition().isSingleton());
65         }
66         if (parserContext.isDefaultLazyInit()) {
67             // Default-lazy-init applies to custom bean definitions as well.
68
builder.setLazyInit(true);
69         }
70         doParse(element, parserContext, builder);
71         return builder.getBeanDefinition();
72     }
73
74     /**
75      * Determine the bean class corresponding to the supplied {@link Element}.
76      * @param element the <code>Element</code> that is being parsed
77      * @return the {@link Class} of the bean that is being defined via parsing the supplied <code>Element</code>
78      * (must <b>not</b> be <code>null</code>)
79      * @see #parseInternal(org.w3c.dom.Element, ParserContext)
80      */

81     protected abstract Class JavaDoc getBeanClass(Element JavaDoc element);
82
83     /**
84      * Parse the supplied {@link Element} and populate the supplied
85      * {@link BeanDefinitionBuilder} as required.
86      * <p>The default implementation delegates to the <code>doParse</code>
87      * version without ParserContext argument.
88      * @param element the XML element being parsed
89      * @param parserContext the object encapsulating the current state of the parsing process
90      * @param builder used to define the <code>BeanDefinition</code>
91      * @see #doParse(Element, BeanDefinitionBuilder)
92      */

93     protected void doParse(Element JavaDoc element, ParserContext parserContext, BeanDefinitionBuilder builder) {
94         doParse(element, builder);
95     }
96
97     /**
98      * Parse the supplied {@link Element} and populate the supplied
99      * {@link BeanDefinitionBuilder} as required.
100      * <p>The default implementation does nothing.
101      * @param element the XML element being parsed
102      * @param builder used to define the <code>BeanDefinition</code>
103      */

104     protected void doParse(Element JavaDoc element, BeanDefinitionBuilder builder) {
105     }
106
107 }
108
Popular Tags