package advantage.alice.service.bond.webimpl;

import java.io.IOException;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.encoding.Serializer;
import org.apache.axis.utils.XMLUtils;
import org.apache.axis.wsdl.fromJava.Types;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;

import advantage.alice.service.bond.IrpBond;
import advantage.alice.service.bond.xml.IrpBondXmlConverter;

/**
 * Implementation of AXIS serialization interface to convert Issuers to the proper
 * xml stream.
 *
 * @author Robert West
 * @version 1.0
 */

public class IrpBondSerializer implements Serializer
{

  /**
   * Called when AXIS needs to serialize the given object for SOAP transmission
   *
   * @param name ???
   * @param attrs ???
   * @param obj The Issuer to be serialized.  Must be an issuer.
   * @param context The context in which to serialize the issuer.
   * @throws java.io.IOException If the object passed is not an issuer.
   */
  public void serialize(QName name, Attributes attrs, Object obj, SerializationContext context) throws java.io.IOException
  {
    if(!(obj instanceof IrpBond))
    {
      throw new IOException("Can't serialize an instance of " + obj.getClass().getName() + " with an IrpBondSerializer!");
    }

    try {
      Element element = IrpBondXmlConverter.writeBond((IrpBond) obj,  XMLUtils.newDocument());
      
      for(int i = 0; i < attrs.getLength(); i++)
      {
        element.setAttribute(attrs.getQName(i), attrs.getValue(i));
      }
      
      context.writeDOMElement(element);
    } catch (ParserConfigurationException e) {
			System.out.println("ParserConfigurationException encoding bond!");
      e.printStackTrace();
    }
  }

  public String getMechanismType()
  {
    return org.apache.axis.Constants.AXIS_SAX;
  }

	/**
	 * Called by AXIS to request the schema used in this encoding.
	 *
	 * @param cls The class of the type needing a schema definition.
	 * @param types The object representing the cumulative types being sent to the client.
	 * @return A DOM element representing the schema.
	 * @throws java.lang.Exception If the schema is not available or cannot be parsed.
	 */
  public Element writeSchema(Class cls, Types types) throws Exception {
  	Element result = null;
  	
  	if(cls == IrpBond.class) {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setNamespaceAware(true);
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(IrpBondSerializer.class.getResource("alicebond.xsd").toExternalForm());

			NodeList list = doc.getElementsByTagNameNS("*", "complexType");
      
			if(list.getLength() == 1) {
				result = (Element) list.item(0);
				System.out.println("Retrieved bond type and returning!");
				types.writeSchemaElement(new QName("http://alice.bankofamerica.com/schemas/bond", "irpbond"), result);
			}  		
  	}
  	
    return null;
  }
}
