1 7 8 package org.dom4j.rule; 9 10 import java.util.HashMap ; 11 12 import org.dom4j.Document; 13 import org.dom4j.Element; 14 import org.dom4j.Node; 15 import org.dom4j.rule.pattern.NodeTypePattern; 16 17 26 public class RuleManager { 27 28 private HashMap modes = new HashMap (); 29 30 34 private int appearenceCount; 35 36 37 private Action valueOfAction; 38 39 public RuleManager() { 40 } 41 42 51 public Mode getMode(String modeName) { 52 Mode mode = (Mode) modes.get(modeName); 53 54 if (mode == null) { 55 mode = createMode(); 56 modes.put(modeName, mode); 57 } 58 59 return mode; 60 } 61 62 public void addRule(Rule rule) { 63 rule.setAppearenceCount(++appearenceCount); 64 65 Mode mode = getMode(rule.getMode()); 66 Rule[] childRules = rule.getUnionRules(); 67 68 if (childRules != null) { 69 for (int i = 0, size = childRules.length; i < size; i++) { 70 mode.addRule(childRules[i]); 71 } 72 } else { 73 mode.addRule(rule); 74 } 75 } 76 77 public void removeRule(Rule rule) { 78 Mode mode = getMode(rule.getMode()); 79 Rule[] childRules = rule.getUnionRules(); 80 81 if (childRules != null) { 82 for (int i = 0, size = childRules.length; i < size; i++) { 83 mode.removeRule(childRules[i]); 84 } 85 } else { 86 mode.removeRule(rule); 87 } 88 } 89 90 101 public Rule getMatchingRule(String modeName, Node node) { 102 Mode mode = (Mode) modes.get(modeName); 103 104 if (mode != null) { 105 return mode.getMatchingRule(node); 106 } else { 107 System.out.println("Warning: No Mode for mode: " + mode); 108 109 return null; 110 } 111 } 112 113 public void clear() { 114 modes.clear(); 115 appearenceCount = 0; 116 } 117 118 121 127 public Action getValueOfAction() { 128 return valueOfAction; 129 } 130 131 138 public void setValueOfAction(Action valueOfAction) { 139 this.valueOfAction = valueOfAction; 140 } 141 142 145 151 protected Mode createMode() { 152 Mode mode = new Mode(); 153 addDefaultRules(mode); 154 155 return mode; 156 } 157 158 164 protected void addDefaultRules(final Mode mode) { 165 Action applyTemplates = new Action() { 167 public void run(Node node) throws Exception { 168 if (node instanceof Element) { 169 mode.applyTemplates((Element) node); 170 } else if (node instanceof Document) { 171 mode.applyTemplates((Document) node); 172 } 173 } 174 }; 175 176 Action valueOf = getValueOfAction(); 177 178 addDefaultRule(mode, NodeTypePattern.ANY_DOCUMENT, applyTemplates); 179 addDefaultRule(mode, NodeTypePattern.ANY_ELEMENT, applyTemplates); 180 181 if (valueOf != null) { 182 addDefaultRule(mode, NodeTypePattern.ANY_ATTRIBUTE, valueOf); 183 addDefaultRule(mode, NodeTypePattern.ANY_TEXT, valueOf); 184 } 185 } 186 187 protected void addDefaultRule(Mode mode, Pattern pattern, Action action) { 188 Rule rule = createDefaultRule(pattern, action); 189 mode.addRule(rule); 190 } 191 192 protected Rule createDefaultRule(Pattern pattern, Action action) { 193 Rule rule = new Rule(pattern, action); 194 rule.setImportPrecedence(-1); 195 196 return rule; 197 } 198 } 199 200 236 | Popular Tags |