|
| 1 | +/* |
| 2 | + * Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.ocpsoft.rewrite.config; |
| 17 | + |
| 18 | +import org.ocpsoft.rewrite.util.Visitor; |
| 19 | + |
| 20 | +/** |
| 21 | + * This class allows {@link Visitor} instances to walk through a {@link Rule} tree which may contain |
| 22 | + * {@link CompositeCondition} or {@link CompositeOperation} elements containing other rules. |
| 23 | + * |
| 24 | + * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
| 25 | + */ |
| 26 | +public class RuleVisit |
| 27 | +{ |
| 28 | + |
| 29 | + private final Rule root; |
| 30 | + |
| 31 | + /** |
| 32 | + * Initialize class with the supplied root {@link Rule} |
| 33 | + */ |
| 34 | + public RuleVisit(Rule root) |
| 35 | + { |
| 36 | + this.root = root; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Submit the supplied {@link Visitor} and apply it to all {@link Rule} instances in the tree |
| 41 | + */ |
| 42 | + public void accept(Visitor<Rule> visitor) |
| 43 | + { |
| 44 | + visit(root, visitor); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Method to call the {@link Visitor} for the supplied {@link Rule} and recursively calls itself, if the {@link Rule} |
| 49 | + * is a {@link CompositeRule}. |
| 50 | + */ |
| 51 | + private void visit(Rule rule, Visitor<Rule> visitor) |
| 52 | + { |
| 53 | + visitor.visit(rule); |
| 54 | + |
| 55 | + if (rule instanceof CompositeRule) { |
| 56 | + for (Rule child : ((CompositeRule) rule).getRules()) { |
| 57 | + visit(child, visitor); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments