1 17 18 19 package org.apache.commons.digester; 20 21 import org.xml.sax.Attributes ; 22 23 import org.apache.commons.collections.ArrayStack; 24 25 26 36 37 public class FactoryCreateRule extends Rule { 38 39 41 42 private boolean ignoreCreateExceptions; 43 44 private ArrayStack exceptionIgnoredStack; 45 46 48 49 60 public FactoryCreateRule(Digester digester, String className) { 61 62 this(className); 63 64 } 65 66 67 78 public FactoryCreateRule(Digester digester, Class clazz) { 79 80 this(clazz); 81 82 } 83 84 85 99 public FactoryCreateRule(Digester digester, 100 String className, String attributeName) { 101 102 this(className, attributeName); 103 104 } 105 106 107 121 public FactoryCreateRule(Digester digester, 122 Class clazz, String attributeName) { 123 124 this(clazz, attributeName); 125 126 } 127 128 129 139 public FactoryCreateRule(Digester digester, 140 ObjectCreationFactory creationFactory) { 141 142 this(creationFactory); 143 144 } 145 146 155 public FactoryCreateRule(String className) { 156 157 this(className, false); 158 159 } 160 161 162 171 public FactoryCreateRule(Class clazz) { 172 173 this(clazz, false); 174 175 } 176 177 178 190 public FactoryCreateRule(String className, String attributeName) { 191 192 this(className, attributeName, false); 193 194 } 195 196 197 209 public FactoryCreateRule(Class clazz, String attributeName) { 210 211 this(clazz, attributeName, false); 212 213 } 214 215 216 224 public FactoryCreateRule(ObjectCreationFactory creationFactory) { 225 226 this(creationFactory, false); 227 228 } 229 230 240 public FactoryCreateRule(String className, boolean ignoreCreateExceptions) { 241 242 this(className, null, ignoreCreateExceptions); 243 244 } 245 246 247 257 public FactoryCreateRule(Class clazz, boolean ignoreCreateExceptions) { 258 259 this(clazz, null, ignoreCreateExceptions); 260 261 } 262 263 264 276 public FactoryCreateRule( 277 String className, 278 String attributeName, 279 boolean ignoreCreateExceptions) { 280 281 this.className = className; 282 this.attributeName = attributeName; 283 this.ignoreCreateExceptions = ignoreCreateExceptions; 284 285 } 286 287 288 300 public FactoryCreateRule( 301 Class clazz, 302 String attributeName, 303 boolean ignoreCreateExceptions) { 304 305 this(clazz.getName(), attributeName, ignoreCreateExceptions); 306 307 } 308 309 310 318 public FactoryCreateRule( 319 ObjectCreationFactory creationFactory, 320 boolean ignoreCreateExceptions) { 321 322 this.creationFactory = creationFactory; 323 this.ignoreCreateExceptions = ignoreCreateExceptions; 324 } 325 326 328 329 332 protected String attributeName = null; 333 334 335 339 protected String className = null; 340 341 342 347 protected ObjectCreationFactory creationFactory = null; 348 349 350 352 353 358 public void begin(String namespace, String name, Attributes attributes) throws Exception { 359 360 if (ignoreCreateExceptions) { 361 362 if (exceptionIgnoredStack == null) { 363 exceptionIgnoredStack = new ArrayStack(); 364 } 365 366 try { 367 Object instance = getFactory(attributes).createObject(attributes); 368 369 if (digester.log.isDebugEnabled()) { 370 digester.log.debug("[FactoryCreateRule]{" + digester.match + 371 "} New " + instance.getClass().getName()); 372 } 373 digester.push(instance); 374 exceptionIgnoredStack.push(Boolean.FALSE); 375 376 } catch (Exception e) { 377 if (digester.log.isInfoEnabled()) { 379 digester.log.info("[FactoryCreateRule] Create exception ignored: " + 380 ((e.getMessage() == null) ? e.getClass().getName() : e.getMessage())); 381 if (digester.log.isDebugEnabled()) { 382 digester.log.debug("[FactoryCreateRule] Ignored exception:", e); 383 } 384 } 385 exceptionIgnoredStack.push(Boolean.TRUE); 386 } 387 388 } else { 389 Object instance = getFactory(attributes).createObject(attributes); 390 391 if (digester.log.isDebugEnabled()) { 392 digester.log.debug("[FactoryCreateRule]{" + digester.match + 393 "} New " + instance.getClass().getName()); 394 } 395 digester.push(instance); 396 } 397 } 398 399 400 403 public void end(String namespace, String name) throws Exception { 404 405 if ( 408 ignoreCreateExceptions && 409 exceptionIgnoredStack != null && 410 !(exceptionIgnoredStack.empty())) { 411 412 if (((Boolean ) exceptionIgnoredStack.pop()).booleanValue()) { 413 if (digester.log.isTraceEnabled()) { 416 digester.log.trace("[FactoryCreateRule] No creation so no push so no pop"); 417 } 418 return; 419 } 420 } 421 422 Object top = digester.pop(); 423 if (digester.log.isDebugEnabled()) { 424 digester.log.debug("[FactoryCreateRule]{" + digester.match + 425 "} Pop " + top.getClass().getName()); 426 } 427 428 } 429 430 431 434 public void finish() throws Exception { 435 436 if (attributeName != null) { 437 creationFactory = null; 438 } 439 440 } 441 442 443 446 public String toString() { 447 448 StringBuffer sb = new StringBuffer ("FactoryCreateRule["); 449 sb.append("className="); 450 sb.append(className); 451 sb.append(", attributeName="); 452 sb.append(attributeName); 453 if (creationFactory != null) { 454 sb.append(", creationFactory="); 455 sb.append(creationFactory); 456 } 457 sb.append("]"); 458 return (sb.toString()); 459 460 } 461 462 463 465 466 474 protected ObjectCreationFactory getFactory(Attributes attributes) 475 throws Exception { 476 477 if (creationFactory == null) { 478 String realClassName = className; 479 if (attributeName != null) { 480 String value = attributes.getValue(attributeName); 481 if (value != null) { 482 realClassName = value; 483 } 484 } 485 if (digester.log.isDebugEnabled()) { 486 digester.log.debug("[FactoryCreateRule]{" + digester.match + 487 "} New factory " + realClassName); 488 } 489 Class clazz = digester.getClassLoader().loadClass(realClassName); 490 creationFactory = (ObjectCreationFactory) 491 clazz.newInstance(); 492 creationFactory.setDigester(digester); 493 } 494 return (creationFactory); 495 496 } 497 } 498 | Popular Tags |