// SOAPEventHandler.h: interface for the WMSOAPEventHandler class.
//
//////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2000 - 2002 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.
//
//////////////////////////////////////////////////////////////////////
//
// WMSOAPParseEventHandler class - implementation of the parser event
// interface used as part of a SOAP processor. Uses a stack based approach
// inspired by the article "Loading XML Documents in C++" by Jim Beveridge
// in Visual C++ Developers Journal, January, 2000.
//  
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SOAPEVENTHANDLER_H__443CE235_34D5_11D4_9E67_00105AABC815__INCLUDED_)
#define AFX_SOAPEVENTHANDLER_H__443CE235_34D5_11D4_9E67_00105AABC815__INCLUDED_

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

#include "SOAPElements.h"

class WMSOAPParseEventHandler : public WMXMLEventHandler 
{
public:
	WMSOAPParseEventHandler() {}
	virtual ~WMSOAPParseEventHandler() {}

    void StartDocument()
	{
		if (m_pTopHandler != NULL)
			m_Handlers.push(m_pTopHandler);
		else
			m_Handlers.push(new WMSOAPNullHandler());
	}

    void EndDocument() {}

    WMXMLEventHandler *StartElement(const wstring& wstrURI,
									const wstring& wstrLocalName,
									const wstring& wstrQualifiedName,
									WMXMLAttributeMap& mapAttributes,
									WMXMLElement *pElement)
	{
		WMSOAPEventHandler* curHandler = m_Handlers.top();
		WMSOAPEventHandler* nextHandler = m_Handlers.top()->StartElement(wstrURI,
																		wstrLocalName,
																		wstrQualifiedName,
																		mapAttributes,
																		pElement->GetNSPrefixMap(),
																		pElement);
		m_Handlers.push(nextHandler != NULL ? nextHandler : new WMSOAPNullHandler());
		return NULL;
	}

	bool EndElement(const wstring& wstrURI,
					const wstring& wstrLocalName,
					const wstring& wstrQualifiedName,
					WMXMLElement *pElement)
	{
		WMSOAPEventHandler* curHandler = m_Handlers.top();
		if (m_Handlers.top()->EndElement(wstrURI, wstrLocalName, wstrQualifiedName, pElement) == true)
			m_Handlers.pop();
		return true;
	}

    void Characters(const wstring& wstrChars)
	{
		m_Handlers.top()->Characters(wstrChars);
	}

	void ParseError(int m_nLineNbr) {}

	void ProcessingInstruction(const wstring& wstrTarget, const wstring& wstrContent) {}

	void StartNSPrefixMapping(const wstring& wstrPrefix, const wstring& wstrURI)
						{ m_mapNSPrefixes[wstrPrefix] = wstrURI; }

	void EndNSPrefixMapping(const wstring& wstrPrefix)
						{ m_mapNSPrefixes.erase(wstrPrefix); }

	void SetTopHandler(WMSOAPEventHandler *pTopHandler)
	{
		m_pTopHandler = pTopHandler;
	}

private:
	stack<WMSOAPEventHandler *>	m_Handlers;
	NSPrefixMap					m_mapNSPrefixes;
	WMSOAPEventHandler			*m_pTopHandler;
};

#endif // !defined(AFX_SOAPEVENTHANDLER_H__443CE235_34D5_11D4_9E67_00105AABC815__INCLUDED_)
