xmlreader.cpp

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

CPP
1,501
字号
    {        if (!refreshCharBuffer())            return false;    }    const XMLCh curCh = fCharBuf[fCharIndex];    if ((curCh == chDoubleQuote) || (curCh == chSingleQuote))    {        chGotten = curCh;        fCharIndex++;        fCurCol++;        return true;    }    return false;}bool XMLReader::skipSpaces(bool& skippedSomething, bool inDecl){    // Remember the current line and column    XMLSSize_t    orgLine = fCurLine;    XMLSSize_t    orgCol  = fCurCol;    //  We enter a loop where we skip over spaces until we hit the end of    //  this reader or a non-space value. The return indicates whether we    //  hit the non-space (true) or the end (false).    while (true)    {        // Loop through the current chars in the buffer        while (fCharIndex < fCharsAvail)        {            //  See if its a white space char. If so, then process it. Else            //  we've hit a non-space and need to return.            if (isWhitespace(fCharBuf[fCharIndex]))            {                // Get the current char out of the buffer and eat it                XMLCh curCh = fCharBuf[fCharIndex++];                handleEOL(curCh, inDecl);            }            else            {                skippedSomething = (orgLine != fCurLine) || (orgCol != fCurCol);                return true;            }        }        //  We've eaten up the current buffer, so lets try to reload it. If        //  we don't get anything new, then break out. If we do, then we go        //  back to the top to keep getting spaces.        if (!refreshCharBuffer())            break;    }    // We never hit any non-space and ate up the whole reader    skippedSomething = (orgLine != fCurLine) || (orgCol != fCurCol);    return false;}bool XMLReader::skippedChar(const XMLCh toSkip){    //    //  If the buffer is empty, then try to reload it. If we still get    //  nothing, then return false.    //    if (fCharIndex == fCharsAvail)    {        if (!refreshCharBuffer())            return false;    }    //    //  See if the current char is the one we want. If so, then we need    //  to eat it and return true.    //    if (fCharBuf[fCharIndex] == toSkip)    {        fCharIndex++;        fCurCol++;        return true;    }    return false;}bool XMLReader::skippedSpace(){    //    //  If the buffer is empty, then try to reload it. If we still get    //  nothing, then return false.    //    if (fCharIndex == fCharsAvail)    {        if (!refreshCharBuffer())            return false;    }    //    //  See if the current char is a whitespace. If so, then we need to eat    //  it and return true.    //    const XMLCh curCh = fCharBuf[fCharIndex];    if (isWhitespace(curCh))    {        // Eat the character        fCharIndex++;        handleEOL((XMLCh&)curCh, false);        return true;    }    return false;}bool XMLReader::skippedString(const XMLCh* const toSkip){    // Get the length of the string to skip    const unsigned int srcLen = XMLString::stringLen(toSkip);    //    //  See if the current reader has enough chars to test against this    //  string. If not, then ask it to reload its buffer. If that does not    //  get us enough, then it cannot match.    //    //  NOTE: This works because strings never have to cross a reader! And    //  a string to skip will never have a new line in it, so we will never    //  miss adjusting the current line.    //    unsigned int charsLeft = charsLeftInBuffer();    while (charsLeft < srcLen)    {         refreshCharBuffer();         unsigned int t = charsLeftInBuffer();         if (t == charsLeft)   // if the refreshCharBuf() did not add anything new             return false;     //   give up and return.         charsLeft = t;	}    //    //  Ok, now we now that the current reader has enough chars in its    //  buffer and that its index is back at zero. So we can do a quick and    //  dirty comparison straight to its buffer with no requirement to unget    //  if it fails.    //    if (XMLString::compareNString(&fCharBuf[fCharIndex], toSkip, srcLen))        return false;    // Add the source length to the current column to get it back right    fCurCol += srcLen;    //    //  And get the character buffer index back right by just adding the    //  source len to it.    //    fCharIndex += srcLen;    return true;}//// This is just to peek if the next coming buffer// matches the string toPeek.// Similar to skippedString, but just the fCharIndex and fCurCol are not updated//bool XMLReader::peekString(const XMLCh* const toPeek){    // Get the length of the string to skip    const unsigned int srcLen = XMLString::stringLen(toPeek);    //    //  See if the current reader has enough chars to test against this    //  string. If not, then ask it to reload its buffer. If that does not    //  get us enough, then it cannot match.    //    //  NOTE: This works because strings never have to cross a reader! And    //  a string to skip will never have a new line in it, so we will never    //  miss adjusting the current line.    //    unsigned int charsLeft = charsLeftInBuffer();    while (charsLeft < srcLen)    {         refreshCharBuffer();         unsigned int t = charsLeftInBuffer();         if (t == charsLeft)   // if the refreshCharBuf() did not add anything new             return false;     //   give up and return.         charsLeft = t;	}    //    //  Ok, now we now that the current reader has enough chars in its    //  buffer and that its index is back at zero. So we can do a quick and    //  dirty comparison straight to its buffer with no requirement to unget    //  if it fails.    //    if (XMLString::compareNString(&fCharBuf[fCharIndex], toPeek, srcLen))        return false;    return true;}// ---------------------------------------------------------------------------//  XMLReader: Setter methods (most are inlined)// ---------------------------------------------------------------------------bool XMLReader::setEncoding(const XMLCh* const newEncoding){    //    //  If the encoding was forced, then we ignore the new value and just    //  return with success. If it was forced, then we are to use that    //  encoding without question. Note that, if we are forced, we created    //  a transcoder up front so there is no need to do one here in that    //  case.    //    if (fForcedEncoding)        return true;    //    // upperCase the newEncoding first for better performance    //    XMLCh* inputEncoding = XMLString::replicate(newEncoding, fMemoryManager);    XMLString::upperCase(inputEncoding);    //    //  Try to map the string to one of our standard encodings. If its not    //  one of them, then it has to be one of the non-intrinsic encodings,    //  in which case we have to delete our intrinsic encoder and create a    //  new one.    //    XMLRecognizer::Encodings newBaseEncoding = XMLRecognizer::encodingForName    (        inputEncoding    );    //    //  If it does not come back as one of the auto-sensed encodings, then we    //  have to possibly replace it and at least check a few things.    //    if (newBaseEncoding == XMLRecognizer::OtherEncoding)    {        //        //  Check for non-endian specific UTF-16 or UCS-4. If so, and if we        //  are already in one of the endian versions of those encodings,        //  then just keep it and go on. Otherwise, its not valid.        //        if (!XMLString::compareString(inputEncoding, XMLUni::fgUTF16EncodingString)        ||  !XMLString::compareString(inputEncoding, XMLUni::fgUTF16EncodingString2)        ||  !XMLString::compareString(inputEncoding, XMLUni::fgUTF16EncodingString3)        ||  !XMLString::compareString(inputEncoding, XMLUni::fgUTF16EncodingString4)        ||  !XMLString::compareString(inputEncoding, XMLUni::fgUTF16EncodingString5))        {            fMemoryManager->deallocate(inputEncoding);            if ((fEncoding != XMLRecognizer::UTF_16L)            &&  (fEncoding != XMLRecognizer::UTF_16B))            {                return false;            }            // Override with the original endian specific encoding            newBaseEncoding = fEncoding;            if (fEncoding == XMLRecognizer::UTF_16L) {                fMemoryManager->deallocate(fEncodingStr);                fEncodingStr = XMLString::replicate(XMLUni::fgUTF16LEncodingString, fMemoryManager);            }            else {                fMemoryManager->deallocate(fEncodingStr);                fEncodingStr = XMLString::replicate(XMLUni::fgUTF16BEncodingString, fMemoryManager);            }        }        else if (!XMLString::compareString(inputEncoding, XMLUni::fgUCS4EncodingString)             ||  !XMLString::compareString(inputEncoding, XMLUni::fgUCS4EncodingString2)             ||  !XMLString::compareString(inputEncoding, XMLUni::fgUCS4EncodingString3))        {            fMemoryManager->deallocate(inputEncoding);            if ((fEncoding != XMLRecognizer::UCS_4L)            &&  (fEncoding != XMLRecognizer::UCS_4B))            {                return false;            }            // Override with the original endian specific encoding            newBaseEncoding = fEncoding;            if (fEncoding == XMLRecognizer::UCS_4L) {                fMemoryManager->deallocate(fEncodingStr);                fEncodingStr = XMLString::replicate(XMLUni::fgUCS4LEncodingString, fMemoryManager);            }            else {                fMemoryManager->deallocate(fEncodingStr);                fEncodingStr = XMLString::replicate(XMLUni::fgUCS4BEncodingString, fMemoryManager);            }        }         else        {            //            // None of those special cases, so just replicate the new name            // and use it directly to create the transcoder            //            fMemoryManager->deallocate(fEncodingStr);            fEncodingStr = inputEncoding;            XMLTransService::Codes failReason;            fTranscoder = XMLPlatformUtils::fgTransService->makeNewTranscoderFor            (                fEncodingStr                , failReason                , kCharBufSize                , fMemoryManager            );        }    }     else    {        // Store the new encoding string since it is just an intrinsic        fMemoryManager->deallocate(fEncodingStr);        fEncodingStr = inputEncoding;    }    if (!fTranscoder) {        //        //  Now we can create a transcoder using the recognized fEncoding.  We        //  might get back a transcoder for an intrinsically supported encoding,        //  or we might get one from the underlying transcoding service.        //        XMLTransService::Codes failReason;        fTranscoder = XMLPlatformUtils::fgTransService->makeNewTranscoderFor        (            newBaseEncoding            , failReason            , kCharBufSize            , fMemoryManager        );        if (!fTranscoder)            ThrowXMLwithMemMgr1(TranscodingException, XMLExcepts::Trans_CantCreateCvtrFor, fEncodingStr, fMemoryManager);    }    // Update the base encoding member with the new base encoding found    fEncoding = newBaseEncoding;    // Looks ok to us    return true;}// ---------------------------------------------------------------------------//  XMLReader: Private helper methods// ---------------------------------------------------------------------------////  This is called when the encoding flag is set and just sets the fSwapped//  flag appropriately.//void XMLReader::checkForSwapped(){    // Assume not swapped    fSwapped = false;    #if defined(ENDIANMODE_LITTLE)        if ((fEncoding == XMLRecognizer::UTF_16B)        ||  (fEncoding == XMLRecognizer::UCS_4B))        {            fSwapped = true;

⌨️ 快捷键说明

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