[jboss-dev-forums] [Design of JBossXB] - Re: SundayContentHandler: Don't add collections into collect

adrian@jboss.org do-not-reply at jboss.com
Tue Oct 10 10:37:39 EDT 2006


It might also have been related to my attempt to implement
holders using a custom particle handler (I can't remember?)
but the code was below.

This basically replaced the original parent and reverted the
object at the end. It didn't do anything with the setParent()
though obviously it would use the replaced parent.


  | /*
  | * JBoss, Home of Professional Open Source
  | * Copyright 2006, 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.builder.runtime;
  | 
  | import java.lang.reflect.InvocationTargetException;
  | import java.util.HashMap;
  | import java.util.Map;
  | 
  | import javax.xml.namespace.NamespaceContext;
  | import javax.xml.namespace.QName;
  | 
  | import org.jboss.util.Strings;
  | import org.jboss.util.collection.WeakIdentityHashMap;
  | import org.jboss.xb.binding.builder.properties.Property;
  | import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
  | import org.jboss.xb.binding.sunday.unmarshalling.ParticleHandler;
  | import org.jboss.xb.binding.sunday.unmarshalling.impl.runtime.RtElementHandler;
  | import org.xml.sax.Attributes;
  | 
  | /**
  |  * HolderParticleHandler.
  |  * 
  |  * @author <a href="adrian at jboss.com">Adrian Brock</a>
  |  * @version $Revision: 1.1 $
  |  */
  | public class HolderParticleHandler implements ParticleHandler
  | {
  |    /** The delegate */
  |    private ParticleHandler delegate = RtElementHandler.INSTANCE;
  | 
  |    /** The holders */
  |    private Map<QName, Property> holders = new HashMap<QName, Property>(); 
  |    
  |    /** Interposed objects TODO This is a hack! */
  |    private ThreadLocal<WeakIdentityHashMap> interposed = new ThreadLocal<WeakIdentityHashMap>();
  |    
  |    /**
  |     * Add a holder
  |     * 
  |     * @param qName the qualified name to hold
  |     * @param property the property
  |     * @throws IllegalArgumentException for a null qName, property, or the property has no read method
  |     */
  |    public void addHolder(QName qName, Property property)
  |    {
  |       if (qName == null)
  |          throw new IllegalArgumentException("Null qName");
  |       if (property == null)
  |          throw new IllegalArgumentException("Null property");
  |       if (property.getRead() == null)
  |          throw new IllegalArgumentException("Property has no getter " + property);
  |       holders.put(qName, property);
  |    }
  |    
  |    public Object startParticle(Object parent, QName elementName, ParticleBinding particle, Attributes attrs, NamespaceContext nsCtx)
  |    {
  |       Object result = delegate.startParticle(parent, elementName, particle, attrs, nsCtx);
  |       Object original = result;
  |       if (result != null)
  |       {
  |          Property property = holders.get(elementName);
  |          if (property != null)
  |          {
  |             try
  |             {
  |                result = property.getRead().invoke(original, null);
  |             }
  |             catch (InvocationTargetException e)
  |             {
  |                throw new RuntimeException("Error retrieving holder " + property.getRead().getName() + " for " + Strings.defaultToString(original), e.getCause());
  |             } 
  |             catch (Exception e)
  |             {
  |                throw new RuntimeException("Error retrieving holder " + property.getRead().getName() + " for " + Strings.defaultToString(original), e);
  |             }
  |             if (result == null)
  |             {
  |                if (property.getWrite() == null)
  |                   throw new IllegalStateException("Unable to retrieve holder " + property.getRead().getName() + " for " + Strings.defaultToString(original) + " and the there is not setter method.");
  |                try
  |                {
  |                   result = property.getPropertyImpl().newInstance();
  |                }
  |                catch (Exception e)
  |                {
  |                   throw new RuntimeException("Error instantiating new holder " + property.getPropertyImpl().getName(), e);
  |                }
  |             }
  |             Object[] arguments = { result };
  |             try
  |             {
  |                property.getWrite().invoke(original, arguments);
  |             }
  |             catch (InvocationTargetException e)
  |             {
  |                throw new RuntimeException("Error setting holder " + property.getWrite().getName() + " for " + Strings.defaultToString(original) + " with " + Strings.defaultToString(result), e.getCause());
  |             } 
  |             catch (Exception e)
  |             {
  |                throw new RuntimeException("Error setting holder " + property.getWrite().getName() + " for " + Strings.defaultToString(original) + " with " + Strings.defaultToString(result), e);
  |             }
  |             
  |             if (result != original)
  |             {
  |                WeakIdentityHashMap map = interposed.get();
  |                if (map == null)
  |                {
  |                   map = new WeakIdentityHashMap();
  |                   interposed.set(map);
  |                }
  |                map.put(result, original);
  |             }
  |          }
  |       }
  |       return result;
  |    }
  | 
  |    public Object endParticle(Object o, QName elementName, ParticleBinding particle)
  |    {
  |       WeakIdentityHashMap map = interposed.get();
  |       if (map != null)
  |       {
  |          Object object = map.remove(o);
  |          if (object != null)
  |             o = object;
  |       }
  |       return delegate.endParticle(o, elementName, particle);
  |    }
  | 
  |    public void setParent(Object parent, Object o, QName elementName, ParticleBinding particle, ParticleBinding parentParticle)
  |    {
  |       delegate.setParent(parent, o, elementName, particle, parentParticle);
  |    }
  | }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3977261#3977261

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3977261



More information about the jboss-dev-forums mailing list