[jboss-svn-commits] JBoss Common SVN: r4186 - in jbossxb/trunk/src: main/java/org/jboss/xb/builder/runtime and 2 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Mar 18 09:37:30 EDT 2010


Author: alex.loubyansky at jboss.com
Date: 2010-03-18 09:37:28 -0400 (Thu, 18 Mar 2010)
New Revision: 4186

Added:
   jbossxb/trunk/src/main/java/org/jboss/xb/builder/runtime/AppendingArrayRepeatableHandler.java
Modified:
   jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java
   jbossxb/trunk/src/test/java/org/jboss/test/xml/unorderedsequence/support/UnorderedSequenceWithCollections.java
   jbossxb/trunk/src/test/java/org/jboss/test/xml/unorderedsequence/test/UnorderedSequenceWithCollectionsUnitTestCase.java
Log:
JBXB-244

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java	2010-03-18 10:26:45 UTC (rev 4185)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java	2010-03-18 13:37:28 UTC (rev 4186)
@@ -122,6 +122,7 @@
 import org.jboss.xb.binding.sunday.unmarshalling.WildcardBinding;
 import org.jboss.xb.builder.runtime.AbstractPropertyHandler;
 import org.jboss.xb.builder.runtime.AnyAttributePropertyHandler;
+import org.jboss.xb.builder.runtime.AppendingArrayRepeatableHandler;
 import org.jboss.xb.builder.runtime.ArrayWrapperRepeatableParticleHandler;
 import org.jboss.xb.builder.runtime.BeanHandler;
 import org.jboss.xb.builder.runtime.BuilderParticleHandler;
@@ -1306,7 +1307,10 @@
             {
                particleBinding.setMaxOccursUnbounded(true);
                wildcardHandler = new PropertyWildcardHandler(wildcardProperty, wildcardType);
-               wildcard.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(wildcardHandler));
+               if(currentRepeatableHandlers)
+                  wildcard.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(wildcardHandler));
+               else
+                  wildcard.setRepeatableHandler(new AppendingArrayRepeatableHandler(wildcardHandler));
                type = ((ArrayInfo) wildcardType).getComponentType();
                if (trace)
                   log.trace("Wildcard " + wildcardProperty.getName() + " is an array of type " + type.getName());
@@ -1531,7 +1535,12 @@
          if(xmlWrapper == null)
          {
             if(propertyType.isArray())
-               choice.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(new PropertyHandler(property, propertyType)));
+            {
+               if(currentRepeatableHandlers)
+                  choice.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(new PropertyHandler(property, propertyType)));
+               else
+                  choice.setRepeatableHandler(new AppendingArrayRepeatableHandler(new PropertyHandler(property, propertyType)));
+            }
             else if(currentRepeatableHandlers && propertyType.isCollection())
                choice.setRepeatableHandler(new CollectionRepeatableParticleHandler(new PropertyHandler(property, propertyType), (ClassInfo) propertyType, null));
          }
@@ -1560,6 +1569,7 @@
             if(!repeatableChoice)
                particleBinding.setMaxOccursUnbounded(true);
             wildcardHandler = new PropertyWildcardHandler(property, propertyType);
+            // TODO test this!
             wildcard.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(wildcardHandler));
             wildcardType = ((ArrayInfo) propertyType).getComponentType();
             if (trace)
@@ -1939,7 +1949,10 @@
                if(propertyType.isArray())
                {
                   isCol = true;
-                  repeatableHandler = new ArrayWrapperRepeatableParticleHandler(propertyHandler);
+                  if(currentRepeatableHandlers)
+                     repeatableHandler = new ArrayWrapperRepeatableParticleHandler(propertyHandler);
+                  else
+                     repeatableHandler = new AppendingArrayRepeatableHandler(propertyHandler);
                }
                else if(isCol && currentRepeatableHandlers)
                   repeatableHandler = new CollectionRepeatableParticleHandler(propertyHandler, (ClassInfo) colType, null);
@@ -2073,7 +2086,12 @@
             group = createModelGroup(annotation.kind(), groupType, propOrderMissing, annotation.propOrder(), groupName);
 
          if(property.getType().isArray())
-            group.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(propertyHandler));
+         {
+            if(currentRepeatableHandlers)
+               group.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(propertyHandler));
+            else
+               group.setRepeatableHandler(new AppendingArrayRepeatableHandler(propertyHandler));
+         }
       }
       
       parentGroup.addParticle(new ParticleBinding(group, 0, 1, repeatable));
@@ -2170,7 +2188,10 @@
       parentModel.addParticle(particle);
 
       if (propertyType.isArray())
-         wrapperElement.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(setParentProperty));
+         if(currentRepeatableHandlers)
+            wrapperElement.setRepeatableHandler(new ArrayWrapperRepeatableParticleHandler(setParentProperty));
+         else
+            wrapperElement.setRepeatableHandler(new AppendingArrayRepeatableHandler(setParentProperty));
       else if (propertyType.isCollection() && currentRepeatableHandlers)
          wrapperElement.setRepeatableHandler(new CollectionRepeatableParticleHandler(setParentProperty, (ClassInfo) propertyType, null));
       

Added: jbossxb/trunk/src/main/java/org/jboss/xb/builder/runtime/AppendingArrayRepeatableHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/builder/runtime/AppendingArrayRepeatableHandler.java	                        (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/builder/runtime/AppendingArrayRepeatableHandler.java	2010-03-18 13:37:28 UTC (rev 4186)
@@ -0,0 +1,131 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.xb.builder.runtime;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import org.jboss.beans.info.spi.PropertyInfo;
+import org.jboss.reflect.spi.ArrayInfo;
+import org.jboss.reflect.spi.TypeInfo;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleHandler;
+import org.jboss.xb.binding.sunday.unmarshalling.RepeatableParticleHandler;
+import org.jboss.xb.binding.sunday.unmarshalling.ValueAdapter;
+import org.jboss.xb.spi.BeanAdapter;
+
+/**
+ * This repeatable particle handler is used when repeatable particle handlers are actually disabled.
+ * The reason is instead of creating, copying and setting a new array for every new element added,
+ * it collects elements that appear one after another in XML and then appends to the current
+ * property value.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public class AppendingArrayRepeatableHandler implements RepeatableParticleHandler
+{
+   private final PropertyInfo propertyInfo;
+   private final ArrayInfo arrayInfo;
+   private final AbstractPropertyHandler targetHandler;
+   
+   public AppendingArrayRepeatableHandler(AbstractPropertyHandler propertyHandler)
+   {
+      this.propertyInfo = propertyHandler.getPropertyInfo();
+      this.arrayInfo = (ArrayInfo) propertyHandler.getPropertyType();
+      this.targetHandler = propertyHandler;
+   }
+
+   public void handle(PropertyInfo propertyInfo, TypeInfo propertyType, Object parent, Object child, QName name)
+   {
+   }
+
+   public void addTermValue(Object particleValue, Object termValue, QName elementName, ParticleBinding particle,
+         ParticleBinding parentParticle, ParticleHandler handler)
+   {
+      ((List<Object>)particleValue).add(termValue);
+   }
+
+   public void endRepeatableParticle(Object parent, Object o, QName name, ParticleBinding particle,
+         ParticleBinding parentParticle)
+   {
+      ValueAdapter valueAdapter = particle.getTerm().getValueAdapter();
+      if(valueAdapter != null)
+         o = valueAdapter.cast(o, null);
+
+      BeanAdapter beanAdapter = (BeanAdapter) parent;
+      
+      Object currentArray = null;
+      try
+      {
+         if (propertyInfo.getGetter() != null)
+            currentArray = beanAdapter.get(propertyInfo);
+      }
+      catch (Throwable t)
+      {
+         throw new RuntimeException("QName " + name + " error getting array property " + propertyInfo.getName() + " for " + BuilderUtil.toDebugString(parent), t);
+      }
+
+      List<Object> elements = (List<Object>) o;
+      Object[] arr;
+      if (currentArray == null)
+      {
+         try
+         {
+            arr = (Object[]) arrayInfo.newArrayInstance(elements.size());
+         }
+         catch (Throwable t)
+         {
+            throw new RuntimeException("Error creating array of type " + arrayInfo.getName() + " from " + elements, t);
+         }
+
+         for (int i = 0; i < arr.length; ++i)
+            arr[i] = elements.get(i);
+      }
+      else
+      {
+         int currentLength = Array.getLength(currentArray);
+         try
+         {
+            arr = (Object[]) arrayInfo.newArrayInstance(currentLength + elements.size());
+         }
+         catch (Throwable e)
+         {
+            throw new RuntimeException("Error creating array of type " + arrayInfo.getName() + " from " + elements, e);
+         }
+         
+         System.arraycopy(currentArray, 0, arr, 0, currentLength);
+         for (int i = 0; i < elements.size(); ++i)
+            arr[currentLength + i] = elements.get(i);
+      }
+      
+      targetHandler.doHandle(parent, arr, name);
+   }
+
+   public Object startRepeatableParticle(Object parent, QName startName, ParticleBinding particle)
+   {
+      return new ArrayList<Object>();
+   }
+}

Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/unorderedsequence/support/UnorderedSequenceWithCollections.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/unorderedsequence/support/UnorderedSequenceWithCollections.java	2010-03-18 10:26:45 UTC (rev 4185)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/unorderedsequence/support/UnorderedSequenceWithCollections.java	2010-03-18 13:37:28 UTC (rev 4186)
@@ -39,7 +39,7 @@
 public class UnorderedSequenceWithCollections
 {
    private List<String> strings;
-   private List<Integer> ints;
+   private Integer[] ints;
    
    public List<String> getStrings()
    {
@@ -51,12 +51,12 @@
       this.strings = strings;
    }
    
-   public List<Integer> getInts()
+   public Integer[] getInts()
    {
       return ints;
    }
    
-   public void setInts(List<Integer> ints)
+   public void setInts(Integer[] ints)
    {
       this.ints = ints;
    }

Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/unorderedsequence/test/UnorderedSequenceWithCollectionsUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/unorderedsequence/test/UnorderedSequenceWithCollectionsUnitTestCase.java	2010-03-18 10:26:45 UTC (rev 4185)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/unorderedsequence/test/UnorderedSequenceWithCollectionsUnitTestCase.java	2010-03-18 13:37:28 UTC (rev 4186)
@@ -83,9 +83,9 @@
       assertNotNull(strings);
       assertEquals(4, strings.size());
       assertTrue(Arrays.asList(new String[]{"1", "2", "3", "4"}).containsAll(strings));
-      List<Integer> ints = root.getInts();
+      Integer[] ints = root.getInts();
       assertNotNull(ints);
-      assertEquals(4, ints.size());
-      assertTrue(Arrays.asList(new Integer[]{1, 2, 3, 4}).containsAll(ints));
+      assertEquals(4, ints.length);
+      assertEquals(new Integer[]{1, 2, 3, 4}, ints);
    }
 }



More information about the jboss-svn-commits mailing list