added the reflection stuff to call the set* method in each class to set a property
authormh <mh>
Sat, 27 Oct 2001 17:16:00 +0000 (17:16 +0000)
committermh <mh>
Sat, 27 Oct 2001 17:16:00 +0000 (17:16 +0000)
source/mir/xml/XmlConfigurator.java

index c82f223..4b08487 100755 (executable)
@@ -2,6 +2,7 @@ package mir.xml;
 
 import java.io.*;
 import java.util.*;
 
 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;
 import org.xml.sax.helpers.DefaultHandler;
 import org.xml.sax.*;
 import javax.xml.parsers.ParserConfigurationException;
@@ -403,7 +404,13 @@ public class XmlConfigurator {
             }
             saxContext.push(tag+":"+name);
             matchedCount += checkRequiredTag(saxContext);
             }
             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() {
         }
 
         protected void finished() {
@@ -441,4 +448,74 @@ public class XmlConfigurator {
         return new String(chars);
     }
 
         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();
+        }
+    }
+
 }
 }