Click here to Skip to main content
15,886,724 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.cpp $
// $Header: /SbjDev/SbjCore/XMLUtils.cpp 4     10/16/08 2:41a Steve $
//
//	Copyright � 2008 SbjCat
// All rights reserved.
//
//
// *** Authors ***
//	 Steve Johnson
//
// $Revision: 4 $
//
//-----------------------------------------------------------------------------

#include "stdafx.h"
#include "XMLUtils.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

namespace SbjCore
{
	namespace Utils
	{
		namespace Xml
		{
			MSXML2::IXMLDOMDocument2Ptr CreateDoc(LPCTSTR lpszDocElementName /*= NULL*/)
			{
				MSXML2::IXMLDOMDocument2Ptr spDoc;

				HRESULT hr = spDoc.CreateInstance(L"Msxml2.DOMDocument.4.0");

				if (SUCCEEDED(hr))
				{
					spDoc->async = false;
					spDoc->setProperty(_T("SelectionLanguage"), _T("XPath"));

					if (lpszDocElementName != NULL)
					{
						spDoc->appendChild(CreateElement(lpszDocElementName));
					}
				}

				return spDoc;
			}



			MSXML2::IXMLDOMElementPtr LoadFile(LPCTSTR lpszFileName, 
				LPCTSTR lpszDocElementName /*= NULL*/, 
				MSXML2::IXMLDOMDocument2Ptr spDoc /*= NULL*/)
			{
				MSXML2::IXMLDOMElementPtr spDocElement = NULL;

				if (spDoc == NULL)
				{
					spDoc = CreateDoc();
				}

				spDoc->put_validateOnParse(VARIANT_FALSE);

				if (spDoc->load(lpszFileName))
				{
					spDocElement = GetDocElement(spDoc);

					if ((lpszDocElementName != NULL) && (spDocElement != NULL))
					{
						CString sDocElementName(lpszDocElementName);

						_bstr_t bstr = spDocElement->GetnodeName();			

						if (sDocElementName.CompareNoCase((LPCTSTR)bstr) != 0)
						{
							spDocElement = NULL;
						}

					}
				}

				return spDocElement;
			}



			MSXML2::IXMLDOMElementPtr LoadXML(LPCTSTR lpszXML, MSXML2::IXMLDOMDocument2Ptr spDoc /*= NULL*/)
			{
				MSXML2::IXMLDOMElementPtr spDocElement = NULL;

				if (spDoc == NULL)
				{
					spDoc = CreateDoc();
				}

				if (spDoc->loadXML(lpszXML))
				{
					spDocElement = GetDocElement(spDoc);
				}

				return spDocElement;
			}

			MSXML2::IXMLDOMElementPtr GetDocElement(MSXML2::IXMLDOMDocument2Ptr spDoc)
			{
				MSXML2::IXMLDOMElementPtr spDocElement = NULL;

				HRESULT hr = spDoc->get_documentElement(&spDocElement);

				if (FAILED(hr))
				{
					_com_issue_error(hr);
				}

				return spDocElement;
			}

			MSXML2::IXMLDOMElementPtr AFX_EXT_API GetDocElement( MSXML2::IXMLDOMNodePtr spNode )
			{
				MSXML2::IXMLDOMDocumentPtr spDoc = NULL;
				
				HRESULT hr = spNode->get_ownerDocument(&spDoc);
				MSXML2::IXMLDOMDocument2Ptr spDoc2 = spDoc;

				if (FAILED(hr))
				{
					_com_issue_error(hr);
				}
				return GetDocElement(spDoc2);
			}

			MSXML2::IXMLDOMElementPtr CreateElement(LPCTSTR lpszName)
			{
				MSXML2::IXMLDOMDocument2Ptr spDoc = CreateDoc();
				MSXML2::IXMLDOMElementPtr spElement = spDoc->createElement(lpszName);
				return spElement;
			}


			MSXML2::IXMLDOMElementPtr CreateElement(LPCTSTR lpszName, _variant_t value)
			{
				MSXML2::IXMLDOMDocument2Ptr spDoc = CreateDoc();

				MSXML2::IXMLDOMElementPtr spElement = spDoc->createElement(lpszName);

				value.ChangeType(VT_BSTR);
				spElement->Puttext(value.bstrVal);

				BSTR bstrName;

				spElement->get_nodeName(&bstrName);
				_variant_t v = spElement->Gettext();

				return spElement;

			}

			MSXML2::IXMLDOMElementPtr FromClipboard()
			{
				MSXML2::IXMLDOMElementPtr spElement = NULL;
				COleDataObject obj;
				obj.AttachClipboard();
				STGMEDIUM stg;

				if (obj.GetData(CF_TEXT, &stg))
				{
					CString s((LPCTSTR)::GlobalLock(stg.hGlobal));

					OpenClipboard(NULL);
					EmptyClipboard();
					CloseClipboard();

#if _DEBUG
					CFile f;

					f.Open(_T("CLIPBOARD.Xml"),CFile::modeWrite |
						CFile::shareExclusive | CFile::modeCreate );

					f.Write(s.GetBuffer(0), s.GetLength());

					f.Close();
#endif

					spElement = LoadXML(s);

				}

				::ReleaseStgMedium(&stg);

				return spElement;
			}



			_variant_t GetChildValue(LPCTSTR lpszName, MSXML2::IXMLDOMNodePtr spNode)
			{
				_variant_t v;

				MSXML2::IXMLDOMNodePtr spElement(spNode->selectSingleNode(lpszName));

				if (spElement)
				{
					v = spElement->Gettext();
				}

				return v;
			}

			CString GetChildValueStr(LPCTSTR lpszName, MSXML2::IXMLDOMNodePtr spNode)
			{
				return (LPCTSTR)(_bstr_t)GetChildValue(lpszName, spNode);	
			}

			CString AFX_EXT_API GetAttributeStr( LPCTSTR lpszName, MSXML2::IXMLDOMElementPtr spNode )
			{
				CString s;
				try 
				{
					_variant_t v = spNode->getAttribute(lpszName);
					if (v.vt != VT_NULL)
					{
						s = (LPCTSTR)(_bstr_t)v;
					}
				}
				catch (COleException* p) // the new Atl CString throws this 
				{
					p->Delete();
				}
				catch (_com_error& e) // native type throw this
				{
					e;
				}
				return s;
			}


			MSXML2::IXMLDOMDocument2Ptr LoadXSLFile(LPCTSTR lpszFileName)
			{
				MSXML2::IXMLDOMDocument2Ptr spDoc;

				HRESULT hr = spDoc.CreateInstance(L"Msxml2.DOMDocument.4.0");

				if (FAILED(hr))
				{
					_com_issue_error(hr);
				}

				spDoc->async = false;

				if (!spDoc->load(lpszFileName))
				{
					spDoc = NULL;
				}

				return spDoc;	
			}

			CString MakeTag(const CString& s)
			{
				CString sTag;
				sTag.Format(_T("<%s>\n"), s);
				return sTag;
			}

			CString MakeEndTag(const CString& s)
			{
				CString sTag;
				sTag.Format(_T("</%s>\n"), s);
				return sTag;
			}



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

			Action::Action(t_HandlerMap* pMap /*= NULL*/, CRuntimeClass* pDefault /*= NULL*/) :
				pTheMap(pMap),
				pTheDefault(pDefault)
			{
			}

			Action::~Action()
			{
			}

			void Action::SetDefaultActionHandler(CRuntimeClass* pDefault)
			{
				pTheDefault = pDefault;
			}

			int Action::Apply(MSXML2::IXMLDOMNodePtr spNode)
			{
				Results eRslt = kContinue;
				ActionHandler* pTheHandler = NULL;

				BSTR bstr = spNode->GetnodeName();
				CString s(bstr);
				
				if (pTheMap != NULL)
				{
					t_HandlerMap::iterator iter = pTheMap->find((LPCTSTR)s);

					if (iter != pTheMap->end())
					{
						try
						{
							CRuntimeClass* pClass = iter->second;
							pTheHandler = dynamic_cast<ActionHandler*>(pClass->CreateObject());
							ASSERT(pTheHandler != NULL);
						}
						catch (...)
						{
							ASSERT(FALSE);
						}
					}
				}

				if (NULL == pTheHandler)
				{
					if (pTheDefault != NULL)
					{
						pTheHandler = dynamic_cast<ActionHandler*>(pTheDefault->CreateObject());
					}
					else
					{
						CRuntimeClass* pClass = RUNTIME_CLASS(ActionHandler);
						pTheHandler = dynamic_cast<ActionHandler*>(pClass->CreateObject());
					}
				}

				if (pTheHandler != NULL)
				{
					CString s = (LPCTSTR)spNode->GetnodeName();
					pTheHandler->Initialize(this, spNode);
					
					eRslt = (Results)pTheHandler->BeginHandling();
				}			

				if (kContinue == eRslt)
				{
					if (spNode->hasChildNodes())
					{

						MSXML2::IXMLDOMNodeListPtr spNodes(spNode->GetchildNodes());

						int nCount = spNodes->Getlength();

						for (int nIndex = 0; nIndex < nCount; nIndex++)
						{
							MSXML2::IXMLDOMNodePtr spChild = spNodes->Getitem(nIndex);

							if (spChild != NULL)
							{
								eRslt = (Results)Apply(spChild);
							}
							if (eRslt != kContinue)
							{
								break;
							}
						}
					}
				}

				if (pTheHandler != NULL)
				{
					if (kContinue == eRslt)
					{
						eRslt = (Results)pTheHandler->EndHandling();
					}
					delete pTheHandler;
				}

				return eRslt;
			}


			/////////////////////////////////////////////////////////////////////////////////////////
			
			IMPLEMENT_DYNCREATE(ActionHandler, CObject)

			void ActionHandler::Initialize(Action* pAction, MSXML2::IXMLDOMNodePtr spNode)
			{
				pTheAction = pAction;
				spTheNode = spNode;
			}

			int ActionHandler::BeginHandling()
			{
				return OnBeginHandling();
			}
			int ActionHandler::EndHandling()
			{
				return OnEndHandling();
			}

			ActionHandler::ActionHandler()
			{
			}

			int ActionHandler::OnBeginHandling()
			{
				return Action::kContinue;
			}
			int ActionHandler::OnEndHandling()
			{
				return Action::kContinue;
			}

			
		}
	}
}

//*** Modification History ***
// $Log: /SbjDev/SbjCore/XMLUtils.cpp $
// 
// 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