Click here to Skip to main content
11,408,835 members (62,995 online)
Click here to Skip to main content
Add your own
alternative version

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

, 20 Mar 2009 CPOL
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture
SbjDevBin201.zip
XmlMvc2090.dll
Shapes.exe
SbjCore2090.dll
SbjDevSrc201.zip
Shapes
Data
res
buttons.bmp
filelarge.bmp
filesmall.bmp
filesmall.png
main.bmp
Shapes.ico
ShapesDoc.ico
Toolbar.bmp
Toolbar256.bmp
treeimages.bmp
vssver.scc
Write - Copy.png
Write.png
Write1.png
writelarge.bmp
writesmall - Copy.bmp
writesmall - Copy.png
writesmall.bmp
writesmall.png
XmlMvc
res
vssver.scc
SbjCore
res
bigcat.ico
main.bmp
properties.bmp
properties_hc.bmp
properties_wnd_hc.ico
vssver.scc
CommonSettings.vsprops
CommonAppDebugSettings.vsprops
CommonAppReleaseSettings.vsprops
CommonDLLDebugSettings.vsprops
CommonDLLReleaseSettings.vsprops
//------------------------------------------------------------------------------
// $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)

Share

About the Author

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.5 | Last Updated 20 Mar 2009
Article Copyright 2008 by SBJ
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid