/*############################################################################## HPCC SYSTEMS software Copyright (C) 2015 HPCC Systems®. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ############################################################################## */ #include "jptree.hpp" #include "SchemaRestriction.hpp" #include "SchemaEnumeration.hpp" #include "SchemaAttributes.hpp" #include "XMLTags.h" #include "ConfigSchemaHelper.hpp" #include "DocumentationMarkup.hpp" #include "SchemaMapManager.hpp" #include "SchemaFractionDigits.hpp" #include "SchemaLength.hpp" #include "SchemaTotalDigits.hpp" #include "SchemaWhiteSpace.hpp" #include "SchemaSimpleContent.hpp" using namespace CONFIGURATOR; #define StringBuffer ::StringBuffer #define IPropertyTree ::IPropertyTree #define QUICK_LOAD_XSD_RESTRICTIONS(X, Y) \ strXPathExt.set(xpath); \ strXPathExt.append("/").append(Y); \ C##X *p##X = C##X::load(pRestriction, pSchemaRoot, strXPathExt.str()); \ if (p##X != NULL) pRestriction->set##X(p##X); CRestriction::~CRestriction() { CConfigSchemaHelper::getInstance()->getSchemaMapManager()->removeMapOfXPathToRestriction(this->getEnvXPath()); } void CRestriction::dump(::std::ostream& cout, unsigned int offset) const { offset+= STANDARD_OFFSET_1; quickOutHeader(cout, XSD_RESTRICTION_STR, offset); QUICK_OUT_2(Base); QUICK_OUT_2(ID); QUICK_OUT(cout, XSDXPath, offset); QUICK_OUT(cout, EnvXPath, offset); QUICK_OUT(cout, EnvValueFromXML, offset); QUICK_OUT_3(EnumerationArray) QUICK_OUT_3(FractionDigits) QUICK_OUT_3(Length) QUICK_OUT_3(MaxExclusive) QUICK_OUT_3(MaxInclusive) QUICK_OUT_3(MinExclusive) QUICK_OUT_3(MinInclusive) QUICK_OUT_3(MaxLength) QUICK_OUT_3(MinLength) QUICK_OUT_3(Pattern) QUICK_OUT_3(TotalDigits) QUICK_OUT_3(WhiteSpace) quickOutFooter(cout, XSD_RESTRICTION_STR, offset); } void CRestriction::getDocumentation(StringBuffer &strDoc) const { if (m_pEnumerationArray != NULL) { strDoc.appendf("<%s>",DM_PARA); DEBUG_MARK_STRDOC m_pEnumerationArray->getDocumentation(strDoc); strDoc.appendf("",DM_PARA); DEBUG_MARK_STRDOC } } void CRestriction::populateEnvXPath(StringBuffer strXPath, unsigned int index) { this->setEnvXPath(strXPath); CConfigSchemaHelper::getInstance()->getSchemaMapManager()->addMapOfXPathToRestriction(this->getEnvXPath(), this); if (this->m_pEnumerationArray != NULL) this->m_pEnumerationArray->populateEnvXPath(strXPath); } void CRestriction::loadXMLFromEnvXml(const IPropertyTree *pEnvTree) { if (m_pEnumerationArray != NULL) m_pEnumerationArray->loadXMLFromEnvXml(pEnvTree); } CRestriction* CRestriction::load(CXSDNodeBase* pParentNode, const IPropertyTree *pSchemaRoot, const char* xpath) { if (pParentNode == NULL || pSchemaRoot == NULL) return NULL; IPropertyTree *pTree = pSchemaRoot->queryPropTree(xpath); const char* pID = NULL; const char* pBase = NULL; pID = pTree->queryProp(XML_ATTR_ID); pBase = pTree->queryProp(XML_ATTR_BASE); CRestriction* pRestriction = new CRestriction(pParentNode, pID, pBase); pRestriction->setXSDXPath(xpath); StringBuffer strXPathExt; QUICK_LOAD_XSD_RESTRICTIONS(EnumerationArray, XSD_TAG_ENUMERATION) QUICK_LOAD_XSD_RESTRICTIONS(FractionDigits, XSD_TAG_FRACTION_DIGITS) QUICK_LOAD_XSD_RESTRICTIONS(Length, XSD_TAG_LENGTH) QUICK_LOAD_XSD_RESTRICTIONS(MaxExclusive, XSD_TAG_MAX_EXCLUSIVE) QUICK_LOAD_XSD_RESTRICTIONS(MaxInclusive, XSD_TAG_MAX_INCLUSIVE) QUICK_LOAD_XSD_RESTRICTIONS(MinExclusive, XSD_TAG_MIN_EXCLUSIVE) QUICK_LOAD_XSD_RESTRICTIONS(MinInclusive, XSD_TAG_MIN_INCLUSIVE) QUICK_LOAD_XSD_RESTRICTIONS(MaxLength, XSD_TAG_MAX_LENGTH) QUICK_LOAD_XSD_RESTRICTIONS(MinLength, XSD_TAG_MIN_LENGTH) QUICK_LOAD_XSD_RESTRICTIONS(Pattern, XSD_TAG_PATTERN) QUICK_LOAD_XSD_RESTRICTIONS(TotalDigits, XSD_TAG_TOTAL_DIGITS) QUICK_LOAD_XSD_RESTRICTIONS(WhiteSpace, XSD_TAG_WHITE_SPACE) if (pBase != NULL && *pBase != 0 && pRestriction != NULL) CConfigSchemaHelper::getInstance()->addNodeForBaseProcessing(pRestriction); return pRestriction; } const char* CRestriction::getXML(const char* /*pComponent*/) { if (m_strXML.length () == 0) { m_strXML.append("<").append(getBase()).append("\n"); m_strXML.append("<").append(getID()).append("\n"); m_strXML.append("/>\n"); } return m_strXML.str(); } bool CRestriction::checkConstraint(const char *pValue) const { const CXSDNodeBase *pNodeBase = this->getBaseNode(); assert(pNodeBase != NULL); if (pNodeBase != NULL) { const CXSDBuiltInDataType *pNodeBuiltInType = dynamic_cast(pNodeBase); if (pNodeBuiltInType != NULL && pNodeBuiltInType->checkConstraint(pValue) == false) return false; if (pNodeBase->getNodeType() == XSD_SIMPLE_TYPE) { const CSimpleType *pNodeSimpleType = dynamic_cast(pNodeBase); if (pNodeSimpleType != NULL && pNodeSimpleType->checkConstraint(pValue) == false) return false; } else if (pNodeBase->getNodeType() == XSD_SIMPLE_CONTENT) { const CSimpleContent *pNodeSimpleContent = dynamic_cast(pNodeBase); if (pNodeSimpleContent != NULL && pNodeSimpleContent->checkConstraint(pValue) == false) return false; } assert(!"Unknown base node in restriction"); return false; } return true; }