craigmcc 01/02/13 15:53:37 Modified: src/share/org/apache/struts/digester CallMethodRule.java Digester.java Log: Enhance the configuration of a CallMethod rule so that you can pass an array of Class objects, instead of an array of class names. Among other things, this lets you create rules to call methods that accept parameters that are a Java primitive (like int or boolean): Digester digester = new Digester(); ... Class types[] = new Class[1]; types[0] = Integer.TYPE; // Class representing a Java int digester.addCallMethod("...pattern...", "setIntegerProperty", 1, types); digester.addCallParam("...pattern...", 0); // Converts nested body // content to an int and // calls setIntegerProperty() Submitted by: Yuhong Guo <[EMAIL PROTECTED]> PR: Bugzilla #590 (I will attach a sample program and XML file to this bug report if you want something a little more complete than the above.) Revision Changes Path 1.7 +56 -26 jakarta-struts/src/share/org/apache/struts/digester/CallMethodRule.java Index: CallMethodRule.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/digester/CallMethodRule.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- CallMethodRule.java 2001/01/23 03:35:52 1.6 +++ CallMethodRule.java 2001/02/13 23:53:36 1.7 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/digester/CallMethodRule.java,v 1.6 2001/01/23 03:35:52 craigmcc Exp $ - * $Revision: 1.6 $ - * $Date: 2001/01/23 03:35:52 $ + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/digester/CallMethodRule.java,v 1.7 2001/02/13 23:53:36 craigmcc Exp $ + * $Revision: 1.7 $ + * $Date: 2001/02/13 23:53:36 $ * * ==================================================================== * @@ -75,7 +75,7 @@ * element. * * @author Craig McClanahan - * @version $Revision: 1.6 $ $Date: 2001/01/23 03:35:52 $ + * @version $Revision: 1.7 $ $Date: 2001/02/13 23:53:36 $ */ public final class CallMethodRule extends Rule { @@ -96,7 +96,7 @@ public CallMethodRule(Digester digester, String methodName, int paramCount) { - this(digester, methodName, paramCount, null); + this(digester, methodName, paramCount, (Class[]) null); } @@ -120,25 +120,50 @@ this.methodName = methodName; this.paramCount = paramCount; if (paramTypes == null) { - if (this.paramCount == 0) - this.paramTypes = new Class[1]; - else - this.paramTypes = new Class[this.paramCount]; - for (int i = 0; i < this.paramTypes.length; i++) { - if (i == 0) - this.paramTypes[i] = "abc".getClass(); - else - this.paramTypes[i] = this.paramTypes[0]; - } - } else { - this.paramTypes = new Class[paramTypes.length]; - for (int i = 0; i < this.paramTypes.length; i++) { - try { - this.paramTypes[i] = Class.forName(paramTypes[i]); - } catch (ClassNotFoundException e) { - this.paramTypes[i] = null; // Will trigger NPE later - } - } + this.paramTypes = new Class[paramCount]; + for (int i = 0; i < this.paramTypes.length; i++) + this.paramTypes[i] = "abc".getClass(); + } else { + this.paramTypes = new Class[paramTypes.length]; + for (int i = 0; i < this.paramTypes.length; i++) { + try { + this.paramTypes[i] = Class.forName(paramTypes[i]); + } catch (ClassNotFoundException e) { + this.paramTypes[i] = null; // Will cause NPE later + } + } + } + + } + + + /** + * Construct a "call method" rule with the specified method name. + * + * @param digester The associated Digester + * @param methodName Method name of the parent method to call + * @param paramCount The number of parameters to collect, or + * zero for a single argument from the body of ths element + * @param paramTypes The Java classes that represent the + * parameter types of the method arguments + * (if you wish to use a primitive type, specify the corresonding + * Java wrapper class instead, such as <code>java.lang.Boolean.TYPE</code> + * for a <code>boolean</code> parameter) + */ + public CallMethodRule(Digester digester, String methodName, + int paramCount, Class paramTypes[]) { + + super(digester); + this.methodName = methodName; + this.paramCount = paramCount; + if (paramTypes == null) { + this.paramTypes = new Class[paramCount]; + for (int i = 0; i < this.paramTypes.length; i++) + this.paramTypes[i] = "abc".getClass(); + } else { + this.paramTypes = new Class[paramTypes.length]; + for (int i = 0; i < this.paramTypes.length; i++) + this.paramTypes[i] = paramTypes[i]; } } @@ -224,9 +249,9 @@ // Construct the parameter values array we will need Object paramValues[] = new Object[paramTypes.length]; - for (int i = 0; i < this.paramTypes.length; i++) + for (int i = 0; i < paramTypes.length; i++) paramValues[i] = - ConvertUtils.convert(parameters[i], this.paramTypes[i]); + ConvertUtils.convert(parameters[i], paramTypes[i]); // Invoke the required method on the top object Object top = digester.peek(); @@ -243,6 +268,11 @@ sb.append("null"); else sb.append(paramValues[i].toString()); + sb.append("/"); + if (paramTypes[i] == null) + sb.append("null"); + else + sb.append(paramTypes[i].getName()); } sb.append(")"); digester.log(sb.toString()); 1.15 +26 -4 jakarta-struts/src/share/org/apache/struts/digester/Digester.java Index: Digester.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/digester/Digester.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- Digester.java 2001/01/23 03:35:52 1.14 +++ Digester.java 2001/02/13 23:53:36 1.15 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/digester/Digester.java,v 1.14 2001/01/23 03:35:52 craigmcc Exp $ - * $Revision: 1.14 $ - * $Date: 2001/01/23 03:35:52 $ + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/digester/Digester.java,v 1.15 2001/02/13 23:53:36 craigmcc Exp $ + * $Revision: 1.15 $ + * $Date: 2001/02/13 23:53:36 $ * * ==================================================================== * @@ -102,7 +102,7 @@ * even from the same thread.</p> * * @author Craig McClanahan - * @version $Revision: 1.14 $ $Date: 2001/01/23 03:35:52 $ + * @version $Revision: 1.15 $ $Date: 2001/02/13 23:53:36 $ */ public final class Digester extends HandlerBase { @@ -806,6 +806,28 @@ */ public void addCallMethod(String pattern, String methodName, int paramCount, String paramTypes[]) { + + addRule(pattern, + new CallMethodRule(this, methodName, + paramCount, paramTypes)); + + } + + + /** + * Add an "call method" rule for the specified parameters. + * + * @param pattern Element matching pattern + * @param methodName Method name to be called + * @param paramCount Number of expected parameters (or zero + * for a single parameter from the body of this element) + * @param paramTypes The Java class names of the arguments + * (if you wish to use a primitive type, specify the corresonding + * Java wrapper class instead, such as <code>java.lang.Boolean</code> + * for a <code>boolean</code> parameter) + */ + public void addCallMethod(String pattern, String methodName, + int paramCount, Class paramTypes[]) { addRule(pattern, new CallMethodRule(this, methodName,