readermgr.cpp

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

CPP
1,112
字号
/* * Copyright 1999-2000,2004 The Apache Software Foundation. *  * 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. *//* * $Id: ReaderMgr.cpp,v 1.28 2004/09/08 13:56:13 peiyongz Exp $ */// ---------------------------------------------------------------------------//  Includes// ---------------------------------------------------------------------------#include <xercesc/util/BinMemInputStream.hpp>#include <xercesc/util/Janitor.hpp>#include <xercesc/util/PlatformUtils.hpp>#include <xercesc/util/RuntimeException.hpp>#include <xercesc/util/UnexpectedEOFException.hpp>#include <xercesc/util/XMLURL.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include <xercesc/util/XMLUni.hpp>#include <xercesc/util/XMLUri.hpp>#include <xercesc/sax/InputSource.hpp>#include <xercesc/framework/LocalFileInputSource.hpp>#include <xercesc/framework/URLInputSource.hpp>#include <xercesc/framework/XMLBuffer.hpp>#include <xercesc/framework/XMLDocumentHandler.hpp>#include <xercesc/framework/XMLEntityDecl.hpp>#include <xercesc/framework/XMLEntityHandler.hpp>#include <xercesc/internal/EndOfEntityException.hpp>#include <xercesc/internal/ReaderMgr.hpp>#include <xercesc/util/OutOfMemoryException.hpp>#include <xercesc/util/XMLResourceIdentifier.hpp>XERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------//  ReaderMgr: Constructors and Destructor// ---------------------------------------------------------------------------ReaderMgr::ReaderMgr(MemoryManager* const manager) :    fCurEntity(0)    , fCurReader(0)    , fEntityHandler(0)    , fEntityStack(0)    , fNextReaderNum(1)    , fReaderStack(0)    , fThrowEOE(false)    , fXMLVersion(XMLReader::XMLV1_0)    , fStandardUriConformant(false)    , fMemoryManager(manager){}ReaderMgr::~ReaderMgr(){    //    //  Clean up the reader and entity stacks. Note that we don't own the    //  entities, so we don't delete the current entity (and the entity stack    //  does not own its elements either, so deleting it will not delete the    //  entities it still references!)    //    delete fCurReader;    delete fReaderStack;    delete fEntityStack;}// ---------------------------------------------------------------------------//  ReaderMgr: Getter methods// ---------------------------------------------------------------------------bool ReaderMgr::isEmpty() const{    return fReaderStack->empty();}// ---------------------------------------------------------------------------//  ReaderMgr: Scanning APIs// ---------------------------------------------------------------------------XMLCh ReaderMgr::getNextChar(){    XMLCh chRet;    if (fCurReader->getNextChar(chRet))        return chRet;    //    //  Didn't get anything back so this reader is hosed. So lets move to    //  the next reader on the stack. If this fails, it will be because    //  its the end of the original file, and we just return zero.    //    //  If its the end of an entity and fThrowEOE is set, it will throw out    //  of here. Otherwise, it will take us down to the next reader and    //  we'll have more chars.    //    if (!popReader())        return XMLCh(0);    // Else try again and return the new character    fCurReader->getNextChar(chRet);    return chRet;}void ReaderMgr::getSpaces(XMLBuffer& toFill){    // Reset the buffer before we start    toFill.reset();    while (true)    {        //        //  Get all the spaces from the current reader. If it returns true,        //  it hit a non-space and we are done. Else we have to pop a reader        //  and keep going.        //        if (fCurReader->getSpaces(toFill))            break;        // We wore that one out, so lets pop a reader and try again        if (!popReader())            break;    }}void ReaderMgr::getUpToCharOrWS(XMLBuffer& toFill, const XMLCh toCheck){    // Reset the target buffer before we start    toFill.reset();    //    //  Ok, enter a loop where we ask the current reader to get chars until    //  it meets the criteria. It returns false if it came back due to eating    //  up all of its data. Else it returned because something matched, and    //  we are done.    //    while (true)    {        if (fCurReader->getUpToCharOrWS(toFill, toCheck))            break;        // We ate that one up, lets try to pop another. If not, break out        if (!popReader())            break;    }}XMLCh ReaderMgr::peekNextChar(){    XMLCh chRet;    if (fCurReader->peekNextChar(chRet))        return chRet;    //    //  Didn't get anything back so this reader is hosed. So lets move to    //  the next reader on the stack. If this fails, it will be because    //  its the end of the original file, and we just return zero.    //    if (!popReader())        return XMLCh(0);    // Else peek again and return the character    fCurReader->peekNextChar(chRet);    return chRet;}bool ReaderMgr::skippedChar(const XMLCh toCheck){    while (true)    {        // If we get it, then just return true now        if (fCurReader->skippedChar(toCheck))            return true;        //        //  Check to see if we hit end of input on this reader. If so, then        //  lets pop and try again. Else, we failed. If we cannot pop another        //  then we failed.        //        if (!fCurReader->getNoMoreFlag())            break;        if (!popReader())            break;    }    return false;}bool ReaderMgr::skippedSpace(){    while (true)    {        // If we get it, then just return true now        if (fCurReader->skippedSpace())            return true;        //        //  Check to see if we hit end of input on this reader. If so, then        //  lets pop and try again. Else, we failed. If we cannot pop another        //  then we failed.        //        if (!fCurReader->getNoMoreFlag())            break;        if (!popReader())            break;    }    return false;}bool ReaderMgr::skipIfQuote(XMLCh& chGotten){    while (true)    {        // If we get it, then just return true now        if (fCurReader->skipIfQuote(chGotten))            return true;        //        //  Check to see if we hit end of input on this reader. If so, then        //  lets pop and try again. Else, we failed. If we cannot pop another        //  then we failed.        //        if (!fCurReader->getNoMoreFlag())            break;        if (!popReader())            break;    }    return false;}bool ReaderMgr::skipPastSpaces(bool inDecl){    bool skippedSomething = false;    bool tmpFlag;    while (true)    {        //        //  Skip all the spaces in the current reader. If it returned because        //  it hit a non-space, break out. Else we have to pop another entity        //  and keep going.        //        if (fCurReader->skipSpaces(tmpFlag, inDecl))            break;        if (tmpFlag)            skippedSomething = true;        // Try to pop another enitity. If we can't then we are done        if (!popReader())            break;    }    return (tmpFlag || skippedSomething);}void ReaderMgr::skipQuotedString(const XMLCh quoteCh){    XMLCh nextCh;    while (true)    {        nextCh = getNextChar();        // If we get an end of file char, then return        if (!nextCh)            break;        // If we get the quote char, then break out        if (nextCh == quoteCh)            break;    }}XMLCh ReaderMgr::skipUntilIn(const XMLCh* const listToSkip){    XMLCh nextCh;    while (true)    {        nextCh = peekNextChar();        if (!nextCh)            break;        if (XMLString::indexOf(listToSkip, nextCh) != -1)            break;        // Its one of ours so eat it        getNextChar();    }    return nextCh;}XMLCh ReaderMgr::skipUntilInOrWS(const XMLCh* const listToSkip){    XMLCh nextCh;    while (true)    {        nextCh = peekNextChar();        if (!nextCh)            break;        if (fCurReader->isWhitespace(nextCh))            break;        if (XMLString::indexOf(listToSkip, nextCh) != -1)            break;        // Its one of ours, so eat it        getNextChar();    }    return nextCh;}// ---------------------------------------------------------------------------//  ReaderMgr: Control methods// ---------------------------------------------------------------------------////  If the reader stack is empty, then there is only the original main XML//  entity left. If its empty, then we have no more input.//bool ReaderMgr::atEOF() const{    return fReaderStack->empty() && fCurReader->getNoMoreFlag();}////  This method is called in the case of errors to clean up the stack when//  entities have been incorrectly left on the stack due to syntax errors.//  It just cleans back the stack, and sends no entity events.//void ReaderMgr::cleanStackBackTo(const unsigned int readerNum){    //    //  Just start popping readers until we find the one with the indicated    //  reader number.    //    while (true)    {        if (fCurReader->getReaderNum() == readerNum)            break;        if (fReaderStack->empty())            ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::RdrMgr_ReaderIdNotFound, fMemoryManager);        delete fCurReader;        fCurReader = fReaderStack->pop();        fCurEntity = fEntityStack->pop();    }

⌨️ 快捷键说明

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