/*
 * 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.Constants;
import org.jboss.axis.encoding.SerializationContext;
import org.jboss.axis.encoding.Serializer;
import org.jboss.axis.encoding.XMLType;
import org.jboss.axis.wsdl.fromJava.Types;
import org.w3c.dom.Element;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.AttributesImpl;

import javax.management.Attribute;
import javax.xml.namespace.QName;
import java.io.IOException;

/**
 * An <code>AttributeSer</code> is be used to serialize
 * Attribute objects using the <code>SOAP-ENC</code>
 * encoding style.<p>
 *
 * @author <a href="mailto:a_taherkordi@users.sourceforge.net">Alireza Taherkordi</a>
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @since 26.04.02
 * @version $Revision: 1.6.4.3 $
 */

public class AttributeSer implements Serializer
{

   /** this is the fully-qualified type that we serialize into */
   protected QName xmlType;

   //
   // Constructors
   //

   public AttributeSer(Class javaType, QName xmlType)
   {
      this.xmlType = xmlType;
   }

   //
   // Public API
   //

   /**
    *  turns a JMX objectname into a string-based xml element
    *  @param name the name of the element that carries our type
    *  @param attributes the attributes of the element that carries our type
    *  @param value the objectname to serialize
    *  @param context the serialization context we live into
    */
   public void serialize(QName name,
                         Attributes attributes,
                         Object value,
                         SerializationContext context)
           throws IOException
   {

      // do some initialisation of attributes
      AttributesImpl attrs;
      if (attributes != null)
         attrs = new AttributesImpl(attributes);
      else
         attrs = new AttributesImpl();

      // next we utter the attribute name as an attribute
      QName qname = new QName("", "name");
      attrs.addAttribute(qname.getNamespaceURI(),
              qname.getLocalPart(),
              context.qName2String(qname),
              "CDATA",
              ((Attribute)value).getName());

      // start the attribute tag
      context.startElement(name, attrs);

      // next we utter an embedded value object of any-type with the
      // attributes content
      qname = new QName("", "value");
      Object attrValue = ((Attribute)value).getValue();
      // lets hope that Object is mapped semantically to xsd:any??
      context.serialize(qname, null, attrValue);
      // end the attribute tag
      context.endElement();
   }

   /** we use sax approach */
   public String getMechanismType()
   {
      return Constants.AXIS_SAX;
   }

   /**
    * Return XML schema for the specified type.
    * The Attribute type has a string-based name attribute and an
    * case-typed value element. Note that this function has been
    * adopted to the Axis 1.1 Beta Emitter code.
    */

   public Element writeSchema(Class forClass, Types types) throws Exception
   {
      // ComplexType representation of SimpleType bean class
      Element complexType = types.createElement("complexType");
      //types.writeSchemaElement(xmlType, complexType);
      types.writeSchemaElement(xmlType, complexType);
      complexType.setAttribute("name", xmlType.getLocalPart());

      Element nameAttribute =
              types.createAttributeElement("name",
                      XMLType.XSD_STRING.getClass(),
                      XMLType.XSD_STRING,
                      false,
                      complexType.getOwnerDocument());
      complexType.appendChild(nameAttribute);
      Element complexContent = types.createElement("complexContent");
      complexType.appendChild(complexContent);
      Element all = types.createElement("sequence");
      complexContent.appendChild(all);
      Element valueElement =
              types.createElement("value",
                      types.getNamespaces().getCreatePrefix(XMLType.XSD_ANYTYPE.getNamespaceURI())
              + ":"
              + XMLType.XSD_ANYTYPE.getLocalPart(),
                      true,
                      false,
                      all.getOwnerDocument());
      all.appendChild(valueElement);
      return complexType;
   }

}
