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