⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 docmanage.cpp

📁 文档生成工具 XML例子
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// DocManage.cpp: implementation of the CDocManage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XMLClassDoc.h"
#include "DocManage.h"
#include "StringDefine.h"



#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#include "atlbase.h"
using namespace MSXML2;
extern CXMLClassDocApp theApp;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDocManage::CDocManage()
{
	USES_CONVERSION;
 	HRESULT hr = NULL;
 	hr = ::CoInitialize(NULL);
 	if(FAILED(hr))
 	{
 		MessageBox(0,"error!", 0,0);
 	}
}

CDocManage::~CDocManage()
{
	::CoUninitialize();
}

BOOL CDocManage::ReadClassList(CListCtrl &listctrl,CString strPath)
{
	CString strClassName;
    if (strPath.Right (1) != _T ("\\"))
        strPath += _T ("\\");
    strPath += _T ("*.*");

    HANDLE hFind;
    WIN32_FIND_DATA fd;
    int nCount = 0;

    if ((hFind = ::FindFirstFile (strPath, &fd)) != INVALID_HANDLE_VALUE) 
	{	
		listctrl.DeleteAllItems ();
		CString strFileName;

		 do 
		 {
			  if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
			  {
				 strFileName = fd.cFileName;
				 if(strFileName.Find(FILEEXT) > 0)
				 {
					strClassName = strFileName.Mid(0,strFileName.GetLength()-8);
					int count = listctrl.GetItemCount();
					listctrl.InsertItem(count, strClassName);
					strClassName.Empty();			
				 }
			  }
		 }while (::FindNextFile (hFind, &fd));
		 ::FindClose (hFind);
    }


	/*USES_CONVERSION;
	HRESULT hr = NULL;

	IXMLDOMDocumentPtr pXmlDoc;
	IXMLDOMNodePtr pXmlNode;
	IXMLDOMNodePtr pXmlNameNode;
	IXMLDOMNodeListPtr pXmlNodeList;
	IXMLDOMNamedNodeMapPtr  pXmlNodeMap;
		
	try
	{	
		hr = pXmlDoc.CreateInstance(__uuidof(DOMDocument));
		if(FAILED(hr))
		{
			MessageBox(0,"建立DOMDocument对象失败,请安装msxml组件!",0,0);
			exit(0);
		}
		else
		{
			pXmlDoc->load(_bstr_t(PszClassFilePath));
			pXmlNodeList = pXmlDoc->selectNodes(_bstr_t("Classes/class"));
			pXmlNode = pXmlNodeList->nextNode();
			if(pXmlNode == NULL)
			{
				MessageBox(NULL, "类列表为空", "警告", 0);	
				return true;
			}
			else
			{
				while(pXmlNode != NULL)
				{
					pXmlNodeMap = pXmlNode->Getattributes();
					pXmlNameNode = pXmlNodeMap->getNamedItem(_bstr_t("name"));
					strClassName = W2A(pXmlNameNode->Gettext());
					//listbox.AddString(strClassName);
					int count = listctrl.GetItemCount();
					listctrl.InsertItem(count, strClassName);
					strClassName.Empty();
					pXmlNode = pXmlNodeList->nextNode();
				}
					
			}		
		}
	}
	catch(_com_error& e)
	{
			MessageBox(0, W2A(e.Description()),0,0);
	}
	*/
	return true;
}

CString CDocManage::ReadClassDesc(CString strClassName)
{
	USES_CONVERSION;
	char pszFilePath[160];
	memset(pszFilePath, 0, sizeof(pszFilePath));
	sprintf(pszFilePath,"%s%s%s", theApp.m_strLocPath, strClassName, FILEEXT);
	CString strClassDesc;
	HRESULT hr = NULL;

	IXMLDOMDocumentPtr pXmlDoc;
	IXMLDOMNodePtr pXmlNode;
	IXMLDOMNodeListPtr pXmlNodeList;
	
	try
	{	
		hr = pXmlDoc.CreateInstance(__uuidof(DOMDocument));
		if(FAILED(hr))
		{
			MessageBox(0,"error!",0,0);
		}
		else
		{
			pXmlDoc->load(_bstr_t(pszFilePath));
			pXmlNodeList = pXmlDoc->selectNodes(_bstr_t("Classes/class/classdesc"));
			pXmlNode = pXmlNodeList->nextNode();
			if(pXmlNode == NULL)
			{
				strClassDesc.Empty();
			}
			else
			{
				strClassDesc = W2A(pXmlNode->Gettext());		
			}		
		}
	}
	catch(_com_error& e)
	{
		MessageBox(0, W2A(e.Description()),0,0);
	}
	return strClassDesc;
}

BOOL CDocManage::WriteClassDesc(CString strClassName, CString strClassDesc)
{
	USES_CONVERSION;
 	HRESULT hr = NULL;
	char pszFilePath[160];
	memset(pszFilePath, 0, sizeof(pszFilePath));
	sprintf(pszFilePath,"%s\\%s%s", theApp.m_strLocPath, strClassName, FILEEXT);
	
	IXMLDOMDocumentPtr pXmlDoc;
	IXMLDOMNodePtr pXmlNode;
	IXMLDOMNodeListPtr pXmlNodeList;
	
	try
	{	
		hr = pXmlDoc.CreateInstance(__uuidof(DOMDocument));
		if(FAILED(hr))
		{
			MessageBox(0,"error!",0,0);
		}
		else
		{
			pXmlDoc->load(_bstr_t(pszFilePath));
			pXmlNodeList = pXmlDoc->selectNodes(_bstr_t("Classes/class/classdesc"));
			pXmlNode = pXmlNodeList->nextNode();
			if(pXmlNode == NULL)
			{
				MessageBox(NULL, "类的说明不存在", "警告", 0);	
				strClassDesc.Empty();
			}
			else
			{
				pXmlNode->Puttext(_bstr_t(strClassDesc));		
			}
			pXmlDoc->save(_bstr_t(pszFilePath));
		}
	}
	catch(_com_error& e)
	{
		MessageBox(0, W2A(e.Description()),0,0);
	}
	
	return true;
}

BOOL CDocManage::ReadFunctionInfo(CListCtrl& listCtrl, CString strClassName,  CString strVisit)
{
	USES_CONVERSION;
 	HRESULT hr = NULL;
	char pszFilePath[160];
	memset(pszFilePath, 0, sizeof(pszFilePath));
	sprintf(pszFilePath,"%s\\%s%s", theApp.m_strLocPath, strClassName, FILEEXT);
	IXMLDOMDocumentPtr pXmlDoc;
	IXMLDOMNodePtr pXmlNode;
	IXMLDOMNodePtr pXmlChildNode;
	IXMLDOMNodeListPtr pXmlNodeList;
	IXMLDOMNamedNodeMapPtr  pXmlNodeMap;

	try
	{	
		hr = pXmlDoc.CreateInstance(__uuidof(DOMDocument));
		if(FAILED(hr))
		{
			MessageBox(0,"error!",0,0);
		}
		else
		{
			pXmlDoc->load(_bstr_t(pszFilePath));
			pXmlNodeList = pXmlDoc->selectNodes(_bstr_t("Classes/class/Functions/function[@visit='" + strVisit + "']" ));
			pXmlNode = pXmlNodeList->nextNode();
			
			while(pXmlNode != NULL)
			{
				int nidx = listCtrl.GetItemCount();
				pXmlNodeMap = pXmlNode->Getattributes();
				pXmlChildNode = pXmlNodeMap->getNamedItem(_bstr_t("name"));
				listCtrl.InsertItem(nidx, W2A(pXmlChildNode->Gettext()));

				pXmlChildNode = pXmlNodeMap->getNamedItem(_bstr_t("visit"));
				listCtrl.SetItemText(nidx, 1, W2A(pXmlChildNode->Gettext()));
				
				pXmlChildNode = pXmlNodeMap->getNamedItem(_bstr_t("ID"));
				int ID = atoi (W2A(pXmlChildNode->Gettext()));
				listCtrl.SetItemData(nidx, ID);

				pXmlChildNode = pXmlNode->GetlastChild();
				listCtrl.SetItemText(nidx, 2, W2A(pXmlChildNode->Gettext()));
				pXmlNode = pXmlNodeList->nextNode();		
			}
		}
	}
	catch(_com_error& e)
	{
		MessageBox(0, W2A(e.Description()),0,0);
	}
	
	return true;
}

BOOL CDocManage::ReadPropertyInfo(CListCtrl &listCtrl, CString strClassName, CString strVisit)
{
	USES_CONVERSION;
 	HRESULT hr = NULL;
	char pszFilePath[160];
	memset(pszFilePath, 0, sizeof(pszFilePath));
	sprintf(pszFilePath,"%s\\%s%s", theApp.m_strLocPath, strClassName, FILEEXT);
	
	IXMLDOMDocumentPtr pXmlDoc;
	IXMLDOMNodePtr pXmlNode;
	IXMLDOMNodePtr pXmlChildNode;
	IXMLDOMNodeListPtr pXmlNodeList;
	IXMLDOMNamedNodeMapPtr  pXmlNodeMap;

	try
	{	
		hr = pXmlDoc.CreateInstance(__uuidof(DOMDocument));
		if(FAILED(hr))
		{
			MessageBox(0,"error!",0,0);
		}
		else
		{
			pXmlDoc->load(_bstr_t(pszFilePath));
			pXmlNodeList = pXmlDoc->selectNodes(_bstr_t("Classes/class/Propertys/property[@visit='" + strVisit + "']" ));
			pXmlNode = pXmlNodeList->nextNode();
			
			while(pXmlNode != NULL)
			{
				int nidx = listCtrl.GetItemCount();
				pXmlNodeMap = pXmlNode->Getattributes();
				pXmlChildNode = pXmlNodeMap->getNamedItem(_bstr_t("name"));
				listCtrl.InsertItem(nidx, W2A(pXmlChildNode->Gettext()));

				pXmlChildNode = pXmlNodeMap->getNamedItem(_bstr_t("visit"));
				listCtrl.SetItemText(nidx, 1, W2A(pXmlChildNode->Gettext()));

				pXmlChildNode = pXmlNodeMap->getNamedItem(_bstr_t("ID"));
				int ID = atoi(W2A(pXmlChildNode->Gettext()));
				listCtrl.SetItemData(nidx, ID);

				pXmlChildNode = pXmlNode->GetlastChild();
				listCtrl.SetItemText(nidx, 2, W2A(pXmlChildNode->Gettext()));
				pXmlNode = pXmlNodeList->nextNode();		
			}
		}
	}
	catch(_com_error& e)
	{
		MessageBox(0, W2A(e.Description()),0,0);
	}
	
	return true;
}

BOOL CDocManage::ExistFile(char *pszFilePath)
{
	FILE *pFile;
	pFile = fopen(pszFilePath, "r");
	if(pFile != NULL)
	{
		fclose(pFile);
		return true;
	}
	else
	{
		return false;
	}
}

BOOL CDocManage::WriteClassHead(CString strClassName, CString strBaseClass, CString strClassDesc)
{
	USES_CONVERSION;
 	HRESULT hr = NULL;
	char pszFilePath[160];
	memset(pszFilePath, 0, sizeof(pszFilePath));
	sprintf(pszFilePath,"%s\\%s%s", theApp.m_strLocPath, strClassName, FILEEXT);

	if(ExistFile(pszFilePath))
	{
		if(MessageBox(0, "该类的文档已经存在,要覆盖吗?","警告",MB_YESNO) != IDYES)
		{
			return false;
		}
	}

	IXMLDOMDocumentPtr pXmlDoc;
	IXMLDOMNodePtr pXmlNode;
	IXMLDOMNodeListPtr pXmlNodeList;
	
	try
	{	
		hr = pXmlDoc.CreateInstance(__uuidof(DOMDocument));
		if(FAILED(hr))
		{
			MessageBox(0,"error!",0,0);
			strClassName = "";
		}
		else
		{
			
			//////////////////////////////////////////////
			CString strTemplateXML;
			strTemplateXML = CreateTemplate();
			pXmlDoc->load(_bstr_t(strTemplateXML));
			pXmlNodeList = pXmlDoc->selectNodes(_bstr_t("Classes/class/name"));
			pXmlNode = pXmlNodeList->nextNode();
			pXmlNode->Puttext(_bstr_t(strClassName));
				
			pXmlNodeList = pXmlDoc->selectNodes(_bstr_t("Classes/class/declare"));
			pXmlNode = pXmlNodeList->nextNode();
			pXmlNode->Puttext(_bstr_t("class " + strClassName + ": public " + strBaseClass));

			pXmlNodeList = pXmlDoc->selectNodes(_bstr_t("Classes/class/classdesc"));
			pXmlNode = pXmlNodeList->nextNode();
			pXmlNode->Puttext(_bstr_t(strClassDesc));

			pXmlDoc->save(_bstr_t(pszFilePath));
		}
	}
	catch(_com_error& e)
	{
		MessageBox(0, W2A(e.Description()),0,0);
	}
	return true;
}

CMapStringToString* CDocManage::ReadFunctionInfo(CString strClassName, CString strID)
{
	USES_CONVERSION;
 	HRESULT hr = NULL;
	char pszFilePath[160];
 	memset(pszFilePath, 0, sizeof(pszFilePath));
 	sprintf(pszFilePath,"%s\\%s%s", theApp.m_strLocPath, strClassName, FILEEXT);
 	CMapStringToString* functionMap = new CMapStringToString();
 	
 	IXMLDOMDocumentPtr pXmlDoc;
 	IXMLDOMNodePtr pXmlNode;
 	IXMLDOMNodePtr pXmlChildNode;
 	IXMLDOMNodeListPtr pXmlNodeList;
 	IXMLDOMNamedNodeMapPtr  pXmlNodeMap;
 
 	try
 	{	
 		hr = pXmlDoc.CreateInstance(__uuidof(DOMDocument));
 		if(FAILED(hr))
 		{
 			MessageBox(0,"error!",0,0);
 		}
 		else
 		{
 			pXmlDoc->load(_bstr_t(pszFilePath));
 			pXmlNodeList = pXmlDoc->selectNodes(_bstr_t("Classes/class/Functions/function[@ID='" + strID + "']" ));
 			pXmlNode = pXmlNodeList->nextNode();
 			if(pXmlNode != NULL)
 			{
 				pXmlNodeMap = pXmlNode->Getattributes();
 				pXmlChildNode = pXmlNodeMap->getNamedItem(_bstr_t("visit"));
 				functionMap->SetAt("visit", W2A(pXmlChildNode->Gettext()));
 				
				pXmlNodeList = pXmlNode->GetchildNodes();
				pXmlChildNode = pXmlNodeList->nextNode();
				functionMap->SetAt("fdeclare", W2A(pXmlChildNode->Gettext()));
 				pXmlChildNode = pXmlNodeList->nextNode();
				functionMap->SetAt("freturndesc", W2A(pXmlChildNode->Gettext()));
				pXmlChildNode = pXmlNodeList->nextNode();
				functionMap->SetAt("fparamdesc", W2A(pXmlChildNode->Gettext()));
				pXmlChildNode = pXmlNodeList->nextNode();
				functionMap->SetAt("fexample", W2A(pXmlChildNode->Gettext()));
				pXmlChildNode = pXmlNodeList->nextNode();
				functionMap->SetAt("fremark", W2A(pXmlChildNode->Gettext()));
			}
 		}
 	}
 	catch(_com_error& e)
 	{
 		MessageBox(0, W2A(e.Description()),0,0);
 	}
 	
	return functionMap;
}

CMapStringToString* CDocManage::ReadPropertyInfo(CString strClassName, CString strID)
{
	USES_CONVERSION;
 	HRESULT hr = NULL;
	char pszFilePath[160];
 	memset(pszFilePath, 0, sizeof(pszFilePath));
 	sprintf(pszFilePath,"%s\\%s%s", theApp.m_strLocPath, strClassName, FILEEXT);
 	CMapStringToString* propertyMap = new CMapStringToString();
 	
 	IXMLDOMDocumentPtr pXmlDoc;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -