1 7 8 package org.dom4j.rule; 9 10 import java.util.ArrayList ; 11 import java.util.Collections ; 12 13 import org.dom4j.Node; 14 15 26 public class RuleSet { 27 28 private ArrayList rules = new ArrayList (); 29 30 31 private Rule[] ruleArray; 32 33 public RuleSet() { 34 } 35 36 public String toString() { 37 return super.toString() + " [RuleSet: " + rules + " ]"; 38 } 39 40 49 public Rule getMatchingRule(Node node) { 50 Rule[] matches = getRuleArray(); 51 52 for (int i = matches.length - 1; i >= 0; i--) { 53 Rule rule = matches[i]; 54 55 if (rule.matches(node)) { 56 return rule; 57 } 58 } 59 60 return null; 61 } 62 63 public void addRule(Rule rule) { 64 rules.add(rule); 65 ruleArray = null; 66 } 67 68 public void removeRule(Rule rule) { 69 rules.remove(rule); 70 ruleArray = null; 71 } 72 73 79 public void addAll(RuleSet that) { 80 rules.addAll(that.rules); 81 ruleArray = null; 82 } 83 84 90 protected Rule[] getRuleArray() { 91 if (ruleArray == null) { 92 Collections.sort(rules); 93 94 int size = rules.size(); 95 ruleArray = new Rule[size]; 96 rules.toArray(ruleArray); 97 } 98 99 return ruleArray; 100 } 101 } 102 103 139 | Popular Tags |