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: 3623 $
 
 public class ChoiceBinding
    extends ModelGroupBinding
 {
    private List<ParticleBindingchoices = Collections.emptyList();
 
    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<ParticleBinding>();
          default:
             .add(particle);
       }
 
       super.addParticle(particle);
    }
 
    {
       return Collections.unmodifiableCollection();
    }
 
    public Cursor newCursor(ParticleBinding particle)
    {
       return new Cursor(particle)
       {
          private ParticleBinding currentParticle;
          private ElementBinding element;
          private boolean wildcardContent;
          
          public ParticleBinding getCurrentParticle()
          {
             if( == null)
                throw new JBossXBRuntimeException("The cursor has not been positioned yet! startElement should be called.");
             return ;
          }
 
          public ElementBinding getElement()
          {
             if( == null)
                throw new JBossXBRuntimeException("The cursor has not been positioned yet! startElement should be called.");
             return ;
          }
 
          public boolean isPositioned()
          {
             return  != null;
         }
         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 boolean isWildcardContent()
         {
            return ;
         }
         
         protected List<ModelGroupBinding.CursorstartElement(QName qNameAttributes attsSet<ModelGroupBindingpassedGroupsList<ModelGroupBinding.CursorgroupStackboolean required)
         {
            if()
            {
               StringBuffer sb = new StringBuffer();
               sb.append("startElement ").append(qName).append(" in ").append(ChoiceBinding.this.toString());
               .trace(sb.toString());
            }
            if( != null)
            {
               boolean repeated = false;
               if(.getMaxOccursUnbounded() ||
                   < .getMinOccurs() ||
                   < .getMaxOccurs())
               {
                  TermBinding item = .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);
                        }
                        int groupStackSize = groupStack.size();
                        boolean isRequired =  == 0 ? false : .isRequired();
                        groupStack = modelGroup.newCursor().startElement(qNameattspassedGroupsgroupStackisRequired);
                        repeated = groupStackSize != groupStack.size();
                     }
                  }
                  else if(item.isWildcard())
                  {
                     WildcardBinding wildcard = (WildcardBinding)item;
                      = wildcard.getElement(qNameatts);
                     repeated =  != null;
                  }
               }
               if(repeated)
               {
                  ++;
                  groupStack = addItem(groupStackthis);                  
                  if()
                     .trace("repeated " + qName + " in " + getModelGroup() + ", occurence=" +  + ", term=" + .getTerm());
               }
               else
               {
                   = false;
                   = null;
                   = null;
                   = 0;
               }
               
               return groupStack;
            }
            for(int i = 0; i < .size(); ++i)
            {
               boolean found = false;
               ParticleBinding particle = (ParticleBinding).get(i);
               TermBinding item = particle.getTerm();
               if(item.isElement())
               {
                  ElementBinding element = (ElementBinding)item;
                  if(qName.equals(element.getQName()))
                  {
                     found = true;
                     this. = element;
                  }
               }
               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);
                     }
                     int groupStackSize = groupStack.size();
                     boolean isRequired =  == 0 ? false : particle.isRequired();
                     groupStack = modelGroup.newCursor(particle).startElement(qNameattspassedGroupsgroupStackisRequired);
                     found = groupStackSize != groupStack.size();
                  }
               }
               else if(item.isWildcard())
               {
                  WildcardBinding wildcard = (WildcardBinding)item;
                   = wildcard.getElement(qNameatts);
                  if( != null)
                  {
                     found = true;
                      = true;
                  }
               }
               
               if(found)
               {
                   = 1;
                   = particle;
                  groupStack = addItem(groupStackthis);
                  if()
                     .trace("found " + qName + " in " + getModelGroup() + ", term=" + .getTerm());
                  break;
               }
            }
            return groupStack;
         }
         protected ElementBinding getElement(QName qNameAttributes attsSet<ModelGroupBinding.CursorpassedGroupsboolean ignoreWildcards)
         {
            return getElement(qNameattspassedGroupsignoreWildcards);
         }
      };
   }
   protected boolean mayStartWith(QName qNameSet<ModelGroupBindingset)
   {
      boolean result = false;
      for(int i = 0; i < .size(); ++i)
      {
         ParticleBinding particle = .get(i);
         TermBinding item = particle.getTerm();
         if(item.isElement())
         {
            ElementBinding element = (ElementBinding)item;
            if(qName.equals(element.getQName()))
            {
               result = true;
               break;
            }
         }
         else if(item.isModelGroup())
         {
            ModelGroupBinding modelGroup = (ModelGroupBinding)item;
            if(!set.contains(modelGroup))
            {
               switch(set.size())
               {
                  case 0:
                     set = Collections.singleton((ModelGroupBinding)this);
                     break;
                  case 1:
                     set = new HashSet<ModelGroupBinding>(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;
   }
   public String getGroupType()
   {
      return "choice";
   }
New to GrepCode? Check out our FAQ X