|
19 | 19 | import java.util.Collections; |
20 | 20 | import java.util.List; |
21 | 21 | import java.util.Map.Entry; |
| 22 | +import java.util.Set; |
22 | 23 |
|
23 | 24 | import org.ocpsoft.common.util.Assert; |
24 | 25 | import org.ocpsoft.logging.Logger; |
| 26 | +import org.ocpsoft.rewrite.bind.Evaluation; |
25 | 27 | import org.ocpsoft.rewrite.config.CompositeOperation; |
| 28 | +import org.ocpsoft.rewrite.config.Condition; |
| 29 | +import org.ocpsoft.rewrite.config.ConditionVisit; |
26 | 30 | import org.ocpsoft.rewrite.config.Configuration; |
27 | 31 | import org.ocpsoft.rewrite.config.DefaultOperationBuilder; |
28 | 32 | import org.ocpsoft.rewrite.config.Operation; |
| 33 | +import org.ocpsoft.rewrite.config.OperationVisit; |
| 34 | +import org.ocpsoft.rewrite.config.ParameterizedCallback; |
| 35 | +import org.ocpsoft.rewrite.config.ParameterizedConditionVisitor; |
| 36 | +import org.ocpsoft.rewrite.config.ParameterizedOperationVisitor; |
29 | 37 | import org.ocpsoft.rewrite.config.Rule; |
| 38 | +import org.ocpsoft.rewrite.config.RuleBuilder; |
30 | 39 | import org.ocpsoft.rewrite.context.ContextBase; |
31 | 40 | import org.ocpsoft.rewrite.context.EvaluationContext; |
32 | 41 | import org.ocpsoft.rewrite.context.RewriteState; |
33 | 42 | import org.ocpsoft.rewrite.event.Rewrite; |
| 43 | +import org.ocpsoft.rewrite.param.ConfigurableParameter; |
| 44 | +import org.ocpsoft.rewrite.param.DefaultParameter; |
34 | 45 | import org.ocpsoft.rewrite.param.DefaultParameterStore; |
35 | | -import org.ocpsoft.rewrite.param.DefaultParameterValueStore; |
36 | 46 | import org.ocpsoft.rewrite.param.Parameter; |
37 | 47 | import org.ocpsoft.rewrite.param.ParameterStore; |
38 | 48 | import org.ocpsoft.rewrite.param.ParameterValueStore; |
| 49 | +import org.ocpsoft.rewrite.param.Parameterized; |
39 | 50 | import org.ocpsoft.rewrite.servlet.event.BaseRewrite.Flow; |
40 | 51 | import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite; |
41 | 52 | import org.ocpsoft.rewrite.util.ParameterUtils; |
| 53 | +import org.ocpsoft.rewrite.util.Visitor; |
42 | 54 |
|
43 | 55 | /** |
44 | 56 | * An {@link Operation} that allows for conditional evaluation of nested {@link Rule} sets. |
45 | 57 | * |
46 | 58 | * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
47 | 59 | */ |
48 | | -public class Subset extends DefaultOperationBuilder implements CompositeOperation |
| 60 | +public class Subset extends DefaultOperationBuilder implements CompositeOperation, Parameterized |
49 | 61 | { |
50 | 62 | private static Logger log = Logger.getLogger(Subset.class); |
51 | 63 | private final Configuration config; |
@@ -81,7 +93,8 @@ public void perform(Rewrite rewrite, EvaluationContext context) |
81 | 93 | Rule rule = rules.get(i); |
82 | 94 |
|
83 | 95 | subContext.clear(); |
84 | | - ParameterValueStore values = new DefaultParameterValueStore(); |
| 96 | + subContext.put(ParameterStore.class, context.get(ParameterStore.class)); |
| 97 | + ParameterValueStore values = (ParameterValueStore) context.get(ParameterValueStore.class); |
85 | 98 | subContext.put(ParameterValueStore.class, values); |
86 | 99 |
|
87 | 100 | if (rule.evaluate(event, subContext)) |
@@ -209,4 +222,50 @@ public RewriteState getState() |
209 | 222 | throw new IllegalStateException("not implemented"); |
210 | 223 | } |
211 | 224 | } |
| 225 | + |
| 226 | + @Override |
| 227 | + public Set<String> getRequiredParameterNames() |
| 228 | + { |
| 229 | + return Collections.emptySet(); |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public void setParameterStore(final ParameterStore parent) |
| 234 | + { |
| 235 | + for (final Rule rule : config.getRules()) { |
| 236 | + if (rule instanceof RuleBuilder) { |
| 237 | + ParameterizedCallback callback = new ParameterizedCallback() { |
| 238 | + @Override |
| 239 | + public void call(Parameterized parameterized) |
| 240 | + { |
| 241 | + Set<String> names = parameterized.getRequiredParameterNames(); |
| 242 | + ParameterStore store = ((RuleBuilder) rule).getParameterStore(); |
| 243 | + |
| 244 | + for (Entry<String, Parameter<?>> entry : parent) { |
| 245 | + if (!store.contains(entry.getKey())) |
| 246 | + store.get(entry.getKey(), entry.getValue()); |
| 247 | + else if (!"*".equals(entry.getKey())) |
| 248 | + throw new IllegalStateException("Subset cannot re-configure parameter [" + entry.getKey() |
| 249 | + + "] that was configured in parent Configuration. Re-definition was attempted at [" |
| 250 | + + rule + "] "); |
| 251 | + } |
| 252 | + |
| 253 | + for (String name : names) { |
| 254 | + Parameter<?> parameter = store.get(name, new DefaultParameter(name)); |
| 255 | + if (parameter instanceof ConfigurableParameter<?>) |
| 256 | + ((ConfigurableParameter<?>) parameter).bindsTo(Evaluation.property(name)); |
| 257 | + } |
| 258 | + |
| 259 | + parameterized.setParameterStore(store); |
| 260 | + } |
| 261 | + }; |
| 262 | + |
| 263 | + Visitor<Condition> conditionVisitor = new ParameterizedConditionVisitor(callback); |
| 264 | + new ConditionVisit(rule).accept(conditionVisitor); |
| 265 | + |
| 266 | + Visitor<Operation> operationVisitor = new ParameterizedOperationVisitor(callback); |
| 267 | + new OperationVisit(rule).accept(operationVisitor); |
| 268 | + } |
| 269 | + } |
| 270 | + } |
212 | 271 | } |
0 commit comments