1 7 8 package org.dom4j.rule; 9 10 import java.util.HashMap ; 11 import java.util.Map ; 12 13 import org.dom4j.Attribute; 14 import org.dom4j.Document; 15 import org.dom4j.Element; 16 import org.dom4j.Node; 17 18 29 public class Mode { 30 private RuleSet[] ruleSets = new RuleSet[Pattern.NUMBER_OF_TYPES]; 31 32 33 private Map elementNameRuleSets; 34 35 36 private Map attributeNameRuleSets; 37 38 public Mode() { 39 } 40 41 50 public void fireRule(Node node) throws Exception { 51 if (node != null) { 52 Rule rule = getMatchingRule(node); 53 54 if (rule != null) { 55 Action action = rule.getAction(); 56 57 if (action != null) { 58 action.run(node); 59 } 60 } 61 } 62 } 63 64 public void applyTemplates(Element element) throws Exception { 65 for (int i = 0, size = element.attributeCount(); i < size; i++) { 66 Attribute attribute = element.attribute(i); 67 fireRule(attribute); 68 } 69 70 for (int i = 0, size = element.nodeCount(); i < size; i++) { 71 Node node = element.node(i); 72 fireRule(node); 73 } 74 } 75 76 public void applyTemplates(Document document) throws Exception { 77 for (int i = 0, size = document.nodeCount(); i < size; i++) { 78 Node node = document.node(i); 79 fireRule(node); 80 } 81 } 82 83 public void addRule(Rule rule) { 84 int matchType = rule.getMatchType(); 85 String name = rule.getMatchesNodeName(); 86 87 if (name != null) { 88 if (matchType == Node.ELEMENT_NODE) { 89 elementNameRuleSets = addToNameMap(elementNameRuleSets, name, 90 rule); 91 } else if (matchType == Node.ATTRIBUTE_NODE) { 92 attributeNameRuleSets = addToNameMap(attributeNameRuleSets, 93 name, rule); 94 } 95 } 96 97 if (matchType >= Pattern.NUMBER_OF_TYPES) { 98 matchType = Pattern.ANY_NODE; 99 } 100 101 if (matchType == Pattern.ANY_NODE) { 102 for (int i = 1, size = ruleSets.length; i < size; i++) { 104 RuleSet ruleSet = ruleSets[i]; 105 106 if (ruleSet != null) { 107 ruleSet.addRule(rule); 108 } 109 } 110 } 111 112 getRuleSet(matchType).addRule(rule); 113 } 114 115 public void removeRule(Rule rule) { 116 int matchType = rule.getMatchType(); 117 String name = rule.getMatchesNodeName(); 118 119 if (name != null) { 120 if (matchType == Node.ELEMENT_NODE) { 121 removeFromNameMap(elementNameRuleSets, name, rule); 122 } else if (matchType == Node.ATTRIBUTE_NODE) { 123 removeFromNameMap(attributeNameRuleSets, name, rule); 124 } 125 } 126 127 if (matchType >= Pattern.NUMBER_OF_TYPES) { 128 matchType = Pattern.ANY_NODE; 129 } 130 131 getRuleSet(matchType).removeRule(rule); 132 133 if (matchType != Pattern.ANY_NODE) { 134 getRuleSet(Pattern.ANY_NODE).removeRule(rule); 135 } 136 } 137 138 147 public Rule getMatchingRule(Node node) { 148 int matchType = node.getNodeType(); 149 150 if (matchType == Node.ELEMENT_NODE) { 151 if (elementNameRuleSets != null) { 152 String name = node.getName(); 153 RuleSet ruleSet = (RuleSet) elementNameRuleSets.get(name); 154 155 if (ruleSet != null) { 156 Rule answer = ruleSet.getMatchingRule(node); 157 158 if (answer != null) { 159 return answer; 160 } 161 } 162 } 163 } else if (matchType == Node.ATTRIBUTE_NODE) { 164 if (attributeNameRuleSets != null) { 165 String name = node.getName(); 166 RuleSet ruleSet = (RuleSet) attributeNameRuleSets.get(name); 167 168 if (ruleSet != null) { 169 Rule answer = ruleSet.getMatchingRule(node); 170 171 if (answer != null) { 172 return answer; 173 } 174 } 175 } 176 } 177 178 if ((matchType < 0) || (matchType >= ruleSets.length)) { 179 matchType = Pattern.ANY_NODE; 180 } 181 182 Rule answer = null; 183 RuleSet ruleSet = ruleSets[matchType]; 184 185 if (ruleSet != null) { 186 answer = ruleSet.getMatchingRule(node); 188 } 189 190 if ((answer == null) && (matchType != Pattern.ANY_NODE)) { 191 ruleSet = ruleSets[Pattern.ANY_NODE]; 193 194 if (ruleSet != null) { 195 answer = ruleSet.getMatchingRule(node); 196 } 197 } 198 199 return answer; 200 } 201 202 211 protected RuleSet getRuleSet(int matchType) { 212 RuleSet ruleSet = ruleSets[matchType]; 213 214 if (ruleSet == null) { 215 ruleSet = new RuleSet(); 216 ruleSets[matchType] = ruleSet; 217 218 if (matchType != Pattern.ANY_NODE) { 220 RuleSet allRules = ruleSets[Pattern.ANY_NODE]; 221 222 if (allRules != null) { 223 ruleSet.addAll(allRules); 224 } 225 } 226 } 227 228 return ruleSet; 229 } 230 231 243 protected Map addToNameMap(Map map, String name, Rule rule) { 244 if (map == null) { 245 map = new HashMap (); 246 } 247 248 RuleSet ruleSet = (RuleSet) map.get(name); 249 250 if (ruleSet == null) { 251 ruleSet = new RuleSet(); 252 map.put(name, ruleSet); 253 } 254 255 ruleSet.addRule(rule); 256 257 return map; 258 } 259 260 protected void removeFromNameMap(Map map, String name, Rule rule) { 261 if (map != null) { 262 RuleSet ruleSet = (RuleSet) map.get(name); 263 264 if (ruleSet != null) { 265 ruleSet.removeRule(rule); 266 } 267 } 268 } 269 } 270 271 307
| Popular Tags
|