1 7 8 package org.dom4j.rule; 9 10 import java.util.Iterator ; 11 import java.util.List ; 12 13 import org.dom4j.Document; 14 import org.dom4j.Element; 15 import org.dom4j.Node; 16 import org.dom4j.XPath; 17 18 28 public class Stylesheet { 29 private RuleManager ruleManager = new RuleManager(); 30 31 32 private String modeName; 33 34 37 public Stylesheet() { 38 } 39 40 46 public void addRule(Rule rule) { 47 ruleManager.addRule(rule); 48 } 49 50 56 public void removeRule(Rule rule) { 57 ruleManager.removeRule(rule); 58 } 59 60 70 public void run(Object input) throws Exception { 71 run(input, this.modeName); 72 } 73 74 public void run(Object input, String mode) throws Exception { 75 if (input instanceof Node) { 76 run((Node) input, mode); 77 } else if (input instanceof List ) { 78 run((List ) input, mode); 79 } 80 } 81 82 public void run(List list) throws Exception { 83 run(list, this.modeName); 84 } 85 86 public void run(List list, String mode) throws Exception { 87 for (int i = 0, size = list.size(); i < size; i++) { 88 Object object = list.get(i); 89 90 if (object instanceof Node) { 91 run((Node) object, mode); 92 } 93 } 94 } 95 96 public void run(Node node) throws Exception { 97 run(node, this.modeName); 98 } 99 100 public void run(Node node, String mode) throws Exception { 101 Mode mod = ruleManager.getMode(mode); 102 mod.fireRule(node); 103 } 104 105 116 public void applyTemplates(Object input, XPath xpath) throws Exception { 117 applyTemplates(input, xpath, this.modeName); 118 } 119 120 133 public void applyTemplates(Object input, XPath xpath, String mode) 134 throws Exception { 135 Mode mod = ruleManager.getMode(mode); 136 137 List list = xpath.selectNodes(input); 138 Iterator it = list.iterator(); 139 while (it.hasNext()) { 140 Node current = (Node) it.next(); 141 mod.fireRule(current); 142 } 143 } 144 145 157 public void applyTemplates(Object input, org.jaxen.XPath xpath) 158 throws Exception { 159 applyTemplates(input, xpath, this.modeName); 160 } 161 162 177 public void applyTemplates(Object input, org.jaxen.XPath xpath, String mode) 178 throws Exception { 179 Mode mod = ruleManager.getMode(mode); 180 181 List list = xpath.selectNodes(input); 182 Iterator it = list.iterator(); 183 while (it.hasNext()) { 184 Node current = (Node) it.next(); 185 mod.fireRule(current); 186 } 187 } 188 189 201 public void applyTemplates(Object input) throws Exception { 202 applyTemplates(input, this.modeName); 203 } 204 205 219 public void applyTemplates(Object input, String mode) throws Exception { 220 Mode mod = ruleManager.getMode(mode); 221 222 if (input instanceof Element) { 223 Element element = (Element) input; 225 for (int i = 0, size = element.nodeCount(); i < size; i++) { 226 Node node = element.node(i); 227 mod.fireRule(node); 228 } 229 } else if (input instanceof Document) { 230 Document document = (Document) input; 232 for (int i = 0, size = document.nodeCount(); i < size; i++) { 233 Node node = document.node(i); 234 mod.fireRule(node); 235 } 236 } else if (input instanceof List ) { 237 List list = (List ) input; 238 239 for (int i = 0, size = list.size(); i < size; i++) { 240 Object object = list.get(i); 241 242 if (object instanceof Element) { 243 applyTemplates((Element) object, mode); 244 } else if (object instanceof Document) { 245 applyTemplates((Document) object, mode); 246 } 247 } 248 } 249 } 250 251 public void clear() { 252 ruleManager.clear(); 253 } 254 255 258 263 public String getModeName() { 264 return modeName; 265 } 266 267 273 public void setModeName(String modeName) { 274 this.modeName = modeName; 275 } 276 277 283 public Action getValueOfAction() { 284 return ruleManager.getValueOfAction(); 285 } 286 287 294 public void setValueOfAction(Action valueOfAction) { 295 ruleManager.setValueOfAction(valueOfAction); 296 } 297 } 298 299 335 | Popular Tags |