// WMSOAPSerializer.h: interface for the WMSOAPSerializer class.
//
//////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2003 Robert Cunnings. All rights reserved.
//
// You may copy, modify, distribute or publish this code free of charge.
//
// NO WARRANTY: This software is provided AS IS, without warranty of any kind.
//
//////////////////////////////////////////////////////////////////////
//
// WMSOAPSerializer class - implementation of SOAP message Serialization class
// Creates XML wire representation of SOAP message for transport via HTTP.
// Both "encoded" (SOAP encoding only) or "literal" usage supported.
//  
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_WMSOAPSERIALIZER_H__DA4D8FD2_9872_4A6D_AA9C_272E47F776EA__INCLUDED_)
#define AFX_WMSOAPSERIALIZER_H__DA4D8FD2_9872_4A6D_AA9C_272E47F776EA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <comdef.h>
#include "SOAPEventHandler.h"

interface Iwmwsdlclient2;

class WMSOAPMessage;

class WMSOAPSerializer  
{
public:
	enum XSDTypes {
		XT_ANYTYPE = 256,
		XT_DATE = 257,
		XT_TIME = 258,
		XT_DATETIME = 259,
		XT_BASE64 = 260,
		XT_HEXBIN = 261,
		XT_BOOLEAN = 262,
		XT_ANYURI = 263
	};

	WMSOAPSerializer();
	virtual ~WMSOAPSerializer();

	bool Serialize(WMSOAPMessage& Msg, Iwmwsdlclient2 *pIWSDLClient);

private:
	WMXMLElement *SerializeBuiltInType( const wstring& wstrElemName,
										const wstring& wstrType,
										const wstring& wstrTypeNS,
										_variant_t& varValue,
                                        const wstring& EncStyle,
										WMSOAPMessage& Msg);

	WMXMLElement *SerializeBuiltInTypeLit(  const wstring& wstrElemName,
                                            const wstring& wstrElemNS,
										    const wstring& wstrType,
										    const wstring& wstrTypeNS,
										    _variant_t& varValue,
										    WMSOAPMessage& Msg);

	WMXMLElement *SerializeComplexType( const wstring& wstrElemName,
										BSTR bstrType,
										BSTR bstrTypeNS,
										_variant_t& varValue,
                                        const wstring& wstrEncStyle,
										WMSOAPMessage& Msg,
										Iwmwsdlclient2 *pIWSDLClient);

	WMXMLElement *SerializeComplexTypeLit(  const wstring& wstrElemName,
                                            const wstring& wstrElemNS,
										    BSTR bstrType,
										    BSTR bstrTypeNS,
										    _variant_t& varValue,
										    WMSOAPMessage& Msg,
										    Iwmwsdlclient2 *pIWSDLClient);

	WMXMLElement *SerializeSOAPMsgParam(  _variant_t& varData,
										  const wstring& wstrMethodName,
										  const wstring& wstrElemName,
                                          const wstring& EncStyle,
										  WMSOAPMessage& Msg,
										  Iwmwsdlclient2 *pIWSDLClient);

	WMXMLElement *SerializeSOAPMsgLitParam(  _variant_t& varData,
										    const wstring& wstrMethodName,
										    const wstring& wstrElemName,
										    WMSOAPMessage& Msg,
										    Iwmwsdlclient2 *pIWSDLClient);

	WMXMLElement *SerializeSOAPMsg( WMSOAPMessage& Msg,
										Iwmwsdlclient2 *pIWSDLClient);

	WMXMLElement *SerializeSOAPFaultMsg( WMSOAPMessage& Msg,
										Iwmwsdlclient2 *pIWSDLClient);

	WMXMLElement *SerializeHeaderElem(  const wstring& wstrNS,
										const wstring& wstrName,
										_variant_t& varValue,
										bool bMustUnderstand,
										const wstring& wstrActor,
                                        bool bRelay,
										WMSOAPMessage& Msg,
										Iwmwsdlclient2 *pIWSDLClient);

	WMXMLElement *SerializeFaultDetailElem( const wstring& wstrNS,
											const wstring& wstrName,
											_variant_t& varValue,
											WMSOAPMessage& Msg,
											Iwmwsdlclient2 *pIWSDLClient);

	WMXMLElement *ParseElementLit(const wstring& wstrLiteralXML);

	bool SerializeSafeArray(long lDim,
							long *arrIndex,
							SAFEARRAY *pArray,
							WMXMLElement *pOutParam,
							BSTR bstrType,
							BSTR bstrTypeNS,
                            const wstring& wstrEncStyle,
							WMSOAPMessage& Msg,
							Iwmwsdlclient2 *pIWSDLClient);

	bool SerializeSafeArrayLit( SAFEARRAY *pArray,
							    WMXMLElement *pOutParam,
							    const wstring& wstrItemType,
							    const wstring& wstrItemTypeNS,
                                const wstring& wstrItemName,
                                const wstring& wstrItemNS,
							    WMSOAPMessage& Msg,
							    Iwmwsdlclient2 *pIWSDLClient);

	bool SerializeBase64Vector(SAFEARRAY *pArray, WMXMLElement *pOutParam);
	bool SerializeHexBinaryVector(SAFEARRAY *pArray, WMXMLElement *pOutParam);
	bool SerializeDateTime(DATE d, WMXMLElement *pOutParam);
	bool SerializeDate(DATE d, WMXMLElement *pOutParam);
	bool SerializeTime(DATE d, WMXMLElement *pOutParam);

	bool SerializeStruct(  BSTR bstrStructName,
						   BSTR bstrStructNS,
						   IDispatch *pIDispatch,
						   WMXMLElement *pOutParam,
                           const wstring& wstrEncStyle,
						   WMSOAPMessage& Msg,
						   Iwmwsdlclient2 *pIWSDLClient);
	bool SerializeStructLit(const wstring& wstrStructName,
						    const wstring& strStructNS,
						    IDispatch *pIDispatch,
						    WMXMLElement *pOutParam,
						    WMSOAPMessage& Msg,
						    Iwmwsdlclient2 *pIWSDLClient);

	bool IsXSDType(const wstring& wstrTypeNS)
	{
		if (wstrTypeNS == g_pwszXMLSchemaNS2001 ||
			wstrTypeNS == g_pwszXMLSchemaNS1999 ||
			wstrTypeNS == g_pwszXMLSchemaNS2000)
		{
			return true;
		}
		return false;
	}
	wchar_t *GetXSDInstanceNS(const wstring& wstrTypeNS)
	{
		if (wstrTypeNS == g_pwszXMLSchemaNS2001)
		{
			return g_pwszXMLSchemaInstNS2001;
		}
		if (wstrTypeNS == g_pwszXMLSchemaNS1999)
		{
			return g_pwszXMLSchemaInstNS1999;
		}
		if (wstrTypeNS == g_pwszXMLSchemaNS2000)
		{
			return g_pwszXMLSchemaInstNS2000;
		}
		return NULL;
	}
	bool IsSoapEncType(const wstring& wstrTypeNS)
	{
		if (wstrTypeNS == g_pwszSoapEncodingNS || wstrTypeNS == g_pwszSoap12EncodingNS)
		{
			return true;
		}
		return false;
	}
	wstring GenerateId();
	// the parser
	WMXMLParser m_Parser;
	// type map
	map<wstring, VARTYPE>	m_mapVarTypes;
};

#endif // !defined(AFX_WMSOAPSERIALIZER_H__DA4D8FD2_9872_4A6D_AA9C_272E47F776EA__INCLUDED_)
