XSDComponentParser.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "XSDComponentParser.hpp"
  14. #include "XSDValueSetParser.hpp"
  15. #include "Exceptions.hpp"
  16. namespace pt = boost::property_tree;
  17. void XSDComponentParser::parseXSD(const pt::ptree &compTree)
  18. {
  19. bool foundComponentDef = false;
  20. pt::ptree tree = compTree.get_child("", pt::ptree());
  21. //
  22. // First time through look for attributeGroups that can be defined and for existence of a sequence element that actually defines the component
  23. for (auto it = tree.begin(); it != tree.end(); ++it)
  24. {
  25. //
  26. // Element parent (a type in realilty) and the element name help figure out how to process the XSD schema element
  27. std::string elemType = it->first;
  28. if (elemType == "xs:attributeGroup")
  29. {
  30. parseAttributeGroup(it->second);
  31. }
  32. else if (elemType == "xs:sequence")
  33. {
  34. foundComponentDef = true;
  35. }
  36. }
  37. if (foundComponentDef)
  38. {
  39. pt::ptree elemTree = tree.get_child("xs:sequence.xs:element", pt::ptree());
  40. if (!elemTree.empty())
  41. {
  42. std::string elementName = getXSDAttributeValue(elemTree, "<xmlattr>.name");
  43. m_pSchemaItem->setProperty("name", elementName);
  44. int minOccurs = elemTree.get("<xmlattr>.minOccurs", 1);
  45. std::string maxOccursStr = elemTree.get("<xmlattr>.maxOccurs", "1");
  46. int maxOccurs = (maxOccursStr != "unbounded") ? stoi(maxOccursStr) : -1;
  47. m_pSchemaItem->setMinInstances(minOccurs);
  48. m_pSchemaItem->setMaxInstances(maxOccurs);
  49. //
  50. // See if the element has a type. If so, then the element can have a value (other than attributes). Note does happen, but is rare
  51. std::string elementDataType = elemTree.get("<xmlattr>.type", "");
  52. if (!elementDataType.empty())
  53. {
  54. std::shared_ptr<SchemaValue> pItemCfgValue = std::make_shared<SchemaValue>("elementData");
  55. pItemCfgValue->setType(m_pSchemaItem->getSchemaValueType(elementDataType));
  56. pItemCfgValue->setForcedValue(elemTree.get("<xmlattr>.default", ""));
  57. }
  58. //
  59. // Parse any attributes, these are located in the xs:complexType section
  60. pt::ptree attributeTree = elemTree.get_child("xs:complexType", pt::ptree());
  61. for (auto attrIt = attributeTree.begin(); attrIt != attributeTree.end(); ++attrIt)
  62. {
  63. //
  64. // Element parent (a type in realilty) and the element name help figure out how to process the XSD schema element
  65. std::string elemType = attrIt->first;
  66. if (elemType == "xs:attributeGroup")
  67. {
  68. parseAttributeGroup(attrIt->second);
  69. }
  70. else if (elemType == "xs:attribute")
  71. {
  72. parseAttribute(attrIt->second);
  73. }
  74. }
  75. //
  76. // Now parse the sequence section (these are sub keys for the component)
  77. XSDSchemaParser::parseXSD(elemTree.get_child("xs:complexType.xs:sequence", pt::ptree()));
  78. //
  79. // If there were other keys that we needed to support, this is where a loop would be added
  80. // to parse those.
  81. }
  82. }
  83. }