#include "include/Utils/SerializeInterface/SerializeInterface.h" #include "include/Engine.h" #include namespace SE { void ReplaceText(boost::property_tree::ptree& propertyTree, std::map& replaceMap) { for (boost::property_tree::ptree::iterator i = propertyTree.begin(); i != propertyTree.end(); ) { BOOST_FOREACH(auto& itr, replaceMap) { std::string param = i->second.get(""); if (param != "") { boost::algorithm::replace_all(param, itr.first, itr.second); i->second.put("", param); } } if (i != propertyTree.end()) { i++; } } } boost::property_tree::ptree::iterator ReplaceIncludeFile(boost::property_tree::ptree& propertyTree, boost::property_tree::ptree::iterator propertyTreeItr, std::map replaceMap) { boost::shared_array xmlFileArr; cardinal xmlFileSize; std::string fileName = propertyTreeItr->second.get(".file"); for (boost::property_tree::ptree::iterator i = propertyTreeItr->second.begin(); i != propertyTreeItr->second.end(); ++i) { if (i->first == "Param") { std::string paramName = i->second.get(".name"); std::string paramValue = i->second.get(".value"); //If value already consist template that can be expanded: while (replaceMap.count(paramValue) != 0) { paramValue = replaceMap[paramValue]; } replaceMap[paramName] = paramValue; } } xmlFileArr = CreateMemFromFile(ResourceManager->PathToResources + fileName, xmlFileSize); std::string xmlString = std::string(&xmlFileArr[0], &xmlFileArr[xmlFileSize]); std::shared_ptr includeFileTree = StringToPropertyTree(xmlString, replaceMap); boost::property_tree::ptree::iterator newItr = propertyTree.erase(propertyTreeItr); propertyTree.insert(newItr, includeFileTree->begin(), includeFileTree->end()); if (newItr == propertyTree.begin()) { throw ErrorToLog("Property tree logic error - don't know how to handle :("); } newItr--; return newItr; } void GetReplaceMapRecoursively(boost::property_tree::ptree& propertyTree, std::map replaceMap) { ReplaceText(propertyTree, replaceMap); for (boost::property_tree::ptree::iterator i = propertyTree.begin(); i != propertyTree.end(); ) { std::map localMap = replaceMap; if (i->first == "Include") { i = ReplaceIncludeFile(propertyTree, i, localMap); } else { GetReplaceMapRecoursively(i->second, localMap); } BOOST_FOREACH(auto& itr, localMap) { if (localMap.count(itr.second) != 0) { localMap[itr.first] = localMap[itr.second]; } } if (i != propertyTree.end()) { i++; } } } std::shared_ptr StringToPropertyTree(std::string xmlCode, std::map replaceMap) { std::shared_ptr propertyTree(new boost::property_tree::ptree); std::stringstream stream(xmlCode); try { boost::property_tree::read_xml(stream, *propertyTree); //Parsing tag GetReplaceMapRecoursively(*propertyTree, replaceMap); } catch(boost::property_tree::xml_parser_error) { throw ErrorToLog("ERROR: xml parse error"); } return propertyTree; } std::shared_ptr FileToPropertyTree(boost::shared_array xmlFileArr, cardinal xmlFileSize, std::map replaceMap) { std::string xmlCode = std::string(&xmlFileArr[0], &xmlFileArr[xmlFileSize]); return StringToPropertyTree(xmlCode, replaceMap); } std::shared_ptr FileToPropertyTree(const std::string& fileName, std::map replaceMap) { cardinal byteCount; boost::shared_array file = CreateMemFromFile(ResourceManager->PathToResources + fileName, byteCount); return FileToPropertyTree(file, byteCount, replaceMap); } } //namespace SE