1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qxsdstatemachinebuilder_p.h"
44 #include "qxsdelement_p.h"
45 #include "qxsdmodelgroup_p.h"
46 #include "qxsdschemahelper_p.h"
50 using namespace QPatternist;
53 * This methods takes a list of objects and returns a list of list
54 * of all combinations the objects can be ordered.
56 * e.g. input = [ 1, 2, 3 ]
66 * The method is used to create all possible combinations for the particles
67 * in an <all> model group.
70 QList< QList<T> > allCombinations(const QList<T> &input)
72 if (input.count() == 1)
73 return (QList< QList<T> >() << input);
75 QList< QList<T> > result;
76 for (int i = 0; i < input.count(); ++i) {
77 QList<T> subList = input;
78 T value = subList.takeAt(i);
80 QList< QList<T> > subLists = allCombinations(subList);
81 for (int j = 0; j < subLists.count(); ++j) {
82 subLists[j].prepend(value);
90 XsdStateMachineBuilder::XsdStateMachineBuilder(XsdStateMachine<XsdTerm::Ptr> *machine, const NamePool::Ptr &namePool, Mode mode)
91 : m_stateMachine(machine), m_namePool(namePool), m_mode(mode)
95 XsdStateMachine<XsdTerm::Ptr>::StateId XsdStateMachineBuilder::reset()
97 Q_ASSERT(m_stateMachine);
99 m_stateMachine->clear();
101 return m_stateMachine->addState(XsdStateMachine<XsdTerm::Ptr>::EndState);
104 XsdStateMachine<XsdTerm::Ptr>::StateId XsdStateMachineBuilder::addStartState(XsdStateMachine<XsdTerm::Ptr>::StateId state)
106 const XsdStateMachine<XsdTerm::Ptr>::StateId startState = m_stateMachine->addState(XsdStateMachine<XsdTerm::Ptr>::StartState);
107 m_stateMachine->addEpsilonTransition(startState, state);
113 * Create the FSA according to Algorithm Tp(S) from http://www.ltg.ed.ac.uk/~ht/XML_Europe_2003.html
115 XsdStateMachine<XsdTerm::Ptr>::StateId XsdStateMachineBuilder::buildParticle(const XsdParticle::Ptr &particle, XsdStateMachine<XsdTerm::Ptr>::StateId endState)
117 XsdStateMachine<XsdTerm::Ptr>::StateId currentStartState = endState;
118 XsdStateMachine<XsdTerm::Ptr>::StateId currentEndState = endState;
121 if (particle->maximumOccursUnbounded()) {
122 const XsdStateMachine<XsdTerm::Ptr>::StateId t = m_stateMachine->addState(XsdStateMachine<XsdTerm::Ptr>::InternalState);
123 const XsdStateMachine<XsdTerm::Ptr>::StateId n = buildTerm(particle->term(), t);
125 m_stateMachine->addEpsilonTransition(t, n);
126 m_stateMachine->addEpsilonTransition(n, endState);
129 currentStartState = t;
131 int count = (particle->maximumOccurs() - particle->minimumOccurs());
135 for (int i = 0; i < count; ++i) {
136 currentStartState = buildTerm(particle->term(), currentEndState);
137 m_stateMachine->addEpsilonTransition(currentStartState, endState);
138 currentEndState = currentStartState;
142 int minOccurs = particle->minimumOccurs();
146 for (int i = 0; i < minOccurs; ++i) {
147 currentStartState = buildTerm(particle->term(), currentEndState);
148 currentEndState = currentStartState;
151 return currentStartState;
155 * Create the FSA according to Algorithm Tt(S) from http://www.ltg.ed.ac.uk/~ht/XML_Europe_2003.html
157 XsdStateMachine<XsdTerm::Ptr>::StateId XsdStateMachineBuilder::buildTerm(const XsdTerm::Ptr &term, XsdStateMachine<XsdTerm::Ptr>::StateId endState)
159 if (term->isWildcard()) { // 1
160 const XsdStateMachine<XsdTerm::Ptr>::StateId b = m_stateMachine->addState(XsdStateMachine<XsdTerm::Ptr>::InternalState);
161 m_stateMachine->addTransition(b, term, endState);
163 } else if (term->isElement()) { // 2
164 const XsdStateMachine<XsdTerm::Ptr>::StateId b = m_stateMachine->addState(XsdStateMachine<XsdTerm::Ptr>::InternalState);
165 m_stateMachine->addTransition(b, term, endState);
167 const XsdElement::Ptr element(term);
168 if (m_mode == CheckingMode) {
169 const XsdElement::List substGroups = element->substitutionGroups();
170 for (int i = 0; i < substGroups.count(); ++i)
171 m_stateMachine->addTransition(b, substGroups.at(i), endState);
172 } else if (m_mode == ValidatingMode) {
173 const XsdElement::List substGroups = element->substitutionGroups();
174 for (int i = 0; i < substGroups.count(); ++i) {
175 if (XsdSchemaHelper::substitutionGroupOkTransitive(element, substGroups.at(i), m_namePool))
176 m_stateMachine->addTransition(b, substGroups.at(i), endState);
181 } else if (term->isModelGroup()) {
182 const XsdModelGroup::Ptr group(term);
184 if (group->compositor() == XsdModelGroup::ChoiceCompositor) { // 3
185 const XsdStateMachine<XsdTerm::Ptr>::StateId b = m_stateMachine->addState(XsdStateMachine<XsdTerm::Ptr>::InternalState);
187 for (int i = 0; i < group->particles().count(); ++i) {
188 const XsdParticle::Ptr particle(group->particles().at(i));
189 if (particle->maximumOccurs() != 0) {
190 const XsdStateMachine<XsdTerm::Ptr>::StateId state = buildParticle(particle, endState);
191 m_stateMachine->addEpsilonTransition(b, state);
196 } else if (group->compositor() == XsdModelGroup::SequenceCompositor) { // 4
197 XsdStateMachine<XsdTerm::Ptr>::StateId currentStartState = endState;
198 XsdStateMachine<XsdTerm::Ptr>::StateId currentEndState = endState;
200 for (int i = (group->particles().count() - 1); i >= 0; --i) { // iterate reverse
201 const XsdParticle::Ptr particle(group->particles().at(i));
202 if (particle->maximumOccurs() != 0) {
203 currentStartState = buildParticle(particle, currentEndState);
204 currentEndState = currentStartState;
208 return currentStartState;
209 } else if (group->compositor() == XsdModelGroup::AllCompositor) {
210 const XsdStateMachine<XsdTerm::Ptr>::StateId newStartState = m_stateMachine->addState(XsdStateMachine<XsdTerm::Ptr>::InternalState);
212 const QList<XsdParticle::List> list = allCombinations(group->particles());
214 for (int i = 0; i < list.count(); ++i) {
215 XsdStateMachine<XsdTerm::Ptr>::StateId currentStartState = endState;
216 XsdStateMachine<XsdTerm::Ptr>::StateId currentEndState = endState;
218 const XsdParticle::List particles = list.at(i);
219 for (int j = (particles.count() - 1); j >= 0; --j) { // iterate reverse
220 const XsdParticle::Ptr particle(particles.at(j));
221 if (particle->maximumOccurs() != 0) {
222 currentStartState = buildParticle(particle, currentEndState);
223 currentEndState = currentStartState;
226 m_stateMachine->addEpsilonTransition(newStartState, currentStartState);
232 return newStartState;
240 static void internalParticleLookupMap(const XsdParticle::Ptr &particle, QHash<XsdTerm::Ptr, XsdParticle::Ptr> &hash)
242 hash.insert(particle->term(), particle);
244 if (particle->term()->isModelGroup()) {
245 const XsdModelGroup::Ptr group(particle->term());
246 const XsdParticle::List particles = group->particles();
247 for (int i = 0; i < particles.count(); ++i)
248 internalParticleLookupMap(particles.at(i), hash);
252 QHash<XsdTerm::Ptr, XsdParticle::Ptr> XsdStateMachineBuilder::particleLookupMap(const XsdParticle::Ptr &particle)
254 QHash<XsdTerm::Ptr, XsdParticle::Ptr> result;
255 internalParticleLookupMap(particle, result);