001/* 002 * Copyright 2002-2012 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 * https://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 017package org.springframework.beans.factory.xml; 018 019import org.w3c.dom.Element; 020import org.w3c.dom.Node; 021 022import org.springframework.beans.factory.config.BeanDefinition; 023import org.springframework.beans.factory.config.BeanDefinitionHolder; 024import org.springframework.lang.Nullable; 025 026/** 027 * Base interface used by the {@link DefaultBeanDefinitionDocumentReader} 028 * for handling custom namespaces in a Spring XML configuration file. 029 * 030 * <p>Implementations are expected to return implementations of the 031 * {@link BeanDefinitionParser} interface for custom top-level tags and 032 * implementations of the {@link BeanDefinitionDecorator} interface for 033 * custom nested tags. 034 * 035 * <p>The parser will call {@link #parse} when it encounters a custom tag 036 * directly under the {@code <beans>} tags and {@link #decorate} when 037 * it encounters a custom tag directly under a {@code <bean>} tag. 038 * 039 * <p>Developers writing their own custom element extensions typically will 040 * not implement this interface directly, but rather make use of the provided 041 * {@link NamespaceHandlerSupport} class. 042 * 043 * @author Rob Harrop 044 * @author Erik Wiersma 045 * @since 2.0 046 * @see DefaultBeanDefinitionDocumentReader 047 * @see NamespaceHandlerResolver 048 */ 049public interface NamespaceHandler { 050 051 /** 052 * Invoked by the {@link DefaultBeanDefinitionDocumentReader} after 053 * construction but before any custom elements are parsed. 054 * @see NamespaceHandlerSupport#registerBeanDefinitionParser(String, BeanDefinitionParser) 055 */ 056 void init(); 057 058 /** 059 * Parse the specified {@link Element} and register any resulting 060 * {@link BeanDefinition BeanDefinitions} with the 061 * {@link org.springframework.beans.factory.support.BeanDefinitionRegistry} 062 * that is embedded in the supplied {@link ParserContext}. 063 * <p>Implementations should return the primary {@code BeanDefinition} 064 * that results from the parse phase if they wish to be used nested 065 * inside (for example) a {@code <property>} tag. 066 * <p>Implementations may return {@code null} if they will 067 * <strong>not</strong> be used in a nested scenario. 068 * @param element the element that is to be parsed into one or more {@code BeanDefinitions} 069 * @param parserContext the object encapsulating the current state of the parsing process 070 * @return the primary {@code BeanDefinition} (can be {@code null} as explained above) 071 */ 072 @Nullable 073 BeanDefinition parse(Element element, ParserContext parserContext); 074 075 /** 076 * Parse the specified {@link Node} and decorate the supplied 077 * {@link BeanDefinitionHolder}, returning the decorated definition. 078 * <p>The {@link Node} may be either an {@link org.w3c.dom.Attr} or an 079 * {@link Element}, depending on whether a custom attribute or element 080 * is being parsed. 081 * <p>Implementations may choose to return a completely new definition, 082 * which will replace the original definition in the resulting 083 * {@link org.springframework.beans.factory.BeanFactory}. 084 * <p>The supplied {@link ParserContext} can be used to register any 085 * additional beans needed to support the main definition. 086 * @param source the source element or attribute that is to be parsed 087 * @param definition the current bean definition 088 * @param parserContext the object encapsulating the current state of the parsing process 089 * @return the decorated definition (to be registered in the BeanFactory), 090 * or simply the original bean definition if no decoration is required. 091 * A {@code null} value is strictly speaking invalid, but will be leniently 092 * treated like the case where the original bean definition gets returned. 093 */ 094 @Nullable 095 BeanDefinitionHolder decorate(Node source, BeanDefinitionHolder definition, ParserContext parserContext); 096 097}