SchemaRestriction.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "jptree.hpp"
  14. #include "SchemaRestriction.hpp"
  15. #include "SchemaEnumeration.hpp"
  16. #include "SchemaAttributes.hpp"
  17. #include "XMLTags.h"
  18. #include "ConfigSchemaHelper.hpp"
  19. #include "DocumentationMarkup.hpp"
  20. #include "SchemaMapManager.hpp"
  21. #include "SchemaFractionDigits.hpp"
  22. #include "SchemaLength.hpp"
  23. #include "SchemaTotalDigits.hpp"
  24. #include "SchemaWhiteSpace.hpp"
  25. #include "SchemaSimpleContent.hpp"
  26. using namespace CONFIGURATOR;
  27. #define StringBuffer ::StringBuffer
  28. #define IPropertyTree ::IPropertyTree
  29. #define QUICK_LOAD_XSD_RESTRICTIONS(X, Y) \
  30. strXPathExt.set(xpath); \
  31. strXPathExt.append("/").append(Y); \
  32. C##X *p##X = C##X::load(pRestriction, pSchemaRoot, strXPathExt.str()); \
  33. if (p##X != NULL) pRestriction->set##X(p##X);
  34. CRestriction::~CRestriction()
  35. {
  36. CConfigSchemaHelper::getInstance()->getSchemaMapManager()->removeMapOfXPathToRestriction(this->getEnvXPath());
  37. }
  38. void CRestriction::dump(::std::ostream& cout, unsigned int offset) const
  39. {
  40. offset+= STANDARD_OFFSET_1;
  41. quickOutHeader(cout, XSD_RESTRICTION_STR, offset);
  42. QUICK_OUT_2(Base);
  43. QUICK_OUT_2(ID);
  44. QUICK_OUT(cout, XSDXPath, offset);
  45. QUICK_OUT(cout, EnvXPath, offset);
  46. QUICK_OUT(cout, EnvValueFromXML, offset);
  47. QUICK_OUT_3(EnumerationArray)
  48. QUICK_OUT_3(FractionDigits)
  49. QUICK_OUT_3(Length)
  50. QUICK_OUT_3(MaxExclusive)
  51. QUICK_OUT_3(MaxInclusive)
  52. QUICK_OUT_3(MinExclusive)
  53. QUICK_OUT_3(MinInclusive)
  54. QUICK_OUT_3(MaxLength)
  55. QUICK_OUT_3(MinLength)
  56. QUICK_OUT_3(Pattern)
  57. QUICK_OUT_3(TotalDigits)
  58. QUICK_OUT_3(WhiteSpace)
  59. quickOutFooter(cout, XSD_RESTRICTION_STR, offset);
  60. }
  61. void CRestriction::getDocumentation(StringBuffer &strDoc) const
  62. {
  63. if (m_pEnumerationArray != NULL)
  64. {
  65. strDoc.appendf("<%s>",DM_PARA);
  66. DEBUG_MARK_STRDOC
  67. m_pEnumerationArray->getDocumentation(strDoc);
  68. strDoc.appendf("</%s>",DM_PARA);
  69. DEBUG_MARK_STRDOC
  70. }
  71. }
  72. void CRestriction::populateEnvXPath(StringBuffer strXPath, unsigned int index)
  73. {
  74. this->setEnvXPath(strXPath);
  75. CConfigSchemaHelper::getInstance()->getSchemaMapManager()->addMapOfXPathToRestriction(this->getEnvXPath(), this);
  76. if (this->m_pEnumerationArray != NULL)
  77. this->m_pEnumerationArray->populateEnvXPath(strXPath);
  78. }
  79. void CRestriction::loadXMLFromEnvXml(const IPropertyTree *pEnvTree)
  80. {
  81. if (m_pEnumerationArray != NULL)
  82. m_pEnumerationArray->loadXMLFromEnvXml(pEnvTree);
  83. }
  84. CRestriction* CRestriction::load(CXSDNodeBase* pParentNode, const IPropertyTree *pSchemaRoot, const char* xpath)
  85. {
  86. if (pParentNode == NULL || pSchemaRoot == NULL)
  87. return NULL;
  88. IPropertyTree *pTree = pSchemaRoot->queryPropTree(xpath);
  89. const char* pID = NULL;
  90. const char* pBase = NULL;
  91. pID = pTree->queryProp(XML_ATTR_ID);
  92. pBase = pTree->queryProp(XML_ATTR_BASE);
  93. CRestriction* pRestriction = new CRestriction(pParentNode, pID, pBase);
  94. pRestriction->setXSDXPath(xpath);
  95. StringBuffer strXPathExt;
  96. QUICK_LOAD_XSD_RESTRICTIONS(EnumerationArray, XSD_TAG_ENUMERATION)
  97. QUICK_LOAD_XSD_RESTRICTIONS(FractionDigits, XSD_TAG_FRACTION_DIGITS)
  98. QUICK_LOAD_XSD_RESTRICTIONS(Length, XSD_TAG_LENGTH)
  99. QUICK_LOAD_XSD_RESTRICTIONS(MaxExclusive, XSD_TAG_MAX_EXCLUSIVE)
  100. QUICK_LOAD_XSD_RESTRICTIONS(MaxInclusive, XSD_TAG_MAX_INCLUSIVE)
  101. QUICK_LOAD_XSD_RESTRICTIONS(MinExclusive, XSD_TAG_MIN_EXCLUSIVE)
  102. QUICK_LOAD_XSD_RESTRICTIONS(MinInclusive, XSD_TAG_MIN_INCLUSIVE)
  103. QUICK_LOAD_XSD_RESTRICTIONS(MaxLength, XSD_TAG_MAX_LENGTH)
  104. QUICK_LOAD_XSD_RESTRICTIONS(MinLength, XSD_TAG_MIN_LENGTH)
  105. QUICK_LOAD_XSD_RESTRICTIONS(Pattern, XSD_TAG_PATTERN)
  106. QUICK_LOAD_XSD_RESTRICTIONS(TotalDigits, XSD_TAG_TOTAL_DIGITS)
  107. QUICK_LOAD_XSD_RESTRICTIONS(WhiteSpace, XSD_TAG_WHITE_SPACE)
  108. if (pBase != NULL && *pBase != 0 && pRestriction != NULL)
  109. CConfigSchemaHelper::getInstance()->addNodeForBaseProcessing(pRestriction);
  110. return pRestriction;
  111. }
  112. const char* CRestriction::getXML(const char* /*pComponent*/)
  113. {
  114. if (m_strXML.length () == 0)
  115. {
  116. m_strXML.append("<").append(getBase()).append("\n");
  117. m_strXML.append("<").append(getID()).append("\n");
  118. m_strXML.append("/>\n");
  119. }
  120. return m_strXML.str();
  121. }
  122. bool CRestriction::checkConstraint(const char *pValue) const
  123. {
  124. const CXSDNodeBase *pNodeBase = this->getBaseNode();
  125. assert(pNodeBase != NULL);
  126. if (pNodeBase != NULL)
  127. {
  128. const CXSDBuiltInDataType *pNodeBuiltInType = dynamic_cast<const CXSDBuiltInDataType*>(pNodeBase);
  129. if (pNodeBuiltInType != NULL && pNodeBuiltInType->checkConstraint(pValue) == false)
  130. return false;
  131. if (pNodeBase->getNodeType() == XSD_SIMPLE_TYPE)
  132. {
  133. const CSimpleType *pNodeSimpleType = dynamic_cast<const CSimpleType*>(pNodeBase);
  134. if (pNodeSimpleType != NULL && pNodeSimpleType->checkConstraint(pValue) == false)
  135. return false;
  136. }
  137. else if (pNodeBase->getNodeType() == XSD_SIMPLE_CONTENT)
  138. {
  139. const CSimpleContent *pNodeSimpleContent = dynamic_cast<const CSimpleContent*>(pNodeBase);
  140. if (pNodeSimpleContent != NULL && pNodeSimpleContent->checkConstraint(pValue) == false)
  141. return false;
  142. }
  143. assert(!"Unknown base node in restriction");
  144. return false;
  145. }
  146. return true;
  147. }