source: NonGTP/Xerces/xerces-c_2_8_0/include/xercesc/validators/common/CMLeaf.hpp @ 2674

Revision 2674, 7.3 KB checked in by mattausch, 16 years ago (diff)
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: CMLeaf.hpp 568078 2007-08-21 11:43:25Z amassari $
20 */
21
22#if !defined(CMLEAF_HPP)
23#define CMLEAF_HPP
24
25#include <xercesc/validators/common/CMNode.hpp>
26
27
28XERCES_CPP_NAMESPACE_BEGIN
29
30//
31//  This class represents a leaf in the content spec node tree of an
32//  element's content model. It just has an element qname and a position value,
33//  the latter of which is used during the building of a DFA.
34//
35class CMLeaf : public CMNode
36{
37public :
38    // -----------------------------------------------------------------------
39    //  Constructors
40    // -----------------------------------------------------------------------
41    CMLeaf
42    (
43          QName* const         element
44        , const unsigned int   position = (~0)
45        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
46    );
47    CMLeaf
48    (
49          QName* const         element
50        , const unsigned int   position
51        , const bool           adopt
52        , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
53    );
54    ~CMLeaf();
55
56
57    // -----------------------------------------------------------------------
58    //  Getter methods
59    // -----------------------------------------------------------------------
60    QName* getElement();
61    const QName* getElement() const;
62    unsigned int getPosition() const;
63
64
65    // -----------------------------------------------------------------------
66    //  Setter methods
67    // -----------------------------------------------------------------------
68    void setPosition(const unsigned int newPosition);
69
70
71    // -----------------------------------------------------------------------
72    //  Implementation of public CMNode virtual interface
73    // -----------------------------------------------------------------------
74    bool isNullable() const;
75
76
77protected :
78    // -----------------------------------------------------------------------
79    //  Implementation of protected CMNode virtual interface
80    // -----------------------------------------------------------------------
81    void calcFirstPos(CMStateSet& toSet) const;
82    void calcLastPos(CMStateSet& toSet) const;
83
84
85private :
86    // -----------------------------------------------------------------------
87    //  Private data members
88    //
89    //  fElement
90    //      This is the element that this leaf represents.
91    //
92    //  fPosition
93    //      Part of the algorithm to convert a regex directly to a DFA
94    //      numbers each leaf sequentially. If its -1, that means its an
95    //      epsilon node. All others are non-epsilon positions.
96    //
97    //  fAdopt
98    //      This node is responsible for the storage of the fElement QName.
99    // -----------------------------------------------------------------------
100    QName*          fElement;
101    unsigned int    fPosition;
102    bool            fAdopt;
103
104    // -----------------------------------------------------------------------
105    //  Unimplemented constructors and operators
106    // -----------------------------------------------------------------------
107    CMLeaf(const CMLeaf&);
108    CMLeaf& operator=(const CMLeaf&);
109};
110
111
112// -----------------------------------------------------------------------
113//  Constructors
114// -----------------------------------------------------------------------
115inline CMLeaf::CMLeaf(       QName* const         element
116                     , const unsigned int         position
117                     ,       MemoryManager* const manager) :
118    CMNode(ContentSpecNode::Leaf, manager)
119    , fElement(0)
120    , fPosition(position)
121    , fAdopt(false)
122{
123    if (!element)
124    {
125        fElement = new (fMemoryManager) QName
126        (
127              XMLUni::fgZeroLenString
128            , XMLUni::fgZeroLenString
129            , XMLElementDecl::fgInvalidElemId
130            , fMemoryManager
131        );
132        // We have to be responsible for this QName - override default fAdopt
133        fAdopt = true;
134    }
135    else
136    {
137        fElement = element;
138    }
139}
140
141inline CMLeaf::CMLeaf(       QName* const         element
142                     , const unsigned int         position
143                     , const bool                 adopt
144                     ,       MemoryManager* const manager) :
145    CMNode(ContentSpecNode::Leaf, manager)
146    , fElement(0)
147    , fPosition(position)
148    , fAdopt(adopt)
149{
150    if (!element)
151    {
152        fElement = new (fMemoryManager) QName
153        (
154              XMLUni::fgZeroLenString
155            , XMLUni::fgZeroLenString
156            , XMLElementDecl::fgInvalidElemId
157            , fMemoryManager
158        );
159        // We have to be responsible for this QName - override adopt parameter
160        fAdopt = true;
161    }
162    else
163    {
164        fElement = element;
165    }
166}
167
168inline CMLeaf::~CMLeaf()
169{
170    if (fAdopt)
171        delete fElement;
172}
173
174
175// ---------------------------------------------------------------------------
176//  Getter methods
177// ---------------------------------------------------------------------------
178inline QName* CMLeaf::getElement()
179{
180    return fElement;
181}
182
183inline const QName* CMLeaf::getElement() const
184{
185    return fElement;
186}
187
188inline unsigned int CMLeaf::getPosition() const
189{
190    return fPosition;
191}
192
193
194// ---------------------------------------------------------------------------
195//  Setter methods
196// ---------------------------------------------------------------------------
197inline void CMLeaf::setPosition(const unsigned int newPosition)
198{
199    fPosition = newPosition;
200}
201
202
203// ---------------------------------------------------------------------------
204//  Implementation of public CMNode virtual interface
205// ---------------------------------------------------------------------------
206inline bool CMLeaf::isNullable() const
207{
208    // Leaf nodes are never nullable unless its an epsilon node
209    return (fPosition == -1);
210}
211
212
213// ---------------------------------------------------------------------------
214//  Implementation of protected CMNode virtual interface
215// ---------------------------------------------------------------------------
216inline void CMLeaf::calcFirstPos(CMStateSet& toSet) const
217{
218    // If we are an epsilon node, then the first pos is an empty set
219    if (fPosition == -1)
220    {
221        toSet.zeroBits();
222        return;
223    }
224
225    // Otherwise, its just the one bit of our position
226    toSet.setBit(fPosition);
227}
228
229inline void CMLeaf::calcLastPos(CMStateSet& toSet) const
230{
231    // If we are an epsilon node, then the last pos is an empty set
232    if (fPosition == -1)
233    {
234        toSet.zeroBits();
235        return;
236    }
237
238    // Otherwise, its just the one bit of our position
239    toSet.setBit(fPosition);
240}
241
242XERCES_CPP_NAMESPACE_END
243
244#endif
Note: See TracBrowser for help on using the repository browser.