/*
 * 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.DeserializerTarget;
import org.jboss.axis.message.SOAPHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanOperationInfo;
import javax.xml.namespace.QName;

/**
 * An <code>MBeanInfoDeser</code> is be used to deserialize
 * MBeanInfo using the <code>SOAP-ENC</code>
 * encoding style.<p>
 *
 * @author <a href="mailto:a_taherkordi@users.sourceforge.net">Alireza Taherkordi</a>
 * @version $Revision: 1.5.4.3 $
 */
public class MBeanInfoDeser extends DeserializerImpl
{

   private String className;
   private String description;
   private MBeanAttributeInfo[] attributes;
   private MBeanConstructorInfo[] constructors;
   private MBeanOperationInfo[] operations;
   private MBeanNotificationInfo[] notifications;

   public void onStartElement(String namespace,
                              String localName,
                              String prefix,
                              Attributes attributes,
                              DeserializationContext context)
           throws SAXException
   {

      if (context.isNil(attributes))
      {
         return;
      }

      //nothing to do

   }

   public SOAPHandler onStartChild(String namespace,
                                   String localName,
                                   String prefix,
                                   Attributes attributes,
                                   DeserializationContext context)
           throws SAXException
   {

      // Get the type
      QName itemType =
              context.getTypeFromAttributes(namespace, localName, attributes);
      // Get the deserializer
      Deserializer dSer = null;
      if (itemType != null)
      {
         dSer = context.getDeserializerForType(itemType);
      }
      if (dSer == null)
      {
         dSer = new DeserializerImpl();
      }

      // When the value is deserialized, inform us.
      // Need to pass the localName as a hint
      dSer.registerValueTarget(new DeserializerTarget(this, localName));

      addChildDeserializer(dSer);

      return (SOAPHandler)dSer;
   }

   public void setChildValue(Object value, Object hint) throws SAXException
   {
      if (hint.equals("className"))
         className = (String)value;
      else if (hint.equals("description"))
         description = (String)value;
      else if (hint.equals("attributes"))
         attributes = (MBeanAttributeInfo[])value;
      else if (hint.equals("constructors"))
         constructors = (MBeanConstructorInfo[])value;
      else if (hint.equals("operations"))
         operations = (MBeanOperationInfo[])value;
      else if (hint.equals("notifications"))
         notifications = (MBeanNotificationInfo[])value;

   }

   public void onEndElement(String s,
                            String s1,
                            DeserializationContext deserializationcontext)
   {
      try
      {
         super.value =
                 new MBeanInfo(className,
                         description,
                         attributes,
                         constructors,
                         operations,
                         notifications);
      }
      catch (Exception exception)
      {
         exception.printStackTrace();
      }
   }

}
