| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #if (defined _WIN32 || defined _WIN64)
- #include "stdafx.h"
- #include <io.h>
- #endif
- #include "CStructureInterpreter.h"
- #include <fstream>
- #include <strstream>
- #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;i<this->mFiles.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<CMedthodInterface> messageInterface;
- this->loadMessageInterface(messageInterface, pEntity);
- this->mMessageStructureMap.insert(std::pair<std::string, std::vector<CMedthodInterface>>(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<int,CMedthodInterface> functionInterface;
- this->loadFunctionInterface(functionInterface, pClass);
- this->mMethodStructureMap.insert(std::pair<std::string, std::map<int,CMedthodInterface>>(strEntityName.append(strClassName),functionInterface));
- }
- void CStructureInterpreter::loadFunctionInterface(
- std::map<int,CMedthodInterface> &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<int,CMedthodInterface>(methodID, method));
- methodID++;
- }else{
- int i = convertStringToInt(element->Attribute("method_id"));
- functionInterface.insert(std::pair<int,CMedthodInterface>(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<CMedthodInterface> &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<std::string,std::map<int,CMedthodInterface>>::iterator itEntityMap = this->mMethodStructureMap.find(key1);
- if (this->mMethodStructureMap.end() == itEntityMap)
- {
- return NULL;
- }
- std::map<int,CMedthodInterface>::iterator itMethodMap = itEntityMap->second.find(methodID);
- if (itEntityMap->second.end() == itMethodMap)
- {
- return NULL;
- }
- return &(itMethodMap->second);
- }
- std::vector<CMedthodInterface>* CStructureInterpreter::getAllMessageInterface(const char* entityName){
- std::string entity = entityName;
- std::map<std::string,std::vector<CMedthodInterface>>::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;
- }
- }
|