1 17 18 package org.apache.commons.digester.plugins; 19 20 import java.util.List ; 21 22 import org.apache.commons.digester.Rule; 23 import org.apache.commons.logging.Log; 24 25 32 public class PluginCreateRule extends Rule implements InitializableRule { 33 34 private String pluginClassAttrNs = null; 36 private String pluginClassAttr = null; 37 38 private String pluginIdAttrNs = null; 40 private String pluginIdAttr = null; 41 42 46 private String pattern; 47 48 49 private Class baseClass = null; 50 51 56 private Declaration defaultPlugin; 57 58 64 private PluginConfigurationException initException; 65 66 68 75 public PluginCreateRule(Class baseClass) { 76 this.baseClass = baseClass; 77 } 78 79 90 public PluginCreateRule(Class baseClass, Class dfltPluginClass) { 91 this.baseClass = baseClass; 92 if (dfltPluginClass != null) { 93 defaultPlugin = new Declaration(dfltPluginClass); 94 } 95 } 96 97 110 public PluginCreateRule(Class baseClass, Class dfltPluginClass, 111 RuleLoader dfltPluginRuleLoader) { 112 113 this.baseClass = baseClass; 114 if (dfltPluginClass != null) { 115 defaultPlugin = 116 new Declaration(dfltPluginClass, dfltPluginRuleLoader); 117 } 118 } 119 120 122 128 public void setPluginClassAttribute(String namespaceUri, String attrName) { 129 pluginClassAttrNs = namespaceUri; 130 pluginClassAttr = attrName; 131 } 132 133 139 public void setPluginIdAttribute(String namespaceUri, String attrName) { 140 pluginIdAttrNs = namespaceUri; 141 pluginIdAttr = attrName; 142 } 143 144 146 155 public void postRegisterInit(String matchPattern) 156 throws PluginConfigurationException { 157 Log log = LogUtils.getLogger(digester); 158 boolean debug = log.isDebugEnabled(); 159 if (debug) { 160 log.debug("PluginCreateRule.postRegisterInit" + 161 ": rule registered for pattern [" + matchPattern + "]"); 162 } 163 164 if (digester == null) { 165 initException = new PluginConfigurationException( 170 "Invalid invocation of postRegisterInit" + 171 ": digester not set."); 172 throw initException; 173 } 174 175 if (pattern != null) { 176 initException = new PluginConfigurationException( 184 "A single PluginCreateRule instance has been mapped to" + 185 " multiple patterns; this is not supported."); 186 throw initException; 187 } 188 189 if (matchPattern.indexOf('*') != -1) { 190 initException = new PluginConfigurationException( 202 "A PluginCreateRule instance has been mapped to" + 203 " pattern [" + matchPattern + "]." + 204 " This pattern includes a wildcard character." + 205 " This is not supported by the plugin architecture."); 206 throw initException; 207 } 208 209 if (baseClass == null) { 210 baseClass = Object .class; 211 } 212 213 PluginRules rules = (PluginRules) digester.getRules(); 214 PluginManager pm = rules.getPluginManager(); 215 216 if (defaultPlugin != null) { 218 if (!baseClass.isAssignableFrom(defaultPlugin.getPluginClass())) { 219 initException = new PluginConfigurationException( 220 "Default class [" + 221 defaultPlugin.getPluginClass().getName() + 222 "] does not inherit from [" + 223 baseClass.getName() + "]."); 224 throw initException; 225 } 226 227 try { 228 defaultPlugin.init(digester, pm); 229 230 } catch(PluginException pwe) { 231 232 throw new PluginConfigurationException( 233 pwe.getMessage(), pwe.getCause()); 234 } 235 } 236 237 pattern = matchPattern; 239 240 if (pluginClassAttr == null) { 241 pluginClassAttrNs = rules.getPluginClassAttrNs(); 244 pluginClassAttr = rules.getPluginClassAttr(); 245 246 if (debug) { 247 log.debug( 248 "init: pluginClassAttr set to per-digester values [" 249 + "ns=" + pluginClassAttrNs 250 + ", name=" + pluginClassAttr + "]"); 251 } 252 } else { 253 if (debug) { 254 log.debug( 255 "init: pluginClassAttr set to rule-specific values [" 256 + "ns=" + pluginClassAttrNs 257 + ", name=" + pluginClassAttr + "]"); 258 } 259 } 260 261 if (pluginIdAttr == null) { 262 pluginIdAttrNs = rules.getPluginIdAttrNs(); 265 pluginIdAttr = rules.getPluginIdAttr(); 266 267 if (debug) { 268 log.debug( 269 "init: pluginIdAttr set to per-digester values [" 270 + "ns=" + pluginIdAttrNs 271 + ", name=" + pluginIdAttr + "]"); 272 } 273 } else { 274 if (debug) { 275 log.debug( 276 "init: pluginIdAttr set to rule-specific values [" 277 + "ns=" + pluginIdAttrNs 278 + ", name=" + pluginIdAttr + "]"); 279 } 280 } 281 } 282 283 301 public void begin(String namespace, String name, 302 org.xml.sax.Attributes attributes) 303 throws java.lang.Exception { 304 Log log = digester.getLogger(); 305 boolean debug = log.isDebugEnabled(); 306 if (debug) { 307 log.debug("PluginCreateRule.begin" + ": pattern=[" + pattern + "]" + 308 " match=[" + digester.getMatch() + "]"); 309 } 310 311 if (initException != null) { 312 throw initException; 315 } 316 317 PluginRules oldRules = (PluginRules) digester.getRules(); 319 PluginManager pluginManager = oldRules.getPluginManager(); 320 Declaration currDeclaration = null; 321 322 String pluginClassName; 323 if (pluginClassAttrNs == null) { 324 pluginClassName = attributes.getValue(pluginClassAttr); 332 } else { 333 pluginClassName = 334 attributes.getValue(pluginClassAttrNs, pluginClassAttr); 335 } 336 337 String pluginId; 338 if (pluginIdAttrNs == null) { 339 pluginId = attributes.getValue(pluginIdAttr); 340 } else { 341 pluginId = 342 attributes.getValue(pluginIdAttrNs, pluginIdAttr); 343 } 344 345 if (pluginClassName != null) { 346 352 currDeclaration = pluginManager.getDeclarationByClass( 353 pluginClassName); 354 355 if (currDeclaration == null) { 356 currDeclaration = new Declaration(pluginClassName); 357 try { 358 currDeclaration.init(digester, pluginManager); 359 } catch(PluginException pwe) { 360 throw new PluginInvalidInputException( 361 pwe.getMessage(), pwe.getCause()); 362 } 363 pluginManager.addDeclaration(currDeclaration); 364 } 365 } else if (pluginId != null) { 366 currDeclaration = pluginManager.getDeclarationById(pluginId); 367 368 if (currDeclaration == null) { 369 throw new PluginInvalidInputException( 370 "Plugin id [" + pluginId + "] is not defined."); 371 } 372 } else if (defaultPlugin != null) { 373 currDeclaration = defaultPlugin; 374 } else { 375 throw new PluginInvalidInputException( 376 "No plugin class specified for element " + 377 pattern); 378 } 379 380 Class pluginClass = currDeclaration.getPluginClass(); 382 383 String path = digester.getMatch(); 384 385 PluginRules newRules = new PluginRules(digester, path, oldRules, pluginClass); 391 digester.setRules(newRules); 392 393 if (debug) { 394 log.debug("PluginCreateRule.begin: installing new plugin: " + 395 "oldrules=" + oldRules.toString() + 396 ", newrules=" + newRules.toString()); 397 } 398 399 currDeclaration.configure(digester, pattern); 401 402 Object instance = pluginClass.newInstance(); 404 getDigester().push(instance); 405 if (debug) { 406 log.debug( 407 "PluginCreateRule.begin" + ": pattern=[" + pattern + "]" + 408 " match=[" + digester.getMatch() + "]" + 409 " pushed instance of plugin [" + pluginClass.getName() + "]"); 410 } 411 412 List rules = newRules.getDecoratedRules().match(namespace, path); 416 fireBeginMethods(rules, namespace, name, attributes); 417 } 418 419 424 public void body(String namespace, String name, String text) 425 throws Exception { 426 427 440 String path = digester.getMatch(); 441 PluginRules newRules = (PluginRules) digester.getRules(); 442 List rules = newRules.getDecoratedRules().match(namespace, path); 443 fireBodyMethods(rules, namespace, name, text); 444 } 445 446 457 public void end(String namespace, String name) 458 throws Exception { 459 460 461 String path = digester.getMatch(); 463 PluginRules newRules = (PluginRules) digester.getRules(); 464 List rules = newRules.getDecoratedRules().match(namespace, path); 465 fireEndMethods(rules, namespace, name); 466 467 digester.setRules(newRules.getParent()); 470 471 digester.pop(); 474 } 475 476 488 public String getPattern() { 489 return pattern; 490 } 491 492 498 public void fireBeginMethods(List rules, 499 String namespace, String name, 500 org.xml.sax.Attributes list) 501 throws java.lang.Exception { 502 503 if ((rules != null) && (rules.size() > 0)) { 504 Log log = digester.getLogger(); 505 boolean debug = log.isDebugEnabled(); 506 for (int i = 0; i < rules.size(); i++) { 507 try { 508 Rule rule = (Rule) rules.get(i); 509 if (debug) { 510 log.debug(" Fire begin() for " + rule); 511 } 512 rule.begin(namespace, name, list); 513 } catch (Exception e) { 514 throw digester.createSAXException(e); 515 } catch (Error e) { 516 throw e; 517 } 518 } 519 } 520 } 521 522 528 private void fireBodyMethods(List rules, 529 String namespaceURI, String name, 530 String text) throws Exception { 531 532 if ((rules != null) && (rules.size() > 0)) { 533 Log log = digester.getLogger(); 534 boolean debug = log.isDebugEnabled(); 535 for (int i = 0; i < rules.size(); i++) { 536 try { 537 Rule rule = (Rule) rules.get(i); 538 if (debug) { 539 log.debug(" Fire body() for " + rule); 540 } 541 rule.body(namespaceURI, name, text); 542 } catch (Exception e) { 543 throw digester.createSAXException(e); 544 } catch (Error e) { 545 throw e; 546 } 547 } 548 } 549 } 550 551 557 public void fireEndMethods(List rules, 558 String namespaceURI, String name) 559 throws Exception { 560 561 if (rules != null) { 563 Log log = digester.getLogger(); 564 boolean debug = log.isDebugEnabled(); 565 for (int i = 0; i < rules.size(); i++) { 566 int j = (rules.size() - i) - 1; 567 try { 568 Rule rule = (Rule) rules.get(j); 569 if (debug) { 570 log.debug(" Fire end() for " + rule); 571 } 572 rule.end(namespaceURI, name); 573 } catch (Exception e) { 574 throw digester.createSAXException(e); 575 } catch (Error e) { 576 throw e; 577 } 578 } 579 } 580 } 581 } 582 | Popular Tags |