generalattributecheck.cpp

来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 978 行 · 第 1/3 页

CPP
978
字号
// -----------------------------------------------------------------------//  Notification that lazy data has been deleted// -----------------------------------------------------------------------voidGeneralAttributeCheck::reinitGeneralAttCheck() {    delete sGeneralAttCheckMutex;    sGeneralAttCheckMutex = 0;    sGeneralAttCheckMutexRegistered = false;    delete fAttMap;    delete fFacetsMap;	    fAttMap = fFacetsMap = 0;    fNonNegIntDV = fBooleanDV = fAnyURIDV = 0;}// ---------------------------------------------------------------------------//  GeneralAttributeCheck: Validation methods// ---------------------------------------------------------------------------voidGeneralAttributeCheck::checkAttributes(const DOMElement* const elem,                                       const unsigned short elemContext,                                       TraverseSchema* const schema,                                       const bool isTopLevel,                                       ValueVectorOf<DOMNode*>* const nonXSAttList){    if (nonXSAttList)        nonXSAttList->removeAllElements();    if (elem == 0 || !fAttMap || elemContext>=E_Count)        return;    const XMLCh* elemName = elem->getLocalName();    if (!XMLString::equals(SchemaSymbols::fgURI_SCHEMAFORSCHEMA, elem->getNamespaceURI())) {        schema->reportSchemaError        (            elem            , XMLUni::fgXMLErrDomain            , XMLErrs::ELTSchemaNS            , elemName        );    }    const XMLCh*     contextStr = (isTopLevel) ? fgGlobal : fgLocal;    DOMNamedNodeMap* eltAttrs = elem->getAttributes();    int              attrCount = eltAttrs->getLength();    XMLByte          attList[A_Count];    memset(attList, 0, sizeof(attList));    for (int i = 0; i < attrCount; i++) {        DOMNode*     attribute = eltAttrs->item(i);        const XMLCh* attName = attribute->getNodeName();        // skip namespace declarations        if (XMLString::equals(attName, XMLUni::fgXMLNSString)            || XMLString::startsWith(attName, XMLUni::fgXMLNSColonString))            continue;        // Bypass attributes that start with xml        // add this to the list of "non-schema" attributes        if ((*attName == chLatin_X || *attName == chLatin_x)           && (*(attName+1) == chLatin_M || *(attName+1) == chLatin_m)           && (*(attName+2) == chLatin_L || *(attName+2) == chLatin_l)) {           if (nonXSAttList)                nonXSAttList->addElement(attribute);            continue;        }        // for attributes with namespace prefix        const XMLCh* attrURI = attribute->getNamespaceURI();        if (attrURI != 0 && *attrURI) {            // attributes with schema namespace are not allowed            // and not allowed on "documentation" and "appInfo"            if (XMLString::equals(attrURI, SchemaSymbols::fgURI_SCHEMAFORSCHEMA) ||                XMLString::equals(elemName, SchemaSymbols::fgELT_APPINFO) ||                XMLString::equals(elemName, SchemaSymbols::fgELT_DOCUMENTATION)) {                schema->reportSchemaError(elem, XMLUni::fgXMLErrDomain,                    XMLErrs::AttributeDisallowed, attName, contextStr, elemName);            }            else if (nonXSAttList)            {                nonXSAttList->addElement(attribute);            }            continue;        }        int attNameId = A_Invalid;        attName = attribute->getLocalName();        bool bContinue=false;   // workaround for Borland bug with 'continue' in 'catch'        try {            attNameId= fAttMap->get(attName, fMemoryManager);        }        catch(const OutOfMemoryException&)        {            throw;        }        catch(...) {            schema->reportSchemaError(elem, XMLUni::fgXMLErrDomain,                XMLErrs::AttributeDisallowed, attName, contextStr, elemName);            bContinue=true;        }        if(bContinue)            continue;        if (fgElemAttTable[elemContext][attNameId] & Att_Mask) {            attList[attNameId] = 1;            validate            (                elem                , attName                , attribute->getNodeValue()                , fgElemAttTable[elemContext][attNameId] & DV_Mask                , schema            );        }        else {            schema->reportSchemaError(elem, XMLUni::fgXMLErrDomain,                XMLErrs::AttributeDisallowed, attName, contextStr, elemName);        }    }    // ------------------------------------------------------------------    // Check for required attributes    // ------------------------------------------------------------------    for (unsigned int j=0; j < A_Count; j++) {        if ((fgElemAttTable[elemContext][j] & Att_Required) && attList[j] == 0) {            schema->reportSchemaError(elem, XMLUni::fgXMLErrDomain, XMLErrs::AttributeRequired,                                      fAttNames[j], contextStr, elemName);        }    }}void GeneralAttributeCheck::validate(const DOMElement* const elem,                                     const XMLCh* const attName,                                     const XMLCh* const attValue,                                     const short dvIndex,                                     TraverseSchema* const schema){    bool isInvalid = false;    DatatypeValidator* dv = 0;    fValidationContext = schema->fSchemaInfo->getValidationContext();    switch (dvIndex) {    case DV_Form:        if (!XMLString::equals(attValue, SchemaSymbols::fgATTVAL_QUALIFIED)            && !XMLString::equals(attValue, SchemaSymbols::fgATTVAL_UNQUALIFIED)) {            isInvalid = true;        }        break;    case DV_MaxOccurs:            // maxOccurs = (nonNegativeInteger | unbounded)        if (!XMLString::equals(attValue, fgUnbounded)) {            dv = fNonNegIntDV;        }        break;    case DV_MaxOccurs1:        if (!XMLString::equals(attValue, fgValueOne)) {            isInvalid = true;        }        break;    case DV_MinOccurs1:        if (!XMLString::equals(attValue, fgValueZero)            && !XMLString::equals(attValue, fgValueOne)) {            isInvalid = true;        }        break;    case DV_ProcessContents:        if (!XMLString::equals(attValue, SchemaSymbols::fgATTVAL_SKIP)            && !XMLString::equals(attValue, SchemaSymbols::fgATTVAL_LAX)            && !XMLString::equals(attValue, SchemaSymbols::fgATTVAL_STRICT)) {            isInvalid = true;        }        break;    case DV_Use:        if (!XMLString::equals(attValue, SchemaSymbols::fgATTVAL_OPTIONAL)            && !XMLString::equals(attValue, SchemaSymbols::fgATTVAL_PROHIBITED)            && !XMLString::equals(attValue, SchemaSymbols::fgATTVAL_REQUIRED)) {            isInvalid = true;        }        break;    case DV_WhiteSpace:        if (!XMLString::equals(attValue, SchemaSymbols::fgWS_PRESERVE)            && !XMLString::equals(attValue, SchemaSymbols::fgWS_REPLACE)            && !XMLString::equals(attValue, SchemaSymbols::fgWS_COLLAPSE)) {            isInvalid = true;        }        break;    case DV_Boolean:        dv = fBooleanDV;        break;    case DV_NonNegInt:        dv = fNonNegIntDV;        break;    case DV_AnyURI:        dv = fAnyURIDV;        break;    case DV_ID:        if (fValidationContext)        {            dv = &fIDValidator;        }        break;    }    if (dv) {        try {            dv->validate(attValue, fValidationContext, fMemoryManager);        }        catch(const XMLException& excep) {            schema->reportSchemaError(elem, XMLUni::fgXMLErrDomain, XMLErrs::DisplayErrorMessage, excep.getMessage());        }        catch(const OutOfMemoryException&)        {            throw;        }        catch(...) {            isInvalid = true;        }    }    if (isInvalid) {        schema->reportSchemaError(elem, XMLUni::fgXMLErrDomain, XMLErrs::InvalidAttValue,                                  attValue, attName);    }}// ---------------------------------------------------------------------------//  Conditional methods for building the table// ---------------------------------------------------------------------------////  This code will set up the character flags table. Its defined out since//  this table is now written out and hard coded (at the bottom of this//  file) into the code itself. This code is retained in case there is//  any need to recreate it later.//#if defined(NEED_TO_GEN_ELEM_ATT_MAP_TABLE)#include <stdio.h>void GeneralAttributeCheck::initCharFlagTable(){    unsigned short attList[E_Count][A_Count];    for (unsigned int i=0; i < E_Count; i++) {        for (unsigned int j=0; j < A_Count; j++) {            attList[i][j] = 0;        }    }    //    //  Write it out to a temp file to be read back into this source later.    //    FILE* outFl = fopen("ea_table.out", "wt+");    fprintf(outFl, "unsigned short GeneralAttributeCheck::fgElemAttTable[E_Count][A_Count] =\n{\n    {");    //"all"    attList[E_All][A_ID] = Att_Optional | DV_ID;    attList[E_All][A_MaxOccurs] = Att_Optional | DV_MaxOccurs1;    attList[E_All][A_MinOccurs] = Att_Optional | DV_MinOccurs1;    // "annotation"    attList[E_Annotation][A_ID] = Att_Optional | DV_ID;    // "any"    attList[E_Any][A_ID] = Att_Optional | DV_ID;    attList[E_Any][A_MaxOccurs] = Att_Optional | DV_MaxOccurs;    attList[E_Any][A_MinOccurs] = Att_Optional | DV_NonNegInt;    attList[E_Any][A_Namespace] = Att_Optional;    attList[E_Any][A_ProcessContents] = Att_Optional | DV_ProcessContents;    // "anyAttribute"    attList[E_AnyAttribute][A_ID] = Att_Optional | DV_ID;    attList[E_AnyAttribute][A_Namespace] = Att_Optional;    attList[E_AnyAttribute][A_ProcessContents] = Att_Optional | DV_ProcessContents;    // "appinfo"    attList[E_Appinfo][A_Source]= Att_Optional | DV_AnyURI;    // attribute - global"    attList[E_AttributeGlobal][A_Default] = Att_Optional;    attList[E_AttributeGlobal][A_Fixed] = Att_Optional;    attList[E_AttributeGlobal][A_ID] = Att_Optional | DV_ID;    attList[E_AttributeGlobal][A_Name] = Att_Required;    attList[E_AttributeGlobal][A_Type] = Att_Optional;    // "attribute - local"    attList[E_AttributeLocal][A_Default] = Att_Optional;    attList[E_AttributeLocal][A_Fixed] = Att_Optional;    attList[E_AttributeLocal][A_Form]= Att_Optional | DV_Form;    attList[E_AttributeLocal][A_ID] = Att_Optional | DV_ID;    attList[E_AttributeLocal][A_Name] = Att_Required;    attList[E_AttributeLocal][A_Type] = Att_Optional;    attList[E_AttributeLocal][A_Use] = Att_Optional | DV_Use;    // "attribute - ref"    attList[E_AttributeRef][A_Default] = Att_Optional;    attList[E_AttributeRef][A_Fixed] = Att_Optional;    attList[E_AttributeRef][A_ID] = Att_Optional | DV_ID;    attList[E_AttributeRef][A_Ref]= Att_Required;    attList[E_AttributeRef][A_Use] = Att_Optional | DV_Use;    // "attributeGroup - global"    attList[E_AttributeGroupGlobal][A_ID] = Att_Optional | DV_ID;    attList[E_AttributeGroupGlobal][A_Name] = Att_Required;    // "attributeGroup - ref"    attList[E_AttributeGroupRef][A_ID] = Att_Optional | DV_ID;    attList[E_AttributeGroupRef][A_Ref]= Att_Required;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?