Update of /cvsroot/jaxme/JaxMe2/src/net/sf/jaxme/util
In directory sc8-pr-cvs1:/tmp/cvs-serv32423/src/net/sf/jaxme/util

Modified Files:
        Configurator.java 
Log Message:
Added the events framework to support the development of
SchemaReaders.


Index: Configurator.java
===================================================================
RCS file: /cvsroot/jaxme/JaxMe2/src/net/sf/jaxme/util/Configurator.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- Configurator.java   2 Feb 2003 23:51:04 -0000       1.4
+++ Configurator.java   11 Mar 2003 20:54:43 -0000      1.5
@@ -10,6 +10,7 @@
 
 import net.sf.jaxme.javasource.JavaQName;
 import net.sf.jaxme.javasource.JavaQNameImpl;
+import org.w3c.dom.Node;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.Locator;
@@ -45,32 +46,43 @@
   private static final Class[] oneClassNamespaceResolver = new 
Class[]{NamespaceResolver.class};
   private final Object[] oneObjectNamespaceResolver = new Object[]{this};
 
-  private String namespace;
+  private String[] namespaces;
   private List beans = new ArrayList();
   private List names = new ArrayList();
   private Object currentBean;
   private String currentName;
   private Locator locator;
   private Object beanFactory;
+  private Object rootObject;
   private Object resultBean;
   private int ignoreLevel = 0;
   private NamespaceSupport nss = new NamespaceSupport();
   boolean nssNeedsContext;
 
   /** <p>Sets the namespace handled by the configurator. Defaults
-   * to no namespace.</p>
+   * to no namespace. Shortcut for
+   * <code>setNamespace(new String[]{pNamespace})</code>.</p>
    *
    * @param pNamespace The namespace being set
    */
   public void setNamespace(String pNamespace) {
-    namespace = pNamespace;
+    setNamespaces(new String[]{pNamespace});
   }
 
-  /** <p>Returns the namespace handled by the configurator. Defaults
+  /** <p>Sets the namespaces handled by the configurator. Defaults
    * to no namespace.</p>
+   *
+   * @param pNamespace The namespaces being set
    */
-  public String getNamespace() {
-    return namespace;
+  public void setNamespaces(String[] pNamespaces) {
+    namespaces = pNamespaces;
+  }
+
+  /** <p>Returns the namespaces handled by the configurator. Defaults
+   * to no namespace.</p>
+   */
+  public String[] getNamespaces() {
+    return namespaces;
   }
 
   /** <p>Sets the Locator being used in error messages.</p>
@@ -102,6 +114,20 @@
     return beanFactory == null ? this : beanFactory;
   }
 
+  /** <p>An alternative to using the bean factory. This object
+   * is used as the root object, regardless of its name.</p>
+   */
+  public void setRootObject(Object pRootObject) {
+    rootObject = pRootObject;
+  }
+
+  /** <p>An alternative to using the bean factory. This object
+   * is used as the root object, regardless of its name.</p>
+   */
+  public Object getRootObject() {
+    return rootObject;
+  }
+
   public void startDocument() throws SAXException {
     currentBean = null;
     currentName = null;
@@ -126,11 +152,23 @@
   /** <p>Returns whether a namespace is matching the configured
    * namespace.</p>
   */
   protected boolean isNamespaceMatching(String pNamespace) {
-    String myNamespace = getNamespace();
-    if (pNamespace == null  ||  pNamespace.length() == 0) {
-      return myNamespace == null  ||  myNamespace.length() == 0;
+    if (pNamespace == null) {
+      pNamespace = "";
+    }
+    String[] myNamespaces = getNamespaces();
+    if (myNamespaces == null  ||  myNamespaces.length == 0) {
+      return pNamespace.length() == 0;
     } else {
-      return pNamespace.equals(myNamespace);
+      for (int i = 0;  i < myNamespaces.length;  i++) {
+        String s = myNamespaces[i];
+        if (s == null) {
+          s = "";
+        }
+        if (s.equals(pNamespace)) {
+          return true;
+        }
+      }
+      return false;
     }
   }
 
@@ -171,54 +209,70 @@
     
     if (!isNamespaceMatching(pNamespaceURI)) {
       if (currentBean == null) {
-        if (getNamespace() == null  ||  getNamespace().length() == 0) {
+        String[] namespaces = getNamespaces();
+        if (namespaces == null  ||  namespaces.length == 0) {
           throw new SAXParseException("The document element must have the default 
namespace.",
                                        getDocumentLocator());
         } else {
-          throw new SAXParseException("The document element must have the namespace " 
+ getNamespace() + ".",
-                                       getDocumentLocator());
+          StringBuffer sb = new StringBuffer("The document element must have either 
of the namespaces: ");
+          for (int i = 0;  i < namespaces.length;  i++) {
+            if (i > 0) sb.append(",");
+            String s = namespaces[i];
+            if (s == null) {
+              s = "";
+            } 
+            sb.append('"').append(s).append("'");
+          }
+          throw new SAXParseException(sb.toString(), getDocumentLocator());
         }
       }
 
       ++ignoreLevel;
       return; // Namespace not matching, ignore this element
     }
-    Object factory = (currentBean == null) ? beanFactory : currentBean;
-    String methodName = getMethodNameFor("create", pLocalName);
-    Object o = null;
-    try {
-      o = invokeMethod(methodName, factory, oneClassAttributes,
-                       new Object[]{pAttr});
-    } catch (SAXParseException e) {
-      if (e.getException() != null  &&
-          e.getException() instanceof NoSuchMethodException) {
-        try {
-          o = invokeMethod(methodName, factory, zeroClasses, zeroObjects);
-          e = null;
-        } catch (SAXParseException f) {
-          if (f.getException() != null  &&
-              f.getException() instanceof NoSuchMethodException) {
-            if (currentBean == null) {
-              throw new SAXParseException("Invalid document element: " + pQName,
-                                           getDocumentLocator());
-            } else {
-              throw new SAXParseException("Invalid child element name: " + pQName,
-                                           getDocumentLocator());
+
+    Object o;
+    if (currentBean == null  &&  rootObject != null) {
+      o = rootObject;
+    } else {
+      o = null;
+      Object factory = (currentBean == null) ? beanFactory : currentBean;
+      String methodName = getMethodNameFor("create", pLocalName);
+      try {
+        o = invokeMethod(methodName, factory, oneClassAttributes,
+                         new Object[]{pAttr});
+      } catch (SAXParseException e) {
+        if (e.getException() != null  &&
+            e.getException() instanceof NoSuchMethodException) {
+          try {
+            o = invokeMethod(methodName, factory, zeroClasses, zeroObjects);
+            e = null;
+          } catch (SAXParseException f) {
+            if (f.getException() != null  &&
+                f.getException() instanceof NoSuchMethodException) {
+              if (currentBean == null) {
+                throw new SAXParseException("Invalid document element: " + pQName,
+                                             getDocumentLocator());
+              } else {
+                throw new SAXParseException("Invalid child element name: " + pQName,
+                                             getDocumentLocator());
+              }
             }
+            throw f;
           }
-          throw f;
+        }
+        if (e != null) {
+           throw e;
         }
       }
-      if (e != null) {
-         throw e;
+      if (o == null) {
+        throw new SAXParseException("Method " + methodName + " of class " +
+                                     factory.getClass().getName() +
+                                     " did not return an object.",
+                                     getDocumentLocator());
       }
     }
-    if (o == null) {
-      throw new SAXParseException("Method " + methodName + " of class " +
-                                   factory.getClass().getName() +
-                                   " did not return an object.",
-                                   getDocumentLocator());
-    }
+
     if (currentBean == null) {
       resultBean = o;
     } else {
@@ -497,5 +551,30 @@
         return true;
       }
     }
+  }
+
+  public static void parse(Node pNode, Object pObject) throws SAXException {
+    Configurator configurator = new Configurator();
+    configurator.setRootObject(pObject);
+    DOMSerializer ds = new DOMSerializer();
+    ds.serialize(pNode, configurator);
+  }
+
+  public static void parse(Node pNode, Object pObject, String pNamespace)
+      throws SAXException {
+    Configurator configurator = new Configurator();
+    configurator.setRootObject(pObject);
+    configurator.setNamespace(pNamespace);
+    DOMSerializer ds = new DOMSerializer();
+    ds.serialize(pNode, configurator);
+  }
+
+  public static void parse(Node pNode, Object pObject, String[] pNamespaces)
+      throws SAXException {
+    Configurator configurator = new Configurator();
+    configurator.setRootObject(pObject);
+    configurator.setNamespaces(pNamespaces);
+    DOMSerializer ds = new DOMSerializer();
+    ds.serialize(pNode, configurator);
   }
 }




-------------------------------------------------------
This SF.net email is sponsored by:Crypto Challenge is now open! 
Get cracking and register here for some mind boggling fun and 
the chance of winning an Apple iPod:
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
_______________________________________________
Jaxme-jaxb-dev mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jaxme-jaxb-dev

Reply via email to