XMLEnvironmentMgr.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2017 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "XMLEnvironmentMgr.hpp"
  14. #include "XSDSchemaParser.hpp"
  15. bool XMLEnvironmentMgr::createParser()
  16. {
  17. m_pSchemaParser = std::make_shared<XSDSchemaParser>(m_pSchema);
  18. return true;
  19. }
  20. bool XMLEnvironmentMgr::doLoadEnvironment(std::istream &in)
  21. {
  22. bool rc = true;
  23. pt::ptree envTree;
  24. try
  25. {
  26. pt::read_xml(in, envTree, pt::xml_parser::trim_whitespace | pt::xml_parser::no_comments);
  27. auto rootIt = envTree.begin();
  28. //
  29. // Start at root, these better match!
  30. std::string rootName = rootIt->first;
  31. if (rootName == m_pSchema->getProperty("name"))
  32. {
  33. m_pRootNode = std::make_shared<EnvironmentNode>(m_pSchema, rootName);
  34. m_pRootNode->setId(".");
  35. addPath(m_pRootNode);
  36. parse(rootIt->second, m_pSchema, m_pRootNode);
  37. }
  38. }
  39. catch (const std::exception &e)
  40. {
  41. std::string xmlError = e.what();
  42. m_message = "Unable to read/parse Environment file. Error = " + xmlError;
  43. rc = false;
  44. }
  45. return rc;
  46. }
  47. bool XMLEnvironmentMgr::save(std::ostream &out)
  48. {
  49. bool rc = true;
  50. try
  51. {
  52. pt::ptree envTree, topTree;
  53. serialize(envTree, m_pRootNode);
  54. topTree.add_child("Environment", envTree);
  55. pt::write_xml(out, topTree);
  56. }
  57. catch (const std::exception &e)
  58. {
  59. std::string xmlError = e.what();
  60. m_message = "Unable to save Environment file. Error = " + xmlError;
  61. rc = false;
  62. }
  63. return rc;
  64. }
  65. void XMLEnvironmentMgr::parse(const pt::ptree &envTree, const std::shared_ptr<SchemaItem> &pConfigItem, std::shared_ptr<EnvironmentNode> &pEnvNode)
  66. {
  67. //
  68. // First see if the node has a value
  69. std::string value;
  70. try
  71. {
  72. value = envTree.get<std::string>("");
  73. if (!value.empty())
  74. {
  75. std::shared_ptr<SchemaValue> pCfgValue = pConfigItem->getItemSchemaValue();
  76. std::shared_ptr<EnvironmentValue> pEnvValue = std::make_shared<EnvironmentValue>(pEnvNode, pCfgValue, ""); // node's value has no name
  77. pEnvValue->setValue(value, nullptr);
  78. pEnvNode->setLocalEnvValue(pEnvValue);
  79. }
  80. }
  81. catch (...)
  82. {
  83. // do nothing
  84. }
  85. //
  86. // Find elements in environment tree cooresponding to this config item, then parse each
  87. for (auto it = envTree.begin(); it != envTree.end(); ++it)
  88. {
  89. std::string elemName = it->first;
  90. //
  91. // First see if there are attributes for this element (<xmlattr> === <element attr1="xx" attr2="yy" ...></element> The attr1 and attr2 are in this)
  92. if (elemName == "<xmlattr>")
  93. {
  94. for (auto attrIt = it->second.begin(); attrIt != it->second.end(); ++attrIt)
  95. {
  96. std::shared_ptr<SchemaValue> pSchemaValue = pConfigItem->getAttribute(attrIt->first); // note, undefined attributes in schema will return a generic schema value
  97. std::string curValue = attrIt->second.get_value<std::string>();
  98. std::shared_ptr<EnvironmentValue> pEnvValue = std::make_shared<EnvironmentValue>(pEnvNode, pSchemaValue, attrIt->first, curValue); // this is where we would use a variant
  99. pSchemaValue->addEnvironmentValue(pEnvValue);
  100. pEnvNode->addAttribute(attrIt->first, pEnvValue);
  101. }
  102. }
  103. else
  104. {
  105. std::string typeName = it->second.get("<xmlattr>.buildSet", "");
  106. std::shared_ptr<SchemaItem> pSchemaItem;
  107. if (!typeName.empty())
  108. {
  109. pSchemaItem = pConfigItem->getChildByComponent(elemName, typeName);
  110. }
  111. else
  112. {
  113. pSchemaItem = pConfigItem->getChild(elemName);
  114. }
  115. std::shared_ptr<EnvironmentNode> pElementNode = std::make_shared<EnvironmentNode>(pSchemaItem, elemName, pEnvNode);
  116. pElementNode->setId(getUniqueKey());
  117. addPath(pElementNode);
  118. parse(it->second, pSchemaItem, pElementNode);
  119. pEnvNode->addChild(pElementNode);
  120. }
  121. }
  122. }
  123. void XMLEnvironmentMgr::serialize(pt::ptree &envTree, std::shared_ptr<EnvironmentNode> &pEnvNode) const
  124. {
  125. std::vector<std::shared_ptr<EnvironmentValue>> attributes;
  126. pEnvNode->getAttributes(attributes);
  127. for (auto attrIt = attributes.begin(); attrIt != attributes.end(); ++attrIt)
  128. {
  129. envTree.put("<xmlattr>." + (*attrIt)->getName(), (*attrIt)->getValue());
  130. }
  131. std::shared_ptr<EnvironmentValue> pNodeValue = pEnvNode->getLocalEnvValue();
  132. if (pNodeValue)
  133. {
  134. envTree.put_value(pNodeValue->getValue());
  135. }
  136. std::vector<std::shared_ptr<EnvironmentNode>> children;
  137. pEnvNode->getChildren(children);
  138. for (auto childIt = children.begin(); childIt != children.end(); ++childIt)
  139. {
  140. pt::ptree nodeTree;
  141. serialize(nodeTree, *childIt);
  142. envTree.add_child((*childIt)->getName(), nodeTree);
  143. }
  144. }