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