SchemaElement.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #ifndef _SCHEMA_ELEMENT_HPP_
  14. #define _SCHEMA_ELEMENT_HPP_
  15. #include "jiface.hpp"
  16. #include "jstring.hpp"
  17. #include "jlib.hpp"
  18. #include "jarray.hpp"
  19. #include "SchemaCommon.hpp"
  20. #include "SchemaComplexType.hpp"
  21. #include <climits>
  22. namespace CONFIGURATOR
  23. {
  24. class CAnnotation;
  25. class CComplexTypeArray;
  26. class CKeyArray;
  27. class CKeyRefArray;
  28. class CKeyRef;
  29. class CSchema;
  30. class CElementArray;
  31. class CSimpleType;
  32. static const char* DEFAULT_ELEMENT_ARRAY_XPATH(".");
  33. class CElement : public CXSDNodeWithType
  34. {
  35. public:
  36. virtual ~CElement()
  37. {
  38. }
  39. virtual const CXSDNodeBase* getNodeByTypeAndNameAscending(NODE_TYPES eNodeType, const char *pName) const;
  40. virtual const CXSDNodeBase* getNodeByTypeAndNameDescending(NODE_TYPES eNodeType, const char *pName) const;
  41. virtual const char* getXML(const char* /*pComponent*/);
  42. virtual void dump(::std::ostream &cout, unsigned int offset = 0) const;
  43. virtual void getDocumentation(::StringBuffer &strJS) const;
  44. virtual void getJSON(::StringBuffer &strJSON, unsigned int offset = 0, int idx = -1) const;
  45. virtual void populateEnvXPath(::StringBuffer strXPath, unsigned int index = 1);
  46. virtual void loadXMLFromEnvXml(const ::IPropertyTree *pEnvTree);
  47. virtual bool isTopLevelElement() const;
  48. const CSchema* getConstSchemaNode() const;
  49. void setTopLevelElement(bool b = true)
  50. {
  51. m_bTopLevelElement = b;
  52. }
  53. void setParentIndex(int index)
  54. {
  55. m_nParentIndex = index;
  56. }
  57. int getParentIndex() const
  58. {
  59. return m_nParentIndex;
  60. }
  61. const CAnnotation* getAnnotation() const
  62. {
  63. return m_pAnnotation;
  64. }
  65. const CSimpleType* getSimpleType() const
  66. {
  67. return m_pSimpleType;
  68. }
  69. const CComplexTypeArray* getComplexTypeArray() const
  70. {
  71. return m_pComplexTypeArray;
  72. }
  73. static const CXSDNodeBase* getAncestorElement(const CXSDNodeBase *pNode)
  74. {
  75. return pNode->getParentNodeByType(XSD_ELEMENT);
  76. }
  77. static const CElement* getTopMostElement(const CXSDNodeBase *pNode);
  78. static bool isAncestorTopElement(const CXSDNodeBase *pNode)
  79. {
  80. return (pNode != NULL && pNode->getParentNodeByType(XSD_ELEMENT) == getTopMostElement(pNode));
  81. }
  82. void setRefElementNode(CElement *pElement)
  83. {
  84. assert (pElement != NULL && this->getRefElementNode() != NULL);
  85. if (pElement != NULL)
  86. this->m_pElementRefNode = pElement;
  87. }
  88. CElement* getRefElementNode() const
  89. {
  90. return this->m_pElementRefNode;
  91. }
  92. bool isATab() const;
  93. bool isLastTab(const int idx) const;
  94. bool getIsInXSD() const
  95. {
  96. return m_bIsInXSD;
  97. }
  98. bool hasChildElements() const;
  99. int getMaxOccursInt() const
  100. {
  101. if (m_strMaxOccurs.length() == 0) // not set
  102. return SHRT_MIN;
  103. if (strcmp(m_strMaxOccurs.str(), TAG_UNBOUNDED) == 0)
  104. return SHRT_MAX;
  105. return atoi(m_strMaxOccurs.str());
  106. }
  107. int getMinOccursInt() const
  108. {
  109. if (m_strMinOccurs.length() == 0)
  110. return -1;
  111. else
  112. return atoi(m_strMinOccurs.str());
  113. }
  114. static CElement* load(CXSDNodeBase* pParentNode, const ::IPropertyTree *pSchemaRoot, const char* xpath, bool bIsInXSD = true);
  115. const char * getViewType() const;
  116. GETTERSETTER(Name)
  117. GETTERSETTER(MaxOccurs)
  118. GETTERSETTER(MinOccurs)
  119. GETTERSETTER(Title)
  120. GETTERSETTER(InstanceName)
  121. GETTERSETTER(Ref)
  122. GETTERSETTER(Default)
  123. protected:
  124. CElement(CXSDNodeBase* pParentNode, const char* pName = "") : CXSDNodeWithType::CXSDNodeWithType(pParentNode, XSD_ELEMENT), m_strMinOccurs(""), m_strMaxOccurs(""), m_strName(pName), m_pAnnotation(NULL),
  125. m_pComplexTypeArray(NULL), m_pKeyArray(NULL), m_pKeyRefArray(NULL), m_pReverseKeyRefArray(NULL), m_pElementRefNode(NULL), m_pSimpleType(NULL),\
  126. m_bTopLevelElement(false), m_nParentIndex(-1), m_bIsInXSD(true), m_strDefault("")
  127. {
  128. }
  129. void setIsInXSD(bool b);
  130. CAnnotation * m_pAnnotation;
  131. CComplexTypeArray* m_pComplexTypeArray;
  132. CKeyArray *m_pKeyArray;
  133. CKeyRefArray *m_pKeyRefArray;
  134. CKeyRefArray *m_pReverseKeyRefArray;
  135. CElement *m_pElementRefNode;
  136. CSimpleType *m_pSimpleType;
  137. bool m_bTopLevelElement;
  138. int m_nParentIndex;
  139. bool m_bIsInXSD;
  140. private:
  141. };
  142. class CElementArray : public ::CIArrayOf<CElement>, /*public InterfaceImpl,*/ public CXSDNodeBase, public CInterface
  143. {
  144. friend class CElement;
  145. public:
  146. CElementArray(CXSDNodeBase* pParentNode, const ::IPropertyTree *pSchemaRoot = NULL) : CXSDNodeBase::CXSDNodeBase(pParentNode, XSD_ELEMENT_ARRAY),\
  147. m_pSchemaRoot(pSchemaRoot), m_nCountOfElementsInXSD(0)
  148. {
  149. }
  150. virtual ~CElementArray()
  151. {
  152. }
  153. virtual const CXSDNodeBase* getNodeByTypeAndNameAscending(NODE_TYPES eNodeType, const char *pName) const;
  154. virtual const CXSDNodeBase* getNodeByTypeAndNameDescending(NODE_TYPES eNodeType, const char *pName) const;
  155. const CElement* getElementByNameAscending(const char *pName) const;
  156. virtual void dump(::std::ostream &cout, unsigned int offset = 0) const;
  157. virtual void getDocumentation(::StringBuffer &strDoc) const;
  158. virtual void getJSON(::StringBuffer &strJSON, unsigned int offset = 0, int idx = -1) const;
  159. virtual void populateEnvXPath(::StringBuffer strXPath, unsigned int index = 1);
  160. virtual void loadXMLFromEnvXml(const ::IPropertyTree *pEnvTree);
  161. virtual const char* getXML(const char* /*pComponent*/);
  162. virtual int getCountOfSiblingElements(const char *pXPath) const;
  163. bool anyElementsHaveMaxOccursGreaterThanOne() const;
  164. virtual void setSchemaRoot(const ::IPropertyTree *pSchemaRoot)
  165. {
  166. assert(m_pSchemaRoot == NULL);
  167. assert(pSchemaRoot);
  168. m_pSchemaRoot = pSchemaRoot;
  169. }
  170. const ::IPropertyTree* getSchemaRoot() const
  171. {
  172. return m_pSchemaRoot;
  173. }
  174. int getCountOfElementsInXSD() const
  175. {
  176. return m_nCountOfElementsInXSD;
  177. }
  178. void incCountOfElementsInXSD()
  179. {
  180. m_nCountOfElementsInXSD++;
  181. }
  182. static CElementArray* load(CXSDNodeBase* pParentNode, const ::IPropertyTree *pSchemaRoot, const char* xpath = DEFAULT_ELEMENT_ARRAY_XPATH);
  183. static CElementArray* load(const char *pSchemaFile);
  184. protected:
  185. const ::IPropertyTree *m_pSchemaRoot;
  186. int m_nCountOfElementsInXSD;
  187. int getSiblingIndex(const char* pXSDXPath, const CElement* pElement);
  188. private:
  189. CElementArray() : CXSDNodeBase::CXSDNodeBase(NULL, XSD_ELEMENT_ARRAY)
  190. {
  191. }
  192. };
  193. class CArrayOfElementArrays : public ::CIArrayOf<CElementArray>, public CInterface, public CXSDNodeBase
  194. {
  195. friend class CElementArray;
  196. public:
  197. CArrayOfElementArrays(CXSDNodeBase* pParentNode, const ::IPropertyTree *pSchemaRoot = NULL) : CXSDNodeBase::CXSDNodeBase(pParentNode, XSD_ARRAY_OF_ELEMENT_ARRAYS)
  198. {
  199. }
  200. virtual ~CArrayOfElementArrays()
  201. {
  202. this->clear();
  203. }
  204. virtual void dump(::std::ostream &cout, unsigned int offset = 0) const
  205. {
  206. int len = this->length();
  207. for(int idx = 0; idx < len; idx++)
  208. {
  209. this->item(idx).dump(cout);
  210. }
  211. }
  212. virtual void getJSON(::StringBuffer &strJSON, unsigned int offset = 0, int idx = -1) const;
  213. virtual void getDocumentation(::StringBuffer &strDoc) const;
  214. virtual void populateEnvXPath(::StringBuffer strXPath, unsigned int index = 1);
  215. virtual void loadXMLFromEnvXml(const ::IPropertyTree *pEnvTree);
  216. static CArrayOfElementArrays* load(CXSDNodeBase* pParentNode, const ::IPropertyTree *pSchemaRoot, const char* xpath = DEFAULT_ELEMENT_ARRAY_XPATH);
  217. };
  218. }
  219. #endif // _SCHEMA_ELEMENT_HPP_