| Constructor: |
public CallMethodRule(String methodName) {
this(0, methodName, 0, (Class[]) null);
}
Construct a "call method" rule with the specified method name.
The method should accept no parameters. Parameters:
methodName - Method name of the parent method to call
|
public CallMethodRule(String methodName,
int paramCount) {
this(0, methodName, paramCount);
}
Construct a "call method" rule with the specified method name. The
parameter types (if any) default to java.lang.String. Parameters:
methodName - Method name of the parent method to call
paramCount - The number of parameters to collect, or
zero for a single argument from the body of this element.
|
public CallMethodRule(int targetOffset,
String methodName) {
this(targetOffset, methodName, 0, (Class[]) null);
}
Construct a "call method" rule with the specified method name.
The method should accept no parameters. Parameters:
targetOffset - location of the target object. Positive numbers are
relative to the top of the digester object stack. Negative numbers
are relative to the bottom of the stack. Zero implies the top
object on the stack.
methodName - Method name of the parent method to call
|
public CallMethodRule(Digester digester,
String methodName,
int paramCount) {
this(methodName, paramCount);
}
Construct a "call method" rule with the specified method name. The
parameter types (if any) default to java.lang.String. Parameters:
digester - The associated Digester
methodName - Method name of the parent method to call
paramCount - The number of parameters to collect, or
zero for a single argument from the body of this element.
|
public CallMethodRule(int targetOffset,
String methodName,
int paramCount) {
this.targetOffset = targetOffset;
this.methodName = methodName;
this.paramCount = paramCount;
if (paramCount == 0) {
this.paramTypes = new Class[] { String.class };
} else {
this.paramTypes = new Class[paramCount];
for (int i = 0; i < this.paramTypes.length; i++) {
this.paramTypes[i] = String.class;
}
}
}
Construct a "call method" rule with the specified method name. The
parameter types (if any) default to java.lang.String. Parameters:
targetOffset - location of the target object. Positive numbers are
relative to the top of the digester object stack. Negative numbers
are relative to the bottom of the stack. Zero implies the top
object on the stack.
methodName - Method name of the parent method to call
paramCount - The number of parameters to collect, or
zero for a single argument from the body of this element.
|
public CallMethodRule(String methodName,
int paramCount,
String[] paramTypes) {
this(0, methodName, paramCount, paramTypes);
}
Construct a "call method" rule with the specified method name and
parameter types. If paramCount is set to zero the rule
will use the body of this element as the single argument of the
method, unless paramTypes is null or empty, in this
case the rule will call the specified method with no arguments. Parameters:
methodName - Method name of the parent method to call
paramCount - The number of parameters to collect, or
zero for a single argument from the body of ths element
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 java.lang.Boolean
for a boolean parameter)
|
public CallMethodRule(String methodName,
int paramCount,
Class[] paramTypes) {
this(0, methodName, paramCount, paramTypes);
}
Construct a "call method" rule with the specified method name and
parameter types. If paramCount is set to zero the rule
will use the body of this element as the single argument of the
method, unless paramTypes is null or empty, in this
case the rule will call the specified method with no arguments. Parameters:
methodName - Method name of the parent method to call
paramCount - The number of parameters to collect, or
zero for a single argument from the body of ths element
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 java.lang.Boolean.TYPE
for a boolean parameter)
|
public CallMethodRule(Digester digester,
String methodName,
int paramCount,
String[] paramTypes) {
this(methodName, paramCount, paramTypes);
}
Construct a "call method" rule with the specified method name. Parameters:
digester - The associated Digester
methodName - Method name of the parent method to call
paramCount - The number of parameters to collect, or
zero for a single argument from the body of ths element
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 java.lang.Boolean
for a boolean parameter)
|
public CallMethodRule(Digester digester,
String methodName,
int paramCount,
Class[] paramTypes) {
this(methodName, paramCount, paramTypes);
}
Construct a "call method" rule with the specified method name. Parameters:
digester - The associated Digester
methodName - Method name of the parent method to call
paramCount - The number of parameters to collect, or
zero for a single argument from the body of ths element
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 java.lang.Boolean.TYPE
for a boolean parameter)
|
public CallMethodRule(int targetOffset,
String methodName,
int paramCount,
String[] paramTypes) {
this.targetOffset = targetOffset;
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] = String.class;
}
} else {
// copy the parameter class names into an array
// the classes will be loaded when the digester is set
this.paramClassNames = new String[paramTypes.length];
for (int i = 0; i < this.paramClassNames.length; i++) {
this.paramClassNames[i] = paramTypes[i];
}
}
}
Construct a "call method" rule with the specified method name and
parameter types. If paramCount is set to zero the rule
will use the body of this element as the single argument of the
method, unless paramTypes is null or empty, in this
case the rule will call the specified method with no arguments. Parameters:
targetOffset - location of the target object. Positive numbers are
relative to the top of the digester object stack. Negative numbers
are relative to the bottom of the stack. Zero implies the top
object on the stack.
methodName - Method name of the parent method to call
paramCount - The number of parameters to collect, or
zero for a single argument from the body of ths element
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 java.lang.Boolean
for a boolean parameter)
|
public CallMethodRule(int targetOffset,
String methodName,
int paramCount,
Class[] paramTypes) {
this.targetOffset = targetOffset;
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] = String.class;
}
} else {
this.paramTypes = new Class[paramTypes.length];
for (int i = 0; i < this.paramTypes.length; i++) {
this.paramTypes[i] = paramTypes[i];
}
}
}
Construct a "call method" rule with the specified method name and
parameter types. If paramCount is set to zero the rule
will use the body of this element as the single argument of the
method, unless paramTypes is null or empty, in this
case the rule will call the specified method with no arguments. Parameters:
targetOffset - location of the target object. Positive numbers are
relative to the top of the digester object stack. Negative numbers
are relative to the bottom of the stack. Zero implies the top
object on the stack.
methodName - Method name of the parent method to call
paramCount - The number of parameters to collect, or
zero for a single argument from the body of ths element
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 java.lang.Boolean.TYPE
for a boolean parameter)
|
| Method from org.apache.commons.digester.CallMethodRule Detail: |
public void begin(Attributes attributes) throws Exception {
// Push an array to capture the parameter values if necessary
if (paramCount > 0) {
Object parameters[] = new Object[paramCount];
for (int i = 0; i < parameters.length; i++) {
parameters[i] = null;
}
digester.pushParams(parameters);
}
}
Process the start of this element. |
public void body(String bodyText) throws Exception {
if (paramCount == 0) {
this.bodyText = bodyText.trim();
}
}
Process the body text of this element. |
public void end() throws Exception {
// Retrieve or construct the parameter values array
Object parameters[] = null;
if (paramCount > 0) {
parameters = (Object[]) digester.popParams();
if (digester.log.isTraceEnabled()) {
for (int i=0,size=parameters.length;i< size;i++) {
digester.log.trace("[CallMethodRule](" + i + ")" + parameters[i]) ;
}
}
// In the case where the target method takes a single parameter
// and that parameter does not exist (the CallParamRule never
// executed or the CallParamRule was intended to set the parameter
// from an attribute but the attribute wasn't present etc) then
// skip the method call.
//
// This is useful when a class has a "default" value that should
// only be overridden if data is present in the XML. I don't
// know why this should only apply to methods taking *one*
// parameter, but it always has been so we can't change it now.
if (paramCount == 1 && parameters[0] == null) {
return;
}
} else if (paramTypes != null && paramTypes.length != 0) {
// Having paramCount == 0 and paramTypes.length == 1 indicates
// that we have the special case where the target method has one
// parameter being the body text of the current element.
// There is no body text included in the source XML file,
// so skip the method call
if (bodyText == null) {
return;
}
parameters = new Object[1];
parameters[0] = bodyText;
if (paramTypes.length == 0) {
paramTypes = new Class[1];
paramTypes[0] = String.class;
}
} else {
// When paramCount is zero and paramTypes.length is zero it
// means that we truly are calling a method with no parameters.
// Nothing special needs to be done here.
;
}
// Construct the parameter values array we will need
// We only do the conversion if the param value is a String and
// the specified paramType is not String.
Object paramValues[] = new Object[paramTypes.length];
for (int i = 0; i < paramTypes.length; i++) {
// convert nulls and convert stringy parameters
// for non-stringy param types
if(
parameters[i] == null ||
(parameters[i] instanceof String &&
!String.class.isAssignableFrom(paramTypes[i]))) {
paramValues[i] =
ConvertUtils.convert((String) parameters[i], paramTypes[i]);
} else {
paramValues[i] = parameters[i];
}
}
// Determine the target object for the method call
Object target;
if (targetOffset >= 0) {
target = digester.peek(targetOffset);
} else {
target = digester.peek( digester.getCount() + targetOffset );
}
if (target == null) {
StringBuffer sb = new StringBuffer();
sb.append("[CallMethodRule]{");
sb.append(digester.match);
sb.append("} Call target is null (");
sb.append("targetOffset=");
sb.append(targetOffset);
sb.append(",stackdepth=");
sb.append(digester.getCount());
sb.append(")");
throw new org.xml.sax.SAXException(sb.toString());
}
// Invoke the required method on the top object
if (digester.log.isDebugEnabled()) {
StringBuffer sb = new StringBuffer("[CallMethodRule]{");
sb.append(digester.match);
sb.append("} Call ");
sb.append(target.getClass().getName());
sb.append(".");
sb.append(methodName);
sb.append("(");
for (int i = 0; i < paramValues.length; i++) {
if (i > 0) {
sb.append(",");
}
if (paramValues[i] == null) {
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.debug(sb.toString());
}
Object result = null;
if (useExactMatch) {
// invoke using exact match
result = MethodUtils.invokeExactMethod(target, methodName,
paramValues, paramTypes);
} else {
// invoke using fuzzier match
result = MethodUtils.invokeMethod(target, methodName,
paramValues, paramTypes);
}
processMethodCallResult(result);
}
Process the end of this element. |
public void finish() throws Exception {
bodyText = null;
}
Clean up after parsing is complete. |
public boolean getUseExactMatch() {
// --------------------------------------------------------- Public Methods
return useExactMatch;
}
Should MethodUtils.invokeExactMethod
be used for the reflection. |
protected void processMethodCallResult(Object result) {
// do nothing
}
Subclasses may override this method to perform additional processing of the
invoked method's result. |
public void setDigester(Digester digester) {
// call superclass
super.setDigester(digester);
// if necessary, load parameter classes
if (this.paramClassNames != null) {
this.paramTypes = new Class[paramClassNames.length];
for (int i = 0; i < this.paramClassNames.length; i++) {
try {
this.paramTypes[i] =
digester.getClassLoader().loadClass(this.paramClassNames[i]);
} catch (ClassNotFoundException e) {
// use the digester log
digester.getLogger().error("(CallMethodRule) Cannot load class " + this.paramClassNames[i], e);
this.paramTypes[i] = null; // Will cause NPE later
}
}
}
}
Set the associated digester.
If needed, this class loads the parameter classes from their names. |
public void setUseExactMatch(boolean useExactMatch) {
this.useExactMatch = useExactMatch;
}
Set whether MethodUtils.invokeExactMethod
should be used for the reflection. |
public String toString() {
StringBuffer sb = new StringBuffer("CallMethodRule[");
sb.append("methodName=");
sb.append(methodName);
sb.append(", paramCount=");
sb.append(paramCount);
sb.append(", paramTypes={");
if (paramTypes != null) {
for (int i = 0; i < paramTypes.length; i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(paramTypes[i].getName());
}
}
sb.append("}");
sb.append("]");
return (sb.toString());
}
Render a printable version of this Rule. |