1 18 19 20 package org.apache.tomcat.util.digester; 21 22 23 import org.apache.tomcat.util.IntrospectionUtils; 24 import org.xml.sax.Attributes ; 25 26 27 67 68 public class CallMethodRule extends Rule { 69 70 72 73 86 public CallMethodRule(Digester digester, String methodName, 87 int paramCount) { 88 89 this(methodName, paramCount); 90 91 } 92 93 94 109 public CallMethodRule(Digester digester, String methodName, 110 int paramCount, String paramTypes[]) { 111 112 this(methodName, paramCount, paramTypes); 113 114 } 115 116 117 133 public CallMethodRule(Digester digester, String methodName, 134 int paramCount, Class paramTypes[]) { 135 136 this(methodName, paramCount, paramTypes); 137 } 138 139 140 148 public CallMethodRule(String methodName, 149 int paramCount) { 150 this(0, methodName, paramCount); 151 } 152 153 165 public CallMethodRule(int targetOffset, 166 String methodName, 167 int paramCount) { 168 169 this.targetOffset = targetOffset; 170 this.methodName = methodName; 171 this.paramCount = paramCount; 172 if (paramCount == 0) { 173 this.paramTypes = new Class [] { String .class }; 174 } else { 175 this.paramTypes = new Class [paramCount]; 176 for (int i = 0; i < this.paramTypes.length; i++) { 177 this.paramTypes[i] = String .class; 178 } 179 } 180 181 } 182 183 189 public CallMethodRule(String methodName) { 190 191 this(0, methodName, 0, (Class []) null); 192 193 } 194 195 196 206 public CallMethodRule(int targetOffset, String methodName) { 207 208 this(targetOffset, methodName, 0, (Class []) null); 209 210 } 211 212 213 228 public CallMethodRule( 229 String methodName, 230 int paramCount, 231 String paramTypes[]) { 232 this(0, methodName, paramCount, paramTypes); 233 } 234 235 254 public CallMethodRule( int targetOffset, 255 String methodName, 256 int paramCount, 257 String paramTypes[]) { 258 259 this.targetOffset = targetOffset; 260 this.methodName = methodName; 261 this.paramCount = paramCount; 262 if (paramTypes == null) { 263 this.paramTypes = new Class [paramCount]; 264 for (int i = 0; i < this.paramTypes.length; i++) { 265 this.paramTypes[i] = "abc".getClass(); 266 } 267 } else { 268 this.paramClassNames = new String [paramTypes.length]; 271 for (int i = 0; i < this.paramClassNames.length; i++) { 272 this.paramClassNames[i] = paramTypes[i]; 273 } 274 } 275 276 } 277 278 279 295 public CallMethodRule( 296 String methodName, 297 int paramCount, 298 Class paramTypes[]) { 299 this(0, methodName, paramCount, paramTypes); 300 } 301 302 322 public CallMethodRule( int targetOffset, 323 String methodName, 324 int paramCount, 325 Class paramTypes[]) { 326 327 this.targetOffset = targetOffset; 328 this.methodName = methodName; 329 this.paramCount = paramCount; 330 if (paramTypes == null) { 331 this.paramTypes = new Class [paramCount]; 332 for (int i = 0; i < this.paramTypes.length; i++) { 333 this.paramTypes[i] = "abc".getClass(); 334 } 335 } else { 336 this.paramTypes = new Class [paramTypes.length]; 337 for (int i = 0; i < this.paramTypes.length; i++) { 338 this.paramTypes[i] = paramTypes[i]; 339 } 340 } 341 342 } 343 344 345 347 348 351 protected String bodyText = null; 352 353 354 359 protected int targetOffset = 0; 360 361 364 protected String methodName = null; 365 366 367 372 protected int paramCount = 0; 373 374 375 378 protected Class paramTypes[] = null; 379 380 384 protected String paramClassNames[] = null; 385 386 389 protected boolean useExactMatch = false; 390 391 393 397 public boolean getUseExactMatch() { 398 return useExactMatch; 399 } 400 401 405 public void setUseExactMatch(boolean useExactMatch) 406 { 407 this.useExactMatch = useExactMatch; 408 } 409 410 414 public void setDigester(Digester digester) 415 { 416 super.setDigester(digester); 418 if (this.paramClassNames != null) { 420 this.paramTypes = new Class [paramClassNames.length]; 421 for (int i = 0; i < this.paramClassNames.length; i++) { 422 try { 423 this.paramTypes[i] = 424 digester.getClassLoader().loadClass(this.paramClassNames[i]); 425 } catch (ClassNotFoundException e) { 426 digester.getLogger().error("(CallMethodRule) Cannot load class " + this.paramClassNames[i], e); 428 this.paramTypes[i] = null; } 430 } 431 } 432 } 433 434 439 public void begin(Attributes attributes) throws Exception { 440 441 if (paramCount > 0) { 443 Object parameters[] = new Object [paramCount]; 444 for (int i = 0; i < parameters.length; i++) { 445 parameters[i] = null; 446 } 447 digester.pushParams(parameters); 448 } 449 450 } 451 452 453 458 public void body(String bodyText) throws Exception { 459 460 if (paramCount == 0) { 461 this.bodyText = bodyText.trim(); 462 } 463 464 } 465 466 467 470 public void end() throws Exception { 471 472 Object parameters[] = null; 474 if (paramCount > 0) { 475 476 parameters = (Object []) digester.popParams(); 477 478 if (digester.log.isTraceEnabled()) { 479 for (int i=0,size=parameters.length;i<size;i++) { 480 digester.log.trace("[CallMethodRule](" + i + ")" + parameters[i]) ; 481 } 482 } 483 484 if (paramCount == 1 && parameters[0] == null) { 489 return; 490 } 491 492 } else if (paramTypes != null && paramTypes.length != 0) { 493 494 if (bodyText == null) { 499 return; 500 } 501 502 parameters = new Object [1]; 503 parameters[0] = bodyText; 504 if (paramTypes.length == 0) { 505 paramTypes = new Class [1]; 506 paramTypes[0] = "abc".getClass(); 507 } 508 509 } 510 511 Object paramValues[] = new Object [paramTypes.length]; 515 for (int i = 0; i < paramTypes.length; i++) { 516 if( 519 parameters[i] == null || 520 (parameters[i] instanceof String && 521 !String .class.isAssignableFrom(paramTypes[i]))) { 522 523 paramValues[i] = 524 IntrospectionUtils.convert((String ) parameters[i], paramTypes[i]); 525 } else { 526 paramValues[i] = parameters[i]; 527 } 528 } 529 530 Object target; 532 if (targetOffset >= 0) { 533 target = digester.peek(targetOffset); 534 } else { 535 target = digester.peek( digester.getCount() + targetOffset ); 536 } 537 538 if (target == null) { 539 StringBuffer sb = new StringBuffer (); 540 sb.append("[CallMethodRule]{"); 541 sb.append(digester.match); 542 sb.append("} Call target is null ("); 543 sb.append("targetOffset="); 544 sb.append(targetOffset); 545 sb.append(",stackdepth="); 546 sb.append(digester.getCount()); 547 sb.append(")"); 548 throw new org.xml.sax.SAXException (sb.toString()); 549 } 550 551 if (digester.log.isDebugEnabled()) { 553 StringBuffer sb = new StringBuffer ("[CallMethodRule]{"); 554 sb.append(digester.match); 555 sb.append("} Call "); 556 sb.append(target.getClass().getName()); 557 sb.append("."); 558 sb.append(methodName); 559 sb.append("("); 560 for (int i = 0; i < paramValues.length; i++) { 561 if (i > 0) { 562 sb.append(","); 563 } 564 if (paramValues[i] == null) { 565 sb.append("null"); 566 } else { 567 sb.append(paramValues[i].toString()); 568 } 569 sb.append("/"); 570 if (paramTypes[i] == null) { 571 sb.append("null"); 572 } else { 573 sb.append(paramTypes[i].getName()); 574 } 575 } 576 sb.append(")"); 577 digester.log.debug(sb.toString()); 578 } 579 Object result = IntrospectionUtils.callMethodN(target, methodName, 580 paramValues, paramTypes); 581 processMethodCallResult(result); 582 } 583 584 585 588 public void finish() throws Exception { 589 590 bodyText = null; 591 592 } 593 594 600 protected void processMethodCallResult(Object result) { 601 } 603 604 607 public String toString() { 608 609 StringBuffer sb = new StringBuffer ("CallMethodRule["); 610 sb.append("methodName="); 611 sb.append(methodName); 612 sb.append(", paramCount="); 613 sb.append(paramCount); 614 sb.append(", paramTypes={"); 615 if (paramTypes != null) { 616 for (int i = 0; i < paramTypes.length; i++) { 617 if (i > 0) { 618 sb.append(", "); 619 } 620 sb.append(paramTypes[i].getName()); 621 } 622 } 623 sb.append("}"); 624 sb.append("]"); 625 return (sb.toString()); 626 627 } 628 629 630 } 631 | Popular Tags |