source: icXML/icXML-devel/src/icxercesc/validators/common/DFAContentModel.hpp

Last change on this file was 3564, checked in by cameron, 5 years ago

Changes to icxercesc files

File size: 10.8 KB
Line 
1/*
2 * Unless required by applicable law or agreed to in writing, software
3 * distributed under the License is distributed on an "AS IS" BASIS,
4 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5 * See the License for the specific language governing permissions and
6 * limitations under the License.
7 */
8
9/*
10 * $Id: DFAContentModel.hpp 677705 2008-07-17 20:15:32Z amassari $
11 */
12
13#if !defined(XERCESC_INCLUDE_GUARD_DFACONTENTMODEL_HPP)
14#define XERCESC_INCLUDE_GUARD_DFACONTENTMODEL_HPP
15
16#include <xercesc/util/XercesDefs.hpp>
17#include <xercesc/util/ArrayIndexOutOfBoundsException.hpp>
18#include <icxercesc/framework/XMLContentModel.hpp>
19#include <xercesc/validators/common/ContentLeafNameTypeVector.hpp>
20
21XERCES_CPP_NAMESPACE_BEGIN
22
23class ContentSpecNode;
24class CMLeaf;
25class CMRepeatingLeaf;
26class CMNode;
27class CMStateSet;
28class XMLStringPool;
29
30//
31//  DFAContentModel is the heavy weight derivative of ContentModel that does
32//  all of the non-trivial element content validation. This guy does the full
33//  bore regular expression to DFA conversion to create a DFA that it then
34//  uses in its validation algorithm.
35//
36//  NOTE:   Upstream work insures that this guy will never see a content model
37//          with PCDATA in it. Any model with PCDATA is 'mixed' and is handled
38//          via the MixedContentModel class, since mixed models are very
39//          constrained in form and easily handled via a special case. This
40//          also makes our life much easier here.
41//
42class DFAContentModel : public XMLContentModel
43{
44public:
45        // -----------------------------------------------------------------------
46        //  Constructors and Destructor
47        // -----------------------------------------------------------------------
48    explicit DFAContentModel
49    (
50          const bool             dtd
51        , ContentSpecNode* const elemContentSpec
52        , XMLStringPool * const  stringPool
53        , MemoryManager * const  manager
54    );
55
56    explicit DFAContentModel
57    (
58          const bool             dtd
59        , ContentSpecNode* const elemContentSpec
60        , const bool             isMixed
61        , XMLStringPool * const  stringPool
62        , MemoryManager* const   manager
63    );
64
65        DFAContentModel
66        (
67                  const bool             dtd
68                , ContentSpecNode* const elemContentSpec
69                , MemoryManager* const   manager = XMLPlatformUtils::fgMemoryManager
70        );
71
72        DFAContentModel
73        (
74                  const bool             dtd
75                , ContentSpecNode* const elemContentSpec
76                , const bool             isMixed
77                , MemoryManager* const   manager
78        );
79
80        virtual ~DFAContentModel();
81
82
83        // -----------------------------------------------------------------------
84        //  Implementation of the virtual content model interface
85        // -----------------------------------------------------------------------
86
87        virtual bool validateContent
88        (
89                XMLElementDecl ** const                 children
90                , XMLSize_t                                             childCount
91                , XMLSize_t*                                    indexFailingChild
92                , MemoryManager*    const               manager
93        ) const;
94
95        virtual bool validateContentSpecial
96        (
97                XMLElementDecl ** const                 children
98                , XMLSize_t                                             childCount
99                , GrammarResolver*  const               pGrammarResolver
100                , XMLNamespaceResolver* const   pUriResolver
101                , XMLSize_t*                                    indexFailingChild
102                , MemoryManager*    const               manager = XMLPlatformUtils::fgMemoryManager
103        ) const;
104
105        virtual void checkUniqueParticleAttribution
106        (
107                SchemaGrammar*    const pGrammar
108          , GrammarResolver*  const pGrammarResolver
109          , XMLNamespaceResolver* const pUriResolver
110          , XMLValidator*     const pValidator
111          , unsigned int*     const pContentSpecOrgURI
112          , const XMLCh*            pComplexTypeName = 0
113        );
114
115        virtual ContentLeafNameTypeVector* getContentLeafNameTypeVector() const ;
116
117        virtual unsigned int getNextState(unsigned int currentState,
118                                                                          XMLSize_t    elementIndex) const;
119
120        virtual bool handleRepetitions( const QName* const curElem,
121                                                                        unsigned int curState,
122                                                                        unsigned int currentLoop,
123                                                                        unsigned int& nextState,
124                                                                        unsigned int& nextLoop,
125                                                                        XMLSize_t elementIndex,
126                                                                        SubstitutionGroupComparator * comparator) const;
127
128        #ifdef PRINT_DEBUG_MESSAGE
129        virtual void debug_out(std::ostream & out) const
130        {
131                out << "(DFAContentModel: ";
132                if (fElemMapSize)
133                {
134                        char leadingChar = '{';
135                        for (unsigned int i = 0; i < fElemMapSize; i++)
136                        {
137                                out << leadingChar << fElemMap[i] << ':' << fElemMapType[i];
138                                leadingChar = ',';
139                        }
140                        out << '}';
141                }
142                out << ',' << fEmptyOk
143                        << ',' << fEOCPos
144                        << ',' << fDTD
145                        << ',' << fIsMixed
146                        << ')';
147        }
148        #endif
149
150    // -----------------------------------------------------------------------
151    //  Deprecated methods of the virtual content model interface
152    // -----------------------------------------------------------------------
153
154    virtual bool validateContent(QName** const, XMLSize_t, unsigned int, XMLSize_t*, MemoryManager* const) const;
155    virtual bool validateContentSpecial(QName** const, XMLSize_t, unsigned int, GrammarResolver* const, XMLStringPool* const, XMLSize_t*, MemoryManager* const) const;
156    virtual void checkUniqueParticleAttribution(SchemaGrammar* const, GrammarResolver* const, XMLStringPool* const, XMLValidator* const, unsigned int* const, const XMLCh*);
157
158private :
159        // -----------------------------------------------------------------------
160        //  Unimplemented constructors and operators
161        // -----------------------------------------------------------------------
162        DFAContentModel();
163        DFAContentModel(const DFAContentModel&);
164        DFAContentModel& operator=(const DFAContentModel&);
165
166        // -----------------------------------------------------------------------
167        //  Private helper methods
168        // -----------------------------------------------------------------------
169        void buildDFA(ContentSpecNode* const curNode);
170        CMNode* buildSyntaxTree(ContentSpecNode* const curNode, unsigned int& curIndex);
171        unsigned int* makeDefStateList() const;
172        unsigned int countLeafNodes(ContentSpecNode* const curNode);
173
174        class Occurence : public XMemory
175        {
176        public:
177                Occurence(int minOcc, int maxOcc, int eltIndex);
178
179                int minOccurs;
180                int maxOccurs;
181                int elemIndex;
182        };
183
184        // -----------------------------------------------------------------------
185        //  Private data members
186        //
187        //  fElemMap
188        //  fElemMapSize
189        //      This is the map of unique input symbol elements to indices into
190        //      each state's per-input symbol transition table entry. This is part
191        //      of the built DFA information that must be kept around to do the
192        //      actual validation.
193        //
194        //  fElemMapType
195        //      This is a map of whether the element map contains information
196        //      related to ANY models.
197        //
198        //  fEmptyOk
199        //      This is an optimization. While building the transition table we
200        //      can see whether this content model would approve of an empty
201        //      content (which could happen if everything was optional.) So we
202        //      set this flag and short circuit that check, which would otherwise
203        //      be ugly and time consuming if we tried to determine it at each
204        //      validation call.
205        //
206        //  fEOCPos
207        //      The NFA position of the special EOC (end of content) node. This
208        //      is saved away since its used during the DFA build.
209        //
210        //  fFinalStateFlags
211        //      This is an array of booleans, one per state (there are
212        //      fTransTableSize states in the DFA) that indicates whether that
213        //      state is a final state.
214        //
215        //  fFollowList
216        //      The list of follow positions for each NFA position (i.e. for each
217        //      non-epsilon leaf node.) This is only used during the building of
218        //      the DFA, and is let go afterwards.
219        //
220        //  fHeadNode
221        //      This is the head node of our intermediate representation. It is
222        //      only non-null during the building of the DFA (just so that it
223        //      does not have to be passed all around.) Once the DFA is built,
224        //      this is no longer required so its deleted.
225        //
226        //  fLeafCount
227        //      The count of leaf nodes. This is an important number that set some
228        //      limits on the sizes of data structures in the DFA process.
229        //
230        //  fLeafList
231        //      An array of non-epsilon leaf nodes, which is used during the DFA
232        //      build operation, then dropped. These are just references to nodes
233        //      pointed to by fHeadNode, so we don't have to clean them up, just
234        //      the actually leaf list array itself needs cleanup.
235        //
236        //  fLeafListType
237        //      Array mapping ANY types to the leaf list.
238        //
239        //  fTransTable
240        //  fTransTableSize
241        //      This is the transition table that is the main by product of all
242        //      of the effort here. It is an array of arrays of ints. The first
243        //      dimension is the number of states we end up with in the DFA. The
244        //      second dimensions is the number of unique elements in the content
245        //      model (fElemMapSize). Each entry in the second dimension indicates
246        //      the new state given that input for the first dimension's start
247        //      state.
248        //
249        //      The fElemMap array handles mapping from element indexes to
250        //      positions in the second dimension of the transition table.
251        //
252        //      fTransTableSize is the number of valid entries in the transition
253        //      table, and in the other related tables such as fFinalStateFlags.
254        //
255        //  fCountingStates
256        //      This is the table holding the minOccurs/maxOccurs for elements
257        //      that can be repeated a finite number of times.
258        //
259        //  fDTD
260        //      Boolean to allow DTDs to validate even with namespace support.
261        //
262        //  fIsMixed
263        //      DFA ContentModel with mixed PCDATA.
264        // -----------------------------------------------------------------------
265        QName**                     fElemMap;
266        ContentSpecNode::NodeTypes* fElemMapType;
267        unsigned int                fElemMapSize;
268        bool                        fEmptyOk;
269        unsigned int                fEOCPos;
270        bool*                       fFinalStateFlags;
271        CMStateSet**                fFollowList;
272        CMNode*                     fHeadNode;
273        unsigned int                fLeafCount;
274        CMLeaf**                    fLeafList;
275        ContentSpecNode::NodeTypes* fLeafListType;
276        unsigned int**              fTransTable;
277        unsigned int                fTransTableSize;
278        Occurence**                 fCountingStates;
279        bool                        fDTD;
280        bool                        fIsMixed;
281        ContentLeafNameTypeVector * fLeafNameTypeVector;
282    XMLStringPool *             fStringPool;
283        MemoryManager*              fMemoryManager;
284};
285
286
287inline unsigned int
288DFAContentModel::getNextState(unsigned int currentState,
289                                                          XMLSize_t    elementIndex) const {
290
291        if (currentState == XMLContentModel::gInvalidTrans) {
292                return XMLContentModel::gInvalidTrans;
293        }
294
295        if (currentState >= fTransTableSize || elementIndex >= fElemMapSize) {
296                ThrowXMLwithMemMgr(ArrayIndexOutOfBoundsException, XMLExcepts::Array_BadIndex, fMemoryManager);
297        }
298
299        return fTransTable[currentState][elementIndex];
300}
301
302inline
303DFAContentModel::Occurence::Occurence(int minOcc, int maxOcc, int eltIndex)
304{
305        minOccurs = minOcc;
306        maxOccurs = maxOcc;
307        elemIndex = eltIndex;
308}
309
310XERCES_CPP_NAMESPACE_END
311
312#endif
313
Note: See TracBrowser for help on using the repository browser.