Click here to Skip to main content
15,886,840 members
Articles / Desktop Programming / MFC

The SBJ MVC Framework - The Model, from Abstraction to Realization

Rate me:
Please Sign up or sign in to vote.
5.00/5 (19 votes)
20 Mar 2009CPOL19 min read 109.3K   1.3K   51  
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture
//------------------------------------------------------------------------------
// $Workfile: XMLUtils.h $
// $Header: /SbjDev/SbjCore/XMLUtils.h 5     11/12/08 2:22p Steve $
//
//	Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
//	 Steve Johnson
//
// $Revision: 5 $
//
//-----------------------------------------------------------------------------

#pragma once

#import <msxml6.dll>

#include "StringUtils.h"

namespace SbjCore
{
	namespace Utils
	{
		namespace Xml
		{

			MSXML2::IXMLDOMDocument2Ptr AFX_EXT_API CreateDoc(LPCTSTR lpszDocElementName = NULL);


			MSXML2::IXMLDOMElementPtr AFX_EXT_API GetDocElement(MSXML2::IXMLDOMDocument2Ptr spDoc);

			MSXML2::IXMLDOMElementPtr AFX_EXT_API GetDocElement(MSXML2::IXMLDOMNodePtr spNode);

			MSXML2::IXMLDOMElementPtr AFX_EXT_API LoadFile(LPCTSTR lpszFileName, 
				LPCTSTR lpszDocElementName = NULL, // to verify correct xml 
				MSXML2::IXMLDOMDocument2Ptr spDoc = NULL);

			MSXML2::IXMLDOMElementPtr AFX_EXT_API LoadXML(LPCTSTR lpszXML, MSXML2::IXMLDOMDocument2Ptr spDoc = NULL);
			MSXML2::IXMLDOMElementPtr AFX_EXT_API FromClipboard();	

			MSXML2::IXMLDOMElementPtr AFX_EXT_API CreateElement(LPCTSTR lpszName);
			MSXML2::IXMLDOMElementPtr AFX_EXT_API CreateElement(LPCTSTR lpszName, _variant_t value);

			_variant_t AFX_EXT_API GetChildValue(LPCTSTR lpszName, MSXML2::IXMLDOMNodePtr spNode);
			CString AFX_EXT_API GetChildValueStr(LPCTSTR lpszName, MSXML2::IXMLDOMNodePtr spNode);
			CString AFX_EXT_API GetAttributeStr(LPCTSTR lpszName, MSXML2::IXMLDOMElementPtr spNode);

			MSXML2::IXMLDOMDocument2Ptr AFX_EXT_API LoadXSLFile(LPCTSTR lpszFileName);

			CString AFX_EXT_API MakeTag(const CString& s);
			CString AFX_EXT_API MakeEndTag(const CString& s);
			
			///////////////////////////////////////////////////////////////////////////

			template <class VALUE_TYPE>
			HRESULT GetAttribute(MSXML2::IXMLDOMElementPtr spElement, LPCTSTR lpszName, VALUE_TYPE& val)
			{
				HRESULT hr = S_OK;
				_variant_t v;
				try 
				{
					MSXML2::IXMLDOMAttributePtr spAttr = spElement->getAttributeNode(lpszName);
					if (spAttr != NULL)
					{
						v = spElement->getAttribute(lpszName);
						if (v.vt != NULL)
						{
							VALUE_TYPE tempVal = (VALUE_TYPE)v; // this will throw if incompatible types
							val = tempVal; // otherwise assign it 
						}
					}
				}
				catch (COleException* p) // the new Atl CString throws this 
				{
					hr = p->m_sc;
					p->Delete();
				}
				catch (_com_error& e) // native type throw this
				{
					hr = e.Error();
				}

				return hr;
			}

			template <class VALUE_TYPE>
			HRESULT SetAttribute(MSXML2::IXMLDOMElementPtr spElement, LPCTSTR lpszName, const VALUE_TYPE& val)
			{
				HRESULT hr = S_OK;
				try 
				{
					_variant_t v = val;
					spElement->setAttribute(lpszName, v);
				}
				catch (COleException* p)
				{
					hr = p->m_sc;
					p->Delete();
				}
				catch (_com_error& e)
				{
					hr = e.Error();
				}
				return hr;
			}

			///////////////////////////////////////////////////////////////////////////////////////

			class ActionHandler; // forward 

			class AFX_EXT_API Action
			{
			public:
				typedef enum
				{
					kStop		= (-1),
					kRejected	= ( 0),
					kContinue	= ( 1)
				} Results;

				typedef std::map<CString, CRuntimeClass*> t_HandlerMap;

			public:
				Action(t_HandlerMap* pMap = NULL, CRuntimeClass* pDefault = NULL);
				virtual ~Action();
				
				void SetDefaultActionHandler(CRuntimeClass* pDefault);

				int Apply(MSXML2::IXMLDOMNodePtr spNode);

			private:
				t_HandlerMap* pTheMap;
				CRuntimeClass* pTheDefault;
			};

			///////////////////////////////////////////////////////////////////////////////////

			class AFX_EXT_API ActionHandler : public CObject
			{
				DECLARE_DYNCREATE(ActionHandler)
			protected:
				Action* pTheAction; 
				MSXML2::IXMLDOMNodePtr spTheNode;
			public:
				void Initialize(Action* pAction, MSXML2::IXMLDOMNodePtr spNode);

				int BeginHandling();
				int EndHandling();

			protected:
				ActionHandler();
				
			private:
				virtual int OnBeginHandling();
				virtual int OnEndHandling();
							
			};
			
			

		}
	}
}

//*** Modification History ***
// $Log: /SbjDev/SbjCore/XMLUtils.h $
// 
// 5     11/12/08 2:22p Steve
// Finished Generalization of Model  v2.0.1
// 
// 4     10/16/08 2:41a Steve
// Ready for publishing
// 
// 3     10/14/08 1:12p Steve
// Implemented Deletes

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
SBJ
United States United States
Real name is Steve Johnson. Programming since 1979. Started on a Heathkit Micro with a DEC LSI-11 and UCSD Pascal. Moved to PCs & DOS as soon as Turbo Pascal became available. Did some Assembly, ISR, TSR etc. All this while working for a Manufacturing Co. for 8 years. Had my own solo Co. doing barcode labeling software for 4 years (terrible business man, all I wanted to do was code). Since then working for various software companies. Moved to Windows around the time of 3.1 with Borland C then C++. Then on to VC++ and MFC, and just about anything I could get my hands on or had to learn for my job, and been at it ever since. Of course recently I've been playing with .NET, ASP, C#, WPF etc.

Comments and Discussions