SchemaKeyRef.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "SchemaSelector.hpp"
  15. #include "ConfigSchemaHelper.hpp"
  16. #include "SchemaMapManager.hpp"
  17. #include "SchemaKeyRef.hpp"
  18. using namespace CONFIGURATOR;
  19. #define IPropertyTree ::IPropertyTree
  20. #define StringBuffer ::StringBuffer
  21. CKeyRef* CKeyRef::load(CXSDNodeBase* pParentNode, const IPropertyTree *pSchemaRoot, const char* xpath)
  22. {
  23. assert(pSchemaRoot != nullptr);
  24. assert(pParentNode != nullptr);
  25. assert(pParentNode->getNodeType() == XSD_KEYREF_ARRAY);
  26. if (pSchemaRoot == nullptr || pParentNode == nullptr)
  27. {
  28. // TODO: Throw Exception
  29. return nullptr;
  30. }
  31. CKeyRef *pKeyRef = new CKeyRef(pParentNode);
  32. if (xpath != nullptr && *xpath != 0)
  33. {
  34. IPropertyTree* pTree = pSchemaRoot->queryPropTree(xpath);
  35. if (pTree == nullptr)
  36. return nullptr; // no xs:KeyRef
  37. const char* pName = pTree->queryProp(XML_ATTR_NAME);
  38. const char* pRefer = pTree->queryProp(XML_ATTR_REFER);
  39. if (pName != nullptr && pRefer != nullptr)
  40. {
  41. pKeyRef->setXSDXPath(xpath);
  42. pKeyRef->setName(pName);
  43. }
  44. else
  45. {
  46. delete pKeyRef;
  47. pKeyRef = nullptr;
  48. assert(!"value attribute can be empty!");
  49. // TODO: throw MakeExceptionFromMap(EX_STR_MISSING_VALUE_ATTRIBUTE_IN_LENGTH);
  50. }
  51. const char *pID = pTree->queryProp(XML_ATTR_ID);
  52. if (pID != nullptr)
  53. pKeyRef->setID(pID);
  54. StringBuffer strXPathExt(xpath);
  55. strXPathExt.append("/").append(XSD_TAG_FIELD);
  56. pKeyRef->m_pFieldArray = CFieldArray::load(pKeyRef, pSchemaRoot, strXPathExt.str());
  57. strXPathExt.set(xpath);
  58. strXPathExt.append("/").append(XSD_TAG_SELECTOR);
  59. pKeyRef->m_pSelector = CSelector::load(pKeyRef, pSchemaRoot, strXPathExt.str());
  60. }
  61. return pKeyRef;
  62. }
  63. void CKeyRef::dump(::std::ostream& cout, unsigned int offset) const
  64. {
  65. offset += STANDARD_OFFSET_1;
  66. quickOutHeader(cout, XSD_KEYREF_STR, offset);
  67. QUICK_OUT(cout, Name, offset);
  68. QUICK_OUT(cout, ID, offset);
  69. QUICK_OUT(cout, Refer, offset);
  70. QUICK_OUT(cout, XSDXPath, offset);
  71. if (m_pFieldArray != nullptr)
  72. m_pFieldArray->dump(cout, offset);
  73. if (m_pSelector != nullptr)
  74. m_pSelector->dump(cout, offset);
  75. quickOutFooter(cout, XSD_KEYREF_STR, offset);
  76. }
  77. bool CKeyRef::checkConstraint(const char *pValue) const
  78. {
  79. assert (pValue != nullptr);
  80. if (pValue == nullptr)
  81. return true;
  82. else
  83. {
  84. StringBuffer strQName(this->getXSDXPath());
  85. strQName.append("/").append(this->getRefer());
  86. CKey *pKey = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getKeyFromXSDXPath(strQName.str());
  87. assert(pKey);
  88. if (pKey == nullptr)
  89. return true;
  90. return pKey->checkConstraint(pValue);
  91. }
  92. }
  93. void CKeyRef::populateEnvXPath(StringBuffer strXPath, unsigned int index)
  94. {
  95. assert(this->m_pSelector != nullptr);
  96. this->setEnvXPath(strXPath.str());
  97. if (this->m_pSelector != nullptr)
  98. {
  99. this->m_pSelector->populateEnvXPath(strXPath.str());
  100. CConfigSchemaHelper::getInstance()->addKeyRefForReverseAssociation(this);
  101. }
  102. }
  103. CKeyRefArray* CKeyRefArray::load(CXSDNodeBase* pParentNode, const IPropertyTree *pSchemaRoot, const char* xpath)
  104. {
  105. assert(pSchemaRoot != nullptr);
  106. assert(pParentNode->getNodeType() == XSD_ELEMENT);
  107. if (pSchemaRoot == nullptr || xpath == nullptr)
  108. return nullptr;
  109. StringBuffer strXPathExt(xpath);
  110. CKeyRefArray *pKeyRefArray = new CKeyRefArray(pParentNode);
  111. pKeyRefArray->setXSDXPath(xpath);
  112. Owned<IPropertyTreeIterator> attributeIter = pSchemaRoot->getElements(xpath, ipt_ordered);
  113. int count = 1;
  114. ForEach(*attributeIter)
  115. {
  116. strXPathExt.setf("%s[%d]", xpath, count);
  117. CKeyRef *pKeyRef = CKeyRef::load(pKeyRefArray, pSchemaRoot, strXPathExt.str());
  118. if (pKeyRef != nullptr)
  119. pKeyRefArray->append(*pKeyRef);
  120. count++;
  121. }
  122. if (pKeyRefArray->length() == 0)
  123. {
  124. delete pKeyRefArray;
  125. pKeyRefArray = nullptr;
  126. }
  127. return pKeyRefArray;
  128. }
  129. void CKeyRefArray::dump(::std::ostream &cout, unsigned int offset) const
  130. {
  131. offset+= STANDARD_OFFSET_1;
  132. quickOutHeader(cout, XSD_KEYREF_ARRAY_STR, offset);
  133. QUICK_OUT(cout, XSDXPath, offset);
  134. QUICK_OUT(cout, EnvXPath, offset);
  135. QUICK_OUT_ARRAY(cout, offset);
  136. quickOutFooter(cout, XSD_KEYREF_ARRAY_STR, offset);
  137. }
  138. void CKeyRefArray::populateEnvXPath(StringBuffer strXPath, unsigned int index)
  139. {
  140. this->setEnvXPath(strXPath);
  141. QUICK_ENV_XPATH_WITH_INDEX(strXPath, index)
  142. }