XMLEnvironmentLoader.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2018 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 "XMLEnvironmentLoader.hpp"
  14. #include "XSDSchemaParser.hpp"
  15. #include "Exceptions.hpp"
  16. std::vector<std::shared_ptr<EnvironmentNode>> XMLEnvironmentLoader::load(std::istream &in, const std::shared_ptr<SchemaItem> &pSchemaItem) const
  17. {
  18. std::vector<std::shared_ptr<EnvironmentNode>> envNodes;
  19. std::shared_ptr<EnvironmentNode> pEnvNode;
  20. pt::ptree envTree;
  21. try
  22. {
  23. pt::read_xml(in, envTree, pt::xml_parser::trim_whitespace | pt::xml_parser::no_comments);
  24. //auto rootIt = envTree.begin();
  25. // if root, want to start with rootIt, but auto match rootIt first with schemaItem, then parse on down, but want to return
  26. // envNode for the root
  27. // if not root, to through rootIt with schema item as parent for each elemement, return envNode of each item iterated
  28. std::shared_ptr<SchemaItem> pParseRootSchemaItem;
  29. for (auto envIt = envTree.begin(); envIt != envTree.end(); ++envIt)
  30. {
  31. if (envIt->first == pSchemaItem->getProperty("name"))
  32. {
  33. pParseRootSchemaItem = pSchemaItem;
  34. }
  35. else
  36. {
  37. pParseRootSchemaItem = pSchemaItem->getChild(envIt->first);
  38. }
  39. pEnvNode = std::make_shared<EnvironmentNode>(pParseRootSchemaItem, envIt->first); // caller may need to set the parent
  40. parse(envIt->second, pParseRootSchemaItem, pEnvNode);
  41. envNodes.push_back(pEnvNode);
  42. }
  43. }
  44. catch (const std::exception &e)
  45. {
  46. std::string xmlError = e.what();
  47. std::string msg = "Unable to read/parse Environment file. Error = " + xmlError;
  48. throw (ParseException(msg));
  49. }
  50. return envNodes;
  51. }
  52. void XMLEnvironmentLoader::parse(const pt::ptree &envTree, const std::shared_ptr<SchemaItem> &pConfigItem, std::shared_ptr<EnvironmentNode> &pEnvNode) const
  53. {
  54. //
  55. // First see if the node has a value
  56. std::string value;
  57. try
  58. {
  59. value = envTree.get<std::string>("");
  60. if (!value.empty())
  61. {
  62. std::shared_ptr<SchemaValue> pCfgValue = pConfigItem->getItemSchemaValue();
  63. if (!pCfgValue)
  64. {
  65. pCfgValue = std::make_shared<SchemaValue>("", false);
  66. pCfgValue->setType(pConfigItem->getSchemaValueType("default"));
  67. pConfigItem->setItemSchemaValue(pCfgValue);
  68. }
  69. std::shared_ptr<EnvironmentValue> pEnvValue = std::make_shared<EnvironmentValue>(pEnvNode, pCfgValue, ""); // node's value has no name
  70. pEnvValue->setValue(value, nullptr);
  71. pEnvNode->setLocalEnvValue(pEnvValue);
  72. }
  73. }
  74. catch (...)
  75. {
  76. // do nothing
  77. }
  78. //
  79. // Find elements in environment tree cooresponding to this config item, then parse each
  80. for (auto it = envTree.begin(); it != envTree.end(); ++it)
  81. {
  82. std::string elemName = it->first;
  83. //
  84. // First see if there are attributes for this element (<xmlattr> === <element attr1="xx" attr2="yy" ...></element> The attr1 and attr2 are in this)
  85. if (elemName == "<xmlattr>")
  86. {
  87. for (auto attrIt = it->second.begin(); attrIt != it->second.end(); ++attrIt)
  88. {
  89. std::shared_ptr<SchemaValue> pSchemaValue = pConfigItem->getAttribute(attrIt->first); // note, undefined attributes in schema will return a generic schema value
  90. std::string curValue = attrIt->second.get_value<std::string>();
  91. std::shared_ptr<EnvironmentValue> pEnvValue = std::make_shared<EnvironmentValue>(pEnvNode, pSchemaValue, attrIt->first, curValue); // this is where we would use a variant
  92. pSchemaValue->addEnvironmentValue(pEnvValue);
  93. pEnvNode->addAttribute(attrIt->first, pEnvValue);
  94. }
  95. }
  96. else
  97. {
  98. std::string typeName = it->second.get("<xmlattr>.buildSet", "");
  99. std::shared_ptr<SchemaItem> pSchemaItem;
  100. if (!typeName.empty())
  101. {
  102. pSchemaItem = pConfigItem->getChildByComponent(elemName, typeName);
  103. }
  104. else
  105. {
  106. pSchemaItem = pConfigItem->getChild(elemName);
  107. }
  108. // If no schema item is found, that's ok, the node just has no defined configuration
  109. std::shared_ptr<EnvironmentNode> pElementNode = std::make_shared<EnvironmentNode>(pSchemaItem, elemName, pEnvNode);
  110. parse(it->second, pSchemaItem, pElementNode);
  111. pEnvNode->addChild(pElementNode);
  112. }
  113. }
  114. }