001/* 002 * Copyright 2002-2018 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 java.util.Stack; 020 021import org.springframework.beans.factory.config.BeanDefinition; 022import org.springframework.beans.factory.parsing.BeanComponentDefinition; 023import org.springframework.beans.factory.parsing.ComponentDefinition; 024import org.springframework.beans.factory.parsing.CompositeComponentDefinition; 025import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; 026import org.springframework.beans.factory.support.BeanDefinitionRegistry; 027 028/** 029 * Context that gets passed along a bean definition parsing process, 030 * encapsulating all relevant configuration as well as state. 031 * Nested inside an {@link XmlReaderContext}. 032 * 033 * @author Rob Harrop 034 * @author Juergen Hoeller 035 * @since 2.0 036 * @see XmlReaderContext 037 * @see BeanDefinitionParserDelegate 038 */ 039public final class ParserContext { 040 041 private final XmlReaderContext readerContext; 042 043 private final BeanDefinitionParserDelegate delegate; 044 045 private BeanDefinition containingBeanDefinition; 046 047 private final Stack<CompositeComponentDefinition> containingComponents = new Stack<CompositeComponentDefinition>(); 048 049 050 public ParserContext(XmlReaderContext readerContext, BeanDefinitionParserDelegate delegate) { 051 this.readerContext = readerContext; 052 this.delegate = delegate; 053 } 054 055 public ParserContext(XmlReaderContext readerContext, BeanDefinitionParserDelegate delegate, 056 BeanDefinition containingBeanDefinition) { 057 058 this.readerContext = readerContext; 059 this.delegate = delegate; 060 this.containingBeanDefinition = containingBeanDefinition; 061 } 062 063 064 public final XmlReaderContext getReaderContext() { 065 return this.readerContext; 066 } 067 068 public final BeanDefinitionRegistry getRegistry() { 069 return this.readerContext.getRegistry(); 070 } 071 072 public final BeanDefinitionParserDelegate getDelegate() { 073 return this.delegate; 074 } 075 076 public final BeanDefinition getContainingBeanDefinition() { 077 return this.containingBeanDefinition; 078 } 079 080 public final boolean isNested() { 081 return (this.containingBeanDefinition != null); 082 } 083 084 public boolean isDefaultLazyInit() { 085 return BeanDefinitionParserDelegate.TRUE_VALUE.equals(this.delegate.getDefaults().getLazyInit()); 086 } 087 088 public Object extractSource(Object sourceCandidate) { 089 return this.readerContext.extractSource(sourceCandidate); 090 } 091 092 public CompositeComponentDefinition getContainingComponent() { 093 return (!this.containingComponents.isEmpty() ? this.containingComponents.lastElement() : null); 094 } 095 096 public void pushContainingComponent(CompositeComponentDefinition containingComponent) { 097 this.containingComponents.push(containingComponent); 098 } 099 100 public CompositeComponentDefinition popContainingComponent() { 101 return this.containingComponents.pop(); 102 } 103 104 public void popAndRegisterContainingComponent() { 105 registerComponent(popContainingComponent()); 106 } 107 108 public void registerComponent(ComponentDefinition component) { 109 CompositeComponentDefinition containingComponent = getContainingComponent(); 110 if (containingComponent != null) { 111 containingComponent.addNestedComponent(component); 112 } 113 else { 114 this.readerContext.fireComponentRegistered(component); 115 } 116 } 117 118 public void registerBeanComponent(BeanComponentDefinition component) { 119 BeanDefinitionReaderUtils.registerBeanDefinition(component, getRegistry()); 120 registerComponent(component); 121 } 122 123}