SchemaAttributeGroup.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "SchemaAttributeGroup.hpp"
  14. #include "DocumentationMarkup.hpp"
  15. #include "ConfigSchemaHelper.hpp"
  16. #include "jptree.hpp"
  17. #include "SchemaMapManager.hpp"
  18. #include"JSONMarkUp.hpp"
  19. using namespace CONFIGURATOR;
  20. #define StringBuffer ::StringBuffer
  21. #define IPropertyTree ::IPropertyTree
  22. const CXSDNodeBase* CAttributeGroup::getNodeByTypeAndNameAscending(NODE_TYPES eNodeType, const char *pName) const
  23. {
  24. const CXSDNodeBase* pMatchingNode = NULL;
  25. if (eNodeType == this->getNodeType() && (pName != NULL ? !strcmp(pName, this->getNodeTypeStr()) : true))
  26. {
  27. assert(pName != NULL); // for now pName should always be populated
  28. return this;
  29. }
  30. if (m_pAttributeArray != NULL)
  31. pMatchingNode = m_pAttributeArray->getNodeByTypeAndNameAscending(eNodeType, pName);
  32. if (pMatchingNode == NULL && m_pAttributeArray != NULL)
  33. pMatchingNode = m_pAttributeArray->getNodeByTypeAndNameDescending(eNodeType, pName);
  34. return pMatchingNode;
  35. }
  36. CAttributeGroup::~CAttributeGroup()
  37. {
  38. }
  39. const CXSDNodeBase* CAttributeGroup::getNodeByTypeAndNameDescending(NODE_TYPES eNodeType, const char *pName) const
  40. {
  41. const CXSDNodeBase* pMatchingNode = NULL;
  42. if (eNodeType == this->getNodeType() && (pName != NULL ? !strcmp(pName, this->getNodeTypeStr()) : true))
  43. {
  44. assert(pName != NULL); // for now pName should always be populated
  45. return this;
  46. }
  47. if (m_pAttributeArray != NULL)
  48. pMatchingNode = m_pAttributeArray->getNodeByTypeAndNameDescending(eNodeType, pName);
  49. return pMatchingNode;
  50. }
  51. void CAttributeGroup::dump(::std::ostream &cout, unsigned int offset) const
  52. {
  53. offset+= STANDARD_OFFSET_1;
  54. QuickOutHeader(cout, XSD_ATTRIBUTE_GROUP_STR, offset);
  55. QUICK_OUT(cout, Name, offset);
  56. QUICK_OUT(cout, Ref, offset);
  57. QUICK_OUT(cout, ID, offset);
  58. QUICK_OUT(cout, XSDXPath, offset);
  59. QUICK_OUT(cout, EnvXPath, offset);
  60. if (m_pAttributeArray != NULL)
  61. m_pAttributeArray->dump(cout, offset);
  62. QuickOutFooter(cout, XSD_ATTRIBUTE_GROUP_STR, offset);
  63. }
  64. void CAttributeGroup::getDocumentation(StringBuffer &strDoc) const
  65. {
  66. if (this->getRef() != NULL && this->getRef()[0] != 0 && m_pRefAttributeGroup != NULL)
  67. {
  68. strDoc.appendf("%s%s%s", DM_TITLE_BEGIN, m_pRefAttributeGroup->getName(), DM_TITLE_END);
  69. DEBUG_MARK_STRDOC;
  70. if (m_pRefAttributeGroup->getConstAttributeArray() != NULL)
  71. m_pRefAttributeGroup->getConstAttributeArray()->getDocumentation(strDoc);
  72. }
  73. }
  74. void CAttributeGroup::populateEnvXPath(StringBuffer strXPath, unsigned int index)
  75. {
  76. if (this->getRef() != NULL && this->getRef()[0] != 0 && m_pRefAttributeGroup != NULL)
  77. {
  78. if (m_pRefAttributeGroup->getConstAttributeArray() != NULL)
  79. m_pRefAttributeGroup->getAttributeArray()->populateEnvXPath(strXPath);
  80. }
  81. this->setEnvXPath(strXPath);
  82. }
  83. void CAttributeGroup::loadXMLFromEnvXml(const IPropertyTree *pEnvTree)
  84. {
  85. assert(pEnvTree != NULL);
  86. if (m_pAttributeArray != NULL)
  87. {
  88. try
  89. {
  90. m_pAttributeArray->loadXMLFromEnvXml(pEnvTree);
  91. }
  92. catch (...)
  93. {
  94. // validation check needed here
  95. }
  96. }
  97. if (m_pAnnotation != NULL)
  98. {
  99. try
  100. {
  101. m_pAnnotation->loadXMLFromEnvXml(pEnvTree);
  102. }
  103. catch (...)
  104. {
  105. // validation check needed here
  106. }
  107. }
  108. if (m_pRefAttributeGroup != NULL)
  109. {
  110. try
  111. {
  112. m_pRefAttributeGroup->loadXMLFromEnvXml(pEnvTree);
  113. }
  114. catch (...)
  115. {
  116. // validation check needed here
  117. }
  118. }
  119. }
  120. const char* CAttributeGroup::getXML(const char* /*pComponent*/)
  121. {
  122. if (m_strXML.length() == 0 && m_pAttributeArray != NULL)
  123. {
  124. for (int idx = 0; idx < m_pAttributeArray->length(); idx++)
  125. {
  126. m_strXML.append("\n").append(m_pAttributeArray->item(idx).getXML(NULL));
  127. }
  128. }
  129. return m_strXML.str();
  130. }
  131. CAttributeGroup* CAttributeGroup::load(CXSDNodeBase* pParentNode, const IPropertyTree *pSchemaRoot, const char* xpath)
  132. {
  133. assert(pSchemaRoot != NULL);
  134. if (pSchemaRoot == NULL || xpath == NULL)
  135. return NULL;
  136. assert(pParentNode->getNodeType() != XSD_ATTRIBUTE_GROUP);
  137. CAttributeGroup *pAttributeGroup = new CAttributeGroup(pParentNode);
  138. pAttributeGroup->setXSDXPath(xpath);
  139. assert(pAttributeGroup);
  140. if (pAttributeGroup == NULL)
  141. return NULL;
  142. IPropertyTree *pTree = pSchemaRoot->queryPropTree(xpath);
  143. if (pTree == NULL)
  144. return NULL;
  145. pAttributeGroup->setName(pTree->queryProp(XML_ATTR_NAME));
  146. pAttributeGroup->setID(pTree->queryProp(XML_ATTR_ID));
  147. pAttributeGroup->setRef(pTree->queryProp(XML_ATTR_REF));
  148. if (pAttributeGroup->getRef() != NULL && pAttributeGroup->getRef()[0] != 0)
  149. {
  150. if (pAttributeGroup->getName() != NULL && pAttributeGroup->getName()[0] != 0)
  151. {
  152. assert(false); //can't have both nameand ref set
  153. return NULL;
  154. }
  155. else
  156. CConfigSchemaHelper::getInstance()->addAttributeGroupToBeProcessed(pAttributeGroup);
  157. }
  158. else if (pAttributeGroup->getName() != NULL && pAttributeGroup->getName()[0] != 0)
  159. CConfigSchemaHelper::getInstance()->getSchemaMapManager()->setAttributeGroupTypeWithName(pAttributeGroup->getName(), pAttributeGroup);
  160. StringBuffer strXPath(xpath);
  161. strXPath.append("/").append(XSD_TAG_ATTRIBUTE);
  162. CAttributeArray *pAttribArray = CAttributeArray::load(pAttributeGroup, pSchemaRoot, strXPath.str());
  163. if (pAttribArray != NULL)
  164. pAttributeGroup->setAttributeArray(pAttribArray);
  165. strXPath.setf("%s/%s",xpath, XSD_TAG_ANNOTATION);
  166. pAttributeGroup->setAnnotation(CAnnotation::load(pAttributeGroup, pSchemaRoot, strXPath.str()));
  167. return pAttributeGroup;
  168. }
  169. void CAttributeGroup::getJSON(StringBuffer &strJSON, unsigned int offset, int idx) const
  170. {
  171. assert(this->getRef() != NULL);
  172. if (this->getRef() != NULL && this->getRef()[0] != 0 && m_pRefAttributeGroup != NULL)
  173. {
  174. if (m_pRefAttributeGroup->getConstAttributeArray() != NULL && m_pRefAttributeGroup->getConstAttributeArray()->length() > 0)
  175. {
  176. QuickOutPad(strJSON, offset);
  177. CONTENT_INNER_CONTENT_BEGIN
  178. m_pRefAttributeGroup->getConstAttributeArray()->getJSON(strJSON, offset);
  179. }
  180. }
  181. }
  182. CAttributeGroupArray::~CAttributeGroupArray()
  183. {
  184. }
  185. CAttributeGroupArray* CAttributeGroupArray::load(const char* pSchemaFile)
  186. {
  187. assert(false); // Should never call this?
  188. if (pSchemaFile == NULL)
  189. return NULL;
  190. Linked<IPropertyTree> pSchemaRoot;
  191. StringBuffer schemaPath;
  192. schemaPath.appendf("%s%s", DEFAULT_SCHEMA_DIRECTORY, pSchemaFile);
  193. pSchemaRoot.setown(createPTreeFromXMLFile(schemaPath.str()));
  194. return CAttributeGroupArray::load(NULL, pSchemaRoot, XSD_TAG_ATTRIBUTE_GROUP);
  195. }
  196. CAttributeGroupArray* CAttributeGroupArray::load(CXSDNodeBase* pParentNode, const IPropertyTree *pSchemaRoot, const char* xpath)
  197. {
  198. assert(pSchemaRoot != NULL);
  199. if (pSchemaRoot == NULL || xpath == NULL)
  200. return NULL;
  201. CAttributeGroupArray *pAttribGroupArray = new CAttributeGroupArray(pParentNode);
  202. pAttribGroupArray->setXSDXPath(xpath);
  203. StringBuffer strXPathExt(xpath);
  204. // to iterate over xs:attributeGroup nodes at same level in tree
  205. Owned<IPropertyTreeIterator> attribGroupsIter = pSchemaRoot->getElements(strXPathExt.str());
  206. int count = 1;
  207. ForEach(*attribGroupsIter)
  208. {
  209. strXPathExt.clear().appendf("%s[%d]", xpath, count);
  210. CAttributeGroup *pAttribGroup = CAttributeGroup::load(pAttribGroupArray, pSchemaRoot, strXPathExt.str());
  211. if (pAttribGroup != NULL)
  212. pAttribGroupArray->append(*pAttribGroup);
  213. count++;
  214. }
  215. if (pAttribGroupArray->length() == 0)
  216. {
  217. delete pAttribGroupArray;
  218. return NULL;
  219. }
  220. return pAttribGroupArray;
  221. }
  222. void CAttributeGroupArray::dump(::std::ostream& cout, unsigned int offset) const
  223. {
  224. offset+= STANDARD_OFFSET_1;
  225. QuickOutHeader(cout,XSD_ATTRIBUTE_GROUP_ARRAY_STR, offset);
  226. QUICK_OUT(cout, XSDXPath, offset);
  227. QUICK_OUT(cout, EnvXPath, offset);
  228. QUICK_OUT_ARRAY(cout, offset);
  229. QuickOutFooter(cout,XSD_ATTRIBUTE_GROUP_ARRAY_STR, offset);
  230. }
  231. void CAttributeGroupArray::getDocumentation(StringBuffer &strDoc) const
  232. {
  233. StringBuffer strDocDupe1(strDoc);
  234. QUICK_DOC_ARRAY(strDocDupe1);
  235. if (strDocDupe1.length() == strDoc.length()) // hack
  236. return;
  237. for (int idx=0; idx < this->length(); idx++)
  238. {
  239. strDoc.append(DM_SECT3_BEGIN);
  240. (this->item(idx)).getDocumentation(strDoc);
  241. strDoc.append(DM_SECT3_END);
  242. }
  243. }
  244. void CAttributeGroupArray::populateEnvXPath(StringBuffer strXPath, unsigned int index)
  245. {
  246. assert(index == 1); // Only 1 array of elements per node
  247. for (int idx=0; idx < this->length(); idx++)
  248. {
  249. (this->item(idx)).populateEnvXPath(strXPath, 1);
  250. }
  251. this->setEnvXPath(strXPath);
  252. }
  253. void CAttributeGroupArray::loadXMLFromEnvXml(const IPropertyTree *pEnvTree)
  254. {
  255. QUICK_LOAD_ENV_XML(pEnvTree)
  256. }
  257. void CAttributeGroupArray::getJSON(StringBuffer &strJSON, unsigned int offset, int idx) const
  258. {
  259. int nLength = this->length();
  260. offset += STANDARD_OFFSET_1;
  261. QuickOutPad(strJSON, offset);
  262. for (int lidx = 0; lidx < nLength; lidx++)
  263. {
  264. if (lidx != 0)
  265. {
  266. if (lidx != 0)
  267. {
  268. QuickOutPad(strJSON, offset);
  269. strJSON.append(",");
  270. }
  271. }
  272. strJSON.append("{");
  273. CJSONMarkUpHelper::createUIContent(strJSON, offset, JSON_TYPE_TAB, this->item(lidx).getRef(), this->getEnvXPath());
  274. if (lidx != 0)
  275. {
  276. strJSON.append("\n");
  277. }
  278. this->item(lidx).getJSON(strJSON, offset, lidx);
  279. strJSON.append("]}}\n");
  280. if (lidx+1 < nLength)
  281. {
  282. QuickOutPad(strJSON, offset);
  283. }
  284. }
  285. }