source: icXML/icXML-devel/src/icxercesc/internal/ElemStack.cpp @ 2720

Last change on this file since 2720 was 2720, checked in by cameron, 6 years ago

Initial check-in of icXML 0.8 source files

File size: 12.6 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id: ElemStack.cpp 830538 2009-10-28 13:41:11Z amassari $
20 */
21
22// ---------------------------------------------------------------------------
23//  Includes
24// ---------------------------------------------------------------------------
25#include <string.h>
26#include <xercesc/util/EmptyStackException.hpp>
27#include <xercesc/util/NoSuchElementException.hpp>
28#include <xercesc/framework/XMLElementDecl.hpp>
29#include <xercesc/validators/common/Grammar.hpp>
30#include <xercesc/internal/ElemStack.hpp>
31
32XERCES_CPP_NAMESPACE_BEGIN
33
34#pragma warning "QUESTION: is there any benefit in having parabix count the max element depth within a buffer for bulk stack allocation?"
35#ifdef TEST_NAMESPACE_RESOLVER
36#pragma warning "TODO: The namespace resolver is not currently dealing with the global namespaces."
37#endif
38
39// ---------------------------------------------------------------------------
40//  ElemStack: Constructors and Destructor
41// ---------------------------------------------------------------------------
42ElemStack::ElemStack(MemoryManager* const manager) :
43
44        fStack(0)
45        , fStackCapacity(32)
46        , fStackTop(0)
47        , fMemoryManager(manager)
48{
49        // Do an initial allocation of the stack and zero it out
50        fStack = (StackElem**) fMemoryManager->allocate
51        (
52                fStackCapacity * sizeof(StackElem*)
53        );//new StackElem*[fStackCapacity];
54        memset(fStack, 0, fStackCapacity * sizeof(StackElem*));
55}
56
57ElemStack::~ElemStack()
58{
59        //
60        //  Start working from the bottom of the stack and clear it out as we
61        //  go up. Once we hit an uninitialized one, we can break out.
62        //
63        for (XMLSize_t stackInd = 0; stackInd < fStackCapacity; stackInd++)
64        {
65                // If this entry has been set, then lets clean it up
66                if (!fStack[stackInd])
67                        break;
68                #ifndef STORE_CHILDREN_INFORMATION_IN_PARSER
69                fMemoryManager->deallocate(fStack[stackInd]->fChildren);
70                #endif
71                delete fStack[stackInd];
72        }
73
74        // Delete the stack array itself now
75        fMemoryManager->deallocate(fStack);//delete [] fStack;
76}
77
78
79// ---------------------------------------------------------------------------
80//  ElemStack: Stack access
81// ---------------------------------------------------------------------------
82XMLSize_t ElemStack::addLevel()
83{
84#if 0
85        // See if we need to expand the stack
86        if (fStackTop == fStackCapacity)
87                expandStack();
88
89        // If this element has not been initialized yet, then initialize it
90        if (!fStack[fStackTop])
91        {
92                fStack[fStackTop] = new (fMemoryManager) StackElem;
93                #ifndef STORE_CHILDREN_INFORMATION_IN_PARSER
94                fStack[fStackTop]->fChildCapacity = 0;
95                fStack[fStackTop]->fChildren = 0;
96                #endif
97                #ifndef PERFORM_ELEMENT_STACK_MATCHING_IN_PARSER
98                fStack[fStackTop]->fSchemaElemName = 0;
99                #endif
100        }
101
102        // Set up the new top row
103        fStack[fStackTop]->fThisElement = 0;
104        fStack[fStackTop]->fReaderNum = 0xFFFFFFFF;
105        #ifndef STORE_CHILDREN_INFORMATION_IN_PARSER
106        fStack[fStackTop]->fChildCount = 0;
107        #endif
108        fStack[fStackTop]->fValidationFlag = false;
109        fStack[fStackTop]->fCommentOrPISeen = false;
110        fStack[fStackTop]->fCurrentScope = Grammar::TOP_LEVEL_SCOPE;
111        fStack[fStackTop]->fCurrentGrammar = 0;
112        // Bump the top of stack
113        fStackTop++;
114
115        return fStackTop - 1;
116#else
117        DEPRECATED_FEATURE_IN_ICXML;
118#endif
119}
120
121
122XMLSize_t ElemStack::addLevel(XMLElementDecl* const toSet, const XMLSize_t readerNum)
123{
124        // See if we need to expand the stack
125        if (fStackTop == fStackCapacity)
126                expandStack();
127
128        // If this element has not been initialized yet, then initialize it
129        if (!fStack[fStackTop])
130        {
131                fStack[fStackTop] = new (fMemoryManager) StackElem;
132                #ifndef STORE_CHILDREN_INFORMATION_IN_PARSER
133                fStack[fStackTop]->fChildCapacity = 0;
134                fStack[fStackTop]->fChildren = 0;
135                #endif
136                #ifndef PERFORM_ELEMENT_STACK_MATCHING_IN_PARSER
137                fStack[fStackTop]->fSchemaElemName = 0;
138                #endif
139        }
140
141        // Set up the new top row
142        fStack[fStackTop]->fThisElement = toSet;
143        fStack[fStackTop]->fReaderNum = readerNum;
144        #ifndef STORE_CHILDREN_INFORMATION_IN_PARSER
145        fStack[fStackTop]->fChildCount = 0;
146        #endif
147        fStack[fStackTop]->fValidationFlag = false;
148        fStack[fStackTop]->fCommentOrPISeen = false;
149        fStack[fStackTop]->fCurrentScope = Grammar::TOP_LEVEL_SCOPE;
150        fStack[fStackTop]->fCurrentGrammar = 0;
151        // Bump the top of stack
152        fStackTop++;
153
154        return fStackTop - 1;
155}
156
157
158
159const ElemStack::StackElem* ElemStack::popTop()
160{
161        // Watch for an underflow error
162        if (!fStackTop)
163                ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::ElemStack_StackUnderflow, fMemoryManager);
164
165        return fStack[--fStackTop];
166}
167
168
169void
170ElemStack::setElement(XMLElementDecl* const toSet, const XMLSize_t readerNum)
171{
172        if (!fStackTop)
173                ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::ElemStack_EmptyStack, fMemoryManager);
174
175        fStack[fStackTop - 1]->fThisElement = toSet;
176        fStack[fStackTop - 1]->fReaderNum = readerNum;
177}
178
179
180// ---------------------------------------------------------------------------
181//  ElemStack: Stack top access
182// ---------------------------------------------------------------------------
183XMLSize_t ElemStack::addChild(QName* const child, const bool toParent)
184{
185        #ifdef STORE_CHILDREN_INFORMATION_IN_PARSER
186                DEPRECATED_FEATURE_IN_ICXML;
187        #else
188        if (!fStackTop)
189                ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::ElemStack_EmptyStack, fMemoryManager);
190
191        //
192        //  If they want to add to the parent, then we have to have at least two
193        //  elements on the stack.
194        //
195        if (toParent && (fStackTop < 2))
196                ThrowXMLwithMemMgr(NoSuchElementException, XMLExcepts::ElemStack_NoParentPushed, fMemoryManager);
197
198        // Get a convenience pointer to the stack top row
199        StackElem* curRow = toParent ? fStack[fStackTop - 2] : fStack[fStackTop - 1];
200
201        // See if we need to expand this row's child array
202        if (curRow->fChildCount == curRow->fChildCapacity)
203        {
204                // Increase the capacity by a quarter and allocate a new row
205                const XMLSize_t newCapacity =
206                        curRow->fChildCapacity
207                        ? (XMLSize_t)(curRow->fChildCapacity * 1.25)
208                        : 32;
209
210                QName ** newRow = (QName**) fMemoryManager->allocate
211                (
212                        newCapacity * sizeof(QName*)
213                );
214
215                //
216                //  Copy over the old contents. We don't have to initialize the new
217                //  part because The current child count is used to know how much of
218                //  it is valid.
219                //
220                //  Only both doing this if there is any current content, since
221                //  this code also does the initial faulting in of the array when
222                //  both the current capacity and child count are zero.
223                //
224                for (XMLSize_t index = 0; index < curRow->fChildCount; index++)
225                {
226                        newRow[index] = curRow->fChildren[index];
227                }
228
229                // Clean up the old children and store the new info
230                fMemoryManager->deallocate(curRow->fChildren);
231                curRow->fChildren = newRow;
232                curRow->fChildCapacity = newCapacity;
233        }
234
235        // Add this id to the end of the row's child id array and bump the count
236        curRow->fChildren[curRow->fChildCount++] = child;
237
238        // Return the level of the index we just filled (before the bump)
239        return curRow->fChildCount - 1;
240        #endif
241}
242
243const ElemStack::StackElem* ElemStack::topElement() const
244{
245        if (!fStackTop)
246        {
247                ThrowXMLwithMemMgr(EmptyStackException, XMLExcepts::ElemStack_EmptyStack, fMemoryManager);
248        }
249        return fStack[fStackTop - 1];
250}
251
252// ---------------------------------------------------------------------------
253//  ElemStack: Private helpers
254// ---------------------------------------------------------------------------
255
256void ElemStack::expandStack()
257{
258        // Expand the capacity by 25% and allocate a new buffer
259        const XMLSize_t newCapacity = (XMLSize_t)(fStackCapacity * 1.25);
260        StackElem** newStack = (StackElem**) fMemoryManager->allocate
261        (
262                newCapacity * sizeof(StackElem*)
263        );//new StackElem*[newCapacity];
264
265        // Copy over the old stuff
266        memcpy(newStack, fStack, fStackCapacity * sizeof(StackElem*));
267
268        //
269        //  And zero out the new stuff. Though we use a stack top, we reuse old
270        //  stack contents so we need to know if elements have been initially
271        //  allocated or not as we push new stuff onto the stack.
272        //
273        memset
274        (
275                &newStack[fStackCapacity]
276                , 0
277                , (newCapacity - fStackCapacity) * sizeof(StackElem*)
278        );
279
280        // Delete the old array and update our members
281        fMemoryManager->deallocate(fStack);//delete [] fStack;
282        fStack = newStack;
283        fStackCapacity = newCapacity;
284}
285
286// -----------------------------------------------------------------------------
287// DEPRECATED BUT KEPT FOR DLL COMPATIBILITY
288// -----------------------------------------------------------------------------
289
290// ---------------------------------------------------------------------------
291//  WFElemStack: Constructors and Destructor
292// ---------------------------------------------------------------------------
293WFElemStack::WFElemStack(MemoryManager* const manager)
294{
295        DEPRECATED_FEATURE_IN_ICXML;
296}
297
298WFElemStack::~WFElemStack()
299{
300
301}
302
303// ---------------------------------------------------------------------------
304//  WFElemStack: Stack access
305// ---------------------------------------------------------------------------
306XMLSize_t WFElemStack::addLevel()
307{
308        DEPRECATED_FEATURE_IN_ICXML;
309}
310
311
312XMLSize_t
313WFElemStack::addLevel(const XMLCh* const toSet,
314                                          const unsigned int toSetLen,
315                                          const unsigned int readerNum)
316{
317        DEPRECATED_FEATURE_IN_ICXML;
318}
319
320
321
322const WFElemStack::StackElem* WFElemStack::popTop()
323{
324        DEPRECATED_FEATURE_IN_ICXML;
325}
326
327
328void
329WFElemStack::setElement(const XMLCh* const toSet,
330                                          const unsigned int toSetLen,
331                                          const unsigned int readerNum)
332{
333        DEPRECATED_FEATURE_IN_ICXML;
334}
335
336
337// ---------------------------------------------------------------------------
338//  WFElemStack: Stack top access
339// ---------------------------------------------------------------------------
340const WFElemStack::StackElem* WFElemStack::topElement() const
341{
342        DEPRECATED_FEATURE_IN_ICXML;
343}
344
345
346// ---------------------------------------------------------------------------
347//  WFElemStack: Prefix map methods
348// ---------------------------------------------------------------------------
349void WFElemStack::addPrefix(  const   XMLCh* const    prefixToAdd
350                                                          , const unsigned int    uriId)
351{
352        DEPRECATED_FEATURE_IN_ICXML;
353}
354
355
356unsigned int WFElemStack::mapPrefixToURI( const   XMLCh* const    prefixToMap
357                                                                                  ,       bool&           unknown) const
358{
359        DEPRECATED_FEATURE_IN_ICXML;
360}
361
362
363// ---------------------------------------------------------------------------
364//  WFElemStack: Miscellaneous methods
365// ---------------------------------------------------------------------------
366void WFElemStack::reset(  const   unsigned int    emptyId
367                                                  , const unsigned int    unknownId
368                                                  , const unsigned int    xmlId
369                                                  , const unsigned int    xmlNSId)
370{
371        DEPRECATED_FEATURE_IN_ICXML;
372}
373
374
375// ---------------------------------------------------------------------------
376//  WFElemStack: Private helpers
377// ---------------------------------------------------------------------------
378void WFElemStack::expandMap()
379{
380        DEPRECATED_FEATURE_IN_ICXML;
381}
382
383void WFElemStack::expandStack()
384{
385        DEPRECATED_FEATURE_IN_ICXML;
386}
387
388
389// ---------------------------------------------------------------------------
390//  ElemStack: Prefix map methods
391// ---------------------------------------------------------------------------
392void ElemStack::addGlobalPrefix(const XMLCh* const    prefixToAdd
393                                                          , const unsigned int    uriId)
394{
395        DEPRECATED_FEATURE_IN_ICXML;
396}
397
398void ElemStack::addPrefix( const   XMLCh* const  prefixToAdd
399                                                 , const unsigned int    uriId)
400{
401        DEPRECATED_FEATURE_IN_ICXML;
402}
403
404
405unsigned int ElemStack::mapPrefixToURI( const XMLCh* const  prefixToMap
406                                                                          ,       bool&         unknown) const
407{
408        DEPRECATED_FEATURE_IN_ICXML;
409}
410
411ValueVectorOf<PrefMapElem*>* ElemStack::getNamespaceMap() const
412{
413        DEPRECATED_FEATURE_IN_ICXML;
414}
415
416// ---------------------------------------------------------------------------
417//  ElemStack: Miscellaneous methods
418// ---------------------------------------------------------------------------
419void ElemStack::reset(  const   unsigned int    emptyId
420                                                , const unsigned int    unknownId
421                                                , const unsigned int    xmlId
422                                                , const unsigned int    xmlNSId)
423{
424        DEPRECATED_FEATURE_IN_ICXML;
425}
426
427void ElemStack::expandMap(StackElem* const toExpand)
428{
429        DEPRECATED_FEATURE_IN_ICXML;
430}
431
432
433XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.