[jboss-svn-commits] JBoss Common SVN: r2564 - jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Sep 20 16:01:54 EDT 2007


Author: scott.stark at jboss.org
Date: 2007-09-20 16:01:54 -0400 (Thu, 20 Sep 2007)
New Revision: 2564

Modified:
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/DefaultSchemaResolver.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaResolverConfig.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaResolverConfigMBean.java
Log:
JBXB-108, add support for namespace to class binding mappings

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/DefaultSchemaResolver.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/DefaultSchemaResolver.java	2007-09-20 20:01:00 UTC (rev 2563)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/DefaultSchemaResolver.java	2007-09-20 20:01:54 UTC (rev 2564)
@@ -25,9 +25,11 @@
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.WeakHashMap;
 
 import org.jboss.logging.Logger;
 import org.jboss.util.xml.JBossEntityResolver;
+import org.jboss.xb.builder.JBossXBBuilder;
 import org.w3c.dom.ls.LSInput;
 import org.xml.sax.InputSource;
 
@@ -45,9 +47,11 @@
    private String baseURI;
    private JBossEntityResolver resolver;
    private boolean cacheResolvedSchemas = true;
-   private Map schemasByUri = Collections.EMPTY_MAP;
-   private Map schemaInitByUri = Collections.EMPTY_MAP;
-   private Map schemaParseAnnotationsByUri = Collections.EMPTY_MAP;
+   private Map<String, SchemaBinding> schemasByUri = Collections.emptyMap();
+   /** Namespace to JBossXBBuilder binding class */
+   private WeakHashMap<String, Class> uriToClass = new WeakHashMap<String, Class>();
+   private Map<String, SchemaBindingInitializer> schemaInitByUri = Collections.emptyMap();
+   private Map<String, Boolean> schemaParseAnnotationsByUri = Collections.emptyMap();
 
    public DefaultSchemaResolver()
    {
@@ -79,7 +83,7 @@
       this.cacheResolvedSchemas = cacheResolvedSchemas;
       if(!cacheResolvedSchemas)
       {
-         schemasByUri = Collections.EMPTY_MAP;
+         schemasByUri = Collections.emptyMap();
       }
    }
 
@@ -126,7 +130,7 @@
             schemaParseAnnotationsByUri = Collections.singletonMap(nsUri, value);
             break;
          case 1:
-            schemaParseAnnotationsByUri = new HashMap(schemaParseAnnotationsByUri);
+            schemaParseAnnotationsByUri = new HashMap<String, Boolean>(schemaParseAnnotationsByUri);
          default:
             schemaParseAnnotationsByUri.put(nsUri, value);
       }
@@ -142,9 +146,9 @@
    {
       if (nsUri == null)
          throw new IllegalArgumentException("Null namespace uri");
-      return (Boolean) schemaParseAnnotationsByUri.remove(nsUri);
+      return schemaParseAnnotationsByUri.remove(nsUri);
    }
-   
+
    /**
     * Registers a SchemaBindingInitializer for the namespace URI.
     * When the schema binding that corresponds to the namespace URI
@@ -190,7 +194,7 @@
             schemaInitByUri = Collections.singletonMap(nsUri, sbi);
             break;
          case 1:
-            schemaInitByUri = new HashMap(schemaInitByUri);
+            schemaInitByUri = new HashMap<String, SchemaBindingInitializer>(schemaInitByUri);
          default:
             schemaInitByUri.put(nsUri, sbi);
       }
@@ -206,9 +210,18 @@
    {
       if (nsUri == null)
          throw new IllegalArgumentException("Null namespace uri");
-      return (SchemaBindingInitializer)schemaInitByUri.remove(nsUri);
+      return schemaInitByUri.remove(nsUri);
    }
 
+   public void addClassBinding(String nsUri, Class clazz)
+   {
+      uriToClass.put(nsUri, clazz);
+   }
+   public Class removeClassBinding(String nsUri)
+   {
+      return uriToClass.remove(nsUri);      
+   }
+
    public String getBaseURI()
    {
       return baseURI;
@@ -229,28 +242,40 @@
     */
    public SchemaBinding resolve(String nsURI, String baseURI, String schemaLocation)
    {
-      SchemaBinding schema = (SchemaBinding)schemasByUri.get(nsURI);
+      SchemaBinding schema = schemasByUri.get(nsURI);
       if(schema != null)
       {
          return schema;
       }
 
-      InputSource is = getInputSource(nsURI, baseURI, schemaLocation);
-      
-      if (is != null)
+      // Look for a class 
+      Class bindingClass = uriToClass.get(nsURI);
+      if (bindingClass != null)
       {
-         if( baseURI == null )
-            baseURI = this.baseURI;
-
-         Boolean processAnnotationsBoolean = (Boolean) schemaParseAnnotationsByUri.get(nsURI);
-         boolean processAnnotations = (processAnnotationsBoolean == null) ? true : processAnnotationsBoolean.booleanValue();
-         schema = XsdBinder.bind(is.getByteStream(), null, baseURI, processAnnotations);
+         if( log.isTraceEnabled() )
+            log.trace("resolve, nsURI="+nsURI+", baseURI="+baseURI+", class="+bindingClass);
+         schema = JBossXBBuilder.build(bindingClass);
       }
+      else
+      {
+         // Parse the schema
+         InputSource is = getInputSource(nsURI, baseURI, schemaLocation);
+         
+         if (is != null)
+         {
+            if( baseURI == null )
+               baseURI = this.baseURI;
+   
+            Boolean processAnnotationsBoolean = schemaParseAnnotationsByUri.get(nsURI);
+            boolean processAnnotations = (processAnnotationsBoolean == null) ? true : processAnnotationsBoolean.booleanValue();
+            schema = XsdBinder.bind(is.getByteStream(), null, baseURI, processAnnotations);
+         }
+      }
 
       if(schema != null)
       {
          schema.setSchemaResolver(this);
-         SchemaBindingInitializer sbi = (SchemaBindingInitializer)schemaInitByUri.get(nsURI);
+         SchemaBindingInitializer sbi = schemaInitByUri.get(nsURI);
          if(sbi != null)
          {
             schema = sbi.init(schema);
@@ -260,7 +285,7 @@
          {
             if(schemasByUri == Collections.EMPTY_MAP)
             {
-               schemasByUri = new HashMap();
+               schemasByUri = new HashMap<String, SchemaBinding>();
             }
             schemasByUri.put(nsURI, schema);
          }

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaResolverConfig.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaResolverConfig.java	2007-09-20 20:01:00 UTC (rev 2563)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaResolverConfig.java	2007-09-20 20:01:54 UTC (rev 2564)
@@ -32,6 +32,7 @@
  * SchemaResolverConfig.
  * 
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author Scott.Stark at jboss.org
  * @version $Revision$
  */
 public class SchemaResolverConfig implements SchemaResolverConfigMBean
@@ -51,6 +52,9 @@
    /** The parse annotations by namespace */
    protected Properties parseAnnotations;
 
+   /** The binding classes by namespace */
+   protected Properties bindingClasses;
+
    public Properties getSchemaInitializers()
    {
       return schemaInitializers;
@@ -118,4 +122,33 @@
          }
       }
    }
+
+   public Properties getBindingClasses()
+   {
+      return bindingClasses;
+   }
+
+   public void setBindingClasses(Properties bindingClasses)
+   {
+      this.bindingClasses = bindingClasses;
+      if (bindingClasses != null && bindingClasses.size() != 0)
+      {
+         ClassLoader loader = Thread.currentThread().getContextClassLoader();
+         for (Iterator i = bindingClasses.entrySet().iterator(); i.hasNext();)
+         {
+            Map.Entry entry = (Map.Entry) i.next();
+            String namespace = (String) entry.getKey();
+            String value = (String) entry.getValue();
+            try
+            {
+               Class clazz = loader.loadClass(value);
+               resolver.addClassBinding(namespace, clazz);
+            }
+            catch(ClassNotFoundException e)
+            {
+               log.warn("Failed to load class: "+value, e);
+            }
+         }
+      }
+   }
 }

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaResolverConfigMBean.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaResolverConfigMBean.java	2007-09-20 20:01:00 UTC (rev 2563)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaResolverConfigMBean.java	2007-09-20 20:01:54 UTC (rev 2564)
@@ -72,4 +72,18 @@
     * @param parseAnnotations the parseAnnotations.
     */
    void setParseAnnotations(Properties parseAnnotations);
+
+   /**
+    * Get the JBossXBBuilder namespace to Class mappings.
+    * 
+    * @return bindingClasses
+    */
+   public Properties getBindingClasses();
+
+   /**
+    * Set the JBossXBBuilder namespace to Class mappings.
+    * 
+    * @param bindingClasses
+    */
+   public void setBindingClasses(Properties bindingClasses);
 }




More information about the jboss-svn-commits mailing list