EnvironmentModel.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "jlib.hpp"
  14. #include "jlog.hpp"
  15. #include <cassert>
  16. #include "EnvironmentModel.hpp"
  17. #include "SchemaCommon.hpp"
  18. #include "ConfigSchemaHelper.hpp"
  19. #include "SchemaMapManager.hpp"
  20. #include "SchemaElement.hpp"
  21. #include "SchemaSchema.hpp"
  22. #define LOG_CONSTRUCTOR
  23. using namespace CONFIGURATOR;
  24. CEnvironmentModelNode::CEnvironmentModelNode(const CEnvironmentModelNode *pParent, int index, CXSDNodeBase *pNode) : m_pParent(NULL), m_pArrChildNodes(NULL)
  25. {
  26. if (pParent == NULL && index == 0) // if this is the 'Environment' Node
  27. {
  28. this->m_pXSDNode = NULL;
  29. this->m_pArrChildNodes = new PointerArray(); // array of ptrs to each component
  30. int nComps = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getNumberOfComponents();
  31. for (int idx = 0; idx < nComps; idx++)
  32. {
  33. CXSDNodeBase *pNode = CConfigSchemaHelper::getInstance()->getSchemaMapManager()->getComponent(idx);
  34. if (pNode == NULL)
  35. continue;
  36. assert(pNode != NULL);
  37. CEnvironmentModelNode *pModelNode = new CEnvironmentModelNode(this, 0, pNode);
  38. m_pArrChildNodes->append(pModelNode);
  39. }
  40. }
  41. else if (pParent != NULL && pParent->getParent() == NULL) // component tier
  42. {
  43. assert(pParent != NULL);
  44. assert(pNode != NULL);
  45. this->m_pParent = pParent;
  46. this->m_pXSDNode = pNode;
  47. this->m_pArrChildNodes = new PointerArray();
  48. assert(m_pXSDNode->getNodeType() == XSD_ELEMENT);
  49. assert(m_pXSDNode->getConstParentNode()->getNodeType() == XSD_ELEMENT_ARRAY);
  50. const CElement *pElement = static_cast<const CElement*>(m_pXSDNode->getNodeByTypeAndNameDescending(XSD_ELEMENT, NULL));
  51. const CElementArray *pElementArray = pElement != NULL ? static_cast<const CElementArray*>(pElement->getParentNode()) : NULL;
  52. for (int idx = 0; (pElementArray != NULL && idx < pElementArray->length()); idx++)
  53. {
  54. CEnvironmentModelNode *pModelNode = new CEnvironmentModelNode(this, idx, &(pElementArray->item(idx)));
  55. m_pArrChildNodes->append(pModelNode);
  56. }
  57. }
  58. else // instance tier
  59. {
  60. assert(pParent != NULL);
  61. assert(pNode != NULL);
  62. this->m_pParent = pParent;
  63. this->m_pXSDNode = pNode;
  64. this->m_pArrChildNodes = new PointerArray();
  65. }
  66. }
  67. const CEnvironmentModelNode* CEnvironmentModelNode::getChild(int index) const
  68. {
  69. assert(m_pArrChildNodes == NULL || m_pArrChildNodes->length() > index);
  70. if (m_pArrChildNodes != NULL)
  71. {
  72. CEnvironmentModelNode *pNode = static_cast<CEnvironmentModelNode*>(m_pArrChildNodes->item(index));
  73. return pNode;
  74. }
  75. return NULL;
  76. }
  77. CEnvironmentModelNode::~CEnvironmentModelNode()
  78. {
  79. delete m_pArrChildNodes;
  80. }
  81. int CEnvironmentModelNode::getNumberOfChildren() const
  82. {
  83. int nRetVal = 0;
  84. if (m_pArrChildNodes != NULL)
  85. nRetVal = m_pArrChildNodes->length();
  86. else if (m_pArrChildNodes == NULL && m_pXSDNode != NULL && m_pParent != (CEnvironmentModel::getInstance()->getRoot()))
  87. return 0;
  88. else
  89. {
  90. assert(this->getXSDNode()->getNodeType() == XSD_ELEMENT);
  91. const CElement *pElement = static_cast<const CElement*>(this->getXSDNode());
  92. const CElementArray *pElementArray = static_cast<const CElementArray*>(pElement->getConstParentNode());
  93. assert(pElementArray->getNodeType() == XSD_ELEMENT_ARRAY);
  94. nRetVal = pElementArray->getCountOfSiblingElements(pElement->getXSDXPath());
  95. }
  96. return nRetVal;
  97. }
  98. CEnvironmentModel* CEnvironmentModel::getInstance()
  99. {
  100. static CEnvironmentModel *s_pCEnvModel = NULL;
  101. if (s_pCEnvModel == NULL)
  102. s_pCEnvModel = new CEnvironmentModel();
  103. return s_pCEnvModel;
  104. }
  105. CEnvironmentModel::CEnvironmentModel()
  106. {
  107. m_pRootNode = new CEnvironmentModelNode(NULL);
  108. }
  109. CEnvironmentModel::~CEnvironmentModel()
  110. {
  111. delete m_pRootNode;
  112. }
  113. const CEnvironmentModelNode* CEnvironmentModel::getParent(CEnvironmentModelNode *pChild)
  114. {
  115. if (pChild != NULL)
  116. return pChild->getParent();
  117. assert(false);
  118. return NULL;
  119. }
  120. const CEnvironmentModelNode* CEnvironmentModel::getChild(CEnvironmentModelNode *pParent, int index)
  121. {
  122. assert(index >= 0);
  123. assert(pParent != NULL);
  124. if (pParent == NULL)
  125. {
  126. return m_pRootNode;
  127. }
  128. assert(pParent->getNumberOfChildren() > index);
  129. return pParent->getChild(index);
  130. }
  131. const char* CEnvironmentModel::getData(const CEnvironmentModelNode *pChild) const
  132. {
  133. assert(pChild != NULL);
  134. if (pChild == reinterpret_cast<const CEnvironmentModelNode*>(this))
  135. return NULL;
  136. const CElement *pElement = static_cast<const CElement*>(pChild->getXSDNode());
  137. if (pElement != NULL)
  138. {
  139. const char *pInstanceName = pElement->getInstanceName();
  140. if (pChild->getParent() != CEnvironmentModel::getInstance()->getRoot())
  141. return pInstanceName;
  142. else
  143. return pElement->getName();
  144. }
  145. else
  146. return "Environment";
  147. }
  148. const char* CEnvironmentModel::getXSDFileName(const CEnvironmentModelNode *pChild) const
  149. {
  150. assert(pChild != NULL);
  151. const CElement *pElement = static_cast<const CElement*>(pChild->getXSDNode());
  152. if (pElement != NULL && pElement->isTopLevelElement() == true)
  153. {
  154. const CSchema *pSchema = dynamic_cast<const CSchema*>(pElement->getConstAncestorNode(2));
  155. if (pSchema == NULL)
  156. {
  157. assert(false);
  158. return NULL;
  159. }
  160. else
  161. return pSchema->getSchemaFileName();
  162. }
  163. else
  164. return NULL;
  165. }
  166. const char* CEnvironmentModel::getInstanceName(const CEnvironmentModelNode *pChild) const
  167. {
  168. assert(pChild != NULL);
  169. const CElement *pElement = static_cast<const CElement*>(pChild->getXSDNode());
  170. if (pElement != NULL && pElement->isTopLevelElement() == true)
  171. return pElement->getInstanceName();
  172. else
  173. return pElement->getName();
  174. }
  175. int CEnvironmentModel::getNumberOfRootNodes() const
  176. {
  177. return 1;
  178. }
  179. CEnvironmentModelNode* CEnvironmentModel::getRoot(int index)
  180. {
  181. assert(m_pRootNode != 0);
  182. assert(index == 0);
  183. if (index != 0)
  184. return NULL;
  185. else
  186. return m_pRootNode;
  187. }