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

package org.springframework.beans.factory.xml;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.lang.Nullable;

Support class for implementing custom NamespaceHandlers. Parsing and decorating of individual Nodes is done via BeanDefinitionParser and BeanDefinitionDecorator strategy interfaces, respectively.

Provides the registerBeanDefinitionParser and registerBeanDefinitionDecorator methods for registering a BeanDefinitionParser or BeanDefinitionDecorator to handle a specific element.

Author:Rob Harrop, Juergen Hoeller
See Also:
Since:2.0
/** * Support class for implementing custom {@link NamespaceHandler NamespaceHandlers}. * Parsing and decorating of individual {@link Node Nodes} is done via {@link BeanDefinitionParser} * and {@link BeanDefinitionDecorator} strategy interfaces, respectively. * * <p>Provides the {@link #registerBeanDefinitionParser} and {@link #registerBeanDefinitionDecorator} * methods for registering a {@link BeanDefinitionParser} or {@link BeanDefinitionDecorator} * to handle a specific element. * * @author Rob Harrop * @author Juergen Hoeller * @since 2.0 * @see #registerBeanDefinitionParser(String, BeanDefinitionParser) * @see #registerBeanDefinitionDecorator(String, BeanDefinitionDecorator) */
public abstract class NamespaceHandlerSupport implements NamespaceHandler {
Stores the BeanDefinitionParser implementations keyed by the local name of the Elements they handle.
/** * Stores the {@link BeanDefinitionParser} implementations keyed by the * local name of the {@link Element Elements} they handle. */
private final Map<String, BeanDefinitionParser> parsers = new HashMap<>();
Stores the BeanDefinitionDecorator implementations keyed by the local name of the Elements they handle.
/** * Stores the {@link BeanDefinitionDecorator} implementations keyed by the * local name of the {@link Element Elements} they handle. */
private final Map<String, BeanDefinitionDecorator> decorators = new HashMap<>();
Stores the BeanDefinitionDecorator implementations keyed by the local name of the Attrs they handle.
/** * Stores the {@link BeanDefinitionDecorator} implementations keyed by the local * name of the {@link Attr Attrs} they handle. */
private final Map<String, BeanDefinitionDecorator> attributeDecorators = new HashMap<>();
Parses the supplied Element by delegating to the BeanDefinitionParser that is registered for that Element.
/** * Parses the supplied {@link Element} by delegating to the {@link BeanDefinitionParser} that is * registered for that {@link Element}. */
@Override @Nullable public BeanDefinition parse(Element element, ParserContext parserContext) { BeanDefinitionParser parser = findParserForElement(element, parserContext); return (parser != null ? parser.parse(element, parserContext) : null); }
Locates the BeanDefinitionParser from the register implementations using the local name of the supplied Element.
/** * Locates the {@link BeanDefinitionParser} from the register implementations using * the local name of the supplied {@link Element}. */
@Nullable private BeanDefinitionParser findParserForElement(Element element, ParserContext parserContext) { String localName = parserContext.getDelegate().getLocalName(element); BeanDefinitionParser parser = this.parsers.get(localName); if (parser == null) { parserContext.getReaderContext().fatal( "Cannot locate BeanDefinitionParser for element [" + localName + "]", element); } return parser; }
Decorates the supplied Node by delegating to the BeanDefinitionDecorator that is registered to handle that Node.
/** * Decorates the supplied {@link Node} by delegating to the {@link BeanDefinitionDecorator} that * is registered to handle that {@link Node}. */
@Override @Nullable public BeanDefinitionHolder decorate( Node node, BeanDefinitionHolder definition, ParserContext parserContext) { BeanDefinitionDecorator decorator = findDecoratorForNode(node, parserContext); return (decorator != null ? decorator.decorate(node, definition, parserContext) : null); }
Locates the BeanDefinitionParser from the register implementations using the local name of the supplied Node. Supports both Elements and Attrs.
/** * Locates the {@link BeanDefinitionParser} from the register implementations using * the local name of the supplied {@link Node}. Supports both {@link Element Elements} * and {@link Attr Attrs}. */
@Nullable private BeanDefinitionDecorator findDecoratorForNode(Node node, ParserContext parserContext) { BeanDefinitionDecorator decorator = null; String localName = parserContext.getDelegate().getLocalName(node); if (node instanceof Element) { decorator = this.decorators.get(localName); } else if (node instanceof Attr) { decorator = this.attributeDecorators.get(localName); } else { parserContext.getReaderContext().fatal( "Cannot decorate based on Nodes of type [" + node.getClass().getName() + "]", node); } if (decorator == null) { parserContext.getReaderContext().fatal("Cannot locate BeanDefinitionDecorator for " + (node instanceof Element ? "element" : "attribute") + " [" + localName + "]", node); } return decorator; }
Subclasses can call this to register the supplied BeanDefinitionParser to handle the specified element. The element name is the local (non-namespace qualified) name.
/** * Subclasses can call this to register the supplied {@link BeanDefinitionParser} to * handle the specified element. The element name is the local (non-namespace qualified) * name. */
protected final void registerBeanDefinitionParser(String elementName, BeanDefinitionParser parser) { this.parsers.put(elementName, parser); }
Subclasses can call this to register the supplied BeanDefinitionDecorator to handle the specified element. The element name is the local (non-namespace qualified) name.
/** * Subclasses can call this to register the supplied {@link BeanDefinitionDecorator} to * handle the specified element. The element name is the local (non-namespace qualified) * name. */
protected final void registerBeanDefinitionDecorator(String elementName, BeanDefinitionDecorator dec) { this.decorators.put(elementName, dec); }
Subclasses can call this to register the supplied BeanDefinitionDecorator to handle the specified attribute. The attribute name is the local (non-namespace qualified) name.
/** * Subclasses can call this to register the supplied {@link BeanDefinitionDecorator} to * handle the specified attribute. The attribute name is the local (non-namespace qualified) * name. */
protected final void registerBeanDefinitionDecoratorForAttribute(String attrName, BeanDefinitionDecorator dec) { this.attributeDecorators.put(attrName, dec); } }