XMLEnvironmentMgr.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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::serialize(pt::ptree &envTree, std::shared_ptr<EnvironmentNode> &pEnvNode) const
  57. {
  58. std::vector<std::shared_ptr<EnvironmentValue>> attributes;
  59. pEnvNode->getAttributes(attributes);
  60. for (auto attrIt = attributes.begin(); attrIt != attributes.end(); ++attrIt)
  61. {
  62. std::string attrValue;
  63. attrValue = (*attrIt)->getValue();
  64. if (!attrValue.empty())
  65. {
  66. envTree.put("<xmlattr>." + (*attrIt)->getName(), attrValue);
  67. }
  68. }
  69. std::shared_ptr<EnvironmentValue> pNodeValue = pEnvNode->getLocalEnvValue();
  70. if (pNodeValue)
  71. {
  72. envTree.put_value(pNodeValue->getValue());
  73. }
  74. std::vector<std::shared_ptr<EnvironmentNode>> children;
  75. pEnvNode->getChildren(children);
  76. for (auto childIt = children.begin(); childIt != children.end(); ++childIt)
  77. {
  78. pt::ptree nodeTree;
  79. serialize(nodeTree, *childIt);
  80. envTree.add_child((*childIt)->getName(), nodeTree);
  81. }
  82. }