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.Stack;
020:
021: import org.springframework.beans.factory.config.BeanDefinition;
022: import org.springframework.beans.factory.parsing.BeanComponentDefinition;
023: import org.springframework.beans.factory.parsing.ComponentDefinition;
024: import org.springframework.beans.factory.parsing.CompositeComponentDefinition;
025: import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
026: import 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: */
039: public 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 containingComponents = new Stack();
048:
049: public ParserContext(XmlReaderContext readerContext,
050: BeanDefinitionParserDelegate delegate) {
051: this .readerContext = readerContext;
052: this .delegate = delegate;
053: }
054:
055: public ParserContext(XmlReaderContext readerContext,
056: BeanDefinitionParserDelegate delegate,
057: BeanDefinition containingBeanDefinition) {
058:
059: this .readerContext = readerContext;
060: this .delegate = delegate;
061: this .containingBeanDefinition = containingBeanDefinition;
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
086: .equals(this .delegate.getDefaults().getLazyInit());
087: }
088:
089: public Object extractSource(Object sourceCandidate) {
090: return this .readerContext.extractSource(sourceCandidate);
091: }
092:
093: public CompositeComponentDefinition getContainingComponent() {
094: return (!this .containingComponents.isEmpty() ? (CompositeComponentDefinition) this .containingComponents
095: .lastElement()
096: : null);
097: }
098:
099: public void pushContainingComponent(
100: CompositeComponentDefinition containingComponent) {
101: this .containingComponents.push(containingComponent);
102: }
103:
104: public CompositeComponentDefinition popContainingComponent() {
105: return (CompositeComponentDefinition) this .containingComponents
106: .pop();
107: }
108:
109: public void popAndRegisterContainingComponent() {
110: registerComponent(popContainingComponent());
111: }
112:
113: public void registerComponent(ComponentDefinition component) {
114: CompositeComponentDefinition containingComponent = getContainingComponent();
115: if (containingComponent != null) {
116: containingComponent.addNestedComponent(component);
117: } else {
118: this .readerContext.fireComponentRegistered(component);
119: }
120: }
121:
122: public void registerBeanComponent(BeanComponentDefinition component) {
123: BeanDefinitionReaderUtils.registerBeanDefinition(component,
124: getRegistry());
125: registerComponent(component);
126: }
127:
128: }
|