1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
28 | |
|
29 | |
|
30 | |
public class ParentContextDefinitionParser extends AbstractParallelDelegatingDefinitionParser |
31 | |
{ |
32 | |
|
33 | 0 | private Map parsers = new HashMap(); |
34 | 0 | private MuleDefinitionParser otherwise = null; |
35 | |
|
36 | |
public ParentContextDefinitionParser(String context, MuleDefinitionParser parser) |
37 | 0 | { |
38 | 0 | and(context, parser); |
39 | 0 | } |
40 | |
|
41 | |
public ParentContextDefinitionParser and(String context, MuleDefinitionParser parser) |
42 | |
{ |
43 | 0 | StringTokenizer names = new StringTokenizer(context); |
44 | 0 | while (names.hasMoreTokens()) |
45 | |
{ |
46 | 0 | parsers.put(names.nextToken(), parser); |
47 | |
} |
48 | 0 | addDelegate(parser); |
49 | 0 | return this; |
50 | |
} |
51 | |
|
52 | |
public ParentContextDefinitionParser otherwise(MuleDefinitionParser otherwise) |
53 | |
{ |
54 | 0 | this.otherwise = otherwise; |
55 | 0 | return this; |
56 | |
} |
57 | |
|
58 | |
protected MuleDefinitionParser getDelegate(Element element, ParserContext parserContext) |
59 | |
{ |
60 | 0 | String context = element.getParentNode().getLocalName(); |
61 | 0 | if (parsers.containsKey(context)) |
62 | |
{ |
63 | 0 | return (MuleDefinitionParser) parsers.get(context); |
64 | |
} |
65 | 0 | else if (null != otherwise) |
66 | |
{ |
67 | 0 | return otherwise; |
68 | |
} |
69 | |
else |
70 | |
{ |
71 | 0 | throw new IllegalStateException("No parser defined for " + element.getLocalName() + " in the context " |
72 | |
+ context); |
73 | |
} |
74 | |
} |
75 | |
|
76 | |
protected MuleDefinitionParser getOtherwise() |
77 | |
{ |
78 | 0 | return otherwise; |
79 | |
} |
80 | |
|
81 | |
@Override |
82 | |
public MuleDefinitionParserConfiguration addAlias(String alias, String propertyName) |
83 | |
{ |
84 | 0 | super.addAlias(alias, propertyName); |
85 | 0 | if (otherwise != null) |
86 | |
{ |
87 | 0 | otherwise.addAlias(alias, propertyName); |
88 | |
} |
89 | 0 | return this; |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public MuleDefinitionParserConfiguration addBeanFlag(String flag) |
94 | |
{ |
95 | 0 | super.addBeanFlag(flag); |
96 | 0 | if (otherwise != null) |
97 | |
{ |
98 | 0 | otherwise.addBeanFlag(flag); |
99 | |
} |
100 | 0 | return this; |
101 | |
} |
102 | |
|
103 | |
@Override |
104 | |
public MuleDefinitionParserConfiguration addCollection(String propertyName) |
105 | |
{ |
106 | 0 | super.addCollection(propertyName); |
107 | 0 | if (otherwise != null) |
108 | |
{ |
109 | 0 | otherwise.addCollection(propertyName); |
110 | |
} |
111 | 0 | return this; |
112 | |
} |
113 | |
|
114 | |
@Override |
115 | |
public MuleDefinitionParserConfiguration addIgnored(String propertyName) |
116 | |
{ |
117 | 0 | super.addIgnored(propertyName); |
118 | 0 | if (otherwise != null) |
119 | |
{ |
120 | 0 | otherwise.addIgnored(propertyName); |
121 | |
} |
122 | 0 | return this; |
123 | |
} |
124 | |
|
125 | |
@Override |
126 | |
public MuleDefinitionParserConfiguration addMapping(String propertyName, Map mappings) |
127 | |
{ |
128 | 0 | super.addMapping(propertyName, mappings); |
129 | 0 | if (otherwise != null) |
130 | |
{ |
131 | 0 | otherwise.addMapping(propertyName, mappings); |
132 | |
} |
133 | 0 | return this; |
134 | |
} |
135 | |
|
136 | |
@Override |
137 | |
public MuleDefinitionParserConfiguration addMapping(String propertyName, String mappings) |
138 | |
{ |
139 | 0 | super.addMapping(propertyName, mappings); |
140 | 0 | if (otherwise != null) |
141 | |
{ |
142 | 0 | otherwise.addMapping(propertyName, mappings); |
143 | |
} |
144 | 0 | return this; |
145 | |
} |
146 | |
|
147 | |
@Override |
148 | |
public MuleDefinitionParserConfiguration addMapping(String propertyName, ValueMap mappings) |
149 | |
{ |
150 | 0 | super.addMapping(propertyName, mappings); |
151 | 0 | if (otherwise != null) |
152 | |
{ |
153 | 0 | otherwise.addMapping(propertyName, mappings); |
154 | |
} |
155 | 0 | return this; |
156 | |
} |
157 | |
|
158 | |
@Override |
159 | |
public MuleDefinitionParserConfiguration addReference(String propertyName) |
160 | |
{ |
161 | 0 | super.addReference(propertyName); |
162 | 0 | if (otherwise != null) |
163 | |
{ |
164 | 0 | otherwise.addReference(propertyName); |
165 | |
} |
166 | 0 | return this; |
167 | |
} |
168 | |
|
169 | |
@Override |
170 | |
public MuleDefinitionParserConfiguration registerPostProcessor(PostProcessor postProcessor) |
171 | |
{ |
172 | 0 | super.registerPostProcessor(postProcessor); |
173 | 0 | if (otherwise != null) |
174 | |
{ |
175 | 0 | otherwise.registerPostProcessor(postProcessor); |
176 | |
} |
177 | 0 | return this; |
178 | |
} |
179 | |
|
180 | |
@Override |
181 | |
public MuleDefinitionParserConfiguration registerPreProcessor(PreProcessor preProcessor) |
182 | |
{ |
183 | 0 | super.registerPreProcessor(preProcessor); |
184 | 0 | if (otherwise != null) |
185 | |
{ |
186 | 0 | otherwise.registerPreProcessor(preProcessor); |
187 | |
} |
188 | 0 | return this; |
189 | |
} |
190 | |
|
191 | |
@Override |
192 | |
public MuleDefinitionParserConfiguration removeIgnored(String propertyName) |
193 | |
{ |
194 | 0 | super.removeIgnored(propertyName); |
195 | 0 | if (otherwise != null) |
196 | |
{ |
197 | 0 | otherwise.removeIgnored(propertyName); |
198 | |
} |
199 | 0 | return this; |
200 | |
} |
201 | |
|
202 | |
@Override |
203 | |
public MuleDefinitionParserConfiguration setIgnoredDefault(boolean ignoreAll) |
204 | |
{ |
205 | 0 | super.setIgnoredDefault(ignoreAll); |
206 | 0 | if (otherwise != null) |
207 | |
{ |
208 | 0 | otherwise.setIgnoredDefault(ignoreAll); |
209 | |
} |
210 | 0 | return this; |
211 | |
} |
212 | |
|
213 | |
} |