sgxmlscanner.cpp

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

CPP
1,881
字号
        {            currType = tempElement->getComplexTypeInfo();        }        if ((modelType == SchemaElementDecl::Mixed_Simple)          ||  (modelType == SchemaElementDecl::Mixed_Complex)          ||  (modelType == SchemaElementDecl::Children))        {            cm = currType->getContentModel();            cv = cm->getContentLeafNameTypeVector();            currentScope = fElemStack.getCurrentScope();        }        else if (modelType == SchemaElementDecl::Any) {            laxThisOne = true;        }    }    //  Now, since we might have to update the namespace map for this element,    //  but we don't have the element decl yet, we just tell the element stack    //  to expand up to get ready.    unsigned int elemDepth = fElemStack.addLevel();    fElemStack.setValidationFlag(fValidate);    //  Check if there is any external schema location specified, and if we are at root,    //  go through them first before scanning those specified in the instance document    if (isRoot        && (fExternalSchemaLocation || fExternalNoNamespaceSchemaLocation)) {        if (fExternalSchemaLocation)            parseSchemaLocation(fExternalSchemaLocation);        if (fExternalNoNamespaceSchemaLocation)            resolveSchemaGrammar(fExternalNoNamespaceSchemaLocation, XMLUni::fgZeroLenString);    }    //  Make an initial pass through the list and find any xmlns attributes or    //  schema attributes.    if (attCount)        scanRawAttrListforNameSpaces(attCount);    //  Resolve the qualified name to a URI and name so that we can look up    //  the element decl for this element. We have now update the prefix to    //  namespace map so we should get the correct element now.    int prefixColonPos = -1;    unsigned int uriId = resolveQName    (        qnameRawBuf        , fPrefixBuf        , ElemStack::Mode_Element        , prefixColonPos    );    //if schema, check if we should lax or skip the validation of this element    bool parentValidation = fValidate;    if (cv) {        QName element(fPrefixBuf.getRawBuffer(), &qnameRawBuf[prefixColonPos + 1], uriId, fMemoryManager);        // elementDepth will be > 0, as cv is only constructed if element is not        // root.        laxThisOne = laxElementValidation(&element, cv, cm, elemDepth - 1);    }    //  Look up the element now in the grammar. This will get us back a    //  generic element decl object. We tell him to fault one in if he does    //  not find it.    XMLElementDecl* elemDecl = 0;    bool wasAdded = false;    bool errorBeforeElementFound = false;    bool laxBeforeElementFound = false;    const XMLCh* nameRawBuf = &qnameRawBuf[prefixColonPos + 1];    const XMLCh* original_uriStr = fGrammar->getTargetNamespace();    if (uriId != fEmptyNamespaceId) {        // Check in current grammar before switching if necessary        elemDecl = fGrammar->getElemDecl        (          uriId          , nameRawBuf          , qnameRawBuf          , currentScope        );        if(!elemDecl)         {            // look in the list of undeclared elements, as would have been done            // before we made grammars stateless:            elemDecl = fElemNonDeclPool->getByKey(nameRawBuf, uriId, currentScope);        }        // this is initialized correctly only if there is        // no element decl.  The other uses in this scope will only        // be encountered if there continues to be no element decl--which        // implies that this will have been initialized correctly.        unsigned orgGrammarUri = uriId;        if (!elemDecl && ( orgGrammarUri = fURIStringPool->getId(original_uriStr)) != uriId) {            // not found, switch to the specified grammar            const XMLCh* uriStr = getURIText(uriId);            bool errorCondition = !switchGrammar(uriStr) && fValidate;            if (errorCondition && !laxThisOne)            {                fValidator->emitError                (                    XMLValid::GrammarNotFound                    ,uriStr                );                errorBeforeElementFound = true;            }             else if(errorCondition)                laxBeforeElementFound = true;            elemDecl = fGrammar->getElemDecl            (              uriId              , nameRawBuf              , qnameRawBuf              , currentScope            );        }        if (!elemDecl && currentScope != Grammar::TOP_LEVEL_SCOPE) {            // if not found, then it may be a reference, try TOP_LEVEL_SCOPE            elemDecl = fGrammar->getElemDecl                       (                           uriId                           , nameRawBuf                           , qnameRawBuf                           , Grammar::TOP_LEVEL_SCOPE                       );            if(!elemDecl)             {                // look in the list of undeclared elements, as would have been done                // before we made grammars stateless:                elemDecl = fElemNonDeclPool->getByKey(nameRawBuf, uriId, Grammar::TOP_LEVEL_SCOPE);            }            if(!elemDecl) {                // still not found in specified uri                // try emptyNamesapce see if element should be un-qualified.                elemDecl = fGrammar->getElemDecl                           (                               fEmptyNamespaceId                               , nameRawBuf                               , qnameRawBuf                               , currentScope                           );                 bool errorCondition = elemDecl && elemDecl->getCreateReason() != XMLElementDecl::JustFaultIn;                if (errorCondition && fValidate) {                    fValidator->emitError                    (                        XMLValid::ElementNotUnQualified                        , elemDecl->getFullName()                    );                    errorBeforeElementFound = true;                }                else if(errorCondition)                     laxBeforeElementFound = true;            }        }        if (!elemDecl) {            // still not found, fault this in and issue error later            // switch back to original grammar first (if necessary)            if(orgGrammarUri != uriId)            {                switchGrammar(original_uriStr);            }            elemDecl = new (fMemoryManager) SchemaElementDecl            (                fPrefixBuf.getRawBuffer()                , nameRawBuf                , uriId                , SchemaElementDecl::Any                , Grammar::TOP_LEVEL_SCOPE                , fMemoryManager            );            elemDecl->setId(fElemNonDeclPool->put((void*)elemDecl->getBaseName(), uriId, currentScope, (SchemaElementDecl*)elemDecl));            wasAdded = true;        }    }    else if (!elemDecl)    {        //the element has no prefix,        //thus it is either a non-qualified element defined in current targetNS        //or an element that is defined in the globalNS        //try unqualifed first        elemDecl = fGrammar->getElemDecl                   (                      uriId                    , nameRawBuf                    , qnameRawBuf                    , currentScope                    );        if(!elemDecl)        {            // look in the list of undeclared elements, as would have been done            // before we made grammars stateless:            elemDecl = fElemNonDeclPool->getByKey(nameRawBuf, uriId, currentScope);        }        // this is initialized correctly only if there is        // no element decl.  The other uses in this scope will only        // be encountered if there continues to be no element decl--which        // implies that this will have been initialized correctly.        unsigned orgGrammarUri = fEmptyNamespaceId;        if (!elemDecl && (orgGrammarUri = fURIStringPool->getId(original_uriStr)) != fEmptyNamespaceId) {            //not found, switch grammar and try globalNS            bool errorCondition = !switchGrammar(XMLUni::fgZeroLenString) && fValidate;            if (errorCondition && !laxThisOne)            {                fValidator->emitError                (                    XMLValid::GrammarNotFound                  , XMLUni::fgZeroLenString                );                errorBeforeElementFound = true;                            }            else if(errorCondition)                laxBeforeElementFound = true;             elemDecl = fGrammar->getElemDecl            (              uriId              , nameRawBuf              , qnameRawBuf              , currentScope            );        }        if (!elemDecl && currentScope != Grammar::TOP_LEVEL_SCOPE) {            // if not found, then it may be a reference, try TOP_LEVEL_SCOPE            elemDecl = fGrammar->getElemDecl                       (                           uriId                           , nameRawBuf                           , qnameRawBuf                           , Grammar::TOP_LEVEL_SCOPE                       );            if(!elemDecl)            {                // look in the list of undeclared elements, as would have been done                // before we made grammars stateless:                elemDecl = fElemNonDeclPool->getByKey(nameRawBuf, uriId, Grammar::TOP_LEVEL_SCOPE);            }            if (!elemDecl && orgGrammarUri != fEmptyNamespaceId) {                // still Not found in specified uri                // go to original Grammar again to see if element needs to be fully qualified.                bool errorCondition = !switchGrammar(original_uriStr) && fValidate;                if (errorCondition && !laxThisOne)                {                    fValidator->emitError                    (                        XMLValid::GrammarNotFound                        ,original_uriStr                    );                    errorBeforeElementFound = true;                }                else if(errorCondition)                    laxBeforeElementFound = true;                 elemDecl = fGrammar->getElemDecl                           (                               orgGrammarUri                               , nameRawBuf                               , qnameRawBuf                               , currentScope                           );                if (elemDecl && elemDecl->getCreateReason() != XMLElementDecl::JustFaultIn && fValidate) {                    fValidator->emitError                    (                        XMLValid::ElementNotQualified                        , elemDecl->getFullName()                    );                    errorBeforeElementFound = true;                }            }        }        if (!elemDecl) {            // still not found, fault this in and issue error later            // switch back to original grammar first (if necessary)            if(orgGrammarUri != fEmptyNamespaceId)            {                switchGrammar(original_uriStr);            }            elemDecl = new (fMemoryManager) SchemaElementDecl            (                fPrefixBuf.getRawBuffer()                , nameRawBuf                , uriId                , SchemaElementDecl::Any                , Grammar::TOP_LEVEL_SCOPE                , fMemoryManager            );            elemDecl->setId(fElemNonDeclPool->put((void*)elemDecl->getBaseName(), uriId, currentScope, (SchemaElementDecl*)elemDecl));            wasAdded = true;        }    }    // this info needed for DOMTypeInfo    fPSVIElemContext.fErrorOccurred = false;    //  We do something different here according to whether we found the    //  element or not.    if (wasAdded)    {        if (laxThisOne) {            fValidate = false;            fElemStack.setValidationFlag(fValidate);        }        // If validating then emit an error        if (fValidate)        {            // This is to tell the reuse Validator that this element was            // faulted-in, was not an element in the grammar pool originally            elemDecl->setCreateReason(XMLElementDecl::JustFaultIn);            fValidator->emitError            (                XMLValid::ElementNotDefined                , elemDecl->getFullName()            );            fPSVIElemContext.fErrorOccurred = true;        }    }    else    {        // If its not marked declared and validating, then emit an error        if (!elemDecl->isDeclared()) {            if(elemDecl->getCreateReason() == XMLElementDecl::NoReason) {                fPSVIElemContext.fErrorOccurred = true;            }            if (laxThisOne) {                fValidate = false;                fElemStack.setValidationFlag(fValidate);                            }                        if (fValidate)                {                fValidator->emitError                (                    XMLValid::ElementNotDefined                    , elemDecl->getFullName()                );            }        }    }    //  Now we can update the element stack to set the current element    //  decl. We expanded the stack above, but couldn't store the element    //  decl because we didn't know it yet.    fElemStack.setElement(elemDecl, fReaderMgr.getCurrentReaderNum());    fElemStack.setCurrentURI(uriId);    if (isRoot)    {        fRootGrammar = fGrammar;        fRootElemName = XMLString::replicate(qnameRawBuf, fMemoryManager);    }    if (fPSVIHandler)    {        fPSVIElemContext.fElemDepth++;        if (elemDecl->isDeclared())        {            fPSVIElemContext.fNoneValidationDepth = fPSVIElemContext.fElemDepth;        }        else        {            fPSVIElemContext.fFullValidationDepth = fPSVIElemContext.fElemDepth;            /******             * While we report an error for historical reasons, this should             * actually result in lax assessment - NG.            if (isRoot && fValidate)                fPSVIElemContext.fErrorOccurred = true;            ******/

⌨️ 快捷键说明

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