#include "StdAfx.h" #include "FLowFileReader.h" #include "FlowDataProvider.h." #include "FlowTemplate.h" #include "FlowTemplateMgr.h" #include "CellBase.h" CFLowFileReader::CFLowFileReader(CFlowTemplateMgr* pTemplateMgr) : m_pTemplateMgr(pTemplateMgr) { ASSERT(m_pTemplateMgr != NULL); } CFLowFileReader::~CFLowFileReader(void) { } /***************************************************************** **【函数名称】 __open **【函数功能】 打开流程文件 **【参数】 FileName:文件名 FlowDoc:XML文档模板 **【返回值】 成功true,失败false ****************************************************************/ bool CFLowFileReader::__open( LPCTSTR FileName, FLOW_DOC_PTR& FlowDoc ) { bool res = false; try { //创建IXMLDOMDocument接口 HRESULT hr = FlowDoc.CreateInstance(_uuidof(MSXML2::DOMDocument60)); if (FAILED(hr)) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{FileReader}: 创建流程文件读取对象失败, 请检查XML环境")); return false; } ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_NORMAL, _T("{FileReader}: 开始解析流程文件[%s]"), FileName); _variant_t varXml(FileName); //加载文件 if(FALSE == (BOOL)FlowDoc->load(varXml)) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{FileReader}: 打开流程文件[%s]失败"), FileName); return false; } else { res = true; } } catch (_com_error &e) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{FileReader}: 打开流程文件[%s]时出现异常, %s"), FileName, e.ErrorMessage()); } catch (...) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{FileReader}: 打开流程文件[%s]时出现未知异常"), FileName); } return res; } /***************************************************************** **【函数名称】 __parseCell **【函数功能】 解析Cell信息 **【参数】 FlowDoc:XML文档模板 FlowTemplate:cell所属的模板 **【返回值】 成功true,失败false ****************************************************************/ bool CFLowFileReader::__parseCell( FLOW_DOC_PTR& FlowDoc, CFlowTemplate& FlowTemplate ) { CString CellXpath; CellXpath.Format(_T("%s[@%s='%s']/%s"), XPATH_IVR_NODE, FLOW_ATTRIBUTE_NAME, FlowTemplate.name(), XPATH_CELL); FLOW_LIST_PTR CellList = NULL; CellList = FlowDoc->selectNodes(CellXpath.GetBuffer(0)); ASSERT(CellList != NULL); if(CellList == NULL || CellList->Getlength() == 0) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{FileReader}: 流程[%s]中不包含任何有效节点"), FlowTemplate.name()); return false; } //依次解析所有节点 for(int i = 0; i < CellList->Getlength(); ++i) { FLOW_ELEMENT_PTR CellElement = CellList->Getitem(i); // 创建流程数据提供器 CFlowDataProvider Provider(FlowDoc, CellElement, &FlowTemplate); CString CellName; Provider.getData(CELL_ATTRIBUTE_NAME, CellName); // 创建节点对象 CCellBase* pCell = CCellBase::createCell(CellName); ASSERT(pCell != NULL); if(pCell == NULL) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_WARNING, _T("{FileReader}: 创建流程[%s]附属节点[%s]失败"), FlowTemplate.name(), CellName); return false; } if(pCell->fillData(Provider)) FlowTemplate.accept(pCell->position(), pCell); else return false; } return true; } /***************************************************************** **【函数名称】 __generateFlow **【函数功能】 生成流程 **【参数】 FlowDoc:XML文档模板 FlowList:流程列表 **【返回值】 成功true,失败false ****************************************************************/ bool CFLowFileReader::__generateFlow( FLOW_DOC_PTR& FlowDoc, FLOW_LIST_PTR& FlowList ) { bool Res = true; //依次创建所有流程 for(int i = 0; i < FlowList->Getlength(); ++i) { FLOW_ELEMENT_PTR FlowElement = FlowList->Getitem(i); // 创建流程数据提供器 CFlowDataProvider Provider(FlowDoc, FlowElement); // 创建流程对象 CFlowTemplate* pTemplate = m_pTemplateMgr->createTemplate(Provider); ASSERT(pTemplate != NULL); if(pTemplate == NULL) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_NORMAL, _T("{FileReader}: 解析流程文件过程中创建模板失败")); continue; } ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_NORMAL, _T("{FileReader}: 正在解析流程[%s]"), pTemplate->name()); Res &= __parseCell(FlowDoc, *pTemplate); } return Res; } /***************************************************************** **【函数名称】 read **【函数功能】 读取流程文件 **【参数】 FileName:文件名 **【返回值】 成功true,失败false ****************************************************************/ bool CFLowFileReader::read( LPCTSTR FileName ) { FLOW_DOC_PTR pDoc; if(!__open(FileName, pDoc)) return false; try { FLOW_LIST_PTR FlowList = NULL; FlowList = pDoc->selectNodes(XPATH_IVR_NODE); ASSERT(FlowList != NULL); if(FlowList == NULL || FlowList->Getlength() == 0) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{FileReader}: 流程文件中不包含任何有效流程, 请检查流程文件[%s]"), FileName); return false; } return __generateFlow(pDoc, FlowList); } catch (_com_error &e) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{FileReader}:解析流程文件[%s]时出现异常, %s"), FileName, e.ErrorMessage()); } catch (...) { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("{FileReader}: 解析流程文件[%s]时出现未知异常"), FileName); } return false; }