domparser.cpp

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

CPP
1,464
字号
/* * 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. *//***  This file contains code to build the DOM tree. It registers a document*  handler with the scanner. In these handler methods, appropriate DOM nodes*  are created and added to the DOM tree.** $Id: DOMParser.cpp,v 1.33 2004/09/08 13:55:42 peiyongz Exp $**/// ---------------------------------------------------------------------------//  Includes// ---------------------------------------------------------------------------#include <xercesc/internal/XMLScannerResolver.hpp>#include <xercesc/sax/EntityResolver.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include <xercesc/sax/ErrorHandler.hpp>#include <xercesc/sax/SAXParseException.hpp>#include <xercesc/framework/XMLNotationDecl.hpp>#include <xercesc/util/IOException.hpp>#include <xercesc/framework/XMLValidator.hpp>#include <xercesc/validators/common/GrammarResolver.hpp>#include <xercesc/framework/XMLGrammarPool.hpp>#include <xercesc/framework/XMLSchemaDescription.hpp>#include <xercesc/util/Janitor.hpp>#include "DOMParser.hpp"#include "ElementImpl.hpp"#include "AttrImpl.hpp"#include "AttrNSImpl.hpp"#include "TextImpl.hpp"#include "DocumentImpl.hpp"#include "DocumentTypeImpl.hpp"#include "EntityImpl.hpp"#include "NotationImpl.hpp"#include "NamedNodeMapImpl.hpp"#include "NodeIDMap.hpp"#include <xercesc/validators/common/ContentSpecNode.hpp>#include <xercesc/validators/DTD/DTDAttDefList.hpp>#include <xercesc/util/OutOfMemoryException.hpp>#include <xercesc/util/XMLEntityResolver.hpp>XERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------//  DOMParser: Constructors and Destructor// ---------------------------------------------------------------------------DOMParser::DOMParser( XMLValidator* const   valToAdopt                    , MemoryManager* const  manager                    , XMLGrammarPool* const gramPool) :    fToCreateXMLDeclTypeNode(false)    , fCreateEntityReferenceNodes(true)    , fIncludeIgnorableWhitespace(true)    , fParseInProgress(false)    , fWithinElement(false)    , fEntityResolver(0)    , fXMLEntityResolver(0)    , fErrorHandler(0)    , fPSVIHandler(0)    , fNodeStack(0)    , fScanner(0)    , fDocumentType(0)    , fGrammarResolver(0)    , fURIStringPool(0)    , fValidator(valToAdopt)    , fMemoryManager(manager)    , fGrammarPool(gramPool){    try    {        initialize();    }    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        cleanUp();        throw;    }}DOMParser::~DOMParser(){    cleanUp();}// ---------------------------------------------------------------------------//  DOMParser: Initialize/CleanUp methods// ---------------------------------------------------------------------------void DOMParser::initialize(){    // Create grammar resolver and URI string pool to pass to the scanner    fGrammarResolver = new (fMemoryManager) GrammarResolver(fGrammarPool, fMemoryManager);    fURIStringPool = fGrammarResolver->getStringPool();    //  Create a scanner and tell it what validator to use. Then set us    //  as the document event handler so we can fill the DOM document.    fScanner = XMLScannerResolver::getDefaultScanner(fValidator, fGrammarResolver, fMemoryManager);    fScanner->setDocHandler(this);    fScanner->setDocTypeHandler(this);    fScanner->setURIStringPool(fURIStringPool);    fNodeStack = new (fMemoryManager) ValueStackOf<DOM_Node>(64, fMemoryManager, true);    this->reset();}void DOMParser::cleanUp(){    delete fNodeStack;    delete fScanner;    delete fGrammarResolver;    // grammar pool must do this    //delete fURIStringPool;    if (fValidator)        delete fValidator;}void DOMParser::reset(){    //    //  Note: DOM Documents are reference counted. Doing this assignment    //  will cause the old one to go away unless application code is also    //  holding a reference to it.    //    fDocument = DOM_Document::createDocument(fMemoryManager);    resetDocType();    fCurrentParent   = 0;    fCurrentNode     = 0;    fParseInProgress = false;    fWithinElement   = false;    fNodeStack->removeAllElements();};// ---------------------------------------------------------------------------//  DOMParser: Getter methods// ---------------------------------------------------------------------------const XMLValidator& DOMParser::getValidator() const{    return *fScanner->getValidator();}bool DOMParser::getDoNamespaces() const{    return fScanner->getDoNamespaces();}bool DOMParser::getExitOnFirstFatalError() const{    return fScanner->getExitOnFirstFatal();}bool DOMParser::getValidationConstraintFatal() const{    return fScanner->getValidationConstraintFatal();}DOMParser::ValSchemes DOMParser::getValidationScheme() const{    const XMLScanner::ValSchemes scheme = fScanner->getValidationScheme();    if (scheme == XMLScanner::Val_Always)        return Val_Always;    else if (scheme == XMLScanner::Val_Never)        return Val_Never;    return Val_Auto;}bool DOMParser::getDoSchema() const{    return fScanner->getDoSchema();}bool DOMParser::getValidationSchemaFullChecking() const{    return fScanner->getValidationSchemaFullChecking();}bool DOMParser::getIdentityConstraintChecking() const{    return fScanner->getIdentityConstraintChecking();}int DOMParser::getErrorCount() const{    return fScanner->getErrorCount();}XMLCh* DOMParser::getExternalSchemaLocation() const{    return fScanner->getExternalSchemaLocation();}XMLCh* DOMParser::getExternalNoNamespaceSchemaLocation() const{    return fScanner->getExternalNoNamespaceSchemaLocation();}bool DOMParser::isCachingGrammarFromParse() const{    return fScanner->isCachingGrammarFromParse();}bool DOMParser::isUsingCachedGrammarInParse() const{    return fScanner->isUsingCachedGrammarInParse();}Grammar* DOMParser::getGrammar(const XMLCh* const nameSpaceKey){    return fGrammarResolver->getGrammar(nameSpaceKey);}Grammar* DOMParser::getRootGrammar(){    return fScanner->getRootGrammar();}const XMLCh* DOMParser::getURIText(unsigned int uriId) const{    return fScanner->getURIText(uriId);}bool DOMParser::getCalculateSrcOfs() const{    return fScanner->getCalculateSrcOfs();}bool DOMParser::getStandardUriConformant() const{    return fScanner->getStandardUriConformant();}unsigned int DOMParser::getSrcOffset() const{    return fScanner->getSrcOffset();}// ---------------------------------------------------------------------------//  DOMParser: Setter methods// ---------------------------------------------------------------------------void DOMParser::setDoNamespaces(const bool newState){    fScanner->setDoNamespaces(newState);}void DOMParser::setErrorHandler(ErrorHandler* const handler){    fErrorHandler = handler;    if (fErrorHandler) {        fScanner->setErrorReporter(this);        fScanner->setErrorHandler(fErrorHandler);    }    else {        fScanner->setErrorReporter(0);        fScanner->setErrorHandler(0);    }}void DOMParser::setPSVIHandler(PSVIHandler* const handler){    fPSVIHandler = handler;    if (fPSVIHandler) {        fScanner->setPSVIHandler(fPSVIHandler);            }    else {        fScanner->setPSVIHandler(0);           }}void DOMParser::setEntityResolver(EntityResolver* const handler){    fEntityResolver = handler;    if (fEntityResolver) {        fScanner->setEntityHandler(this);        fXMLEntityResolver = 0;    }    else {        fScanner->setEntityHandler(0);    }}void DOMParser::setXMLEntityResolver(XMLEntityResolver* const handler){    fXMLEntityResolver = handler;    if (fXMLEntityResolver) {        fEntityResolver = 0;        fScanner->setEntityHandler(this);    }    else {        fScanner->setEntityHandler(0);    }}void DOMParser::setExitOnFirstFatalError(const bool newState){    fScanner->setExitOnFirstFatal(newState);}void DOMParser::setValidationConstraintFatal(const bool newState){    fScanner->setValidationConstraintFatal(newState);}void DOMParser::setValidationScheme(const ValSchemes newScheme){    if (newScheme == Val_Never)        fScanner->setValidationScheme(XMLScanner::Val_Never);    else if (newScheme == Val_Always)        fScanner->setValidationScheme(XMLScanner::Val_Always);    else        fScanner->setValidationScheme(XMLScanner::Val_Auto);}void DOMParser::setDoSchema(const bool newState){    fScanner->setDoSchema(newState);}void DOMParser::setValidationSchemaFullChecking(const bool schemaFullChecking){    fScanner->setValidationSchemaFullChecking(schemaFullChecking);}void DOMParser::setIdentityConstraintChecking(const bool identityConstraintChecking){    fScanner->setIdentityConstraintChecking(identityConstraintChecking);}void DOMParser::setExternalSchemaLocation(const XMLCh* const schemaLocation){    fScanner->setExternalSchemaLocation(schemaLocation);}void DOMParser::setExternalNoNamespaceSchemaLocation(const XMLCh* const noNamespaceSchemaLocation){    fScanner->setExternalNoNamespaceSchemaLocation(noNamespaceSchemaLocation);}void DOMParser::setExternalSchemaLocation(const char* const schemaLocation){    fScanner->setExternalSchemaLocation(schemaLocation);}void DOMParser::setExternalNoNamespaceSchemaLocation(const char* const noNamespaceSchemaLocation){    fScanner->setExternalNoNamespaceSchemaLocation(noNamespaceSchemaLocation);}void DOMParser::cacheGrammarFromParse(const bool newState){    fScanner->cacheGrammarFromParse(newState);    if (newState)        fScanner->useCachedGrammarInParse(newState);}void DOMParser::useCachedGrammarInParse(const bool newState){    if (newState || !fScanner->isCachingGrammarFromParse())        fScanner->useCachedGrammarInParse(newState);}void DOMParser::setCalculateSrcOfs(const bool newState){    fScanner->setCalculateSrcOfs(newState);}void DOMParser::setStandardUriConformant(const bool newState){    fScanner->setStandardUriConformant(newState);}void DOMParser::useScanner(const XMLCh* const scannerName){    XMLScanner* tempScanner = XMLScannerResolver::resolveScanner    (        scannerName        , fValidator        , fGrammarResolver        , fMemoryManager    );    if (tempScanner) {        tempScanner->setParseSettings(fScanner);        tempScanner->setURIStringPool(fURIStringPool);        delete fScanner;        fScanner = tempScanner;    }}// ---------------------------------------------------------------------------//  DOMParser: Parsing methods// ---------------------------------------------------------------------------void DOMParser::parse(const InputSource& source){    // Avoid multiple entrance    if (fParseInProgress)        ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);    try    {        fParseInProgress = true;        fScanner->scanDocument(source);        fParseInProgress = false;    }    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        fParseInProgress = false;        throw;    }}void DOMParser::parse(const XMLCh* const systemId){    // Avoid multiple entrance    if (fParseInProgress)        ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);    try    {        fParseInProgress = true;        fScanner->scanDocument(systemId);        fParseInProgress = false;    }    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        fParseInProgress = false;        throw;    }}void DOMParser::parse(const char* const systemId){    // Avoid multiple entrance    if (fParseInProgress)        ThrowXMLwithMemMgr(IOException, XMLExcepts::Gen_ParseInProgress, fMemoryManager);    try    {        fParseInProgress = true;        fScanner->scanDocument(systemId);        fParseInProgress = false;    }    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        fParseInProgress = false;        throw;    }

⌨️ 快捷键说明

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