SchemaEnumeration.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2015 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 "SchemaCommon.hpp"
  14. #include "SchemaAttributes.hpp"
  15. #include "SchemaEnumeration.hpp"
  16. #include "SchemaRestriction.hpp"
  17. #include "XMLTags.h"
  18. #include "jptree.hpp"
  19. #include "DocumentationMarkup.hpp"
  20. using namespace CONFIGURATOR;
  21. #define StringBuffer ::StringBuffer
  22. #define IPropertyTree ::IPropertyTree
  23. CEnumeration* CEnumeration::load(CXSDNodeBase* pParentNode, const IPropertyTree *pSchemaRoot, const char* xpath)
  24. {
  25. assert(pSchemaRoot != nullptr);
  26. if (pSchemaRoot == nullptr)
  27. return nullptr;
  28. CEnumeration *pEnumeration = new CEnumeration(pParentNode);
  29. pEnumeration->setXSDXPath(xpath);
  30. if (xpath && *xpath)
  31. {
  32. IPropertyTree* pTree = pSchemaRoot->queryPropTree(xpath);
  33. if (pTree == nullptr)
  34. return pEnumeration;
  35. const char* pValue = pTree->queryProp(XML_ATTR_VALUE);
  36. if (pValue != nullptr)
  37. pEnumeration->setValue(pValue);
  38. }
  39. return pEnumeration;
  40. }
  41. void CEnumeration::dump(::std::ostream &cout, unsigned int offset) const
  42. {
  43. offset += STANDARD_OFFSET_1;
  44. quickOutHeader(cout, XSD_ENUMERATION_STR, offset);
  45. QUICK_OUT(cout, Value, offset);
  46. QUICK_OUT(cout, XSDXPath, offset);
  47. QUICK_OUT(cout, EnvXPath, offset);
  48. QUICK_OUT(cout, EnvValueFromXML, offset);
  49. quickOutFooter(cout, XSD_ENUMERATION_STR, offset);
  50. }
  51. void CEnumeration::getDocumentation(StringBuffer &strDoc) const
  52. {
  53. strDoc.appendf("* %s %s\n", this->getValue(), DM_LINE_BREAK);
  54. }
  55. const char* CEnumeration::getXML(const char* /*pComponent*/)
  56. {
  57. UNIMPLEMENTED;
  58. return nullptr;
  59. }
  60. void CEnumeration::populateEnvXPath(StringBuffer strXPath, unsigned int index)
  61. {
  62. assert(this->getValue() != nullptr);
  63. const CAttribute *pAttribute = dynamic_cast<const CAttribute*>(this->getParentNodeByType(XSD_ATTRIBUTE));
  64. assert(pAttribute != nullptr);
  65. this->setEnvXPath(strXPath.str());
  66. }
  67. void CEnumeration::loadXMLFromEnvXml(const IPropertyTree *pEnvTree)
  68. {
  69. assert(this->getEnvXPath() != nullptr);
  70. const CAttribute *pAttribute = dynamic_cast<const CAttribute*>(this->getParentNodeByType(XSD_ATTRIBUTE) );
  71. assert(pAttribute != nullptr);
  72. StringBuffer strXPath(this->getEnvXPath());
  73. strXPath.append("[@").append(pAttribute->getName()).append("=\"").append(this->getValue()).append("\"]");
  74. this->setInstanceValueValid(pEnvTree->hasProp(strXPath.str()));
  75. }
  76. void CEnumerationArray::dump(::std::ostream &cout, unsigned int offset) const
  77. {
  78. offset+= STANDARD_OFFSET_1;
  79. quickOutHeader(cout, XSD_ENUMERATION_ARRAY_STR, offset);
  80. QUICK_OUT_ARRAY(cout, offset);
  81. QUICK_OUT(cout, XSDXPath, offset);
  82. QUICK_OUT(cout, EnvXPath, offset);
  83. quickOutFooter(cout, XSD_ENUMERATION_ARRAY_STR, offset);
  84. }
  85. void CEnumerationArray::getDocumentation(StringBuffer &strDoc) const
  86. {
  87. strDoc.append("\nChoices are: \n").append(DM_LINE_BREAK);
  88. QUICK_DOC_ARRAY(strDoc);
  89. }
  90. const char* CEnumerationArray::getXML(const char* /*pComponent*/)
  91. {
  92. UNIMPLEMENTED;
  93. return nullptr;
  94. }
  95. void CEnumerationArray::populateEnvXPath(StringBuffer strXPath, unsigned int index)
  96. {
  97. assert(index == 1); // Only 1 array of elements per node
  98. QUICK_ENV_XPATH(strXPath)
  99. this->setEnvXPath(strXPath);
  100. }
  101. void CEnumerationArray::loadXMLFromEnvXml(const IPropertyTree *pEnvTree)
  102. {
  103. assert(pEnvTree != nullptr);
  104. if (!pEnvTree)
  105. return;
  106. if (pEnvTree->hasProp(this->getEnvXPath()) == false)
  107. throw MakeExceptionFromMap(EX_STR_XPATH_DOES_NOT_EXIST_IN_TREE);
  108. else
  109. QUICK_LOAD_ENV_XML(pEnvTree)
  110. }
  111. int CEnumerationArray::getEnvValueNodeIndex() const
  112. {
  113. int len = this->length();
  114. for (int idx = 0; idx < len; idx++)
  115. {
  116. if (this->item(idx).isInstanceValueValid() == true)
  117. return idx;
  118. }
  119. return 1;
  120. }
  121. void CEnumerationArray::setEnvValueNodeIndex(int index)
  122. {
  123. assert(index >= 0);
  124. assert(index < this->length());
  125. for (int idx = 0; idx < this->length(); idx++)
  126. {
  127. if (this->item(idx).isInstanceValueValid() == true)
  128. this->item(idx).setInstanceValueValid(false);
  129. }
  130. this->item(index).setInstanceValueValid(true);
  131. }
  132. CEnumerationArray* CEnumerationArray::load(CXSDNodeBase* pParentNode, const IPropertyTree *pSchemaRoot, const char* xpath)
  133. {
  134. assert(pSchemaRoot != nullptr);
  135. if (pSchemaRoot == nullptr)
  136. return nullptr;
  137. CEnumerationArray *pEnumerationArray = new CEnumerationArray(pParentNode);
  138. pEnumerationArray->setXSDXPath(xpath);
  139. Owned<IPropertyTreeIterator> elemIter = pSchemaRoot->getElements(xpath);
  140. int count = 1;
  141. ForEach(*elemIter)
  142. {
  143. StringBuffer strXPathExt(xpath);
  144. strXPathExt.appendf("[%d]", count);
  145. CEnumeration *pEnumeration = CEnumeration::load(pEnumerationArray, pSchemaRoot, strXPathExt.str());
  146. pEnumerationArray->append(*pEnumeration);
  147. count++;
  148. }
  149. if (pEnumerationArray->length() == 0)
  150. {
  151. delete pEnumerationArray;
  152. return nullptr;
  153. }
  154. SETPARENTNODE(pEnumerationArray, pParentNode);
  155. return pEnumerationArray;
  156. }