Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, 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.binding.sunday.unmarshalling;
 
 import java.util.Set;
 import java.util.List;

Author(s):
Alexey Loubyansky
Version:
$Revision: 2627 $
 
 public class ChoiceBinding
    extends ModelGroupBinding
 {
    private List choices = .;
 
    public ChoiceBinding(SchemaBinding schema)
    {
       super(schema);
    }
 
    public ElementBinding getArrayItem()
    {
       return null;
    }
 
    public void addParticle(ParticleBinding particle)
    {
       switch(.size())
       {
          case 0:
              = Collections.singletonList(particle);
             break;
          case 1:
              = new ArrayList();
          default:
             .add(particle);
       }
 
       super.addParticle(particle);
    }
 
    public Collection getParticles()
    {
       return Collections.unmodifiableCollection();
    }
 
    public Cursor newCursor(ParticleBinding particle)
    {
       return new Cursor(particle)
       {
          private int pos = -1;
          private ElementBinding element;
          private int occurence;
          private boolean wildcardContent;
 
          public ParticleBinding getCurrentParticle()
          {
             if( < 0)
             {
                throw new JBossXBRuntimeException(
                   "The cursor has not been positioned yet! startElement should be called."
                );
             }
             return (ParticleBinding).get();
          }
 
          public ElementBinding getElement()
          {
             if( < 0)
             {
                throw new JBossXBRuntimeException(
                  "The cursor has not been positioned yet! startElement should be called."
               );
            }
            return ;
         }
         public boolean isPositioned()
         {
            return  != -1;
         }
         public void endElement(QName qName)
         {
            if( == null || !.getQName().equals(qName))
            {
               throw new JBossXBRuntimeException("Failed to process endElement for " + qName +
                  " since the current element is " + ( == null ? "null" : .getQName().toString())
               );
            }
            if()
            {
               .trace("endElement " + qName + " in " + getModelGroup());
            }
         }
         public int getOccurence()
         {
            return ;
         }
         public boolean isWildcardContent()
         {
            return ;
         }
         
         protected List startElement(QName qNameAttributes attsSet passedGroupsList groupStackboolean required)
         {
            if()
            {
               StringBuffer sb = new StringBuffer();
               sb.append("startElement " + qName + " in " + getModelGroup() + ", " + .size() + ": ");
               for(int i = 0; i < .size(); ++i)
               {
                  ParticleBinding particle = (ParticleBinding).get(i);
                  Object o = particle.getTerm();
                  if(o instanceof ElementBinding)
                  {
                     sb.append(((ElementBinding)o).getQName());
                  }
                  else if(o instanceof SequenceBinding)
                  {
                     sb.append("sequence");
                  }
                  else if(o instanceof ChoiceBinding)
                  {
                     sb.append("choice");
                  }
                  else if(o instanceof AllBinding)
                  {
                     sb.append("all");
                  }
                  sb.append(" ");
               }
               sb.append("]");
               .trace(sb.toString());
            }
             = false;
            int i = ;
            if( >= 0)
            {
               ParticleBinding particle = getCurrentParticle();
               if(particle.getMaxOccursUnbounded() ||
                   < particle.getMinOccurs() ||
                   < particle.getMaxOccurs())
               {
                  --i;
               }
            }
            // 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
            while(i < .size() - 1)
            {
               ParticleBinding particle = (ParticleBinding).get(++i);
               Object item = particle.getTerm();
               if(item instanceof ElementBinding)
               {
                  ElementBinding element = (ElementBinding)item;
                  if(qName.equals(element.getQName()))
                  {
                     if( == i)
                     {
                        ++;
                     }
                     else
                     {
                         = i;
                         = 1;
                     }
                     groupStack = addItem(groupStackthis);
                     this. = element;
                     if()
                     {
                        .trace("found " + qName + " in " + getModelGroup());
                     }
                     break;
                  }
               }
               else if(item instanceof ModelGroupBinding)
               {
                  ModelGroupBinding modelGroup = (ModelGroupBinding)item;
                  if(!passedGroups.contains(modelGroup))
                  {
                     switch(passedGroups.size())
                     {
                        case 0:
                           passedGroups = Collections.singleton(this);
                           break;
                        case 1:
                           passedGroups = new HashSet(passedGroups);
                        default:
                           passedGroups.add(this);
                     }
                     int groupStackSize = groupStack.size();
                     groupStack = modelGroup.newCursor(particle).startElement(
                        qNameattspassedGroupsgroupStackparticle.isRequired()
                     );
                     if(groupStackSize != groupStack.size())
                     {
                        if( != i)
                        {
                            = i;
                            = 1;
                        }
                        else
                        {
                           ++;
                        }
                        groupStack = addItem(groupStackthis);
                         = null;
                        break;
                     }
                  }
               }
               else if(item instanceof WildcardBinding)
               {
                  WildcardBinding wildcard = (WildcardBinding)item;
                   = wildcard.getElement(qNameatts);
                  if( != null)
                  {
                     if( != i)
                     {
                         = i;
                         = 1;
                     }
                     else
                     {
                        ++;
                     }
                     groupStack = addItem(groupStackthis);
                      = true;
                     if()
                        .trace(qName + " is wildcard content");
                     break;
                  }
               }
            }
            if()
            {
               if(i == .size())
               {
                  .trace(qName + " not found in " + getModelGroup());
               }
               else
               {
                  .trace("leaving " + getModelGroup() + " i=" + i + ", pos=" + );
               }
            }
            return groupStack;
         }
         protected ElementBinding getElement(QName qNameAttributes attsSet passedGroupsboolean ignoreWildcards)
         {
            return getElement(qNameattspassedGroupsignoreWildcards);
         }
      };
   }
   protected boolean mayStartWith(QName qNameSet set)
   {
      boolean result = false;
      for(int i = 0; i < .size(); ++i)
      {
         ParticleBinding particle = (ParticleBinding).get(i);
         Object item = particle.getTerm();
         if(item instanceof ElementBinding)
         {
            ElementBinding element = (ElementBinding)item;
            if(qName.equals(element.getQName()))
            {
               result = true;
               break;
            }
         }
         else if(item instanceof ModelGroupBinding)
         {
            ModelGroupBinding modelGroup = (ModelGroupBinding)item;
            if(!set.contains(modelGroup))
            {
               switch(set.size())
               {
                  case 0:
                     set = Collections.singleton(this);
                     break;
                  case 1:
                     set = new HashSet(set);
                  default:
                     set.add(this);
               }
               result = modelGroup.mayStartWith(qNameset);
               if(result)
               {
                  break;
               }
            }
         }
         else
         {
            throw new JBossXBRuntimeException("Unexpected item type in model group: " + item);
         }
      }
      return result;
   }
New to GrepCode? Check out our FAQ X