/*
 * 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.net.jmx.adaptor;

import org.jboss.axis.encoding.DeserializationContext;
import org.jboss.axis.encoding.Deserializer;
import org.jboss.axis.encoding.DeserializerImpl;
import org.jboss.axis.encoding.Target;
import org.jboss.axis.encoding.TypeMapping;
import org.jboss.axis.message.SOAPHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import javax.management.Attribute;
import javax.xml.namespace.QName;

/**
 * An <code>AttributeDeser</code> is be used to deserialize
 * Attribute objects using the <code>SOAP-ENC</code>
 * encoding style.<p>
 * @since 26.04.03
 * @author <a href="mailto:a_taherkordi@users.sourceforge.net">Alireza Taherkordi</a>
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @version $Revision: 1.5.4.3 $
 */
public class AttributeDeser extends DeserializerImpl
{

   //
   // Attributes
   //

   protected String attributeName;
   protected Object attributeValue;
   protected QName xmlType;

   //
   // Constructors
   //

   public AttributeDeser(Class javaClass, QName xmlType)
   {
      this.xmlType = xmlType;
   }

   /** we can already defer the attribute name */
   public void onStartElement(String namespace,
                              String localName,
                              String qName,
                              Attributes attributes,
                              DeserializationContext context)
           throws SAXException
   {
      attributeName = attributes.getValue("", "name");
   }

   /** dispatch to the deserializer for the value element */
   public SOAPHandler onStartChild(String namespace,
                                   String localName,
                                   String prefix,
                                   Attributes attributes,
                                   DeserializationContext context)
           throws SAXException
   {
      if (localName.equals("value") && namespace.equals(""))
      {
         QName qn =
                 context.getTypeFromAttributes(namespace, localName, attributes);
         // get the deserializer
         Deserializer dSer = context.getDeserializerForType(qn);
         // If no deserializer, use the base DeserializerImpl.
         // There may not be enough information yet to choose the
         // specific deserializer.
         if (dSer == null)
         {
            dSer = new DeserializerImpl();
            // determine a default type for this child element
            TypeMapping tm = context.getTypeMapping();
            dSer.setDefaultType(tm.getTypeQName(Object.class));
            dSer.registerValueTarget(new AttributeValueTarget());
         }
         return (SOAPHandler)dSer;
      }
      else
      {
         return null;
      }
   }

   /**
    * Append any characters to the value.  This method is defined by 
    * Deserializer.
    */
   public void onEndElement(String namespace,
                            String localName,
                            DeserializationContext context)
           throws SAXException
   {
      if (isNil)
      {
         value = null;
         return;
      }

      value = new Attribute(attributeName, attributeValue);
   }

   //
   // Inner classes
   //

   protected class AttributeValueTarget implements Target
   {
      public void set(Object value) throws SAXException
      {
         attributeValue = value;
      }
   }

}
