//---------------------- CSPTemplate.h ------------------------

//*************************************************************
//*****       Copyright ©2002 - 2004 Micronovae Ltd       *****
//*****                All rights reserved                *****
//*************************************************************


#ifndef _CSPTEMPLATE
#define _CSPTEMPLATE

#include "CSPConf.h"
#include "CSPLib.h"
#include <httpext.h>
#include "CSPString.h"
#include "CSPList.h"
#include "CSPMap.h"

#define TAG_TYPE_OPENING		1
#define TAG_TYPE_CLOSING		2
#define TAG_TYPE_OPEN_CLOSE		3

class CSPTOOLS_CLASS_EXPORT CSPTemplate : public CSPObject
{
	public:
		void SetCurrentDirectory( const CSPString& strDir );
		unsigned long GetCount() const;
		CSPTemplate& operator [] ( unsigned long ulIndex ) const;
		const CSPString& GetName() const;
		
		CSPTemplate();
		virtual ~CSPTemplate();
		CSPTemplate( const CSPTemplate& Template );
		CSPTemplate& operator = ( const CSPTemplate& Template );
		CSPTemplate& operator()( const CSPString& strTag ) const;
		operator const CSPString&() const;
		bool LoadFromFile( const CSPString& strFilename );
		void Build( const CSPString& strText );
		void Echo( const CSPString& strText );
		void Echo();
		void Render();
		void Clear();

		bool MustClearChildren;

	protected:
		struct POSITION_ENTRY
		{
			POSITION_ENTRY() : ulPosition( 0 ), pTemplate( 0 ){};
			unsigned long ulPosition;
			CSPTemplate* pTemplate;
		};

		typedef CSPMap< CSPString, CSPTemplate > CSPTemplateMap;
		typedef CSPList< POSITION_ENTRY > CSPPositionList;

		CSPTemplateMap ChildrenMap;
		CSPPositionList ChildrenPositionList;
		CSPString strTemplateText;
		CSPString strName;
		CSPPtr<CSPTemplate> spDeadTemplate;
		CSPString strOutput;
		CSPString strCurDirectory;
		unsigned long ulIndex;

		void CleanUp();
		CSPTemplate( bool bHasDead );
		bool GetNextTag( const char* pszText, CSPString& strTagName, int* piType, unsigned long* pulOffset, const char** pTagEnd );
		void BuildInternal( const char* pszText, const char** pszEndTag = 0, const CSPString& strTagName = CSPString( "__root__" ), bool bRecursive = false );
};

#endif
