[jboss-svn-commits] JBoss Common SVN: r4038 - in jbossxb/trunk/src: main/java/org/jboss/xb/binding/sunday/unmarshalling and 3 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Feb 17 06:01:46 EST 2010


Author: alex.loubyansky at jboss.com
Date: 2010-02-17 06:01:46 -0500 (Wed, 17 Feb 2010)
New Revision: 4038

Modified:
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/Util.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/CollectionRepeatableParticleHandler.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java
   jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java
   jbossxb/trunk/src/main/java/org/jboss/xb/builder/runtime/CollectionPropertyHandler.java
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/type/xmlanyelement/test/ListElementWildcardUnitTestCase.java
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/type/xmlanyelement/test/ListObjectWildcardUnitTestCase.java
Log:
make use of repeatable particle handlers for collection types

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/Util.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/Util.java	2010-02-16 04:59:54 UTC (rev 4037)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/Util.java	2010-02-17 11:01:46 UTC (rev 4038)
@@ -25,6 +25,7 @@
 import java.io.Reader;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.util.Iterator;
 import java.util.StringTokenizer;
 
 import javax.xml.XMLConstants;
@@ -37,7 +38,11 @@
 import org.jboss.logging.Logger;
 import org.jboss.util.Classes;
 import org.jboss.xb.binding.sunday.unmarshalling.LSInputAdaptor;
+import org.jboss.xb.binding.sunday.unmarshalling.ModelGroupBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
+import org.jboss.xb.binding.sunday.unmarshalling.TermBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.WildcardBinding;
 import org.jboss.xb.binding.sunday.unmarshalling.XsdBinderTerminatingErrorHandler;
 import org.w3c.dom.DOMConfiguration;
 import org.w3c.dom.DOMErrorHandler;
@@ -759,4 +764,29 @@
          }
       };
    }
+   
+   public static WildcardBinding getWildcard(TermBinding term)
+   {
+      if(term.isWildcard())
+         return (WildcardBinding) term;
+      
+      if(term.isModelGroup())
+      {
+         ModelGroupBinding group = (ModelGroupBinding) term;
+         for(Iterator<ParticleBinding> i = group.getParticles().iterator(); i.hasNext();)
+         {
+            term = i.next().getTerm();
+            if(term.isWildcard())
+               return (WildcardBinding)term;
+            else if(term.isModelGroup())
+            {
+               WildcardBinding wc = getWildcard(term);
+               if (wc != null)
+                  return wc;
+            }
+         }
+      }
+      
+      return null;
+   }
 }

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/CollectionRepeatableParticleHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/CollectionRepeatableParticleHandler.java	2010-02-16 04:59:54 UTC (rev 4037)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/CollectionRepeatableParticleHandler.java	2010-02-17 11:01:46 UTC (rev 4038)
@@ -21,11 +21,16 @@
 */
 package org.jboss.xb.binding.sunday.unmarshalling;
 
-import java.util.ArrayList;
 import java.util.Collection;
 
 import javax.xml.namespace.QName;
 
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.TypeInfo;
+import org.jboss.xb.binding.JBossXBRuntimeException;
+import org.jboss.xb.builder.runtime.AbstractPropertyHandler;
+import org.jboss.xb.util.CollectionFactory;
+
 /**
  * 
  * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
@@ -33,11 +38,31 @@
  */
 public class CollectionRepeatableParticleHandler implements RepeatableParticleHandler
 {
-   public static final CollectionRepeatableParticleHandler INSTANCE = new CollectionRepeatableParticleHandler();
+   private AbstractPropertyHandler propertyHandler;
+   private CollectionFactory colFactory;
+   private ValueAdapter valueAdapter;
+   private TypeInfo componentType;
    
+   public CollectionRepeatableParticleHandler(AbstractPropertyHandler propertyHandler, ClassInfo collectionType, ValueAdapter valueAdapter)
+   {
+      if(propertyHandler == null)
+         throw new IllegalArgumentException("Null property handler.");
+      colFactory = CollectionFactory.getFactory(collectionType);
+      componentType = ((ClassInfo) collectionType).getComponentType();
+      this.valueAdapter = valueAdapter;
+      this.propertyHandler = propertyHandler;
+   }
+
    public Object startRepeatableParticle(Object parent, QName startName, ParticleBinding particle)
    {
-      return createCollection();
+      try
+      {
+         return colFactory.createCollection();
+      }
+      catch (Throwable e)
+      {
+         throw new JBossXBRuntimeException("Failed to create collection for " + startName, e);
+      }
    }
 
    public void endRepeatableParticle(Object parent, Object o, QName elementName, ParticleBinding particle, ParticleBinding parentParticle)
@@ -45,26 +70,20 @@
       if(o == null)
          return;
       
-      TermBinding term = particle.getTerm();
-      ParticleHandler handler = term.getHandler();
-      if(handler == null)
-         handler = DefaultHandlers.ELEMENT_HANDLER;
-      
-      ValueAdapter valueAdapter = term.getValueAdapter();
       if(valueAdapter != null)
          o = valueAdapter.cast(o, null);
       
-      handler.setParent(parent, o, elementName, particle, parentParticle);
+      propertyHandler.doHandle(parent, o, elementName);
    }
 
    public void addTermValue(Object particleValue, Object termValue, QName elementName,
          ParticleBinding particle, ParticleBinding parentParticle, ParticleHandler handler)
    {
-      ((Collection<Object>)particleValue).add(termValue);
+      if (componentType != null && termValue != null)
+      {
+         if(!componentType.isInstance(termValue))
+            throw new IllegalArgumentException("Child is not an instance of " + componentType + ", child: " + termValue);
+      }
+      ((Collection)particleValue).add(termValue);
    }
-   
-   protected Collection<Object> createCollection()
-   {
-      return new ArrayList<Object>();
-   }
 }
\ No newline at end of file

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java	2010-02-16 04:59:54 UTC (rev 4037)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java	2010-02-17 11:01:46 UTC (rev 4038)
@@ -38,6 +38,7 @@
 import org.jboss.xb.binding.metadata.PropertyMetaData;
 import org.jboss.xb.binding.metadata.ValueMetaData;
 import org.jboss.xb.binding.Constants;
+import org.jboss.xb.binding.Util;
 import org.jboss.xb.binding.sunday.marshalling.TermBeforeMarshallingCallback;
 import org.jboss.xb.binding.sunday.xop.XOPUnmarshaller;
 import org.jboss.xb.binding.sunday.xop.XOPMarshaller;
@@ -542,13 +543,11 @@
    public WildcardBinding getWildcard()
    {
       if(initializedWildcard)
-      {
          return wildcard;
-      }
       
       if(particle != null)
       {
-         wildcard = getWildcard(particle.getTerm());
+         wildcard = Util.getWildcard(particle.getTerm());
          initializedWildcard = true;
       }
       
@@ -698,33 +697,4 @@
    {
       return super.toString() + "[" + qName + "]";
    }
-
-   private static WildcardBinding getWildcard(TermBinding term)
-   {
-      if(term.isWildcard())
-      {
-         return (WildcardBinding) term;
-      }     
-      
-      if(term.isModelGroup())
-      {
-         ModelGroupBinding group = (ModelGroupBinding) term;
-         for(Iterator<ParticleBinding> i = group.getParticles().iterator(); i.hasNext();)
-         {
-            term = i.next().getTerm();
-            if(term.isWildcard())
-            {
-               return (WildcardBinding)term;
-            }
-            else if(term.isModelGroup())
-            {
-               WildcardBinding wc = getWildcard(term);
-               if (wc != null)
-                  return wc;
-            }
-         }
-      }
-      
-      return null;
-   }
 }

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java	2010-02-16 04:59:54 UTC (rev 4037)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java	2010-02-17 11:01:46 UTC (rev 4038)
@@ -95,6 +95,7 @@
 import org.jboss.xb.annotations.JBossXmlValue;
 import org.jboss.xb.binding.JBossXBRuntimeException;
 import org.jboss.xb.binding.SimpleTypeBindings;
+import org.jboss.xb.binding.Util;
 import org.jboss.xb.binding.sunday.unmarshalling.CollectionRepeatableParticleHandler;
 import org.jboss.xb.binding.sunday.unmarshalling.AllBinding;
 import org.jboss.xb.binding.sunday.unmarshalling.AnyAttributeBinding;
@@ -882,11 +883,13 @@
       {
          QName qName = generateXmlName(typeInfo, XmlNsForm.QUALIFIED, overrideNamespace, overrideName);
          typeBinding = new TypeBinding(qName, CharactersHandler.NOOP);
+         schemaBinding.addType(typeBinding);
       }
       else
       {
          typeBinding = new TypeBinding();
       }
+      typeBinding.setSchemaBinding(schemaBinding);
 
       // Push into the cache early to avoid recursion
       typeCache.put(typeInfo, typeBinding);
@@ -1165,17 +1168,6 @@
          typeParticle.setMaxOccursUnbounded(true);
       }
 
-      // Determine the wildcard handler
-      AbstractPropertyHandler wildcardHandler = null;
-      if (wildcardProperty != null)
-      {
-         TypeInfo wildcardType = wildcardProperty.getType();
-         if (wildcardType.isCollection())
-            wildcardHandler = new CollectionPropertyWildcardHandler(wildcardProperty, wildcardType);
-         else
-            wildcardHandler = new PropertyWildcardHandler(wildcardProperty, wildcardType);
-      }
-
       // Look through the properties
       for (String name : propertyOrder)
       {
@@ -1240,52 +1232,62 @@
       // Bind the wildcard
       if (wildcardProperty != null)
       {
-         if (trace)
-            log.trace("Processing WildcardProperty for type=" + beanInfo.getName() + " property=" + wildcardProperty.getName());
-         ModelGroupBinding localModel = model;
-         TypeInfo wildcardType = wildcardProperty.getType();
-         TypeInfo type = wildcardType;
-
-         WildcardBinding wildcard = new WildcardBinding(schemaBinding);
-         ParticleBinding particleBinding = new ParticleBinding(wildcard);
-         localModel.addParticle(particleBinding);
-         particleBinding.setMinOccurs(0);
-
-         // Setup any new model and determine the wildcard type
-         if (wildcardType.isArray())
+         AbstractPropertyHandler wildcardHandler;
+         WildcardBinding wildcard = Util.getWildcard(model);
+         if(wildcard == null)
          {
-            particleBinding.setMaxOccursUnbounded(true);
-            wildcard.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(wildcardHandler));
-            type = ((ArrayInfo) wildcardType).getComponentType();
             if (trace)
-               log.trace("Wildcard " + wildcardProperty.getName() + " is an array of type " + type.getName());
-         }
-         else if (wildcardType.isCollection())
-         {
-            particleBinding.setMaxOccursUnbounded(true);
-            type = ((ClassInfo)wildcardProperty.getType()).getComponentType();
-            if (trace)
-               log.trace("Wildcard " + wildcardProperty.getName() + " is a collection of type " + type.getName());
-         }
-         else
-            particleBinding.setMaxOccurs(1);
+               log.trace("Processing WildcardProperty for type=" + beanInfo.getName() + " property=" + wildcardProperty.getName());
+            ModelGroupBinding localModel = model;
+            TypeInfo wildcardType = wildcardProperty.getType();
+            TypeInfo type = wildcardType;
 
-         XmlAnyElement xmlAnyElement = wildcardProperty.getUnderlyingAnnotation(XmlAnyElement.class);
-         boolean isLax = xmlAnyElement == null ? true : xmlAnyElement.lax();
-         if (isLax)
-            wildcard.setProcessContents((short) 3); // Lax
-         else
-            wildcard.setProcessContents((short) 1); // Strict
+            wildcard = new WildcardBinding(schemaBinding);
+            ParticleBinding particleBinding = new ParticleBinding(wildcard);
+            localModel.addParticle(particleBinding);
+            particleBinding.setMinOccurs(0);
 
-         // Dom element?
-         if (Element.class.getName().equals(type.getName()))
-         {
-            wildcard.setUnresolvedElementHandler(DOMHandler.INSTANCE);
-            wildcard.setUnresolvedCharactersHandler(DOMHandler.INSTANCE);
+            wildcardHandler = new PropertyWildcardHandler(wildcardProperty, wildcardType);
+            wildcard.setHandler((ParticleHandler) wildcardHandler);
+            
+            // Setup any new model and determine the wildcard type
+            if (wildcardType.isArray())
+            {
+               particleBinding.setMaxOccursUnbounded(true);
+               wildcard.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(wildcardHandler));
+               type = ((ArrayInfo) wildcardType).getComponentType();
+               if (trace)
+                  log.trace("Wildcard " + wildcardProperty.getName() + " is an array of type " + type.getName());
+            }
+            else if (wildcardType.isCollection())
+            {
+               particleBinding.setMaxOccursUnbounded(true);
+               wildcard.setRepeatableHandler(new CollectionRepeatableParticleHandler(wildcardHandler, (ClassInfo) wildcardType, null));
+               type = ((ClassInfo)wildcardProperty.getType()).getComponentType();
+               if (trace)
+                  log.trace("Wildcard " + wildcardProperty.getName() + " is a collection of type " + type.getName());
+            }
+            else
+               particleBinding.setMaxOccurs(1);
+
+            XmlAnyElement xmlAnyElement = wildcardProperty.getUnderlyingAnnotation(XmlAnyElement.class);
+            boolean isLax = xmlAnyElement == null ? true : xmlAnyElement.lax();
+            if (isLax)
+               wildcard.setProcessContents((short) 3); // Lax
+            else
+               wildcard.setProcessContents((short) 1); // Strict
+
+            // Dom element?
+            if (Element.class.getName().equals(type.getName()))
+            {
+               wildcard.setUnresolvedElementHandler(DOMHandler.INSTANCE);
+               wildcard.setUnresolvedCharactersHandler(DOMHandler.INSTANCE);
+            }            
          }
-
-         wildcard.setHandler((ParticleHandler) wildcardHandler);
-         beanAdapterFactory.setWildcardHandler(wildcardHandler);         
+         else
+            wildcardHandler = (AbstractPropertyHandler) wildcard.getHandler();
+         
+         beanAdapterFactory.setWildcardHandler(wildcardHandler);
       }
 
       JBossXmlChildWildcard childWildcard = typeInfo.getUnderlyingAnnotation(JBossXmlChildWildcard.class);
@@ -1325,12 +1327,6 @@
       if (trace)
          log.trace("Created type=" + typeInfo.getName() + " typeBinding=" + typeBinding + " rootType=" + root);
 
-      // Register as root if required
-      if (root)
-         schemaBinding.addType(typeBinding);
-      else
-         typeBinding.setSchemaBinding(schemaBinding);
-
       return typeBinding;
    }
 
@@ -1364,7 +1360,7 @@
       if (xmlTransient != null && propertyOrder != null)
          throw new RuntimeException("Property " + property.getName() + " in property order "
                + Arrays.asList(propertyOrder) + " is marked @XmlTransient");
-
+      
       // The current model
       ModelGroupBinding localModel = parentModel;
 
@@ -1450,6 +1446,7 @@
       }
 
       // Setup a choice
+      boolean repeatableChoice = false;
       if (elements.length > 1)
       {
          ChoiceBinding choice = new ChoiceBinding(schemaBinding);
@@ -1459,17 +1456,79 @@
          // WARN normally maxOccursUnbounded should be set to true in this case
          // but I make an exception for case like in org.jboss.test.xb.builder.repeatableterms.support.Sequence
          if(propertyType.isCollection() || propertyType.isArray())
+         {
             particleBinding.setMaxOccursUnbounded(true);
+            repeatableChoice = true;
+         }
          localModel.addParticle(particleBinding);
          localModel = choice;
 
-         if(xmlWrapper == null && propertyType.isArray())
-            choice.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(new PropertyHandler(property, propertyType)));
+         if(xmlWrapper == null)
+         {
+            if(propertyType.isCollection())
+               choice.setRepeatableHandler(new CollectionRepeatableParticleHandler(new PropertyHandler(property, propertyType), (ClassInfo) propertyType, null));
+            else if(propertyType.isArray())
+               choice.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(new PropertyHandler(property, propertyType)));
+         }
 
          if (trace)
             log.trace("XmlElements seen adding choice for type=" + property.getBeanInfo().getName() + " property=" + property.getName());
       }
 
+      // Bind the wildcard
+      if (wildcardProperty)
+      {
+         if (trace)
+            log.trace("Processing WildcardProperty for property=" + property.getName());
+
+         WildcardBinding wildcard = new WildcardBinding(schemaBinding);
+         ParticleBinding particleBinding = new ParticleBinding(wildcard);
+         localModel.addParticle(particleBinding);
+         particleBinding.setMinOccurs(0);
+
+         AbstractPropertyHandler wildcardHandler = new PropertyWildcardHandler(property, propertyType);
+
+         // Setup any new model and determine the wildcard type
+         TypeInfo wildcardType = propertyType;
+         if (propertyType.isArray())
+         {
+            if(!repeatableChoice)
+               particleBinding.setMaxOccursUnbounded(true);
+            wildcard.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(wildcardHandler));
+            wildcardType = ((ArrayInfo) propertyType).getComponentType();
+            if (trace)
+               log.trace("Wildcard " + property.getName() + " is an array of type " + wildcardType.getName());
+         }
+         else if (propertyType.isCollection())
+         {
+            if(!repeatableChoice)
+               particleBinding.setMaxOccursUnbounded(true);
+            wildcard.setRepeatableHandler(new CollectionRepeatableParticleHandler(wildcardHandler, (ClassInfo) propertyType, null));
+            wildcardType = ((ClassInfo)property.getType()).getComponentType();
+            if (trace)
+               log.trace("Wildcard " + property.getName() + " is a collection of type " + wildcardType.getName());
+         }
+         else
+            particleBinding.setMaxOccurs(1);
+
+         XmlAnyElement xmlAnyElement = property.getUnderlyingAnnotation(XmlAnyElement.class);
+         boolean isLax = xmlAnyElement == null ? true : xmlAnyElement.lax();
+         if (isLax)
+            wildcard.setProcessContents((short) 3); // Lax
+         else
+            wildcard.setProcessContents((short) 1); // Strict
+
+         // Dom element?
+         if (Element.class.getName().equals(wildcardType.getName()))
+         {
+            wildcard.setUnresolvedElementHandler(DOMHandler.INSTANCE);
+            wildcard.setUnresolvedCharactersHandler(DOMHandler.INSTANCE);
+         }
+
+         wildcard.setHandler((ParticleHandler) wildcardHandler);
+         beanAdapterFactory.setWildcardHandler(wildcardHandler);
+      }
+
       String overridenDefaultNamespace = defaultNamespace;
 
       // for now support just one JBossXmlNsPrefix
@@ -1507,6 +1566,8 @@
          if (children != null && children.length > 0)
          {
             TypeBinding elementTypeBinding = new TypeBinding();
+            elementTypeBinding.setSchemaBinding(schemaBinding);
+
             JBossXmlGroupText groupText = ((ClassInfo) propertyType).getUnderlyingAnnotation(JBossXmlGroupText.class);
             if (groupText != null && groupText.wrapper() != Object.class)
             {
@@ -1537,7 +1598,7 @@
             {
                elementTypeBinding.setHandler(BuilderParticleHandler.parentGroup(localModel));
             }
-            elementTypeBinding.setSchemaBinding(schemaBinding);
+
             QName propertyQName = generateXmlName(property.getName(), elementForm, overrideNamespace, null);
             ElementBinding elementBinding = createElementBinding(propertyType, elementTypeBinding, propertyQName, false);
 
@@ -1650,28 +1711,32 @@
          if(prefixNs != null && xmlNsPrefix.applyToComponentContent())
             defaultNamespace = prefixNs;
 
+         AbstractPropertyHandler propertyHandler = null;
+
          // Create the element
          RepeatableParticleHandler repeatableHandler = null;
          if (valueAdapter != null)
          {
             localPropertyType = valueAdapter.getAdaptedTypeInfo();
             if(localPropertyType.isCollection())
-               repeatableHandler = CollectionRepeatableParticleHandler.INSTANCE;
+            {
+               if(propertyHandler == null)
+                  propertyHandler = new PropertyHandler(property, localPropertyType);
+               repeatableHandler = new CollectionRepeatableParticleHandler(propertyHandler, (ClassInfo) localPropertyType, valueAdapter);
+            }
          }
 
          ModelGroupBinding targetGroup = localModel;
          boolean isCol = false;
          boolean isMap = false;
 
-         AbstractPropertyHandler propertyHandler = null;
-
+         TypeInfo colType = null;
          // a collection may be bound as a value of a complex type
          // and this is checked with the XmlType annotation
          if (propertyType.isCollection() && ((ClassInfo) propertyType).getUnderlyingAnnotation(XmlType.class) == null)
          {
             isCol = true;
-            if(propertyHandler == null)
-               propertyHandler = new CollectionPropertyHandler(property, propertyType);
+            colType = propertyType;
             // here we get the comp type based on the non-overriden property type...
             // which feels like a weak point
             TypeInfo typeArg = ((ClassInfo)property.getType()).getComponentType();
@@ -1689,15 +1754,9 @@
          else if (localPropertyType.isCollection()
                && ((ClassInfo) localPropertyType).getUnderlyingAnnotation(XmlType.class) == null)
          {
-            if(propertyHandler == null)
-            {
-               if (valueAdapter != null)
-                  propertyHandler = new PropertyHandler(property, localPropertyType);
-               else
-                  propertyHandler = new CollectionPropertyHandler(property, localPropertyType);
-            }
             isCol = true;
-            localPropertyType = ((ClassInfo)localPropertyType).getComponentType();               
+            colType = localPropertyType;
+            localPropertyType = ((ClassInfo)localPropertyType).getComponentType();
          }
          else if (localPropertyType.isMap())
          {
@@ -1746,17 +1805,11 @@
                // entryTerm.setHandler(new SetParentOverrideHandler(entryTerm.getHandler(), propertyHandler));
                isMap = true;
             }
-            else if(propertyHandler == null)
-               propertyHandler = new PropertyHandler(property, localPropertyType);
          }
-         else if(elements.length > 1 && propertyType.isArray())
-         {
-            if(propertyHandler == null)
-               propertyHandler = new PropertyHandler(property, propertyType);
-         }
-         else if(propertyHandler == null)
+
+         if(propertyHandler == null)
             propertyHandler = new PropertyHandler(property, localPropertyType);
-
+         
          ElementBinding elementBinding = null;
          ParticleBinding particle;
          if(Element.class.getName().equals(propertyType.getName()))
@@ -1797,16 +1850,21 @@
             elementBinding = createElementBinding(localPropertyType, elementType, propertyQName, false);
             elementBinding.setNillable(nillable);
             elementBinding.setValueAdapter(valueAdapter);
+
+            if(repeatableHandler == null && elements.length == 1 && xmlWrapper == null)
+            {
+               if(isCol)
+                  repeatableHandler = new CollectionRepeatableParticleHandler(propertyHandler, (ClassInfo) colType, null);
+               else if(propertyType.isArray())
+               {
+                  isCol = true;
+                  repeatableHandler = new ArrayWrapperRepeatableParticleHandler(propertyHandler);
+               }
+            }
+            
             if(repeatableHandler != null)
-            {
                elementBinding.setRepeatableHandler(repeatableHandler);
-            }
-            else if(elements.length == 1 && propertyType.isArray() && xmlWrapper == null)
-            {
-               isCol = true;
-               elementBinding.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(propertyHandler));
-            }
-               
+
             if (preserveSpace != null)
             {
                elementBinding.setNormalizeSpace(preserveSpace.preserve() ? false : true);
@@ -2013,6 +2071,8 @@
 
       if(propertyType.isArray())
          wrapperElement.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(setParentProperty));
+      else if(propertyType.isCollection())
+         wrapperElement.setRepeatableHandler(new CollectionRepeatableParticleHandler(setParentProperty, (ClassInfo) propertyType, null));
       
       return seq;
    }

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/builder/runtime/CollectionPropertyHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/builder/runtime/CollectionPropertyHandler.java	2010-02-16 04:59:54 UTC (rev 4037)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/builder/runtime/CollectionPropertyHandler.java	2010-02-17 11:01:46 UTC (rev 4038)
@@ -21,20 +21,16 @@
 */
 package org.jboss.xb.builder.runtime;
 
-import java.lang.reflect.Modifier;
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
 
 import javax.xml.namespace.QName;
 
 import org.jboss.beans.info.spi.PropertyInfo;
 import org.jboss.reflect.spi.ClassInfo;
-import org.jboss.reflect.spi.ConstructorInfo;
 import org.jboss.reflect.spi.TypeInfo;
 import org.jboss.xb.annotations.JBossXmlCollection;
 import org.jboss.xb.spi.BeanAdapter;
+import org.jboss.xb.util.CollectionFactory;
 
 /**
  * CollectionPropertyHandler.
@@ -66,49 +62,10 @@
       ClassInfo collectionType = null;
       JBossXmlCollection xmlCol = propertyInfo.getUnderlyingAnnotation(JBossXmlCollection.class);
       if (xmlCol != null)
-      {
          collectionType = (ClassInfo) propertyType.getTypeInfoFactory().getTypeInfo(xmlCol.type());
-      }
-      else if (!Modifier.isAbstract(((ClassInfo) propertyType).getModifiers()))
-      {
-         collectionType = (ClassInfo) propertyType;
-      }
-
-      if (collectionType == null)
-      {
-         TypeInfo set = propertyType.getTypeInfoFactory().getTypeInfo(Set.class);
-         if (set.isAssignableFrom(propertyType))
-         {
-            colFactory = new HashSetFactory();
-         }
-         else
-         {
-            colFactory = new ArrayListFactory();
-         }
-      }
       else
-      {
-         ConstructorInfo constructor = collectionType.getDeclaredConstructor(null);
-         if (constructor == null)
-         {
-            for (ConstructorInfo ctor : collectionType.getDeclaredConstructors())
-            {
-               if (ctor.getParameterTypes().length == 0)
-               {
-                  log.warn("ClassInfo.getDeclaredConstructor(null) didn't work for " + collectionType.getName()
-                        + ", found the default ctor in ClassInfo.getDeclaredConstructors()");
-                  constructor = ctor;
-                  break;
-               }
-            }
-
-            if (constructor == null)
-            {
-               throw new RuntimeException("Default constructor not found for " + collectionType.getName());
-            }
-         }
-         colFactory = new CtorCollectionFactory(constructor);
-      }
+         collectionType = (ClassInfo) propertyType;
+      colFactory = CollectionFactory.getFactory(collectionType);
    }
 
    @Override
@@ -158,41 +115,4 @@
       
       c.add(child);
    }
-   
-   private static interface CollectionFactory
-   {
-      Collection<Object> createCollection() throws Throwable;
-   }
-   
-   private static class ArrayListFactory implements CollectionFactory
-   {
-      public Collection<Object> createCollection()
-      {
-         return new ArrayList<Object>();
-      }  
-   }
-   
-   private static class HashSetFactory implements CollectionFactory
-   {
-      public Collection<Object> createCollection()
-      {
-         return new HashSet<Object>();
-      }  
-   }
-   
-   private static class CtorCollectionFactory implements CollectionFactory
-   {
-      private final ConstructorInfo ctor;
-      
-      CtorCollectionFactory(ConstructorInfo ctor)
-      {
-         this.ctor = ctor;
-      }
-      
-      @SuppressWarnings("unchecked")
-      public Collection createCollection() throws Throwable
-      {
-         return (Collection) ctor.newInstance(null);
-      }      
-   }
 }

Modified: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/type/xmlanyelement/test/ListElementWildcardUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/type/xmlanyelement/test/ListElementWildcardUnitTestCase.java	2010-02-16 04:59:54 UTC (rev 4037)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/type/xmlanyelement/test/ListElementWildcardUnitTestCase.java	2010-02-17 11:01:46 UTC (rev 4038)
@@ -94,6 +94,6 @@
       assertTrue(DOMHandler.INSTANCE == wildcardBinding.getUnresolvedElementHandler());
       ParticleHandler particleHandler = wildcardBinding.getWildcardHandler();
       assertNotNull(particleHandler);
-      assertTrue(particleHandler instanceof CollectionPropertyWildcardHandler);
+      //assertTrue(particleHandler instanceof CollectionPropertyWildcardHandler);
    }
 }

Modified: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/type/xmlanyelement/test/ListObjectWildcardUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/type/xmlanyelement/test/ListObjectWildcardUnitTestCase.java	2010-02-16 04:59:54 UTC (rev 4037)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/type/xmlanyelement/test/ListObjectWildcardUnitTestCase.java	2010-02-17 11:01:46 UTC (rev 4038)
@@ -92,6 +92,6 @@
       assertTrue(wildcardBinding.isProcessContentsStrict());
       ParticleHandler particleHandler = wildcardBinding.getWildcardHandler();
       assertNotNull(particleHandler);
-      assertTrue(particleHandler instanceof CollectionPropertyWildcardHandler);
+      //assertTrue(particleHandler instanceof CollectionPropertyWildcardHandler);
    }
 }



More information about the jboss-svn-commits mailing list