1 17 18 19 package org.apache.commons.digester; 20 21 22 import org.apache.commons.beanutils.ConvertUtils; 23 import org.apache.commons.beanutils.MethodUtils; 24 import org.xml.sax.Attributes ; 25 26 27 93 94 public class CallMethodRule extends Rule { 95 96 98 111 public CallMethodRule(Digester digester, String methodName, 112 int paramCount) { 113 114 this(methodName, paramCount); 115 116 } 117 118 119 134 public CallMethodRule(Digester digester, String methodName, 135 int paramCount, String paramTypes[]) { 136 137 this(methodName, paramCount, paramTypes); 138 139 } 140 141 142 158 public CallMethodRule(Digester digester, String methodName, 159 int paramCount, Class paramTypes[]) { 160 161 this(methodName, paramCount, paramTypes); 162 } 163 164 165 173 public CallMethodRule(String methodName, 174 int paramCount) { 175 this(0, methodName, paramCount); 176 } 177 178 190 public CallMethodRule(int targetOffset, 191 String methodName, 192 int paramCount) { 193 194 this.targetOffset = targetOffset; 195 this.methodName = methodName; 196 this.paramCount = paramCount; 197 if (paramCount == 0) { 198 this.paramTypes = new Class [] { String .class }; 199 } else { 200 this.paramTypes = new Class [paramCount]; 201 for (int i = 0; i < this.paramTypes.length; i++) { 202 this.paramTypes[i] = String .class; 203 } 204 } 205 206 } 207 208 214 public CallMethodRule(String methodName) { 215 216 this(0, methodName, 0, (Class []) null); 217 218 } 219 220 221 231 public CallMethodRule(int targetOffset, String methodName) { 232 233 this(targetOffset, methodName, 0, (Class []) null); 234 235 } 236 237 238 253 public CallMethodRule( 254 String methodName, 255 int paramCount, 256 String paramTypes[]) { 257 this(0, methodName, paramCount, paramTypes); 258 } 259 260 279 public CallMethodRule( int targetOffset, 280 String methodName, 281 int paramCount, 282 String paramTypes[]) { 283 284 this.targetOffset = targetOffset; 285 this.methodName = methodName; 286 this.paramCount = paramCount; 287 if (paramTypes == null) { 288 this.paramTypes = new Class [paramCount]; 289 for (int i = 0; i < this.paramTypes.length; i++) { 290 this.paramTypes[i] = String .class; 291 } 292 } else { 293 this.paramClassNames = new String [paramTypes.length]; 296 for (int i = 0; i < this.paramClassNames.length; i++) { 297 this.paramClassNames[i] = paramTypes[i]; 298 } 299 } 300 301 } 302 303 304 320 public CallMethodRule( 321 String methodName, 322 int paramCount, 323 Class paramTypes[]) { 324 this(0, methodName, paramCount, paramTypes); 325 } 326 327 347 public CallMethodRule( int targetOffset, 348 String methodName, 349 int paramCount, 350 Class paramTypes[]) { 351 352 this.targetOffset = targetOffset; 353 this.methodName = methodName; 354 this.paramCount = paramCount; 355 if (paramTypes == null) { 356 this.paramTypes = new Class [paramCount]; 357 for (int i = 0; i < this.paramTypes.length; i++) { 358 this.paramTypes[i] = String .class; 359 } 360 } else { 361 this.paramTypes = new Class [paramTypes.length]; 362 for (int i = 0; i < this.paramTypes.length; i++) { 363 this.paramTypes[i] = paramTypes[i]; 364 } 365 } 366 367 } 368 369 370 372 373 376 protected String bodyText = null; 377 378 379 384 private int targetOffset = 0; 385 386 389 protected String methodName = null; 390 391 392 397 protected int paramCount = 0; 398 399 400 403 protected Class paramTypes[] = null; 404 405 409 private String paramClassNames[] = null; 410 411 414 protected boolean useExactMatch = false; 415 416 418 422 public boolean getUseExactMatch() { 423 return useExactMatch; 424 } 425 426 430 public void setUseExactMatch(boolean useExactMatch) 431 { 432 this.useExactMatch = useExactMatch; 433 } 434 435 439 public void setDigester(Digester digester) 440 { 441 super.setDigester(digester); 443 if (this.paramClassNames != null) { 445 this.paramTypes = new Class [paramClassNames.length]; 446 for (int i = 0; i < this.paramClassNames.length; i++) { 447 try { 448 this.paramTypes[i] = 449 digester.getClassLoader().loadClass(this.paramClassNames[i]); 450 } catch (ClassNotFoundException e) { 451 digester.getLogger().error("(CallMethodRule) Cannot load class " + this.paramClassNames[i], e); 453 this.paramTypes[i] = null; } 455 } 456 } 457 } 458 459 464 public void begin(Attributes attributes) throws Exception { 465 466 if (paramCount > 0) { 468 Object parameters[] = new Object [paramCount]; 469 for (int i = 0; i < parameters.length; i++) { 470 parameters[i] = null; 471 } 472 digester.pushParams(parameters); 473 } 474 475 } 476 477 478 483 public void body(String bodyText) throws Exception { 484 485 if (paramCount == 0) { 486 this.bodyText = bodyText.trim(); 487 } 488 489 } 490 491 492 495 public void end() throws Exception { 496 497 Object parameters[] = null; 499 if (paramCount > 0) { 500 501 parameters = (Object []) digester.popParams(); 502 503 if (digester.log.isTraceEnabled()) { 504 for (int i=0,size=parameters.length;i<size;i++) { 505 digester.log.trace("[CallMethodRule](" + i + ")" + parameters[i]) ; 506 } 507 } 508 509 if (paramCount == 1 && parameters[0] == null) { 520 return; 521 } 522 523 } else if (paramTypes != null && paramTypes.length != 0) { 524 528 if (bodyText == null) { 531 return; 532 } 533 534 parameters = new Object [1]; 535 parameters[0] = bodyText; 536 if (paramTypes.length == 0) { 537 paramTypes = new Class [1]; 538 paramTypes[0] = String .class; 539 } 540 541 } else { 542 ; 546 } 547 548 Object paramValues[] = new Object [paramTypes.length]; 552 for (int i = 0; i < paramTypes.length; i++) { 553 if( 556 parameters[i] == null || 557 (parameters[i] instanceof String && 558 !String .class.isAssignableFrom(paramTypes[i]))) { 559 560 paramValues[i] = 561 ConvertUtils.convert((String ) parameters[i], paramTypes[i]); 562 } else { 563 paramValues[i] = parameters[i]; 564 } 565 } 566 567 Object target; 569 if (targetOffset >= 0) { 570 target = digester.peek(targetOffset); 571 } else { 572 target = digester.peek( digester.getCount() + targetOffset ); 573 } 574 575 if (target == null) { 576 StringBuffer sb = new StringBuffer (); 577 sb.append("[CallMethodRule]{"); 578 sb.append(digester.match); 579 sb.append("} Call target is null ("); 580 sb.append("targetOffset="); 581 sb.append(targetOffset); 582 sb.append(",stackdepth="); 583 sb.append(digester.getCount()); 584 sb.append(")"); 585 throw new org.xml.sax.SAXException (sb.toString()); 586 } 587 588 if (digester.log.isDebugEnabled()) { 590 StringBuffer sb = new StringBuffer ("[CallMethodRule]{"); 591 sb.append(digester.match); 592 sb.append("} Call "); 593 sb.append(target.getClass().getName()); 594 sb.append("."); 595 sb.append(methodName); 596 sb.append("("); 597 for (int i = 0; i < paramValues.length; i++) { 598 if (i > 0) { 599 sb.append(","); 600 } 601 if (paramValues[i] == null) { 602 sb.append("null"); 603 } else { 604 sb.append(paramValues[i].toString()); 605 } 606 sb.append("/"); 607 if (paramTypes[i] == null) { 608 sb.append("null"); 609 } else { 610 sb.append(paramTypes[i].getName()); 611 } 612 } 613 sb.append(")"); 614 digester.log.debug(sb.toString()); 615 } 616 617 Object result = null; 618 if (useExactMatch) { 619 result = MethodUtils.invokeExactMethod(target, methodName, 621 paramValues, paramTypes); 622 623 } else { 624 result = MethodUtils.invokeMethod(target, methodName, 626 paramValues, paramTypes); 627 } 628 629 processMethodCallResult(result); 630 } 631 632 633 636 public void finish() throws Exception { 637 638 bodyText = null; 639 640 } 641 642 648 protected void processMethodCallResult(Object result) { 649 } 651 652 655 public String toString() { 656 657 StringBuffer sb = new StringBuffer ("CallMethodRule["); 658 sb.append("methodName="); 659 sb.append(methodName); 660 sb.append(", paramCount="); 661 sb.append(paramCount); 662 sb.append(", paramTypes={"); 663 if (paramTypes != null) { 664 for (int i = 0; i < paramTypes.length; i++) { 665 if (i > 0) { 666 sb.append(", "); 667 } 668 sb.append(paramTypes[i].getName()); 669 } 670 } 671 sb.append("}"); 672 sb.append("]"); 673 return (sb.toString()); 674 675 } 676 677 678 } 679 | Popular Tags |