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.builder.runtime;
 
 
A GroupBeanHandler. Before creating a new instance for the group during unmarshalling, this handler will try to obtain the current value of the group from the parent object using the property the group is bound to. If the current group value is not null then it will be returned, otherwise a new instance will be created. This is necessary to support unordered sequences. If the property for the group is not readable then the step to get the group value will be skipped. It doesn't have to be a requirement for the property to be readable.

Author(s):
Alexey Loubyansky
Version:
$Revision: 1.1 $
 
 public class GroupBeanHandler extends BeanHandler
 {
    
    public GroupBeanHandler(String nameBeanAdapterFactory beanAdapterFactory)
    {
       super(namebeanAdapterFactory);
    }
 
    @Override
    public Object startParticle(Object parentQName qNameParticleBinding particleAttributes attrsNamespaceContext nsCtx)
    {
       if(!(parent instanceof BeanAdapter))
          throw new JBossXBRuntimeException("Parent expected to be an instance of BeanAdapter: " + parent);
       
       TermBinding term = particle.getTerm();
       if(!term.isModelGroup())
          throw new JBossXBRuntimeException("The term expected to be a model group: " + term);
       
       ModelGroupBinding group = (ModelGroupBindingterm;
       QName groupName = group.getQName();
       if(groupName == null)
          throw new JBossXBRuntimeException("The group has to have a non-null QName. Failed to start element " + qName);
       
       AbstractPropertyHandler groupHandler = ((BeanAdapterparent).getPropertyHandler(groupName);
       if (groupHandler == null)
          throw new JBossXBRuntimeException("No property mapped for group " + qName + " in bean adapter" + ((BeanAdapter)parent).getValue()
                + ", available: " + ((BeanAdapterparent).getAvailable());
 
       Object groupValue = null;
       PropertyInfo propertyInfo = groupHandler.getPropertyInfo();
       if(propertyInfo.isReadable())
       {
          Object parentValue = ((BeanAdapterparent).getValue();
          try
          {
             groupValue = ((BeanAdapterparent).get(propertyInfo);
          }
          catch (Throwable e)
          {
             throw new JBossXBRuntimeException("Failed to get group value from parent: parent=" + parentValue
                   + ", property=" + propertyInfo.getName() + ", qName=" + qNamee);
          }
       }
 
       if(groupValue == null)
          return super.startParticle(parentqNameparticleattrsnsCtx);
       else
          return new SingletonBeanAdapter(this.getBeanAdapterFactory(), groupValue);
    }
       
   private static class SingletonBeanAdapter extends BeanAdapter
   {
      private final Object value;
      public SingletonBeanAdapter(BeanAdapterFactory beanAdapterFactoryObject instance)
      {
         super(beanAdapterFactory);
         this. = instance;
      }
      
      protected Object construct()
      {
         return ;
      }
      @Override
      public Object get(PropertyInfo propertyInfothrows Throwable
      {
         return propertyInfo.get();
      }
      @Override
      public Object getValue()
      {
         return ;
      }
      @Override
      public void set(PropertyInfo propertyInfoObject childthrows Throwable
      {
         propertyInfo.set(child);
      }
   }
New to GrepCode? Check out our FAQ X