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: SimpleContentModel.hpp 901107 2010-01-20 08:45:02Z borisk $
20  */
21 
22 #if !defined(XERCESC_INCLUDE_GUARD_SIMPLECONTENTMODEL_HPP)
23 #define XERCESC_INCLUDE_GUARD_SIMPLECONTENTMODEL_HPP
24 
25 #include <xercesc/framework/XMLContentModel.hpp>
26 #include <xercesc/validators/common/ContentSpecNode.hpp>
27 
28 XERCES_CPP_NAMESPACE_BEGIN
29 
30 //
31 //  SimpleContentModel is a derivative of the abstract content model base
32 //  class that handles a small set of simple content models that are just
33 //  way overkill to give the DFA treatment.
34 //
35 //  DESCRIPTION:
36 //
37 //  This guy handles the following scenarios:
38 //
39 //      a
40 //      a?
41 //      a*
42 //      a+
43 //      a,b
44 //      a|b
45 //
46 //  These all involve a unary operation with one element type, or a binary
47 //  operation with two elements. These are very simple and can be checked
48 //  in a simple way without a DFA and without the overhead of setting up a
49 //  DFA for such a simple check.
50 //
51 //  NOTE:   Pass the XMLElementDecl::fgPCDataElemId value to represent a
52 //          PCData node. Pass XMLElementDecl::fgInvalidElemId for unused element
53 //
54 class SimpleContentModel : public XMLContentModel
55 {
56 public :
57     // -----------------------------------------------------------------------
58     //  Constructors and Destructor
59     // -----------------------------------------------------------------------
60     SimpleContentModel
61     (
62         const bool                        dtd
63       , QName* const                      firstChild
64       , QName* const                      secondChild
65       , const ContentSpecNode::NodeTypes  cmOp
66       , MemoryManager* const              manager = XMLPlatformUtils::fgMemoryManager
67     );
68 
69     ~SimpleContentModel();
70 
71 
72     // -----------------------------------------------------------------------
73     //  Implementation of the ContentModel virtual interface
74     // -----------------------------------------------------------------------
75     virtual bool validateContent
76     (
77         QName** const         children
78       , XMLSize_t             childCount
79       , unsigned int          emptyNamespaceId
80       , XMLSize_t*            indexFailingChild
81       , MemoryManager*  const manager = XMLPlatformUtils::fgMemoryManager
82     ) const;
83 
84     virtual bool validateContentSpecial
85     (
86         QName** const           children
87       , XMLSize_t               childCount
88       , unsigned int            emptyNamespaceId
89       , GrammarResolver*  const pGrammarResolver
90       , XMLStringPool*    const pStringPool
91       , XMLSize_t*              indexFailingChild
92       , MemoryManager*    const manager = XMLPlatformUtils::fgMemoryManager
93     ) const;
94 
95     virtual ContentLeafNameTypeVector *getContentLeafNameTypeVector() const;
96 
97     virtual unsigned int getNextState(unsigned int currentState,
98                                       XMLSize_t    elementIndex) const;
99 
100     virtual bool handleRepetitions( const QName* const curElem,
101                                     unsigned int curState,
102                                     unsigned int currentLoop,
103                                     unsigned int& nextState,
104                                     unsigned int& nextLoop,
105                                     XMLSize_t elementIndex,
106                                     SubstitutionGroupComparator * comparator) const;
107 
108     virtual void checkUniqueParticleAttribution
109     (
110         SchemaGrammar*    const pGrammar
111       , GrammarResolver*  const pGrammarResolver
112       , XMLStringPool*    const pStringPool
113       , XMLValidator*     const pValidator
114       , unsigned int*     const pContentSpecOrgURI
115       , const XMLCh*            pComplexTypeName = 0
116     ) ;
117 
118  private :
119     // -----------------------------------------------------------------------
120     //  Unimplemented constructors and operators
121     // -----------------------------------------------------------------------
122     SimpleContentModel();
123     SimpleContentModel(const SimpleContentModel&);
124     SimpleContentModel& operator=(const SimpleContentModel&);
125 
126 
127     // -----------------------------------------------------------------------
128     //  Private data members
129     //
130     //  fFirstChild
131     //  fSecondChild
132     //      The first (and optional second) child node. The
133     //      operation code tells us whether the second child is used or not.
134     //
135     //  fOp
136     //      The operation that this object represents. Since this class only
137     //      does simple contents, there is only ever a single operation
138     //      involved (i.e. the children of the operation are always one or
139     //      two leafs.)
140     //
141     //  fDTD
142     //      Boolean to allow DTDs to validate even with namespace support. */
143     //
144     // -----------------------------------------------------------------------
145     QName*                     fFirstChild;
146     QName*                     fSecondChild;
147     ContentSpecNode::NodeTypes fOp;
148     bool                       fDTD;
149     MemoryManager* const       fMemoryManager;
150 };
151 
152 
153 // ---------------------------------------------------------------------------
154 //  SimpleContentModel: Constructors and Destructor
155 // ---------------------------------------------------------------------------
SimpleContentModel(const bool dtd,QName * const firstChild,QName * const secondChild,const ContentSpecNode::NodeTypes cmOp,MemoryManager * const manager)156 inline SimpleContentModel::SimpleContentModel
157 (
158       const bool                       dtd
159     , QName* const                     firstChild
160     , QName* const                     secondChild
161     , const ContentSpecNode::NodeTypes cmOp
162      , MemoryManager* const            manager
163 )
164     : fFirstChild(0)
165     , fSecondChild(0)
166     , fOp(cmOp)
167     , fDTD(dtd)
168     , fMemoryManager(manager)
169 {
170     if (firstChild)
171         fFirstChild = new (manager) QName(*firstChild);
172     else
173         fFirstChild = new (manager) QName(XMLUni::fgZeroLenString, XMLUni::fgZeroLenString, XMLElementDecl::fgInvalidElemId, manager);
174 
175     if (secondChild)
176         fSecondChild = new (manager) QName(*secondChild);
177     else
178         fSecondChild = new (manager) QName(XMLUni::fgZeroLenString, XMLUni::fgZeroLenString, XMLElementDecl::fgInvalidElemId, manager);
179 }
180 
~SimpleContentModel()181 inline SimpleContentModel::~SimpleContentModel()
182 {
183     delete fFirstChild;
184     delete fSecondChild;
185 }
186 
187 
188 // ---------------------------------------------------------------------------
189 //  SimpleContentModel: Virtual methods
190 // ---------------------------------------------------------------------------
191 inline unsigned int
getNextState(unsigned int,XMLSize_t) const192 SimpleContentModel::getNextState(unsigned int,
193                                  XMLSize_t) const {
194 
195     return XMLContentModel::gInvalidTrans;
196 }
197 
198 inline bool
handleRepetitions(const QName * const,unsigned int,unsigned int,unsigned int &,unsigned int &,XMLSize_t,SubstitutionGroupComparator *) const199 SimpleContentModel::handleRepetitions( const QName* const /*curElem*/,
200                                        unsigned int /*curState*/,
201                                        unsigned int /*currentLoop*/,
202                                        unsigned int& /*nextState*/,
203                                        unsigned int& /*nextLoop*/,
204                                        XMLSize_t /*elementIndex*/,
205                                        SubstitutionGroupComparator * /*comparator*/) const
206 {
207     return true;
208 }
209 
210 XERCES_CPP_NAMESPACE_END
211 
212 #endif
213