View Javadoc

1   /*
2    * $Id: ParentContextDefinitionParser.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.spring.parsers.delegate;
12  
13  import org.mule.config.spring.parsers.MuleDefinitionParser;
14  import org.mule.config.spring.parsers.MuleDefinitionParserConfiguration;
15  import org.mule.config.spring.parsers.PostProcessor;
16  import org.mule.config.spring.parsers.PreProcessor;
17  import org.mule.config.spring.parsers.assembly.configuration.ValueMap;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.StringTokenizer;
22  
23  import org.springframework.beans.factory.xml.ParserContext;
24  import org.w3c.dom.Element;
25  
26  /**
27   * This encapsulates several definition parsers, selected depending on the parent
28   * element in the DOM.
29   */
30  public class ParentContextDefinitionParser extends AbstractParallelDelegatingDefinitionParser
31  {
32  
33      private Map parsers = new HashMap();
34      private MuleDefinitionParser otherwise = null;
35  
36      public ParentContextDefinitionParser(String context, MuleDefinitionParser parser)
37      {
38          and(context, parser);
39      }
40  
41      public ParentContextDefinitionParser and(String context, MuleDefinitionParser parser)
42      {
43          StringTokenizer names = new StringTokenizer(context);
44          while (names.hasMoreTokens())
45          {
46              parsers.put(names.nextToken(), parser);
47          }
48          addDelegate(parser);
49          return this;
50      }
51  
52      public ParentContextDefinitionParser otherwise(MuleDefinitionParser otherwise)
53      {
54          this.otherwise = otherwise;
55          return this;
56      }
57  
58      protected MuleDefinitionParser getDelegate(Element element, ParserContext parserContext)
59      {
60          String context = element.getParentNode().getLocalName();
61          if (parsers.containsKey(context))
62          {
63              return (MuleDefinitionParser) parsers.get(context);
64          }
65          else if (null != otherwise)
66          {
67              return otherwise;
68          }
69          else
70          {
71              throw new IllegalStateException("No parser defined for " + element.getLocalName() + " in the context "
72                                              + context);
73          }
74      }
75  
76      protected MuleDefinitionParser getOtherwise()
77      {
78          return otherwise;
79      }
80  
81      @Override
82      public MuleDefinitionParserConfiguration addAlias(String alias, String propertyName)
83      {
84          super.addAlias(alias, propertyName);
85          if (otherwise != null)
86          {
87              otherwise.addAlias(alias, propertyName);
88          }
89          return this;
90      }
91  
92      @Override
93      public MuleDefinitionParserConfiguration addBeanFlag(String flag)
94      {
95          super.addBeanFlag(flag);
96          if (otherwise != null)
97          {
98              otherwise.addBeanFlag(flag);
99          }
100         return this;
101     }
102 
103     @Override
104     public MuleDefinitionParserConfiguration addCollection(String propertyName)
105     {
106         super.addCollection(propertyName);
107         if (otherwise != null)
108         {
109             otherwise.addCollection(propertyName);
110         }
111         return this;
112     }
113 
114     @Override
115     public MuleDefinitionParserConfiguration addIgnored(String propertyName)
116     {
117         super.addIgnored(propertyName);
118         if (otherwise != null)
119         {
120             otherwise.addIgnored(propertyName);
121         }
122         return this;
123     }
124 
125     @Override
126     public MuleDefinitionParserConfiguration addMapping(String propertyName, Map mappings)
127     {
128         super.addMapping(propertyName, mappings);
129         if (otherwise != null)
130         {
131             otherwise.addMapping(propertyName, mappings);
132         }
133         return this;
134     }
135 
136     @Override
137     public MuleDefinitionParserConfiguration addMapping(String propertyName, String mappings)
138     {
139         super.addMapping(propertyName, mappings);
140         if (otherwise != null)
141         {
142             otherwise.addMapping(propertyName, mappings);
143         }
144         return this;
145     }
146 
147     @Override
148     public MuleDefinitionParserConfiguration addMapping(String propertyName, ValueMap mappings)
149     {
150         super.addMapping(propertyName, mappings);
151         if (otherwise != null)
152         {
153             otherwise.addMapping(propertyName, mappings);
154         }
155         return this;
156     }
157 
158     @Override
159     public MuleDefinitionParserConfiguration addReference(String propertyName)
160     {
161         super.addReference(propertyName);
162         if (otherwise != null)
163         {
164             otherwise.addReference(propertyName);
165         }
166         return this;
167     }
168 
169     @Override
170     public MuleDefinitionParserConfiguration registerPostProcessor(PostProcessor postProcessor)
171     {
172         super.registerPostProcessor(postProcessor);
173         if (otherwise != null)
174         {
175             otherwise.registerPostProcessor(postProcessor);
176         }
177         return this;
178     }
179 
180     @Override
181     public MuleDefinitionParserConfiguration registerPreProcessor(PreProcessor preProcessor)
182     {
183         super.registerPreProcessor(preProcessor);
184         if (otherwise != null)
185         {
186             otherwise.registerPreProcessor(preProcessor);
187         }
188         return this;
189     }
190 
191     @Override
192     public MuleDefinitionParserConfiguration removeIgnored(String propertyName)
193     {
194         super.removeIgnored(propertyName);
195         if (otherwise != null)
196         {
197             otherwise.removeIgnored(propertyName);
198         }
199         return this;
200     }
201 
202     @Override
203     public MuleDefinitionParserConfiguration setIgnoredDefault(boolean ignoreAll)
204     {
205         super.setIgnoredDefault(ignoreAll);
206         if (otherwise != null)
207         {
208             otherwise.setIgnoredDefault(ignoreAll);
209         }
210         return this;
211     }
212 
213 }