KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.w3c.dom.Attr JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25
26 import org.springframework.beans.factory.config.BeanDefinition;
27 import org.springframework.beans.factory.config.BeanDefinitionHolder;
28
29 /**
30  * Support class for implementing custom {@link NamespaceHandler NamespaceHandlers}. Parsing and
31  * decorating of individual {@link Node Nodes} is done via {@link BeanDefinitionParser} and
32  * {@link BeanDefinitionDecorator} strategy interfaces respectively. Provides the
33  * {@link #registerBeanDefinitionParser}, {@link #registerBeanDefinitionDecorator} methods
34  * for registering a {@link BeanDefinitionParser} or {@link BeanDefinitionDecorator} to handle
35  * a specific element.
36  *
37  * @author Rob Harrop
38  * @author Juergen Hoeller
39  * @since 2.0
40  * @see #registerBeanDefinitionParser(String, BeanDefinitionParser)
41  * @see #registerBeanDefinitionDecorator(String, BeanDefinitionDecorator)
42  */

43 public abstract class NamespaceHandlerSupport implements NamespaceHandler {
44
45     /**
46      * Stores the {@link BeanDefinitionParser} implementations keyed by the
47      * local name of the {@link Element Elements} they handle.
48      */

49     private final Map JavaDoc parsers = new HashMap JavaDoc();
50
51     /**
52      * Stores the {@link BeanDefinitionDecorator} implementations keyed by the
53      * local name of the {@link Element Elements} they handle.
54      */

55     private final Map JavaDoc decorators = new HashMap JavaDoc();
56
57     /**
58      * Stores the {@link BeanDefinitionParser} implementations keyed by the local
59      * name of the {@link Attr Attrs} they handle.
60      */

61     private final Map JavaDoc attributeDecorators = new HashMap JavaDoc();
62
63
64     /**
65      * Parses the supplied {@link Element} by delegating to the {@link BeanDefinitionParser} that is
66      * registered for that {@link Element}.
67      */

68     public final BeanDefinition parse(Element JavaDoc element, ParserContext parserContext) {
69         return findParserForElement(element, parserContext).parse(element, parserContext);
70     }
71
72     /**
73      * Locates the {@link BeanDefinitionParser} from the register implementations using
74      * the local name of the supplied {@link Element}.
75      */

76     private BeanDefinitionParser findParserForElement(Element JavaDoc element, ParserContext parserContext) {
77         BeanDefinitionParser parser = (BeanDefinitionParser) this.parsers.get(element.getLocalName());
78         if (parser == null) {
79             parserContext.getReaderContext().fatal(
80                     "Cannot locate BeanDefinitionParser for element [" + element.getLocalName() + "]", element);
81         }
82         return parser;
83     }
84
85     /**
86      * Locate the {@link BeanDefinitionParser} from the register implementations using
87      * the local name of the supplied {@link Element}.
88      * @deprecated as of Spring 2.0.2; there should be no need to call this directly.
89      */

90     protected final BeanDefinitionParser findParserForElement(Element JavaDoc element) {
91         BeanDefinitionParser parser = (BeanDefinitionParser) this.parsers.get(element.getLocalName());
92         if (parser == null) {
93             throw new IllegalStateException JavaDoc(
94                     "Cannot locate BeanDefinitionParser for element [" + element.getLocalName() + "]");
95         }
96         return parser;
97     }
98
99     /**
100      * Decorates the supplied {@link Node} by delegating to the {@link BeanDefinitionDecorator} that
101      * is registered to handle that {@link Node}.
102      */

103     public final BeanDefinitionHolder decorate(
104             Node JavaDoc node, BeanDefinitionHolder definition, ParserContext parserContext) {
105
106         return findDecoratorForNode(node, parserContext).decorate(node, definition, parserContext);
107     }
108
109     /**
110      * Locates the {@link BeanDefinitionParser} from the register implementations using
111      * the local name of the supplied {@link Node}. Supports both {@link Element Elements}
112      * and {@link Attr Attrs}.
113      */

114     private BeanDefinitionDecorator findDecoratorForNode(Node JavaDoc node, ParserContext parserContext) {
115         BeanDefinitionDecorator decorator = null;
116         if (node instanceof Element JavaDoc) {
117             decorator = (BeanDefinitionDecorator) this.decorators.get(node.getLocalName());
118         }
119         else if (node instanceof Attr JavaDoc) {
120             decorator = (BeanDefinitionDecorator) this.attributeDecorators.get(node.getLocalName());
121         }
122         else {
123             parserContext.getReaderContext().fatal(
124                     "Cannot decorate based on Nodes of type [" + node.getClass().getName() + "]", node);
125         }
126         if (decorator == null) {
127             parserContext.getReaderContext().fatal("Cannot locate BeanDefinitionDecorator for " +
128                     (node instanceof Element JavaDoc ? "element" : "attribute") + " [" + node.getLocalName() + "]", node);
129         }
130         return decorator;
131     }
132
133     /**
134      * Locate the {@link BeanDefinitionParser} from the register implementations using
135      * the local name of the supplied {@link Node}. Supports both {@link Element Elements}
136      * and {@link Attr Attrs}.
137      * @deprecated as of Spring 2.0.2; there should be no need to call this directly.
138      */

139     protected final BeanDefinitionDecorator findDecoratorForNode(Node JavaDoc node) {
140         BeanDefinitionDecorator decorator = null;
141         if (node instanceof Element JavaDoc) {
142             decorator = (BeanDefinitionDecorator) this.decorators.get(node.getLocalName());
143         }
144         else if (node instanceof Attr JavaDoc) {
145             decorator = (BeanDefinitionDecorator) this.attributeDecorators.get(node.getLocalName());
146         }
147         else {
148             throw new IllegalStateException JavaDoc(
149                     "Cannot decorate based on Nodes of type [" + node.getClass().getName() + "]");
150         }
151         if (decorator == null) {
152             throw new IllegalStateException JavaDoc("Cannot locate BeanDefinitionDecorator for " +
153                     (node instanceof Element JavaDoc ? "element" : "attribute") + " [" + node.getLocalName() + "]");
154         }
155         return decorator;
156     }
157
158
159     /**
160      * Subclasses can call this to register the supplied {@link BeanDefinitionParser} to
161      * handle the specified element. The element name is the local (non-namespace qualified)
162      * name.
163      */

164     protected final void registerBeanDefinitionParser(String JavaDoc elementName, BeanDefinitionParser parser) {
165         this.parsers.put(elementName, parser);
166     }
167
168     /**
169      * Subclasses can call this to register the supplied {@link BeanDefinitionDecorator} to
170      * handle the specified element. The element name is the local (non-namespace qualified)
171      * name.
172      */

173     protected final void registerBeanDefinitionDecorator(String JavaDoc elementName, BeanDefinitionDecorator decorator) {
174         this.decorators.put(elementName, decorator);
175     }
176
177     /**
178      * Subclasses can call this to register the supplied {@link BeanDefinitionDecorator} to
179      * handle the specified attribute. The attribute name is the local (non-namespace qualified)
180      * name.
181      */

182     protected final void registerBeanDefinitionDecoratorForAttribute(
183             String JavaDoc attributeName, BeanDefinitionDecorator decorator) {
184
185         this.attributeDecorators.put(attributeName, decorator);
186     }
187
188 }
189
Popular Tags