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.HashMap;
020: import java.util.Map;
021:
022: import org.w3c.dom.Attr;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.Node;
025:
026: import org.springframework.beans.factory.config.BeanDefinition;
027: import org.springframework.beans.factory.config.BeanDefinitionHolder;
028:
029: /**
030: * Support class for implementing custom {@link NamespaceHandler NamespaceHandlers}. Parsing and
031: * decorating of individual {@link Node Nodes} is done via {@link BeanDefinitionParser} and
032: * {@link BeanDefinitionDecorator} strategy interfaces respectively. Provides the
033: * {@link #registerBeanDefinitionParser}, {@link #registerBeanDefinitionDecorator} methods
034: * for registering a {@link BeanDefinitionParser} or {@link BeanDefinitionDecorator} to handle
035: * a specific element.
036: *
037: * @author Rob Harrop
038: * @author Juergen Hoeller
039: * @since 2.0
040: * @see #registerBeanDefinitionParser(String, BeanDefinitionParser)
041: * @see #registerBeanDefinitionDecorator(String, BeanDefinitionDecorator)
042: */
043: public abstract class NamespaceHandlerSupport implements
044: NamespaceHandler {
045:
046: /**
047: * Stores the {@link BeanDefinitionParser} implementations keyed by the
048: * local name of the {@link Element Elements} they handle.
049: */
050: private final Map parsers = new HashMap();
051:
052: /**
053: * Stores the {@link BeanDefinitionDecorator} implementations keyed by the
054: * local name of the {@link Element Elements} they handle.
055: */
056: private final Map decorators = new HashMap();
057:
058: /**
059: * Stores the {@link BeanDefinitionParser} implementations keyed by the local
060: * name of the {@link Attr Attrs} they handle.
061: */
062: private final Map attributeDecorators = new HashMap();
063:
064: /**
065: * Parses the supplied {@link Element} by delegating to the {@link BeanDefinitionParser} that is
066: * registered for that {@link Element}.
067: */
068: public final BeanDefinition parse(Element element,
069: ParserContext parserContext) {
070: return findParserForElement(element, parserContext).parse(
071: element, parserContext);
072: }
073:
074: /**
075: * Locates the {@link BeanDefinitionParser} from the register implementations using
076: * the local name of the supplied {@link Element}.
077: */
078: private BeanDefinitionParser findParserForElement(Element element,
079: ParserContext parserContext) {
080: BeanDefinitionParser parser = (BeanDefinitionParser) this .parsers
081: .get(element.getLocalName());
082: if (parser == null) {
083: parserContext.getReaderContext().fatal(
084: "Cannot locate BeanDefinitionParser for element ["
085: + element.getLocalName() + "]", element);
086: }
087: return parser;
088: }
089:
090: /**
091: * Locate the {@link BeanDefinitionParser} from the register implementations using
092: * the local name of the supplied {@link Element}.
093: * @deprecated as of Spring 2.0.2; there should be no need to call this directly.
094: */
095: protected final BeanDefinitionParser findParserForElement(
096: Element element) {
097: BeanDefinitionParser parser = (BeanDefinitionParser) this .parsers
098: .get(element.getLocalName());
099: if (parser == null) {
100: throw new IllegalStateException(
101: "Cannot locate BeanDefinitionParser for element ["
102: + element.getLocalName() + "]");
103: }
104: return parser;
105: }
106:
107: /**
108: * Decorates the supplied {@link Node} by delegating to the {@link BeanDefinitionDecorator} that
109: * is registered to handle that {@link Node}.
110: */
111: public final BeanDefinitionHolder decorate(Node node,
112: BeanDefinitionHolder definition, ParserContext parserContext) {
113:
114: return findDecoratorForNode(node, parserContext).decorate(node,
115: definition, parserContext);
116: }
117:
118: /**
119: * Locates the {@link BeanDefinitionParser} from the register implementations using
120: * the local name of the supplied {@link Node}. Supports both {@link Element Elements}
121: * and {@link Attr Attrs}.
122: */
123: private BeanDefinitionDecorator findDecoratorForNode(Node node,
124: ParserContext parserContext) {
125: BeanDefinitionDecorator decorator = null;
126: if (node instanceof Element) {
127: decorator = (BeanDefinitionDecorator) this .decorators
128: .get(node.getLocalName());
129: } else if (node instanceof Attr) {
130: decorator = (BeanDefinitionDecorator) this .attributeDecorators
131: .get(node.getLocalName());
132: } else {
133: parserContext.getReaderContext().fatal(
134: "Cannot decorate based on Nodes of type ["
135: + node.getClass().getName() + "]", node);
136: }
137: if (decorator == null) {
138: parserContext.getReaderContext().fatal(
139: "Cannot locate BeanDefinitionDecorator for "
140: + (node instanceof Element ? "element"
141: : "attribute") + " ["
142: + node.getLocalName() + "]", node);
143: }
144: return decorator;
145: }
146:
147: /**
148: * Locate the {@link BeanDefinitionParser} from the register implementations using
149: * the local name of the supplied {@link Node}. Supports both {@link Element Elements}
150: * and {@link Attr Attrs}.
151: * @deprecated as of Spring 2.0.2; there should be no need to call this directly.
152: */
153: protected final BeanDefinitionDecorator findDecoratorForNode(
154: Node node) {
155: BeanDefinitionDecorator decorator = null;
156: if (node instanceof Element) {
157: decorator = (BeanDefinitionDecorator) this .decorators
158: .get(node.getLocalName());
159: } else if (node instanceof Attr) {
160: decorator = (BeanDefinitionDecorator) this .attributeDecorators
161: .get(node.getLocalName());
162: } else {
163: throw new IllegalStateException(
164: "Cannot decorate based on Nodes of type ["
165: + node.getClass().getName() + "]");
166: }
167: if (decorator == null) {
168: throw new IllegalStateException(
169: "Cannot locate BeanDefinitionDecorator for "
170: + (node instanceof Element ? "element"
171: : "attribute") + " ["
172: + node.getLocalName() + "]");
173: }
174: return decorator;
175: }
176:
177: /**
178: * Subclasses can call this to register the supplied {@link BeanDefinitionParser} to
179: * handle the specified element. The element name is the local (non-namespace qualified)
180: * name.
181: */
182: protected final void registerBeanDefinitionParser(
183: String elementName, BeanDefinitionParser parser) {
184: this .parsers.put(elementName, parser);
185: }
186:
187: /**
188: * Subclasses can call this to register the supplied {@link BeanDefinitionDecorator} to
189: * handle the specified element. The element name is the local (non-namespace qualified)
190: * name.
191: */
192: protected final void registerBeanDefinitionDecorator(
193: String elementName, BeanDefinitionDecorator decorator) {
194: this .decorators.put(elementName, decorator);
195: }
196:
197: /**
198: * Subclasses can call this to register the supplied {@link BeanDefinitionDecorator} to
199: * handle the specified attribute. The attribute name is the local (non-namespace qualified)
200: * name.
201: */
202: protected final void registerBeanDefinitionDecoratorForAttribute(
203: String attributeName, BeanDefinitionDecorator decorator) {
204:
205: this.attributeDecorators.put(attributeName, decorator);
206: }
207:
208: }
|