import java.io.*;
import java.util.*;
+import java.lang.reflect.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import javax.xml.parsers.ParserConfigurationException;
}
saxContext.push(tag+":"+name);
matchedCount += checkRequiredTag(saxContext);
- /////
+
+ //finally try to set the property
+ try {
+ setProperty(classN, name, value);
+ } catch (Exception e) {
+ throw new SAXParseException(e.toString(), locator);
+ }
}
protected void finished() {
return new String(chars);
}
+ /** Find a method with the right name
+ * If found, call the method ( if param is int or boolean we'll convert
+ * value to the right type before) - that means you can have setDebug(1).
+ */
+ static void setProperty( Class classN, String name, String value )
+ throws Exception {
+
+ String setter= "set" +capitalize(name);
+
+ try {
+ Method methods[]=classN.getMethods();
+ Method setPropertyMethod=null;
+
+ // First, the ideal case - a setFoo( String ) method
+ for( int i=0; i< methods.length; i++ ) {
+ Class paramT[]=methods[i].getParameterTypes();
+ if( setter.equals( methods[i].getName() ) &&
+ paramT.length == 1 &&
+ "java.lang.String".equals( paramT[0].getName())) {
+
+ methods[i].invoke( null, new Object[] { value } );
+ return;
+ }
+ } //end for
+
+ // Try a setFoo ( int ) or ( boolean )
+ for( int i=0; i< methods.length; i++ ) {
+ boolean ok=true;
+ if( setter.equals( methods[i].getName() ) &&
+ methods[i].getParameterTypes().length == 1) {
+
+ // match - find the type and invoke it
+ Class paramType=methods[i].getParameterTypes()[0];
+ Object params[]=new Object[1];
+ if ("java.lang.Integer".equals( paramType.getName()) ||
+ "int".equals( paramType.getName())) {
+ try {
+ params[0]=new Integer(value);
+ } catch( NumberFormatException ex ) {ok=false;}
+ } else if ("java.lang.Boolean".equals( paramType.getName()) ||
+ "boolean".equals( paramType.getName())) {
+ params[0]=new Boolean(value);
+ } else {
+ throw new Exception("Unknown type " + paramType.getName() + "for property \""+name+"\"with value \""+value+"\"");
+ }
+
+ if( ok ) {
+ System.out.println("XXX: " + methods[i] + " " + classN + " " + params[0] );
+ methods[i].invoke( null, params );
+ return;
+ } //end if
+ } //end if setter
+ } //end for
+
+ //if we got this far it means we were not successful in setting the
+ //property
+ throw new Exception("Count not find method \""+setter+"\" in Class \""+classN.getName()+"\" in order to set property \""+name+"\"");
+
+ } catch( SecurityException ex1 ) {
+ throw new Exception("SecurityException for " + classN.getName() + " " + name + "=" + value +")" );
+ //if( ctx.getDebug() > 1 ) ex1.printStackTrace();
+ } catch (IllegalAccessException iae) {
+ throw new Exception("IllegalAccessException for " + classN.getName() + " " + name + "=" + value +")" );
+ //if( ctx.getDebug() > 1 ) iae.printStackTrace();
+ } catch (InvocationTargetException ie) {
+ throw new Exception("InvocationTargetException for " + classN.getName() + " " + name + "=" + value +")" );
+ //if( ctx.getDebug() > 1 ) ie.printStackTrace();
+ }
+ }
+
}