grammarresolver.cpp

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

CPP
549
字号
    Grammar* grammar = fGrammarBucket->get(gramDesc->getGrammarKey());    if (grammar)        return grammar;    if (fUseCachedGrammar)    {        grammar = fGrammarFromPool->get(gramDesc->getGrammarKey());        if (grammar)        {            return grammar;        }        else        {            grammar = fGrammarPool->retrieveGrammar(gramDesc);            if (grammar)            {                fGrammarFromPool->put((void*) grammar->getGrammarDescription()->getGrammarKey(), grammar);            }            return grammar;        }    }    return 0;}RefHashTableOfEnumerator<Grammar>GrammarResolver::getGrammarEnumerator() const{    return RefHashTableOfEnumerator<Grammar>(fGrammarBucket, false, fMemoryManager);}RefHashTableOfEnumerator<Grammar>GrammarResolver::getReferencedGrammarEnumerator() const{    return RefHashTableOfEnumerator<Grammar>(fGrammarFromPool, false, fMemoryManager);}RefHashTableOfEnumerator<Grammar>GrammarResolver::getCachedGrammarEnumerator() const{    return fGrammarPool->getGrammarEnumerator();}bool GrammarResolver::containsNameSpace( const XMLCh* const nameSpaceKey ){    if (!nameSpaceKey)        return false;    if (fGrammarBucket->containsKey(nameSpaceKey))        return true;    if (fUseCachedGrammar)    {        if (fGrammarFromPool->containsKey(nameSpaceKey))            return true;        // Lastly, need to check in fGrammarPool                XMLSchemaDescription* gramDesc = fGrammarPool->createSchemaDescription(nameSpaceKey);        Janitor<XMLGrammarDescription> janName(gramDesc);        Grammar* grammar = fGrammarPool->retrieveGrammar(gramDesc);        if (grammar)            return true;    }    return false;}void GrammarResolver::putGrammar(Grammar* const grammarToAdopt){    if (!grammarToAdopt)        return;    /***     * the grammar will be either in the grammarpool, or in the grammarbucket     */    if (!fCacheGrammar || !fGrammarPool->cacheGrammar(grammarToAdopt))    {        // either we aren't caching or the grammar pool doesn't want it        // so we need to look after it        fGrammarBucket->put( (void*) grammarToAdopt->getGrammarDescription()->getGrammarKey(), grammarToAdopt );        if (grammarToAdopt->getGrammarType() == Grammar::SchemaGrammarType)        {            fGrammarsToAddToXSModel->addElement((SchemaGrammar*) grammarToAdopt);        }    }}// ---------------------------------------------------------------------------//  GrammarResolver: methods// ---------------------------------------------------------------------------void GrammarResolver::reset() {    fGrammarBucket->removeAll();    fGrammarsToAddToXSModel->removeAllElements();    delete fXSModel;    fXSModel = 0;}void GrammarResolver::resetCachedGrammar(){    //REVISIT: if the pool is locked this will fail... should throw an exception?    fGrammarPool->clear();    // Even though fXSModel and fGrammarPoolXSModel will be invalid don't touch     // them here as getXSModel will handle this.    //to clear all other references to the grammars just deleted from fGrammarPool    fGrammarFromPool->removeAll(); }void GrammarResolver::cacheGrammars(){    RefHashTableOfEnumerator<Grammar> grammarEnum(fGrammarBucket, false, fMemoryManager);    ValueVectorOf<XMLCh*> keys(8, fMemoryManager);    unsigned int keyCount = 0;    // Build key set    while (grammarEnum.hasMoreElements())     {        XMLCh* grammarKey = (XMLCh*) grammarEnum.nextElementKey();        keys.addElement(grammarKey);        keyCount++;    }    // PSVI: assume everything will be added, if caching fails add grammar back     //       into vector    fGrammarsToAddToXSModel->removeAllElements();    // Cache    for (unsigned int i = 0; i < keyCount; i++)     {        XMLCh* grammarKey = keys.elementAt(i);            /***         * It is up to the GrammarPool implementation to handle duplicated grammar         */        Grammar* grammar = fGrammarBucket->get(grammarKey);        if(fGrammarPool->cacheGrammar(grammar))        {            // only orphan grammar if grammar pool accepts caching of it            fGrammarBucket->orphanKey(grammarKey);        }        else if (grammar->getGrammarType() == Grammar::SchemaGrammarType)        {            // add it back to list of grammars not in grammar pool            fGrammarsToAddToXSModel->addElement((SchemaGrammar*) grammar);                   }    }}// ---------------------------------------------------------------------------//  GrammarResolver: Setter methods// ---------------------------------------------------------------------------void GrammarResolver::cacheGrammarFromParse(const bool aValue){    reset();    fCacheGrammar = aValue;        fGrammarBucket->setAdoptElements(!fCacheGrammar);}Grammar* GrammarResolver::orphanGrammar(const XMLCh* const nameSpaceKey){    if (fCacheGrammar)    {        Grammar* grammar = fGrammarPool->orphanGrammar(nameSpaceKey);        if (grammar)        {            fGrammarFromPool->removeKey(nameSpaceKey);            return grammar;        }               // It failed to remove it from the grammar pool either because it        // didn't exist or because it is locked.        return 0;    }    else    {        return fGrammarBucket->orphanKey(nameSpaceKey);    }}XSModel *GrammarResolver::getXSModel(){    XSModel* xsModel;    if (fCacheGrammar)    {        // We know if the grammarpool changed thru caching, orphaning and erasing        // but NOT by other mechanisms such as lockPool() or unlockPool() so it        // is safest to always get it.  The grammarPool XSModel will only be         // regenerated if something changed.        bool XSModelWasChanged;        // The grammarpool will always return an xsmodel, even if it is just        // the schema for schema xsmodel...        xsModel = fGrammarPool->getXSModel(XSModelWasChanged);        if (XSModelWasChanged)        {            // we know the grammarpool XSModel has changed or this is the            // first call to getXSModel            if (!fGrammarPoolXSModel && (fGrammarsToAddToXSModel->size() == 0) &&                !fXSModel)            {                 fGrammarPoolXSModel = xsModel;                return fGrammarPoolXSModel;            }            else            {                fGrammarPoolXSModel = xsModel;                // We had previously augmented the grammar pool XSModel                // with our our grammars or we would like to upate it now                // so we have to regenerate the XSModel                fGrammarsToAddToXSModel->removeAllElements();                RefHashTableOfEnumerator<Grammar> grammarEnum(fGrammarBucket, false, fMemoryManager);                while (grammarEnum.hasMoreElements())                 {                    Grammar& grammar = (Grammar&) grammarEnum.nextElement();                    if (grammar.getGrammarType() == Grammar::SchemaGrammarType)                        fGrammarsToAddToXSModel->addElement((SchemaGrammar*)&grammar);                }                delete fXSModel;                if (fGrammarsToAddToXSModel->size())                {                                        fXSModel = new (fMemoryManager) XSModel(fGrammarPoolXSModel, this, fMemoryManager);                    fGrammarsToAddToXSModel->removeAllElements();                    return fXSModel;                }                       fXSModel = 0;                 return fGrammarPoolXSModel;                                                                  }               }        else {            // we know that the grammar pool XSModel is the same as before            if (fGrammarsToAddToXSModel->size())            {                // we need to update our fXSModel with the new grammars                               if (fXSModel)                {                    xsModel = new (fMemoryManager) XSModel(fXSModel, this, fMemoryManager);                                       fXSModel = xsModel;                }                else                {                    fXSModel = new (fMemoryManager) XSModel(fGrammarPoolXSModel, this, fMemoryManager);                }                fGrammarsToAddToXSModel->removeAllElements();                return fXSModel;            }            // Nothing has changed!            if (fXSModel)            {                return fXSModel;            }            else if (fGrammarPoolXSModel)            {                return fGrammarPoolXSModel;            }            fXSModel = new (fMemoryManager) XSModel(0, this, fMemoryManager);                return fXSModel;            }    }    // Not Caching...    if (fGrammarsToAddToXSModel->size())    {              xsModel = new (fMemoryManager) XSModel(fXSModel, this, fMemoryManager);        fGrammarsToAddToXSModel->removeAllElements();        fXSModel = xsModel;                 }    else if (!fXSModel)    {        // create a new model only if we didn't have one already        fXSModel = new (fMemoryManager) XSModel(0, this, fMemoryManager);    }    return fXSModel; }XERCES_CPP_NAMESPACE_END

⌨️ 快捷键说明

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