#if (defined _WIN32 || defined _WIN64) #include "stdafx.h" #include #endif #include "CStructureInterpreter.h" #include #include #include "baseEx.h" namespace Chromium{ CStructureInterpreter::CStructureInterpreter(const char* path) { DbgEx("CStructureInterpreter constructor begin"); this->mPathOfXmls = path; this->loadXmlFiles(); /* for(auto it : mMethodStructureMap) { auto funArr = it.second; std::string funStr = "Entity Fun Map:"; funStr.append(it.first).append(", funArr:"); for(auto funIt : funArr) funStr.append(funIt.second.mMethodName).append(","); DbgEx(funStr.c_str()); } for(auto it : mMessageStructureMap) { auto msgArr = it.second; std::string msgStr = "Entity Msg Map:"; msgStr.append(it.first).append(", MsgArr:"); if(msgArr.size() == 0) continue; for (auto funIt : msgArr) msgStr.append(funIt.mMethodName).append(","); DbgEx(msgStr.c_str()); } */ }; void CStructureInterpreter::getAllFiles(){ mFiles = find_files(this->mPathOfXmls, "*.xml"); } void CStructureInterpreter::loadXmlFiles(){ this->getAllFiles(); int i = 0; for (i=0;imFiles.size();++i) { tinyxml2::XMLDocument doc; doc.LoadFile( this->mFiles.at(i).c_str() ); if( tinyxml2::XML_SUCCESS == doc.ErrorID()){ this->parseXmlToMap(doc); } } } void CStructureInterpreter::parseXmlToMap(tinyxml2::XMLDocument& xml){ //DbgEx("CStructureInterpreter parseXmlToMap"); // get entity node tinyxml2::XMLElement* pEntity = xml.FirstChildElement("entity"); if (NULL == pEntity) return; // load messages std::string strEntityName = pEntity->Attribute("name"); std::vector messageInterface; this->loadMessageInterface(messageInterface, pEntity); this->mMessageStructureMap.insert(std::pair>(strEntityName, messageInterface)); // get class node tinyxml2::XMLElement* pClass = pEntity->FirstChildElement("class"); if (NULL == pClass) return; //load functions std::string strClassName = pClass->Attribute("name"); std::map functionInterface; this->loadFunctionInterface(functionInterface, pClass); this->mMethodStructureMap.insert(std::pair>(strEntityName.append(strClassName),functionInterface)); } void CStructureInterpreter::loadFunctionInterface( std::map &functionInterface, tinyxml2::XMLElement* pClass){ // load function structure //DbgEx("CStructureInterpreter loadMethodInterface"); tinyxml2::XMLElement* element = pClass->FirstChildElement(); int methodID = 0; while(NULL != element){ CMedthodInterface method; method.mMethodName = element->Attribute("name"); method.mMethodType = element->Value(); if(0 == method.mMethodType.compare("twoway")){ // twoway function this->loadParams(method.mRequestInterpreter, element->FirstChildElement("req")); this->loadParams(method.mResponseInterpreter, element->FirstChildElement("res")); }else if (0 == method.mMethodType.compare("oneway")){ // oneway function => info this->loadParams(method.mRequestInterpreter, element); } if (element->Attribute("method_id") == NULL) { functionInterface.insert(std::pair(methodID, method)); methodID++; }else{ int i = convertStringToInt(element->Attribute("method_id")); functionInterface.insert(std::pair(i, method)); } // functionInterface.push_back(method); element = element->NextSiblingElement(); } } void CStructureInterpreter::loadParams(CTransStruct ¶ms, tinyxml2::XMLElement* transParams){ //DbgEx("CStructureInterpreter loadParams"); if (NULL == transParams) return; tinyxml2::XMLElement* param = transParams->FirstChildElement(); while(NULL != param){ CMethodParam mp; mp.mName = param->Attribute("name"); mp.mType = param->Attribute("type"); params.mParamList.push_back(mp); param = param->NextSiblingElement(); } } void CStructureInterpreter::loadMessageInterface( std::vector &messageInterface, tinyxml2::XMLElement* pEntity){ //DbgEx("CStructureInterpreter loadMessageInterface"); // load message structure tinyxml2::XMLElement* msgElement = pEntity->FirstChildElement("message"); while (NULL != msgElement) { CMedthodInterface mi; mi.mMethodName = msgElement->Attribute("name"); mi.mMethodType = msgElement->Value(); this->loadParams(mi.mResponseInterpreter, msgElement); messageInterface.push_back(mi); msgElement = msgElement->NextSiblingElement("message"); } } CMedthodInterface* CStructureInterpreter::getFunctionInterface( char* entityName, char* className, int methodID){ //DbgEx("CStructureInterpreter getMethodInterface"); if (methodID < 0) return NULL; // key1: entityName+className, key2:methodName std::string key1 = entityName; key1.append(className); std::map>::iterator itEntityMap = this->mMethodStructureMap.find(key1); if (this->mMethodStructureMap.end() == itEntityMap) { return NULL; } std::map::iterator itMethodMap = itEntityMap->second.find(methodID); if (itEntityMap->second.end() == itMethodMap) { return NULL; } return &(itMethodMap->second); } std::vector* CStructureInterpreter::getAllMessageInterface(const char* entityName){ std::string entity = entityName; std::map>::iterator iter = this->mMessageStructureMap.find(entity); if (this->mMessageStructureMap.end() == iter) { return NULL; } return &(iter->second); } int CStructureInterpreter::convertStringToInt(std::string s){ int val; std::strstream ss; ss<< s; ss >> val; return val; } }