xmlscanner.cpp

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

CPP
1,855
字号
    if (XMLErrs::isFatal(toEmit) && fExitOnFirstFatal && !fInException)        return true;    return false;}void XMLScanner::emitError(const XMLErrs::Codes toEmit){    // Bump the error count if it is not a warning    if (XMLErrs::errorType(toEmit) != XMLErrorReporter::ErrType_Warning)        incrementErrorCount();    if (fErrorReporter)    {        // Load the message into a local for display        const unsigned int msgSize = 1023;        XMLCh errText[msgSize + 1];        if (!gScannerMsgLoader().loadMsg(toEmit, errText, msgSize))        {                // <TBD> Probably should load a default msg here        }        //  Create a LastExtEntityInfo structure and get the reader manager        //  to fill it in for us. This will give us the information about        //  the last reader on the stack that was an external entity of some        //  sort (i.e. it will ignore internal entities.        ReaderMgr::LastExtEntityInfo lastInfo;        fReaderMgr.getLastExtEntityInfo(lastInfo);        fErrorReporter->error        (            toEmit            , XMLUni::fgXMLErrDomain            , XMLErrs::errorType(toEmit)            , errText            , lastInfo.systemId            , lastInfo.publicId            , lastInfo.lineNumber            , lastInfo.colNumber        );    }    // Bail out if its fatal an we are to give up on the first fatal error    if (emitErrorWillThrowException(toEmit))        throw toEmit;}void XMLScanner::emitError( const   XMLErrs::Codes    toEmit                            , const XMLCh* const        text1                            , const XMLCh* const        text2                            , const XMLCh* const        text3                            , const XMLCh* const        text4){    // Bump the error count if it is not a warning    if (XMLErrs::errorType(toEmit) != XMLErrorReporter::ErrType_Warning)        incrementErrorCount();    if (fErrorReporter)    {        //  Load the message into alocal and replace any tokens found in        //  the text.        const unsigned int maxChars = 2047;        XMLCh errText[maxChars + 1];        if (!gScannerMsgLoader().loadMsg(toEmit, errText, maxChars, text1, text2, text3, text4, fMemoryManager))        {                // <TBD> Should probably load a default message here        }        //  Create a LastExtEntityInfo structure and get the reader manager        //  to fill it in for us. This will give us the information about        //  the last reader on the stack that was an external entity of some        //  sort (i.e. it will ignore internal entities.        ReaderMgr::LastExtEntityInfo lastInfo;        fReaderMgr.getLastExtEntityInfo(lastInfo);        fErrorReporter->error        (            toEmit            , XMLUni::fgXMLErrDomain            , XMLErrs::errorType(toEmit)            , errText            , lastInfo.systemId            , lastInfo.publicId            , lastInfo.lineNumber            , lastInfo.colNumber        );    }    // Bail out if its fatal an we are to give up on the first fatal error    if (emitErrorWillThrowException(toEmit))        throw toEmit;}void XMLScanner::emitError( const   XMLErrs::Codes    toEmit                            , const char* const         text1                            , const char* const         text2                            , const char* const         text3                            , const char* const         text4){    // Bump the error count if it is not a warning    if (XMLErrs::errorType(toEmit) != XMLErrorReporter::ErrType_Warning)        incrementErrorCount();    if (fErrorReporter)    {        //  Load the message into alocal and replace any tokens found in        //  the text.        const unsigned int maxChars = 2047;        XMLCh errText[maxChars + 1];        if (!gScannerMsgLoader().loadMsg(toEmit, errText, maxChars, text1, text2, text3, text4, fMemoryManager))        {                // <TBD> Should probably load a default message here        }        //  Create a LastExtEntityInfo structure and get the reader manager        //  to fill it in for us. This will give us the information about        //  the last reader on the stack that was an external entity of some        //  sort (i.e. it will ignore internal entities.        ReaderMgr::LastExtEntityInfo lastInfo;        fReaderMgr.getLastExtEntityInfo(lastInfo);        fErrorReporter->error        (            toEmit            , XMLUni::fgXMLErrDomain            , XMLErrs::errorType(toEmit)            , errText            , lastInfo.systemId            , lastInfo.publicId            , lastInfo.lineNumber            , lastInfo.colNumber        );    }    // Bail out if its fatal an we are to give up on the first fatal error    if (emitErrorWillThrowException(toEmit))        throw toEmit;}// ---------------------------------------------------------------------------//  XMLScanner: Getter methods// ---------------------------------------------------------------------------//  This method allows the caller to query the current location of the scanner.//  It will return the sys/public ids of the current entity, and the line/col//  position within it.////  NOTE: This API returns the location with the last external file. So if its//  currently scanning an entity, the position returned will be the end of//  the entity reference in the file that had the reference.///*boolXMLScanner::getLastExtLocation(         XMLCh* const    sysIdToFill                                , const unsigned int    maxSysIdChars                                ,       XMLCh* const    pubIdToFill                                , const unsigned int    maxPubIdChars                                ,       XMLSSize_t&     lineToFill                                ,       XMLSSize_t&     colToFill) const{    // Create a local info object and get it filled in by the reader manager    ReaderMgr::LastExtEntityInfo lastInfo;    fReaderMgr.getLastExtEntityInfo(lastInfo);    // Fill in the line and column number    lineToFill = lastInfo.lineNumber;    colToFill = lastInfo.colNumber;    // And copy over as much of the ids as will fit    sysIdToFill[0] = 0;    if (lastInfo.systemId)    {        if (XMLString::stringLen(lastInfo.systemId) > maxSysIdChars)            return false;        XMLString::copyString(sysIdToFill, lastInfo.systemId);    }    pubIdToFill[0] = 0;    if (lastInfo.publicId)    {        if (XMLString::stringLen(lastInfo.publicId) > maxPubIdChars)            return false;        XMLString::copyString(pubIdToFill, lastInfo.publicId);    }    return true;}*/// ---------------------------------------------------------------------------//  XMLScanner: Private scanning methods// ---------------------------------------------------------------------------//  This method is called after the end of the root element, to handle//  any miscellaneous stuff hanging around.void XMLScanner::scanMiscellaneous(){    // Get a buffer for this work    XMLBufBid bbCData(&fBufMgr);    while (true)    {        try        {            const XMLCh nextCh = fReaderMgr.peekNextChar();            // Watch for end of file and break out            if (!nextCh)                break;            if (nextCh == chOpenAngle)            {                if (checkXMLDecl(true))                {                    // Can't have an XML decl here                    emitError(XMLErrs::NotValidAfterContent);                    fReaderMgr.skipPastChar(chCloseAngle);                }                else if (fReaderMgr.skippedString(XMLUni::fgPIString))                {                    scanPI();                }                 else if (fReaderMgr.skippedString(XMLUni::fgCommentString))                {                    scanComment();                }                else                {                    // This can't be possible, so just give up                    emitError(XMLErrs::ExpectedCommentOrPI);                    fReaderMgr.skipPastChar(chCloseAngle);                }            }            else if (fReaderMgr.getCurrentReader()->isWhitespace(nextCh))            {                //  If we have a doc handler, then gather up the spaces and                //  call back. Otherwise, just skip over whitespace.                if (fDocHandler)                {                    fReaderMgr.getSpaces(bbCData.getBuffer());                    fDocHandler->ignorableWhitespace                    (                        bbCData.getRawBuffer()                        , bbCData.getLen()                        , false                    );                }                else                {                    fReaderMgr.skipPastSpaces();                }            }            else            {                emitError(XMLErrs::ExpectedCommentOrPI);                fReaderMgr.skipPastChar(chCloseAngle);            }        }        catch(const EndOfEntityException&)        {            //  Some entity leaked out of the content part of the document. Issue            //  a warning and keep going.            emitError(XMLErrs::EntityPropogated);        }    }}//  Scans a PI and calls the appropriate callbacks. At entry we have just//  scanned the <? part, and need to now start on the PI target name.void XMLScanner::scanPI(){    const XMLCh* namePtr = 0;    const XMLCh* targetPtr = 0;    //  If there are any spaces here, then warn about it. If we aren't in    //  'first error' mode, then we'll come back and can easily pick up    //  again by just skipping them.    if (fReaderMgr.lookingAtSpace())    {        emitError(XMLErrs::PINameExpected);        fReaderMgr.skipPastSpaces();    }    // Get a buffer for the PI name and scan it in    XMLBufBid bbName(&fBufMgr);    if (!fReaderMgr.getName(bbName.getBuffer()))    {        emitError(XMLErrs::PINameExpected);        fReaderMgr.skipPastChar(chCloseAngle);        return;    }    // Point the name pointer at the raw data    namePtr = bbName.getRawBuffer();    // See if it is some form of 'xml' and emit a warning    if (!XMLString::compareIString(namePtr, XMLUni::fgXMLString))        emitError(XMLErrs::NoPIStartsWithXML);    // If namespaces are enabled, then no colons allowed    if (fDoNamespaces)    {        if (XMLString::indexOf(namePtr, chColon) != -1)            emitError(XMLErrs::ColonNotLegalWithNS);    }    //  If we don't hit a space next, then the PI has no target. If we do    //  then get out the target. Get a buffer for it as well    XMLBufBid bbTarget(&fBufMgr);    if (fReaderMgr.skippedSpace())    {        // Skip any leading spaces        fReaderMgr.skipPastSpaces();        bool gotLeadingSurrogate = false;        // It does have a target, so lets move on to deal with that.        while (1)        {            const XMLCh nextCh = fReaderMgr.getNextChar();            // Watch for an end of file, which is always bad here            if (!nextCh)            {                emitError(XMLErrs::UnterminatedPI);                ThrowXMLwithMemMgr(UnexpectedEOFException, XMLExcepts::Gen_UnexpectedEOF, fMemoryManager);            }            // Watch for potential terminating character            if (nextCh == chQuestion)            {                // It must be followed by '>' to be a termination of the target                if (fReaderMgr.skippedChar(chCloseAngle))                    break;            }            // Check for correct surrogate pairs            if ((nextCh >= 0xD800) && (nextCh <= 0xDBFF))            {                if (gotLeadingSurrogate)                    emitError(XMLErrs::Expected2ndSurrogateChar);                else                    gotLeadingSurrogate = true;            }             else            {                if (gotLeadingSurrogate)                {                    if ((nextCh < 0xDC00) || (nextCh > 0xDFFF))                        emitError(XMLErrs::Expected2ndSurrogateChar);                }                // Its got to at least be a valid XML character                else if (!fReaderMgr.getCurrentReader()->isXMLChar(nextCh)) {                    XMLCh tmpBuf[9];                    XMLString::binToText                    (                        nextCh                        , tmpBuf                        , 8                        , 16                        , fMemoryManager                    );                    emitError(XMLErrs::InvalidCharacter, tmpBuf);                }                gotLeadingSurrogate = false;            }

⌨️ 快捷键说明

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