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