001: /*
002: * $Id: AbstractDelegatingDefinitionParser.java 10790 2008-02-12 20:53:32Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.config.spring.parsers.delegate;
012:
013: import org.mule.config.spring.parsers.MuleDefinitionParser;
014: import org.mule.config.spring.parsers.MuleDefinitionParserConfiguration;
015: import org.mule.config.spring.parsers.PostProcessor;
016: import org.mule.config.spring.parsers.PreProcessor;
017: import org.mule.config.spring.parsers.assembly.configuration.ValueMap;
018: import org.mule.config.spring.parsers.generic.AutoIdUtils;
019: import org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate;
020: import org.mule.util.ArrayUtils;
021:
022: import java.util.Map;
023:
024: import org.springframework.beans.factory.support.AbstractBeanDefinition;
025: import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
026: import org.springframework.beans.factory.xml.ParserContext;
027: import org.w3c.dom.Element;
028:
029: /**
030: * This allows a definition parsers to be dynamically represented by different
031: * definition parsers, depending on the context. For example, a single model may
032: * be defined across file - the first use defines the model and subsequent uses
033: * extend it (for this particular case, see {@link InheritDefinitionParser}).
034: *
035: * <p>Note that the sub-parsers must be consistent. That includes matching the
036: * same schema, for example.</p>
037: */
038: public abstract class AbstractDelegatingDefinitionParser extends
039: AbstractBeanDefinitionParser implements MuleDefinitionParser {
040:
041: private MuleDefinitionParser[] delegates;
042:
043: protected AbstractDelegatingDefinitionParser() {
044: this (new MuleDefinitionParser[0]);
045: }
046:
047: protected AbstractDelegatingDefinitionParser(
048: MuleDefinitionParser[] delegates) {
049: this .delegates = delegates;
050: addBeanFlag(MuleHierarchicalBeanDefinitionParserDelegate.MULE_FORCE_RECURSE);
051: }
052:
053: protected AbstractBeanDefinition parseInternal(Element element,
054: ParserContext parserContext) {
055: return muleParse(element, parserContext);
056: }
057:
058: protected MuleDefinitionParserConfiguration addDelegate(
059: MuleDefinitionParser delegate) {
060: delegates = (MuleDefinitionParser[]) ArrayUtils.add(delegates,
061: delegate);
062: return delegate;
063: }
064:
065: protected int size() {
066: return delegates.length;
067: }
068:
069: protected MuleDefinitionParser getDelegate(int index) {
070: return delegates[index];
071: }
072:
073: public MuleDefinitionParserConfiguration registerPreProcessor(
074: PreProcessor preProcessor) {
075: for (int i = 0; i < delegates.length; ++i) {
076: delegates[i].registerPreProcessor(preProcessor);
077: }
078: return this ;
079: }
080:
081: public MuleDefinitionParserConfiguration registerPostProcessor(
082: PostProcessor postProcessor) {
083: for (int i = 0; i < delegates.length; ++i) {
084: delegates[i].registerPostProcessor(postProcessor);
085: }
086: return this ;
087: }
088:
089: public MuleDefinitionParserConfiguration addReference(
090: String propertyName) {
091: for (int i = 0; i < delegates.length; ++i) {
092: delegates[i].addReference(propertyName);
093: }
094: return this ;
095: }
096:
097: public MuleDefinitionParserConfiguration addMapping(
098: String propertyName, Map mappings) {
099: for (int i = 0; i < delegates.length; ++i) {
100: delegates[i].addMapping(propertyName, mappings);
101: }
102: return this ;
103: }
104:
105: public MuleDefinitionParserConfiguration addMapping(
106: String propertyName, String mappings) {
107: for (int i = 0; i < delegates.length; ++i) {
108: delegates[i].addMapping(propertyName, mappings);
109: }
110: return this ;
111: }
112:
113: public MuleDefinitionParserConfiguration addMapping(
114: String propertyName, ValueMap mappings) {
115: for (int i = 0; i < delegates.length; ++i) {
116: delegates[i].addMapping(propertyName, mappings);
117: }
118: return this ;
119: }
120:
121: public MuleDefinitionParserConfiguration addAlias(String alias,
122: String propertyName) {
123: for (int i = 0; i < delegates.length; ++i) {
124: delegates[i].addAlias(alias, propertyName);
125: }
126: return this ;
127: }
128:
129: public MuleDefinitionParserConfiguration addCollection(
130: String propertyName) {
131: for (int i = 0; i < delegates.length; ++i) {
132: delegates[i].addCollection(propertyName);
133: }
134: return this ;
135: }
136:
137: public MuleDefinitionParserConfiguration addIgnored(
138: String propertyName) {
139: for (int i = 0; i < delegates.length; ++i) {
140: delegates[i].addIgnored(propertyName);
141: }
142: return this ;
143: }
144:
145: public MuleDefinitionParserConfiguration removeIgnored(
146: String propertyName) {
147: for (int i = 0; i < delegates.length; ++i) {
148: delegates[i].removeIgnored(propertyName);
149: }
150: return this ;
151: }
152:
153: public MuleDefinitionParserConfiguration setIgnoredDefault(
154: boolean ignoreAll) {
155: for (int i = 0; i < delegates.length; ++i) {
156: delegates[i].setIgnoredDefault(ignoreAll);
157: }
158: return this ;
159: }
160:
161: public String getBeanName(Element element) {
162: return AutoIdUtils.getUniqueName(element, "delegate");
163: }
164:
165: public MuleDefinitionParserConfiguration addBeanFlag(String flag) {
166: for (int i = 0; i < delegates.length; ++i) {
167: delegates[i].addBeanFlag(flag);
168: }
169: return this;
170: }
171:
172: }
|