1 17 package org.apache.commons.digester.plugins; 18 19 import java.util.List ; 20 21 import org.apache.commons.digester.Digester; 22 import org.apache.commons.digester.Rule; 23 import org.apache.commons.digester.Rules; 24 import org.apache.commons.digester.RulesBase; 25 import org.apache.commons.logging.Log; 26 27 50 51 public class PluginRules implements Rules { 52 53 56 protected Digester digester = null; 57 58 61 private RulesFactory rulesFactory; 62 63 67 private Rules decoratedRules; 68 69 70 private PluginManager pluginManager; 71 72 77 private String mountPoint = null; 78 79 83 private PluginRules parent = null; 84 85 89 private PluginContext pluginContext = null; 90 91 93 98 public PluginRules() { 99 this(new RulesBase()); 100 } 101 102 106 public PluginRules(Rules decoratedRules) { 107 this.decoratedRules = decoratedRules; 108 109 pluginContext = new PluginContext(); 110 pluginManager = new PluginManager(pluginContext); 111 } 112 113 129 PluginRules( 130 Digester digester, 131 String mountPoint, 132 PluginRules parent, 133 Class pluginClass) 134 throws PluginException { 135 139 this.digester = digester; 140 this.mountPoint = mountPoint; 141 this.parent = parent; 142 this.rulesFactory = parent.rulesFactory; 143 144 if (rulesFactory == null) { 145 decoratedRules = new RulesBase(); 146 } else { 147 decoratedRules = rulesFactory.newRules(digester, pluginClass); 148 } 149 150 pluginContext = parent.pluginContext; 151 pluginManager = new PluginManager(parent.pluginManager); 152 } 153 154 156 159 public Rules getParent() { 160 return parent; 161 } 162 163 166 public Digester getDigester() { 167 return digester; 168 } 169 170 175 public void setDigester(Digester digester) { 176 this.digester = digester; 177 decoratedRules.setDigester(digester); 178 } 179 180 184 public String getNamespaceURI() { 185 return decoratedRules.getNamespaceURI(); 186 } 187 188 196 public void setNamespaceURI(String namespaceURI) { 197 decoratedRules.setNamespaceURI(namespaceURI); 198 } 199 200 205 public PluginManager getPluginManager() { 206 return pluginManager; 207 } 208 209 212 public List getRuleFinders() { 213 return pluginContext.getRuleFinders(); 214 } 215 216 219 public void setRuleFinders(List ruleFinders) { 220 pluginContext.setRuleFinders(ruleFinders); 221 } 222 223 226 public RulesFactory getRulesFactory() { 227 return rulesFactory; 228 } 229 230 234 public void setRulesFactory(RulesFactory factory) { 235 rulesFactory = factory; 236 } 237 238 240 245 Rules getDecoratedRules() { 246 return decoratedRules; 247 } 248 249 258 public List rules() { 259 return decoratedRules.rules(); 260 } 261 262 270 public void add(String pattern, Rule rule) { 271 Log log = LogUtils.getLogger(digester); 272 boolean debug = log.isDebugEnabled(); 273 274 if (debug) { 275 log.debug("add entry" + ": mapping pattern [" + pattern + "]" + 276 " to rule of type [" + rule.getClass().getName() + "]"); 277 } 278 279 if (pattern.startsWith("/")) 281 { 282 pattern = pattern.substring(1); 283 } 284 285 if (mountPoint != null) { 286 if (!pattern.equals(mountPoint) 287 && !pattern.startsWith(mountPoint + "/")) { 288 294 log.warn( 296 "An attempt was made to add a rule with a pattern that" 297 + "is not at or below the mountpoint of the current" 298 + " PluginRules object." 299 + " Rule pattern: " + pattern 300 + ", mountpoint: " + mountPoint 301 + ", rule type: " + rule.getClass().getName()); 302 return; 303 } 304 } 305 306 decoratedRules.add(pattern, rule); 307 308 if (rule instanceof InitializableRule) { 309 try { 310 ((InitializableRule)rule).postRegisterInit(pattern); 311 } catch (PluginConfigurationException e) { 312 if (debug) { 318 log.debug("Rule initialisation failed", e); 319 } 320 return; 322 } 323 } 324 325 if (debug) { 326 log.debug("add exit" + ": mapped pattern [" + pattern + "]" + 327 " to rule of type [" + rule.getClass().getName() + "]"); 328 } 329 } 330 331 334 public void clear() { 335 decoratedRules.clear(); 336 } 337 338 349 public List match(String path) { 350 return (match(null, path)); 351 } 352 353 364 public List match(String namespaceURI, String path) { 365 Log log = LogUtils.getLogger(digester); 366 boolean debug = log.isDebugEnabled(); 367 368 if (debug) { 369 log.debug( 370 "Matching path [" + path + 371 "] on rules object " + this.toString()); 372 } 373 374 List matches; 375 if ((mountPoint != null) && 376 (path.length() <= mountPoint.length())) { 377 if (debug) { 378 log.debug( 379 "Path [" + path + "] delegated to parent."); 380 } 381 382 matches = parent.match(namespaceURI, path); 383 384 } else { 390 log.debug("delegating to decorated rules."); 391 matches = decoratedRules.match(namespaceURI, path); 392 } 393 394 return matches; 395 } 396 397 398 public void setPluginClassAttribute(String namespaceUri, 399 String attrName) { 400 pluginContext.setPluginClassAttribute(namespaceUri, attrName); 401 } 402 403 404 public void setPluginIdAttribute(String namespaceUri, 405 String attrName) { 406 pluginContext.setPluginIdAttribute(namespaceUri, attrName); 407 } 408 409 410 public String getPluginClassAttrNs() { 411 return pluginContext.getPluginClassAttrNs(); 412 } 413 414 415 public String getPluginClassAttr() { 416 return pluginContext.getPluginClassAttr(); 417 } 418 419 420 public String getPluginIdAttrNs() { 421 return pluginContext.getPluginIdAttrNs(); 422 } 423 424 425 public String getPluginIdAttr() { 426 return pluginContext.getPluginIdAttr(); 427 } 428 } 429 | Popular Tags |