xmlscanner.cpp

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

CPP
1,855
字号
/* * Copyright 1999-2002,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: XMLScanner.cpp,v 1.73 2004/09/30 15:20:14 peiyongz Exp $ */// ---------------------------------------------------------------------------//  Includes// ---------------------------------------------------------------------------#include <xercesc/internal/XMLScanner.hpp>#include <xercesc/internal/ValidationContextImpl.hpp>#include <xercesc/util/Janitor.hpp>#include <xercesc/util/Mutexes.hpp>#include <xercesc/util/RuntimeException.hpp>#include <xercesc/util/UnexpectedEOFException.hpp>#include <xercesc/util/XMLMsgLoader.hpp>#include <xercesc/util/XMLRegisterCleanup.hpp>#include <xercesc/framework/LocalFileInputSource.hpp>#include <xercesc/framework/URLInputSource.hpp>#include <xercesc/framework/XMLDocumentHandler.hpp>#include <xercesc/framework/XMLEntityHandler.hpp>#include <xercesc/framework/XMLPScanToken.hpp>#include <xercesc/framework/XMLValidator.hpp>#include <xercesc/internal/EndOfEntityException.hpp>#include <xercesc/validators/DTD/DocTypeHandler.hpp>#include <xercesc/validators/common/GrammarResolver.hpp>#include <xercesc/util/OutOfMemoryException.hpp>#include <xercesc/util/XMLResourceIdentifier.hpp>XERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------//  Local static data// ---------------------------------------------------------------------------static XMLUInt32       gScannerId;static bool            sRegistered = false;static XMLMutex*       sScannerMutex = 0;static XMLRegisterCleanup scannerMutexCleanup;static XMLMsgLoader*   gMsgLoader = 0;static XMLRegisterCleanup cleanupMsgLoader;// ---------------------------------------------------------------------------//  Local, static functions// ---------------------------------------------------------------------------//  Cleanup for the message loadervoid XMLScanner::reinitMsgLoader(){	delete gMsgLoader;	gMsgLoader = 0;}//  Cleanup for the scanner mutexvoid XMLScanner::reinitScannerMutex(){    delete sScannerMutex;    sScannerMutex = 0;    sRegistered = false;}////  We need to fault in this mutex. But, since its used for synchronization//  itself, we have to do this the low level way using a compare and swap.//static XMLMutex& gScannerMutex(){    if (!sRegistered)    {        XMLMutexLock lockInit(XMLPlatformUtils::fgAtomicMutex);        if (!sRegistered)        {            sScannerMutex = new XMLMutex;            scannerMutexCleanup.registerCleanup(XMLScanner::reinitScannerMutex);            sRegistered = true;        }    }    return *sScannerMutex;}static XMLMsgLoader& gScannerMsgLoader(){    if (!gMsgLoader)    {        XMLMutexLock lockInit(&gScannerMutex());        // If we haven't loaded our message yet, then do that        if (!gMsgLoader)        {            gMsgLoader = XMLPlatformUtils::loadMsgSet(XMLUni::fgXMLErrDomain);            if (!gMsgLoader)                XMLPlatformUtils::panic(PanicHandler::Panic_CantLoadMsgDomain);            // Register this object to be cleaned up at termination            cleanupMsgLoader.registerCleanup(XMLScanner::reinitMsgLoader);        }    }    return *gMsgLoader;}// ---------------------------------------------------------------------------//  XMLScanner: Constructors and Destructor// ---------------------------------------------------------------------------XMLScanner::XMLScanner(XMLValidator* const valToAdopt,                       GrammarResolver* const grammarResolver,                       MemoryManager* const manager)    : fBufferSize(1024 * 1024)    , fStandardUriConformant(false)    , fCalculateSrcOfs(false)    , fDoNamespaces(false)    , fExitOnFirstFatal(true)    , fValidationConstraintFatal(false)    , fInException(false)    , fStandalone(false)    , fHasNoDTD(true)    , fValidate(false)    , fValidatorFromUser(false)    , fDoSchema(false)    , fSchemaFullChecking(false)    , fIdentityConstraintChecking(true)    , fToCacheGrammar(false)    , fUseCachedGrammar(false)    , fLoadExternalDTD(true)    , fNormalizeData(true)    , fGenerateSyntheticAnnotations(false)    , fValidateAnnotations(false)    , fErrorCount(0)    , fEntityExpansionLimit(0)    , fEntityExpansionCount(0)    , fEmptyNamespaceId(0)    , fUnknownNamespaceId(0)    , fXMLNamespaceId(0)    , fXMLNSNamespaceId(0)    , fSchemaNamespaceId(0)    , fUIntPool(0)    , fUIntPoolRow(0)    , fUIntPoolCol(0)    , fUIntPoolRowTotal(2)    , fScannerId(0)    , fSequenceId(0)    , fAttrList(0)    , fAttrDupChkRegistry(0)    , fDocHandler(0)    , fDocTypeHandler(0)    , fEntityHandler(0)    , fErrorReporter(0)    , fErrorHandler(0)    , fPSVIHandler(0)    , fValidationContext(0)    , fEntityDeclPoolRetrieved(false)    , fReaderMgr(manager)    , fValidator(valToAdopt)    , fValScheme(Val_Never)    , fGrammarResolver(grammarResolver)    , fGrammarPoolMemoryManager(grammarResolver->getGrammarPoolMemoryManager())    , fGrammar(0)    , fRootGrammar(0)    , fURIStringPool(0)    , fRootElemName(0)    , fExternalSchemaLocation(0)    , fExternalNoNamespaceSchemaLocation(0)        , fSecurityManager(0)    , fXMLVersion(XMLReader::XMLV1_0)    , fMemoryManager(manager)    , fBufMgr(manager)    , fAttNameBuf(1023, manager)    , fAttValueBuf(1023, manager)    , fCDataBuf(1023, manager)    , fQNameBuf(1023, manager)    , fPrefixBuf(1023, manager)    , fURIBuf(1023, manager)    , fElemStack(manager)   {   commonInit();   if (fValidator) {       fValidatorFromUser = true;       initValidator(fValidator);   }}XMLScanner::XMLScanner( XMLDocumentHandler* const  docHandler                          , DocTypeHandler* const    docTypeHandler                          , XMLEntityHandler* const  entityHandler                          , XMLErrorReporter* const  errHandler                          , XMLValidator* const      valToAdopt                          , GrammarResolver* const   grammarResolver                          , MemoryManager* const     manager)    : fBufferSize(1024 * 1024)    , fStandardUriConformant(false)    , fCalculateSrcOfs(false)    , fDoNamespaces(false)    , fExitOnFirstFatal(true)    , fValidationConstraintFatal(false)    , fInException(false)    , fStandalone(false)    , fHasNoDTD(true)    , fValidate(false)    , fValidatorFromUser(false)    , fDoSchema(false)    , fSchemaFullChecking(false)    , fIdentityConstraintChecking(true)    , fToCacheGrammar(false)    , fUseCachedGrammar(false)	, fLoadExternalDTD(true)    , fNormalizeData(true)    , fGenerateSyntheticAnnotations(false)    , fValidateAnnotations(false)    , fErrorCount(0)    , fEntityExpansionLimit(0)    , fEntityExpansionCount(0)    , fEmptyNamespaceId(0)    , fUnknownNamespaceId(0)    , fXMLNamespaceId(0)    , fXMLNSNamespaceId(0)    , fSchemaNamespaceId(0)    , fUIntPool(0)    , fUIntPoolRow(0)    , fUIntPoolCol(0)    , fUIntPoolRowTotal(2)    , fScannerId(0)    , fSequenceId(0)    , fAttrList(0)    , fAttrDupChkRegistry(0)    , fDocHandler(docHandler)    , fDocTypeHandler(docTypeHandler)    , fEntityHandler(entityHandler)    , fErrorReporter(errHandler)    , fErrorHandler(0)    , fPSVIHandler(0)    , fValidationContext(0)    , fEntityDeclPoolRetrieved(false)    , fReaderMgr(manager)    , fValidator(valToAdopt)    , fValScheme(Val_Never)    , fGrammarResolver(grammarResolver)    , fGrammarPoolMemoryManager(grammarResolver->getGrammarPoolMemoryManager())    , fGrammar(0)    , fRootGrammar(0)    , fURIStringPool(0)    , fRootElemName(0)    , fExternalSchemaLocation(0)    , fExternalNoNamespaceSchemaLocation(0)        , fSecurityManager(0)    , fXMLVersion(XMLReader::XMLV1_0)    , fMemoryManager(manager)    , fBufMgr(manager)    , fAttNameBuf(1023, manager)    , fAttValueBuf(1023, manager)    , fCDataBuf(1023, manager)    , fQNameBuf(1023, manager)    , fPrefixBuf(1023, manager)    , fURIBuf(1023, manager)    , fElemStack(manager){   commonInit();   if (valToAdopt){       fValidatorFromUser = true;       initValidator(fValidator);   }}XMLScanner::~XMLScanner(){    delete fAttrList;    delete fAttrDupChkRegistry;    delete fValidationContext;    fMemoryManager->deallocate(fRootElemName);//delete [] fRootElemName;    fMemoryManager->deallocate(fExternalSchemaLocation);//delete [] fExternalSchemaLocation;    fMemoryManager->deallocate(fExternalNoNamespaceSchemaLocation);//delete [] fExternalNoNamespaceSchemaLocation;    // delete fUIntPool    for (unsigned int i=0; i<=fUIntPoolRow; i++)    {        fMemoryManager->deallocate(fUIntPool[i]);    }    fMemoryManager->deallocate(fUIntPool);}void XMLScanner::setValidator(XMLValidator* const valToAdopt){    if (fValidatorFromUser)        delete fValidator;    fValidator = valToAdopt;    fValidatorFromUser = true;    initValidator(fValidator);}// ---------------------------------------------------------------------------//  XMLScanner: Main entry point to scan a document// ---------------------------------------------------------------------------void XMLScanner::scanDocument(  const   XMLCh* const    systemId){    //  First we try to parse it as a URL. If that fails, we assume its    //  a file and try it that way.    InputSource* srcToUse = 0;    try    {        //  Create a temporary URL. Since this is the primary document,        //  it has to be fully qualified. If not, then assume we are just        //  mistaking a file for a URL.        XMLURL tmpURL(fMemoryManager);        if (XMLURL::parse(systemId, tmpURL)) {            if (tmpURL.isRelative()) {                if (!fStandardUriConformant)                    srcToUse = new (fMemoryManager) LocalFileInputSource(systemId, fMemoryManager);                else {                    // since this is the top of the try/catch, cannot call ThrowXMLwithMemMgr                    // emit the error directly                    MalformedURLException e(__FILE__, __LINE__, XMLExcepts::URL_NoProtocolPresent, fMemoryManager);                    fInException = true;                    emitError                    (                        XMLErrs::XMLException_Fatal                        , e.getType()                        , e.getMessage()                    );                    return;                }            }            else            {                if (fStandardUriConformant && tmpURL.hasInvalidChar()) {                    MalformedURLException e(__FILE__, __LINE__, XMLExcepts::URL_MalformedURL, fMemoryManager);                    fInException = true;                    emitError                    (                        XMLErrs::XMLException_Fatal                        , e.getType()                        , e.getMessage()                    );                    return;                }                srcToUse = new (fMemoryManager) URLInputSource(tmpURL, fMemoryManager);            }        }        else {            if (!fStandardUriConformant)                srcToUse = new (fMemoryManager) LocalFileInputSource(systemId, fMemoryManager);            else {                // since this is the top of the try/catch, cannot call ThrowXMLwithMemMgr                // emit the error directly                // lazy bypass ... since all MalformedURLException are fatal, no need to check the type                MalformedURLException e(__FILE__, __LINE__, XMLExcepts::URL_MalformedURL, fMemoryManager);                fInException = true;

⌨️ 快捷键说明

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