CStructureInterpreter.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #if (defined _WIN32 || defined _WIN64)
  2. #include "stdafx.h"
  3. #include <io.h>
  4. #endif
  5. #include "CStructureInterpreter.h"
  6. #include <fstream>
  7. #include <strstream>
  8. #include "baseEx.h"
  9. namespace Chromium{
  10. CStructureInterpreter::CStructureInterpreter(const char* path)
  11. {
  12. DbgEx("CStructureInterpreter constructor begin");
  13. this->mPathOfXmls = path;
  14. this->loadXmlFiles();
  15. /*
  16. for(auto it : mMethodStructureMap)
  17. {
  18. auto funArr = it.second;
  19. std::string funStr = "Entity Fun Map:";
  20. funStr.append(it.first).append(", funArr:");
  21. for(auto funIt : funArr)
  22. funStr.append(funIt.second.mMethodName).append(",");
  23. DbgEx(funStr.c_str());
  24. }
  25. for(auto it : mMessageStructureMap)
  26. {
  27. auto msgArr = it.second;
  28. std::string msgStr = "Entity Msg Map:";
  29. msgStr.append(it.first).append(", MsgArr:");
  30. if(msgArr.size() == 0)
  31. continue;
  32. for (auto funIt : msgArr)
  33. msgStr.append(funIt.mMethodName).append(",");
  34. DbgEx(msgStr.c_str());
  35. }
  36. */
  37. };
  38. void CStructureInterpreter::getAllFiles(){
  39. mFiles = find_files(this->mPathOfXmls, "*.xml");
  40. }
  41. void CStructureInterpreter::loadXmlFiles(){
  42. this->getAllFiles();
  43. int i = 0;
  44. for (i=0;i<this->mFiles.size();++i)
  45. {
  46. tinyxml2::XMLDocument doc;
  47. doc.LoadFile( this->mFiles.at(i).c_str() );
  48. if( tinyxml2::XML_SUCCESS == doc.ErrorID()){
  49. this->parseXmlToMap(doc);
  50. }
  51. }
  52. }
  53. void CStructureInterpreter::parseXmlToMap(tinyxml2::XMLDocument& xml){
  54. //DbgEx("CStructureInterpreter parseXmlToMap");
  55. // get entity node
  56. tinyxml2::XMLElement* pEntity = xml.FirstChildElement("entity");
  57. if (NULL == pEntity) return;
  58. // load messages
  59. std::string strEntityName = pEntity->Attribute("name");
  60. std::vector<CMedthodInterface> messageInterface;
  61. this->loadMessageInterface(messageInterface, pEntity);
  62. this->mMessageStructureMap.insert(std::pair<std::string, std::vector<CMedthodInterface>>(strEntityName, messageInterface));
  63. // get class node
  64. tinyxml2::XMLElement* pClass = pEntity->FirstChildElement("class");
  65. if (NULL == pClass) return;
  66. //load functions
  67. std::string strClassName = pClass->Attribute("name");
  68. std::map<int,CMedthodInterface> functionInterface;
  69. this->loadFunctionInterface(functionInterface, pClass);
  70. this->mMethodStructureMap.insert(std::pair<std::string, std::map<int,CMedthodInterface>>(strEntityName.append(strClassName),functionInterface));
  71. }
  72. void CStructureInterpreter::loadFunctionInterface(
  73. std::map<int,CMedthodInterface> &functionInterface,
  74. tinyxml2::XMLElement* pClass){
  75. // load function structure
  76. //DbgEx("CStructureInterpreter loadMethodInterface");
  77. tinyxml2::XMLElement* element = pClass->FirstChildElement();
  78. int methodID = 0;
  79. while(NULL != element){
  80. CMedthodInterface method;
  81. method.mMethodName = element->Attribute("name");
  82. method.mMethodType = element->Value();
  83. if(0 == method.mMethodType.compare("twoway")){
  84. // twoway function
  85. this->loadParams(method.mRequestInterpreter, element->FirstChildElement("req"));
  86. this->loadParams(method.mResponseInterpreter, element->FirstChildElement("res"));
  87. }else if (0 == method.mMethodType.compare("oneway")){
  88. // oneway function => info
  89. this->loadParams(method.mRequestInterpreter, element);
  90. }
  91. if (element->Attribute("method_id") == NULL)
  92. {
  93. functionInterface.insert(std::pair<int,CMedthodInterface>(methodID, method));
  94. methodID++;
  95. }else{
  96. int i = convertStringToInt(element->Attribute("method_id"));
  97. functionInterface.insert(std::pair<int,CMedthodInterface>(i, method));
  98. }
  99. // functionInterface.push_back(method);
  100. element = element->NextSiblingElement();
  101. }
  102. }
  103. void CStructureInterpreter::loadParams(CTransStruct &params,
  104. tinyxml2::XMLElement* transParams){
  105. //DbgEx("CStructureInterpreter loadParams");
  106. if (NULL == transParams) return;
  107. tinyxml2::XMLElement* param = transParams->FirstChildElement();
  108. while(NULL != param){
  109. CMethodParam mp;
  110. mp.mName = param->Attribute("name");
  111. mp.mType = param->Attribute("type");
  112. params.mParamList.push_back(mp);
  113. param = param->NextSiblingElement();
  114. }
  115. }
  116. void CStructureInterpreter::loadMessageInterface(
  117. std::vector<CMedthodInterface> &messageInterface,
  118. tinyxml2::XMLElement* pEntity){
  119. //DbgEx("CStructureInterpreter loadMessageInterface");
  120. // load message structure
  121. tinyxml2::XMLElement* msgElement = pEntity->FirstChildElement("message");
  122. while (NULL != msgElement)
  123. {
  124. CMedthodInterface mi;
  125. mi.mMethodName = msgElement->Attribute("name");
  126. mi.mMethodType = msgElement->Value();
  127. this->loadParams(mi.mResponseInterpreter, msgElement);
  128. messageInterface.push_back(mi);
  129. msgElement = msgElement->NextSiblingElement("message");
  130. }
  131. }
  132. CMedthodInterface* CStructureInterpreter::getFunctionInterface(
  133. char* entityName, char* className, int methodID){
  134. //DbgEx("CStructureInterpreter getMethodInterface");
  135. if (methodID < 0) return NULL;
  136. // key1: entityName+className, key2:methodName
  137. std::string key1 = entityName;
  138. key1.append(className);
  139. std::map<std::string,std::map<int,CMedthodInterface>>::iterator itEntityMap = this->mMethodStructureMap.find(key1);
  140. if (this->mMethodStructureMap.end() == itEntityMap)
  141. {
  142. return NULL;
  143. }
  144. std::map<int,CMedthodInterface>::iterator itMethodMap = itEntityMap->second.find(methodID);
  145. if (itEntityMap->second.end() == itMethodMap)
  146. {
  147. return NULL;
  148. }
  149. return &(itMethodMap->second);
  150. }
  151. std::vector<CMedthodInterface>* CStructureInterpreter::getAllMessageInterface(const char* entityName){
  152. std::string entity = entityName;
  153. std::map<std::string,std::vector<CMedthodInterface>>::iterator iter = this->mMessageStructureMap.find(entity);
  154. if (this->mMessageStructureMap.end() == iter)
  155. {
  156. return NULL;
  157. }
  158. return &(iter->second);
  159. }
  160. int CStructureInterpreter::convertStringToInt(std::string s){
  161. int val;
  162. std::strstream ss;
  163. ss<< s;
  164. ss >> val;
  165. return val;
  166. }
  167. }