XMLEnvironmentMgr.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include "XMLEnvironmentLoader.hpp"
  16. #include "Exceptions.hpp"
  17. bool XMLEnvironmentMgr::createParser()
  18. {
  19. m_pSchemaParser = std::make_shared<XSDSchemaParser>(m_pSchema);
  20. return true;
  21. }
  22. std::vector<std::shared_ptr<EnvironmentNode>> XMLEnvironmentMgr::doLoadEnvironment(std::istream &in, const std::shared_ptr<SchemaItem> &pSchemaItem)
  23. {
  24. std::vector<std::shared_ptr<EnvironmentNode>> envNodes;
  25. try
  26. {
  27. XMLEnvironmentLoader envLoader;
  28. envNodes = envLoader.load(in, pSchemaItem);
  29. }
  30. catch (const std::exception &e)
  31. {
  32. std::string xmlError = e.what();
  33. std::string msg = "Unable to read/parse Environment file. Error = " + xmlError;
  34. throw (ParseException(msg));
  35. }
  36. return envNodes;
  37. }
  38. bool XMLEnvironmentMgr::save(std::ostream &out)
  39. {
  40. bool rc = true;
  41. try
  42. {
  43. pt::ptree envTree, topTree;
  44. serialize(envTree, m_pRootNode);
  45. topTree.add_child("Environment", envTree);
  46. pt::write_xml(out, topTree);
  47. }
  48. catch (const std::exception &e)
  49. {
  50. std::string xmlError = e.what();
  51. m_message = "Unable to save Environment file. Error = " + xmlError;
  52. rc = false;
  53. }
  54. return rc;
  55. }
  56. void XMLEnvironmentMgr::parse(const pt::ptree &envTree, const std::shared_ptr<SchemaItem> &pConfigItem, std::shared_ptr<EnvironmentNode> &pEnvNode)
  57. {
  58. //
  59. // First see if the node has a value
  60. std::string value;
  61. try
  62. {
  63. value = envTree.get<std::string>("");
  64. if (!value.empty())
  65. {
  66. std::shared_ptr<SchemaValue> pCfgValue = pConfigItem->getItemSchemaValue();
  67. std::shared_ptr<EnvironmentValue> pEnvValue = std::make_shared<EnvironmentValue>(pEnvNode, pCfgValue, ""); // node's value has no name
  68. pEnvValue->setValue(value, nullptr);
  69. pEnvNode->setLocalEnvValue(pEnvValue);
  70. }
  71. }
  72. catch (...)
  73. {
  74. // do nothing
  75. }
  76. //
  77. // Find elements in environment tree cooresponding to this config item, then parse each
  78. for (auto it = envTree.begin(); it != envTree.end(); ++it)
  79. {
  80. std::string elemName = it->first;
  81. //
  82. // First see if there are attributes for this element (<xmlattr> === <element attr1="xx" attr2="yy" ...></element> The attr1 and attr2 are in this)
  83. if (elemName == "<xmlattr>")
  84. {
  85. for (auto attrIt = it->second.begin(); attrIt != it->second.end(); ++attrIt)
  86. {
  87. std::shared_ptr<SchemaValue> pSchemaValue = pConfigItem->getAttribute(attrIt->first); // note, undefined attributes in schema will return a generic schema value
  88. std::string curValue = attrIt->second.get_value<std::string>();
  89. std::shared_ptr<EnvironmentValue> pEnvValue = std::make_shared<EnvironmentValue>(pEnvNode, pSchemaValue, attrIt->first, curValue); // this is where we would use a variant
  90. pSchemaValue->addEnvironmentValue(pEnvValue);
  91. pEnvNode->addAttribute(attrIt->first, pEnvValue);
  92. }
  93. }
  94. else
  95. {
  96. std::string typeName = it->second.get("<xmlattr>.buildSet", "");
  97. std::shared_ptr<SchemaItem> pSchemaItem;
  98. if (!typeName.empty())
  99. {
  100. pSchemaItem = pConfigItem->getChildByComponent(elemName, typeName);
  101. }
  102. else
  103. {
  104. pSchemaItem = pConfigItem->getChild(elemName);
  105. }
  106. // todo: need to handle pSchemaitem (remant to pChildConfigItem) not found (throw exception or make a default config item)
  107. std::shared_ptr<EnvironmentNode> pElementNode = std::make_shared<EnvironmentNode>(pSchemaItem, elemName, pEnvNode);
  108. pElementNode->setId(getUniqueKey());
  109. addPath(pElementNode);
  110. parse(it->second, pSchemaItem, pElementNode);
  111. pEnvNode->addChild(pElementNode);
  112. }
  113. }
  114. }
  115. void XMLEnvironmentMgr::serialize(pt::ptree &envTree, std::shared_ptr<EnvironmentNode> &pEnvNode) const
  116. {
  117. std::vector<std::shared_ptr<EnvironmentValue>> attributes;
  118. pEnvNode->getAttributes(attributes);
  119. for (auto attrIt = attributes.begin(); attrIt != attributes.end(); ++attrIt)
  120. {
  121. if ((*attrIt)->isValueSet())
  122. envTree.put("<xmlattr>." + (*attrIt)->getName(), (*attrIt)->getValue());
  123. }
  124. std::shared_ptr<EnvironmentValue> pNodeValue = pEnvNode->getLocalEnvValue();
  125. if (pNodeValue)
  126. {
  127. envTree.put_value(pNodeValue->getValue());
  128. }
  129. std::vector<std::shared_ptr<EnvironmentNode>> children;
  130. pEnvNode->getChildren(children);
  131. for (auto childIt = children.begin(); childIt != children.end(); ++childIt)
  132. {
  133. pt::ptree nodeTree;
  134. serialize(nodeTree, *childIt);
  135. envTree.add_child((*childIt)->getName(), nodeTree);
  136. }
  137. }