readermgr.cpp

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

CPP
1,112
字号
}XMLReader* ReaderMgr::createReader( const   InputSource&        src                                    , const bool                                                    , const XMLReader::RefFrom  refFrom                                    , const XMLReader::Types    type                                    , const XMLReader::Sources  source                                    , const bool                calcSrcOfs){    //    //  Ask the input source to create us an input stream. The particular    //  type of input source will know what kind to create.    //    BinInputStream* newStream = src.makeStream();    if (!newStream)        return 0;    //    //  Create a new reader and return it. If the source has an encoding that    //  it wants to force, then we call the constructor that does that.    //  Otherwise, we just call the one that provides the provisional encoding    //  to be possibly updated later by the encoding="" setting.    //    XMLReader* retVal = 0;    // XMLReader ctor invokes refreshRawBuffer() which calls    // newStream->readBytes().    // This readBytes() may throw exception, which neither    // refresRawBuffer(), nor XMLReader ctor catches.    // We need to handle this exception to avoid leak on newStream.    try {        if (src.getEncoding())        {            retVal = new (fMemoryManager) XMLReader                (                src.getPublicId()                , src.getSystemId()                , newStream                , src.getEncoding()                , refFrom                , type                , source                , false                , calcSrcOfs                , fXMLVersion                , fMemoryManager                );        }        else        {            retVal = new (fMemoryManager) XMLReader                (                src.getPublicId()                , src.getSystemId()                , newStream                , refFrom                , type                , source                , false                , calcSrcOfs                , fXMLVersion                , fMemoryManager                );        }    }    catch(const OutOfMemoryException&)    {        throw;    }    catch (...) //NetAccessorException&    {        delete newStream;        throw;    }    // If it failed for any reason, then return zero.    if (!retVal) {        delete newStream;        return 0;    }    // Set the next available reader number on this reader    retVal->setReaderNum(fNextReaderNum++);    return retVal;}XMLReader* ReaderMgr::createReader( const   XMLCh* const        sysId                                    , const XMLCh* const        pubId                                    , const bool                xmlDecl                                    , const XMLReader::RefFrom  refFrom                                    , const XMLReader::Types    type                                    , const XMLReader::Sources  source                                    ,       InputSource*&       srcToFill                                    , const bool                calcSrcOfs){    //Normalize sysId     XMLBuffer normalizedSysId(1023, fMemoryManager);    XMLString::removeChar(sysId, 0xFFFF, normalizedSysId);    const XMLCh* normalizedURI = normalizedSysId.getRawBuffer();    // Create a buffer for expanding the system id    XMLBuffer expSysId(1023, fMemoryManager);    //    //  Allow the entity handler to expand the system id if they choose    //  to do so.    //    if (fEntityHandler)    {        if (!fEntityHandler->expandSystemId(normalizedURI, expSysId))            expSysId.set(normalizedURI);    }     else    {        expSysId.set(normalizedURI);    }    // Call the entity resolver interface to get an input source    srcToFill = 0;    if (fEntityHandler)    {        LastExtEntityInfo lastInfo;        getLastExtEntityInfo(lastInfo);        XMLResourceIdentifier resourceIdentifier(XMLResourceIdentifier::ExternalEntity,                            expSysId.getRawBuffer(), XMLUni::fgZeroLenString, pubId, lastInfo.systemId);        srcToFill = fEntityHandler->resolveEntity(&resourceIdentifier);    }    //    //  If they didn't create a source via the entity resolver, then we    //  have to create one on our own.    //    if (!srcToFill)    {        LastExtEntityInfo lastInfo;        getLastExtEntityInfo(lastInfo);// Keep this #if 0 block as it was exposing a threading problem on AIX.// Got rid of the problem by changing XMLURL to not throw malformedurl// exceptions.        #if 0        try        {            XMLURL urlTmp(lastInfo.systemId, expSysId.getRawBuffer(), fMemoryManager);            if (urlTmp.isRelative())            {                ThrowXMLwithMemMgr                (                    MalformedURLException                    , XMLExcepts::URL_NoProtocolPresent                    , fMemoryManager                );            }            else {                if (fStandardUriConformant && urlTmp.hasInvalidChar())                    ThrowXMLwithMemMgr(MalformedURLException, XMLExcepts::URL_MalformedURL, fMemoryManager);                srcToFill = new (fMemoryManager) URLInputSource(urlTmp, fMemoryManager);            }        }        catch(const MalformedURLException& e)        {            // Its not a URL, so lets assume its a local file name if non-standard uri is allowed            if (!fStandardUriConformant)                srcToFill = new (fMemoryManager) LocalFileInputSource                (                    lastInfo.systemId                    , expSysId.getRawBuffer()                    , fMemoryManager                );            else                throw e;        }#else        XMLURL urlTmp(fMemoryManager);        if ((!urlTmp.setURL(lastInfo.systemId, expSysId.getRawBuffer(), urlTmp)) ||            (urlTmp.isRelative()))        {            if (!fStandardUriConformant)            {                XMLBuffer resolvedSysId(1023, fMemoryManager);                XMLUri::normalizeURI(expSysId.getRawBuffer(), resolvedSysId);                srcToFill = new (fMemoryManager) LocalFileInputSource                (                    lastInfo.systemId                    , resolvedSysId.getRawBuffer()                    , fMemoryManager                );            }            else                ThrowXMLwithMemMgr(MalformedURLException, XMLExcepts::URL_MalformedURL, fMemoryManager);                    }        else        {            if (fStandardUriConformant && urlTmp.hasInvalidChar())                ThrowXMLwithMemMgr(MalformedURLException, XMLExcepts::URL_MalformedURL, fMemoryManager);            srcToFill = new (fMemoryManager) URLInputSource(urlTmp, fMemoryManager);        }        #endif    }    // Put a janitor on the input source    Janitor<InputSource> janSrc(srcToFill);    //    //  Now call the other version with the input source that we have, and    //  return the resulting reader.    //    XMLReader* retVal = createReader    (        *srcToFill        , xmlDecl        , refFrom        , type        , source        , calcSrcOfs    );    // Either way, we can release the input source now    janSrc.orphan();    // If it failed for any reason, then return zero.    if (!retVal)        return 0;    // Give this reader the next available reader number and return it    retVal->setReaderNum(fNextReaderNum++);    return retVal;}XMLReader* ReaderMgr::createReader( const   XMLCh* const        baseURI                                    , const XMLCh* const        sysId                                    , const XMLCh* const        pubId                                    , const bool                xmlDecl                                    , const XMLReader::RefFrom  refFrom                                    , const XMLReader::Types    type                                    , const XMLReader::Sources  source                                    ,       InputSource*&       srcToFill                                    , const bool                calcSrcOfs){    //Normalize sysId     XMLBuffer normalizedSysId(1023, fMemoryManager);    XMLString::removeChar(sysId, 0xFFFF, normalizedSysId);    const XMLCh* normalizedURI = normalizedSysId.getRawBuffer();    // Create a buffer for expanding the system id    XMLBuffer expSysId(1023, fMemoryManager);    //    //  Allow the entity handler to expand the system id if they choose    //  to do so.    //    if (fEntityHandler)    {        if (!fEntityHandler->expandSystemId(normalizedURI, expSysId))            expSysId.set(normalizedURI);    }     else    {        expSysId.set(normalizedURI);    }    // Call the entity resolver interface to get an input source    srcToFill = 0;    if (fEntityHandler)    {        XMLResourceIdentifier resourceIdentifier(XMLResourceIdentifier::ExternalEntity,                            expSysId.getRawBuffer(), XMLUni::fgZeroLenString, pubId, baseURI);        srcToFill = fEntityHandler->resolveEntity(&resourceIdentifier);    }    //    //  If they didn't create a source via the entity resolver, then we    //  have to create one on our own.    //    if (!srcToFill)    {        LastExtEntityInfo lastInfo;        const XMLCh* baseuri=baseURI;        if(!baseuri || !*baseuri)        {            getLastExtEntityInfo(lastInfo);            baseuri = lastInfo.systemId;        }        XMLURL urlTmp(fMemoryManager);        if ((!urlTmp.setURL(baseuri, expSysId.getRawBuffer(), urlTmp)) ||            (urlTmp.isRelative()))        {            if (!fStandardUriConformant)            {                XMLBuffer resolvedSysId(1023, fMemoryManager);                XMLUri::normalizeURI(expSysId.getRawBuffer(), resolvedSysId);                srcToFill = new (fMemoryManager) LocalFileInputSource                (                    baseuri                    , resolvedSysId.getRawBuffer()                    , fMemoryManager                );            }            else                ThrowXMLwithMemMgr(MalformedURLException, XMLExcepts::URL_MalformedURL, fMemoryManager);                    }        else        {            if (fStandardUriConformant && urlTmp.hasInvalidChar())                ThrowXMLwithMemMgr(MalformedURLException, XMLExcepts::URL_MalformedURL, fMemoryManager);            srcToFill = new (fMemoryManager) URLInputSource(urlTmp, fMemoryManager);        }            }    // Put a janitor on the input source    Janitor<InputSource> janSrc(srcToFill);    //    //  Now call the other version with the input source that we have, and    //  return the resulting reader.    //    XMLReader* retVal = createReader    (        *srcToFill        , xmlDecl        , refFrom        , type        , source        , calcSrcOfs    );    // Either way, we can release the input source now    janSrc.orphan();    // If it failed for any reason, then return zero.    if (!retVal)        return 0;    // Give this reader the next available reader number and return it    retVal->setReaderNum(fNextReaderNum++);    return retVal;}XMLReader*ReaderMgr::createIntEntReader(  const   XMLCh* const        sysId                                , const XMLReader::RefFrom  refFrom                                , const XMLReader::Types    type                                , const XMLCh* const        dataBuf                                , const unsigned int        dataLen                                , const bool                copyBuf                                , const bool                calcSrcOfs){    //    //  This one is easy, we just create an input stream for the data and    //  provide a few extra goodies.    //    //  NOTE: We use a special encoding string that will be recognized    //  as a 'do nothing' transcoder for the already internalized XMLCh    //  data that makes up an internal entity.    //    BinMemInputStream* newStream = new (fMemoryManager) BinMemInputStream                                   (                                     (const XMLByte*)dataBuf                                     , dataLen * sizeof(XMLCh)                                     , copyBuf ? BinMemInputStream::BufOpt_Copy                                               : BinMemInputStream::BufOpt_Reference

⌨️ 快捷键说明

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