elemstack.cpp

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

CPP
951
字号
/* * Copyright 1999-2001,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. *//* * $Log: ElemStack.cpp,v $ * Revision 1.14  2004/09/08 13:56:13  peiyongz * Apache License Version 2.0 * * Revision 1.13  2004/06/02 19:58:10  neilg * Fix bug where scanners would accept malformed tags of the form * <p:a xmlns:p="b" xmlns:q="b"></q:a> when namespace processing was * enabled.  This also opened the way for some end-tag scanning * performance improvements. * * Revision 1.12  2004/04/27 19:17:52  peiyongz * XML1.0-3rd VC: element content(children) dont allow white space from * EntityRef/CharRef * * Revision 1.11  2004/04/23 21:20:40  peiyongz * fCommentOrPISeen to keep track if any comment or PI seen for the current * element * * Revision 1.10  2003/12/17 00:18:34  cargilld * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data. * * Revision 1.9  2003/10/23 14:11:07  knoaman * Fix memory leak. * * Revision 1.8  2003/10/22 20:22:30  knoaman * Prepare for annotation support. * * Revision 1.7  2003/05/18 14:02:04  knoaman * Memory manager implementation: pass per instance manager. * * Revision 1.6  2003/05/16 21:36:57  knoaman * Memory manager implementation: Modify constructors to pass in the memory manager. * * Revision 1.5  2003/05/15 18:26:29  knoaman * Partial implementation of the configurable memory manager. * * Revision 1.4  2003/01/02 16:38:00  knoaman * Some cleanup. * * Revision 1.3  2002/12/04 02:23:50  knoaman * Scanner re-organization. * * Revision 1.2  2002/11/04 14:58:18  tng * C++ Namespace Support. * * Revision 1.1.1.1  2002/02/01 22:21:57  peiyongz * sane_include * * Revision 1.17  2001/12/12 14:29:50  tng * Remove obsolete code in ElemStack which can help performance. * * Revision 1.16  2001/08/29 16:42:27  tng * No need to new the child QName in ElemStack addChild.  Remove it for performance gain. * * Revision 1.15  2001/08/07 13:47:47  tng * Schema: Fix unmatched end tag for qualified/unqualifed start tag. * * Revision 1.14  2001/06/18 21:33:57  peiyongz * Memory leak fix: to addlevel(), by Erik Rydgren. * * Revision 1.13  2001/06/12 19:08:27  peiyongz * Memory leak: fixed by Erik Rydgren * * Revision 1.12  2001/05/28 20:55:19  tng * Schema: Store Grammar in ElemStack as well. * * Revision 1.11  2001/05/11 13:26:16  tng * Copyright update. * * Revision 1.10  2001/05/03 20:34:28  tng * Schema: SchemaValidator update * * Revision 1.9  2001/04/19 18:16:57  tng * Schema: SchemaValidator update, and use QName in Content Model * * Revision 1.8  2001/02/16 17:58:02  tng * use EmptyNamespaceId for attribute, GlobalNamespaceId for element. * * Revision 1.7  2000/07/05 05:20:17  roddey * Fixed a memory leak when namespaces are enabled. * * Revision 1.6  2000/05/15 22:31:15  andyh * Replace #include<memory.h> with <string.h> everywhere. * * Revision 1.5  2000/03/02 19:54:28  roddey * This checkin includes many changes done while waiting for the * 1.1.0 code to be finished. I can't list them all here, but a list is * available elsewhere. * * Revision 1.4  2000/02/08 19:38:58  roddey * xmlns:xxx="" should affect the mapping of the prefixes of sibling attributes, * which was not being done. * * Revision 1.3  2000/02/06 07:47:52  rahulj * Year 2K copyright swat. * * Revision 1.2  2000/01/19 00:55:45  roddey * Changes to get rid of dependence on old utils standard streams classes * and a small fix in the progressive parseFirst() call. * * Revision 1.1.1.1  1999/11/09 01:08:04  twl * Initial checkin * * Revision 1.4  1999/11/08 20:44:41  rahul * Swat for adding in Product name and CVS comment log variable. * */// ---------------------------------------------------------------------------//  Includes// ---------------------------------------------------------------------------#include <string.h>#include <xercesc/util/EmptyStackException.hpp>#include <xercesc/util/NoSuchElementException.hpp>#include <xercesc/framework/XMLElementDecl.hpp>#include <xercesc/internal/ElemStack.hpp>#include <xercesc/validators/common/Grammar.hpp>XERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------//  ElemStack: Constructors and Destructor// ---------------------------------------------------------------------------ElemStack::ElemStack(MemoryManager* const manager) :    fEmptyNamespaceId(0)    , fGlobalPoolId(0)    , fPrefixPool(109, manager)    , fStack(0)    , fStackCapacity(32)    , fStackTop(0)    , fUnknownNamespaceId(0)    , fXMLNamespaceId(0)    , fXMLPoolId(0)    , fXMLNSNamespaceId(0)    , fXMLNSPoolId(0)    , fNamespaceMap(0)    , fMemoryManager(manager){    // Do an initial allocation of the stack and zero it out    fStack = (StackElem**) fMemoryManager->allocate    (        fStackCapacity * sizeof(StackElem*)    );//new StackElem*[fStackCapacity];    memset(fStack, 0, fStackCapacity * sizeof(StackElem*));    fNamespaceMap = new (fMemoryManager) ValueVectorOf<PrefMapElem*>(16, fMemoryManager);}ElemStack::~ElemStack(){    //    //  Start working from the bottom of the stack and clear it out as we    //  go up. Once we hit an uninitialized one, we can break out.    //    for (unsigned int stackInd = 0; stackInd < fStackCapacity; stackInd++)    {        // If this entry has been set, then lets clean it up        if (!fStack[stackInd])            break;        fMemoryManager->deallocate(fStack[stackInd]->fChildren);//delete [] fStack[stackInd]->fChildren;        fMemoryManager->deallocate(fStack[stackInd]->fMap);//delete [] fStack[stackInd]->fMap;        fMemoryManager->deallocate(fStack[stackInd]->fSchemaElemName);        delete fStack[stackInd];    }    // Delete the stack array itself now    fMemoryManager->deallocate(fStack);//delete [] fStack;    delete fNamespaceMap;}// ---------------------------------------------------------------------------//  ElemStack: Stack access// ---------------------------------------------------------------------------unsigned int ElemStack::addLevel(){    // See if we need to expand the stack    if (fStackTop == fStackCapacity)        expandStack();    // If this element has not been initialized yet, then initialize it    if (!fStack[fStackTop])    {        fStack[fStackTop] = new (fMemoryManager) StackElem;        fStack[fStackTop]->fChildCapacity = 0;        fStack[fStackTop]->fChildren = 0;        fStack[fStackTop]->fMapCapacity = 0;        fStack[fStackTop]->fMap = 0;        fStack[fStackTop]->fSchemaElemName = 0;        fStack[fStackTop]->fSchemaElemNameMaxLen = 0;    }    // Set up the new top row    fStack[fStackTop]->fThisElement = 0;    fStack[fStackTop]->fReaderNum = 0xFFFFFFFF;    fStack[fStackTop]->fChildCount = 0;    fStack[fStackTop]->fMapCount = 0;    fStack[fStackTop]->fValidationFlag = false;    fStack[fStackTop]->fCommentOrPISeen = false;    fStack[fStackTop]->fReferenceEscaped = false;    fStack[fStackTop]->fCurrentURI = fUnknownNamespaceId;    fStack[fStackTop]->fCurrentScope = Grammar::TOP_LEVEL_SCOPE;    fStack[fStackTop]->fCurrentGrammar = 0;    // Bump the top of stack    fStackTop++;    return fStackTop-1;}unsigned intElemStack::addLevel(XMLElementDecl* const toSet, const unsigned int readerNum){    // See if we need to expand the stack    if (fStackTop == fStackCapacity)        expandStack();    // If this element has not been initialized yet, then initialize it    if (!fStack[fStackTop])    {        fStack[fStackTop] = new (fMemoryManager) StackElem;        fStack[fStackTop]->fChildCapacity = 0;        fStack[fStackTop]->fChildren = 0;        fStack[fStackTop]->fMapCapacity = 0;        fStack[fStackTop]->fMap = 0;        fStack[fStackTop]->fSchemaElemName = 0;        fStack[fStackTop]->fSchemaElemNameMaxLen = 0;    }    // Set up the new top row    fStack[fStackTop]->fThisElement = toSet;    fStack[fStackTop]->fReaderNum = readerNum;    fStack[fStackTop]->fChildCount = 0;    fStack[fStackTop]->fMapCount = 0;    fStack[fStackTop]->fValidationFlag = false;    fStack[fStackTop]->fCommentOrPISeen = false;    fStack[fStackTop]->fReferenceEscaped = false;    fStack[fStackTop]->fCurrentURI = fUnknownNamespaceId;    fStack[fStackTop]->fCurrentScope = Grammar::TOP_LEVEL_SCOPE;    fStack[fStackTop]->fCurrentGrammar = 0;    // Bump the top of stack    fStackTop++;    return fStackTop-1;}const ElemStack::StackElem* ElemStack::popTop(){    // Watch for an underflow error    if (!fStackTop)        ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::ElemStack_StackUnderflow, fMemoryManager);    fStackTop--;    return fStack[fStackTop];}voidElemStack::setElement(XMLElementDecl* const toSet, const unsigned int readerNum){    if (!fStackTop)        ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::ElemStack_EmptyStack, fMemoryManager);    fStack[fStackTop - 1]->fThisElement = toSet;    fStack[fStackTop - 1]->fReaderNum = readerNum;}// ---------------------------------------------------------------------------//  ElemStack: Stack top access// ---------------------------------------------------------------------------unsigned int ElemStack::addChild(QName* const child, const bool toParent){    if (!fStackTop)        ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::ElemStack_EmptyStack, fMemoryManager);    //    //  If they want to add to the parent, then we have to have at least two    //  elements on the stack.    //    if (toParent && (fStackTop < 2))        ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::ElemStack_NoParentPushed, fMemoryManager);    // Get a convenience pointer to the stack top row    StackElem* curRow = toParent                        ? fStack[fStackTop - 2] : fStack[fStackTop - 1];    // See if we need to expand this row's child array    if (curRow->fChildCount == curRow->fChildCapacity)    {        // Increase the capacity by a quarter and allocate a new row        const unsigned int newCapacity = curRow->fChildCapacity ?                                         (unsigned int)(curRow->fChildCapacity * 1.25) :                                         32;

⌨️ 快捷键说明

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