[jboss-svn-commits] JBoss Common SVN: r3766 - 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
Fri Nov 20 06:23:55 EST 2009


Author: alex.loubyansky at jboss.com
Date: 2009-11-20 06:23:55 -0500 (Fri, 20 Nov 2009)
New Revision: 3766

Modified:
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/AllBinding.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/ChoiceBinding.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/ModelGroupBinding.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SequenceBinding.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java
   jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/UnorderedSequenceBinding.java
Log:
refactoring of ModelGroupBinding.Cursor API: generalize starting and repetition of terms across different model group types

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/AllBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/AllBinding.java	2009-11-19 21:07:24 UTC (rev 3765)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/AllBinding.java	2009-11-20 11:23:55 UTC (rev 3766)
@@ -82,48 +82,19 @@
    {
       return new Cursor(particle)
       {
-         private ParticleBinding curParticle;
-
-         public ParticleBinding getCurrentParticle()
-         {
-            if(curParticle == null)
-            {
-               throw new JBossXBRuntimeException("The cursor in all group has not been positioned yet!");
-            }
-            return curParticle;
-         }
-
-         public boolean isPositioned()
-         {
-            return curParticle != null;
-         }
-
-         public boolean isWildcardContent()
-         {
-            return false;
-         }
-
-         public ElementBinding getWildcardContent()
-         {
-            throw new UnsupportedOperationException("Model group 'all' can contain only elements.");
-         }
-
          protected ModelGroupBinding.Cursor startElement(QName qName, Attributes atts, Set<ModelGroupBinding> passedGroups, boolean required)
          {
+            if(currentParticle != null && repeatTerm(qName, atts))
+               throw new IllegalStateException("maxOccurs in all model group can only be 1: " + qName);
+
             ParticleBinding particle = elements.get(qName);
             if(particle != null)
             {
-               if(curParticle == particle)
-               {
-                  ++occurence;
-               }
-               else
-               {
-                  curParticle = particle;
-                  occurence = 1;
-               }
+               currentParticle = particle;
+               occurence = 1;
                return this;
-            }
+            }               
+
             return null;
          }
 

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/ChoiceBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/ChoiceBinding.java	2009-11-19 21:07:24 UTC (rev 3765)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/ChoiceBinding.java	2009-11-20 11:23:55 UTC (rev 3766)
@@ -69,40 +69,13 @@
 
    public Collection<ParticleBinding> getParticles()
    {
-      return Collections.unmodifiableCollection(choices);
+      return choices;
    }
 
    public Cursor newCursor(ParticleBinding particle)
    {
       return new Cursor(particle)
-      {
-         private ParticleBinding currentParticle;
-         private ElementBinding wildcardContent;
-         
-         public ParticleBinding getCurrentParticle()
-         {
-            if(currentParticle == null)
-               throw new JBossXBRuntimeException("The cursor has not been positioned yet! startElement should be called.");
-            return currentParticle;
-         }
-
-         public boolean isPositioned()
-         {
-            return currentParticle != null;
-         }
-
-         public boolean isWildcardContent()
-         {
-            return wildcardContent != null;
-         }
-
-         public ElementBinding getWildcardContent()
-         {
-            if(currentParticle == null)
-               throw new JBossXBRuntimeException("The cursor has not been positioned yet! startElement should be called.");
-            return wildcardContent;
-         }
-
+      {         
          protected ModelGroupBinding.Cursor startElement(QName qName, Attributes atts, Set<ModelGroupBinding> passedGroups, boolean required)
          {
             if(trace)
@@ -116,63 +89,12 @@
             
             if(currentParticle != null)
             {
-               boolean repeated = false;
-               if(currentParticle.getMaxOccursUnbounded() ||
-                  occurence < currentParticle.getMinOccurs() ||
-                  occurence < currentParticle.getMaxOccurs())
-               {
-                  TermBinding item = currentParticle.getTerm();
-                  if(item.isElement())
-                  {
-                     ElementBinding element = (ElementBinding)item;
-                     repeated = qName.equals(element.getQName());
-                  }
-                  else if(item.isModelGroup())
-                  {
-                     ModelGroupBinding modelGroup = (ModelGroupBinding)item;
-                     if(!passedGroups.contains(modelGroup))
-                     {
-                        switch(passedGroups.size())
-                        {
-                           case 0:
-                              passedGroups = Collections.singleton((ModelGroupBinding)ChoiceBinding.this);
-                              break;
-                           case 1:
-                              passedGroups = new HashSet<ModelGroupBinding>(passedGroups);
-                           default:
-                              passedGroups.add(ChoiceBinding.this);
-                        }
-
-                        boolean isRequired = occurence == 0 ? false : currentParticle.isRequired(occurence);
-                        next = modelGroup.newCursor(currentParticle).startElement(qName, atts, passedGroups, isRequired);
-                        repeated = next != null;
-                     }
-                  }
-                  else if(item.isWildcard())
-                  {
-                     WildcardBinding wildcard = (WildcardBinding)item;
-                     wildcardContent = wildcard.getElement(qName, atts);
-                     repeated = wildcardContent != null;
-                  }
-               }
-
-               if(repeated)
-               {
-                  ++occurence;
-                  if(trace)
-                     log.trace("repeated " + qName + " in " + ChoiceBinding.this + ", occurence=" + occurence + ", term=" + currentParticle.getTerm());
+               if(repeatTerm(qName, atts))
                   return this;
-               }
                else
-               {
-                  wildcardContent = null;
-                  currentParticle = null;
-                  occurence = 0;
-               }
-               
-               return null;
+                  return null;
             }
-
+            
             for(int i = 0; i < choices.size(); ++i)
             {
                boolean found = false;
@@ -225,11 +147,6 @@
 
             return null;
          }
-
-         protected ElementBinding getElement(QName qName, Attributes atts, Set<ModelGroupBinding.Cursor> passedGroups, boolean ignoreWildcards)
-         {
-            return getElement(choices, qName, atts, passedGroups, ignoreWildcards);
-         }
       };
    }
 

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/ModelGroupBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/ModelGroupBinding.java	2009-11-19 21:07:24 UTC (rev 3765)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/ModelGroupBinding.java	2009-11-20 11:23:55 UTC (rev 3766)
@@ -21,11 +21,9 @@
   */
 package org.jboss.xb.binding.sunday.unmarshalling;
 
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Set;
 
 import javax.xml.namespace.QName;
@@ -124,11 +122,6 @@
 
    public abstract Cursor newCursor(ParticleBinding particle);
 
-   public Object clone() throws CloneNotSupportedException
-   {
-      return super.clone();
-   }
-
    // Protected
 
    protected abstract boolean mayStartWith(QName qName, Set<ModelGroupBinding> set);
@@ -186,21 +179,24 @@
    }
 
    public abstract String getGroupType();
-   
+
    // Inner
    public abstract class Cursor
    {
+      protected final boolean trace = log.isTraceEnabled();
       protected final ParticleBinding particle;
-      protected final boolean trace = log.isTraceEnabled();
       protected int occurence;
 
+      protected ParticleBinding currentParticle;
+      protected ElementBinding wildcardContent;
+
       protected Cursor next;
       
       protected Cursor(ParticleBinding particle)
       {
+         if(particle.getTerm() != ModelGroupBinding.this)
+            throw new IllegalStateException("Particle term " + particle.getTerm() + " is not the model group " + ModelGroupBinding.this);
          this.particle = particle;
-         if(particle.getTerm() != ModelGroupBinding.this)
-            throw new IllegalStateException();
       }
 
       public ParticleBinding getParticle()
@@ -213,14 +209,21 @@
          return next;
       }
       
-      public abstract ParticleBinding getCurrentParticle();
+      public ParticleBinding getCurrentParticle()
+      {
+         return currentParticle;
+      }
 
-      public abstract boolean isWildcardContent();
+      public boolean isWildcardContent()
+      {
+         return wildcardContent != null;
+      }
 
-      public abstract ElementBinding getWildcardContent();
+      public ElementBinding getWildcardContent()
+      {
+         return wildcardContent;
+      }
 
-      public abstract boolean isPositioned();
-
       public ModelGroupBinding.Cursor startElement(QName qName, Attributes attrs)
       {
          return startElement(qName, attrs, Collections.<ModelGroupBinding>emptySet(), true);
@@ -247,22 +250,51 @@
       }
 */
 
-      public int getOccurence()
+      public boolean repeatTerm(QName qName, Attributes atts)
       {
-         return occurence;
-      }
+         if(currentParticle == null)
+            throw new IllegalStateException("The cursor has not been positioned yet!");
+         
+         boolean repeated = false;
+         if(currentParticle.getMaxOccursUnbounded() ||
+            occurence < currentParticle.getMinOccurs() ||
+            occurence < currentParticle.getMaxOccurs())
+         {
+            TermBinding item = currentParticle.getTerm();
+            if(item.isElement())
+            {
+               ElementBinding element = (ElementBinding)item;
+               repeated = qName.equals(element.getQName());
+            }
+            else if(item.isModelGroup())
+            {
+               ModelGroupBinding modelGroup = (ModelGroupBinding)item;
+               boolean isRequired = occurence == 0 ? false : currentParticle.isRequired(occurence);
+               next = modelGroup.newCursor(currentParticle).startElement(qName, atts, Collections.<ModelGroupBinding>emptySet(), isRequired);
+               repeated = next != null;
+            }
+            else if(item.isWildcard())
+            {
+               WildcardBinding wildcard = (WildcardBinding)item;
+               wildcardContent = wildcard.getElement(qName, atts);
+               repeated = wildcardContent != null;
+            }
+         }
 
-      public boolean repeatElement(QName qName)
-      {
-         ParticleBinding particle = getCurrentParticle();
-         if(particle.getMaxOccursUnbounded() ||
-            occurence < particle.getMinOccurs() ||
-            occurence < particle.getMaxOccurs())
+         if(repeated)
          {
             ++occurence;
-            return true;
+            if(trace)
+               log.trace("repeated " + qName + " in " + ModelGroupBinding.this + ", occurence=" + occurence + ", term=" + currentParticle.getTerm());
          }
-         return false;
+         else
+         {
+            wildcardContent = null;
+            currentParticle = null;
+            occurence = 0;
+         }
+
+         return repeated;
       }
 
       // Protected
@@ -272,12 +304,10 @@
             Set<ModelGroupBinding> passedGroups,
             boolean required);
 
-      protected abstract ElementBinding getElement(QName qName, Attributes atts, Set<ModelGroupBinding.Cursor> passedGroups, boolean ignoreWildcards);
-
-      protected ElementBinding getElement(List<ParticleBinding> group, QName qName, Attributes atts, Set<ModelGroupBinding.Cursor> passedGroups, boolean ignoreWildcards)
+      protected ElementBinding getElement(QName qName, Attributes atts, Set<ModelGroupBinding.Cursor> passedGroups, boolean ignoreWildcards)
       {
          ElementBinding element = null;
-         for (ParticleBinding nextParticle : group)
+         for (ParticleBinding nextParticle : getParticles())
          {
             TermBinding item = nextParticle.getTerm();
             if (item.isElement())
@@ -338,20 +368,5 @@
          }
          return element;
       }
-
-      protected List<ModelGroupBinding.Cursor> addItem(List<ModelGroupBinding.Cursor> list, ModelGroupBinding.Cursor o)
-      {
-         switch(list.size())
-         {
-            case 0:
-               list = Collections.singletonList(o);
-               break;
-            case 1:
-               list = new ArrayList<ModelGroupBinding.Cursor>(list);
-            default:
-               list.add(o);
-         }
-         return list;
-      }
    }
 }

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SequenceBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SequenceBinding.java	2009-11-19 21:07:24 UTC (rev 3765)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SequenceBinding.java	2009-11-20 11:23:55 UTC (rev 3766)
@@ -78,7 +78,7 @@
 
    public Collection<ParticleBinding> getParticles()
    {
-      return Collections.unmodifiableCollection(sequence);
+      return sequence;
    }
 
    public Cursor newCursor(ParticleBinding particle)
@@ -86,32 +86,7 @@
       return new Cursor(particle)
       {
          private int pos = -1;
-         private ElementBinding wildcardContent;
 
-         public ParticleBinding getCurrentParticle()
-         {
-            if(pos < 0)
-               throw new JBossXBRuntimeException("The cursor has not been positioned yet for " + SequenceBinding.this);
-            return sequence.get(pos);
-         }
-
-         public boolean isPositioned()
-         {
-            return pos != -1;
-         }
-         
-         public boolean isWildcardContent()
-         {
-            return wildcardContent != null;
-         }
-
-         public ElementBinding getWildcardContent()
-         {
-            if(pos < 0)
-               throw new JBossXBRuntimeException("The cursor has not been positioned yet for " + SequenceBinding.this);
-            return wildcardContent;
-         }
-
          protected ModelGroupBinding.Cursor startElement(QName qName, Attributes atts, Set<ModelGroupBinding> passedGroups, boolean required)
          {
             if(trace)
@@ -122,21 +97,16 @@
             }
 
             next = null;
-            wildcardContent = null;
-            int i = pos;
-            if(pos >= 0)
-            {
-               ParticleBinding particle = getCurrentParticle();
-               if(particle.getMaxOccursUnbounded() ||
-                  occurence < particle.getMinOccurs() ||
-                  occurence < particle.getMaxOccurs())
-               {
-                  --i;
-               }
-            }
 
+            // if positioned try repeating
+            if(currentParticle != null && repeatTerm(qName, atts))
+               return this;
+            
+            // this will be the first occurence
+            
             // i update pos only if the element has been found, though it seems to be irrelevant
             // since the cursor is going to be thrown away in case the element has not been found
+            int i = pos;
             while(i < sequence.size() - 1)
             {
                ParticleBinding particle = sequence.get(++i);
@@ -146,24 +116,16 @@
                   ElementBinding element = (ElementBinding)item;
                   if(qName.equals(element.getQName()))
                   {
-                     if(pos == i)
-                     {
-                        ++occurence;
-                     }
-                     else
-                     {
-                        pos = i;
-                        occurence = 1;
-                     }
+                     pos = i;
+                     occurence = 1;
+                     currentParticle = particle;
 
                      if(trace)
-                     {
                         log.trace("found " + qName + " in " + SequenceBinding.this);
-                     }
                      return this;
                   }
 
-                  if(i != pos && particle.getMinOccurs() > 0)
+                  if(particle.getMinOccurs() > 0)
                   {
                      if(required)
                      {
@@ -200,20 +162,13 @@
 
                      if(next != null)
                      {
-                        if(pos != i)
-                        {
-                           pos = i;
-                           occurence = 1;
-                        }
-                        else
-                        {
-                           ++occurence;
-                        }
-                        
+                        pos = i;
+                        occurence = 1;
+                        currentParticle = particle;
                         return this;
                      }
 
-                     if(i != pos && particle.isRequired())
+                     if(particle.isRequired())
                      {
                         if(required)
                         {
@@ -228,7 +183,7 @@
                         }
                      }
                   }
-                  else if(i != pos && particle.isRequired())
+                  else if(particle.isRequired())
                   {
                      if(required)
                      {
@@ -249,19 +204,13 @@
                   wildcardContent = wildcard.getElement(qName, atts);
                   if(wildcardContent != null)
                   {
-                     if(pos != i)
-                     {
-                        pos = i;
-                        occurence = 1;
-                     }
-                     else
-                     {
-                        ++occurence;
-                     }
+                     pos = i;
+                     occurence = 1;
+                     currentParticle = particle;
                      return this;
                   }
 
-                  if(i != pos && particle.getMinOccurs() > 0)
+                  if(particle.getMinOccurs() > 0)
                   {
                      if(required)
                      {
@@ -278,17 +227,10 @@
             }
 
             if(trace && i == sequence.size())
-            {
                log.trace(qName + " not found in " + SequenceBinding.this);
-            }
 
             return null;
          }
-
-         protected ElementBinding getElement(QName qName, Attributes atts, Set<ModelGroupBinding.Cursor> passedGroups, boolean ignoreWildcards)
-         {
-            return getElement(sequence, qName, atts, passedGroups, ignoreWildcards);
-         }
       };
    }
 

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java	2009-11-19 21:07:24 UTC (rev 3765)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java	2009-11-20 11:23:55 UTC (rev 3766)
@@ -303,7 +303,7 @@
                      if(item.particle.isRepeatable())
                      {
                         StackItem parentItem = stack.peek1();
-                        if(parentItem.cursor.repeatElement(startName))
+                        if(parentItem.cursor.repeatTerm(startName, atts))
                         {
                            item.reset();
                            particle = item.particle;
@@ -422,7 +422,7 @@
                if(cursor == null)
                   throw new JBossXBRuntimeException("No cursor for " + startName);
 
-               ParticleBinding prevParticle = cursor.isPositioned() ? cursor.getCurrentParticle() : null;
+               ParticleBinding prevParticle = cursor.getCurrentParticle();
                ModelGroupBinding.Cursor newCursor = cursor.startElement(startName, atts);               
                if(newCursor == null)
                {

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/UnorderedSequenceBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/UnorderedSequenceBinding.java	2009-11-19 21:07:24 UTC (rev 3765)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/UnorderedSequenceBinding.java	2009-11-20 11:23:55 UTC (rev 3766)
@@ -113,51 +113,7 @@
    {
       return new Cursor(particle)
       {
-         private ParticleBinding curParticle;
-         private int occurence;
-         private ElementBinding wildcardContent;
-
          @Override
-         public ParticleBinding getCurrentParticle()
-         {
-            if(curParticle == null)
-               throw new JBossXBRuntimeException("The cursor in all group has not been positioned yet!");
-            return curParticle;
-         }
-
-         @Override
-         protected ElementBinding getElement(QName name, Attributes atts, Set<Cursor> passedGroups, boolean ignoreWildcards)
-         {
-            return getElement((List<ParticleBinding>) getParticles(), name, atts, passedGroups, ignoreWildcards);
-         }
-
-         @Override
-         public int getOccurence()
-         {
-            return occurence;
-         }
-
-         @Override
-         public boolean isPositioned()
-         {
-            return curParticle != null;
-         }
-
-         @Override
-         public boolean isWildcardContent()
-         {
-            return wildcardContent != null;
-         }
-
-         @Override
-         public ElementBinding getWildcardContent()
-         {
-            if(curParticle == null)
-               throw new JBossXBRuntimeException("The cursor in all group has not been positioned yet!");
-            return wildcardContent;
-         }
-
-         @Override
          protected Cursor startElement(QName qName, Attributes atts, Set<ModelGroupBinding> passedGroups, boolean required)
          {
             if(trace)
@@ -169,53 +125,13 @@
 
             next = null;
             
-            if(curParticle != null &&
-                  (curParticle.getMaxOccursUnbounded() || occurence < curParticle.getMinOccurs() || occurence < curParticle.getMaxOccurs()))
-            {
-               TermBinding term = curParticle.getTerm();
-               if(term.isElement() && ((ElementBinding)term).getQName().equals(qName))
-               {
-                  ++occurence;
-                  if(trace)
-                     log.trace("found " + qName + " in " + UnorderedSequenceBinding.this);
-                  return this;
-               }
-               else if(term.isModelGroup())
-               {
-                  ModelGroupBinding modelGroup = (ModelGroupBinding)term;
-                  if(!passedGroups.contains(modelGroup))
-                  {
-                     switch(passedGroups.size())
-                     {
-                        case 0:
-                           passedGroups = Collections.singleton((ModelGroupBinding)UnorderedSequenceBinding.this);
-                           break;
-                        case 1:
-                           passedGroups = new HashSet<ModelGroupBinding>(passedGroups);
-                        default:
-                           passedGroups.add(UnorderedSequenceBinding.this);
-                     }
+            if(currentParticle != null && repeatTerm(qName, atts))
+               return this;               
 
-                     next = modelGroup.newCursor(curParticle).startElement(
-                        qName, atts, passedGroups, curParticle.isRequired(occurence)
-                     );
-
-                     if(next != null)
-                     {
-                        ++occurence;
-                        return this;
-                     }
-                  }
-               }
-            }
-            
-            wildcardContent = null;
-            occurence = 0;
-
-            curParticle = elementParticles.get(qName);
-            if (curParticle != null)
+            currentParticle = elementParticles.get(qName);
+            if (currentParticle != null)
             {
-               ++occurence;
+               occurence = 1;
                if (trace)
                   log.trace("found " + qName + " in " + UnorderedSequenceBinding.this);
                return this;
@@ -241,8 +157,8 @@
 
                   if (next != null)
                   {
-                     ++occurence;
-                     curParticle = particle;
+                     occurence = 1;
+                     currentParticle = particle;
                      return this;
                   }
                }
@@ -254,8 +170,8 @@
                wildcardContent = wildcard.getElement(qName, atts);
                if (wildcardContent != null)
                {
-                  ++occurence;
-                  curParticle = particle;
+                  occurence = 1;
+                  currentParticle = particle;
                   return this;
                }
             }



More information about the jboss-svn-commits mailing list