Ok, big merge. here's the new xml-config stuff in action. There's a few
[mir.git] / source / mir / xml / XmlConfigurator.java
index 4b08487..101d659 100755 (executable)
@@ -23,6 +23,9 @@ import mir.misc.Location;
  * classes as well as the inclusion of a method to
  * add parameters (nested tags) that are required.
  * (the addRequired method) in the config file.
+ * that part is from tomcat.
+ *
+ * much code is stolen from ant ProjectHelper.java.
  *
  * @author -mh <heckmann@hbe.ca>
  * @version 2001.10.21
@@ -45,6 +48,15 @@ public class XmlConfigurator {
     boolean matched[] = new boolean[256];
     int matchedCount=0;
 
+    XmlMatch mustComeFirstMatch[]=new XmlMatch[256]; //maximum amount of rules
+    int comeFirstMatchCount=0;
+
+    Property comesFirstArr[]=new Property[128];
+    int comesFirstCount=0;
+
+    Property propertyArr[]=new Property[128];
+    int propertyCount=0;
+
     private static XmlConfigurator instance = new XmlConfigurator();
     public static XmlConfigurator getInstance() { return instance; }
 
@@ -97,7 +109,18 @@ public class XmlConfigurator {
                         throw new ConfigException("Error parsing config file, missing required element: "+requiredXmlMatch[i].toString());
                 }
             }
-        }
+            try {
+                for(int i=0; i<comesFirstCount;i++) {
+                    comesFirstArr[i].set();
+                }
+                for(int i=0; i<propertyCount;i++) {
+                    propertyArr[i].set();
+                    System.out.println("about to set: "+i);
+                }
+            } catch (Exception e) {
+                throw new SAXParseException(e.toString(), locator);
+            }
+}
         catch(ParserConfigurationException exc) {
             throw new ConfigException("Parser has not been configured correctly", exc);
         }
@@ -339,7 +362,7 @@ public class XmlConfigurator {
             saxContext.push(tag+":"+name);
             matchedCount += checkRequiredTag(saxContext);
             try {
-                classN=Class.forName(name);
+                classN=Class.forName(name, false, this.getClass().getClassLoader());
             } catch (ClassNotFoundException e) {
                 throw new ConfigException("Error invoking class: \""+name+
                     "\"",e);
@@ -405,11 +428,14 @@ public class XmlConfigurator {
             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);
+            //finally add it to the lists
+            //to be processed later
+            if (checkComesFirstTag(saxContext)) {
+                comesFirstArr[comesFirstCount]=new Property(classN, name, value);
+                comesFirstCount++;
+            } else {
+                propertyArr[propertyCount]=new Property(classN, name, value);
+                propertyCount++;
             }
         }
 
@@ -421,6 +447,20 @@ public class XmlConfigurator {
 
     }
 
+    public void addComesFirstTag(String xmlPath) {
+        mustComeFirstMatch[comeFirstMatchCount]=new XmlMatch(xmlPath);
+        comeFirstMatchCount++;
+    }
+
+    private boolean checkComesFirstTag(SaxContext ctx) {
+        for( int i=0; i<comeFirstMatchCount; i++ ) {
+            if( mustComeFirstMatch[i].match(ctx) ) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     public void addRequiredTag(String xmlPath) {
         requiredXmlMatch[requiredXmlMatchCount]=new XmlMatch(xmlPath);
         matched[requiredXmlMatchCount]=false;
@@ -448,73 +488,89 @@ public class XmlConfigurator {
         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);
+    private class Property {
+        Class classN;
+        String name;
+        String value;
 
-        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+"\"");
-                    }
+        public Property( Class classN, String name, String value) {
+            this.classN=classN;
+            this.name=name;
+            this.value=value;
+        }
+            
+        /** 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).
+         */
+        public void set() throws Exception {
+            
+            String setter= "set" +capitalize(name);
 
-                    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();
+            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 ), (float) 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.Float".equals( paramType.getName()) ||
+                            "float".equals( paramType.getName())) {
+                            try {
+                                params[0]=new Float(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();
+            }
         }
     }