bool XsdParticleChecker::isUPAConformXsdAll(const XsdParticle::Ptr &particle, const NamePool::Ptr &namePool) { /** * see http://www.w3.org/TR/xmlschema-1/#non-ambig */ const XsdModelGroup::Ptr group(particle->term()); const XsdParticle::List particles = group->particles(); const int count = particles.count(); for (int left = 0; left < count; ++left) { for (int right = left+1; right < count; ++right) { if (termMatches(particles.at(left)->term(), particles.at(right)->term(), namePool)) return false; } } return true; }
/** * Internal helper method that checks if the given @p particle contains an element with the * same name and type twice. */ static bool hasDuplicatedElementsInternal(const XsdParticle::Ptr &particle, const NamePool::Ptr &namePool, ElementHash &hash, XsdElement::Ptr &conflictingElement) { const XsdTerm::Ptr term = particle->term(); if (term->isElement()) { const XsdElement::Ptr mainElement(term); XsdElement::WeakList substGroups = mainElement->substitutionGroups(); if (substGroups.isEmpty()) substGroups << mainElement.data(); for (int i = 0; i < substGroups.count(); ++i) { const XsdElement::Ptr element(substGroups.at(i)); if (hash.contains(element->name(namePool))) { if (element->type()->name(namePool) != hash.value(element->name(namePool))->type()->name(namePool)) { conflictingElement = element; return true; } } else { hash.insert(element->name(namePool), element); } } } else if (term->isModelGroup()) { const XsdModelGroup::Ptr group(term); const XsdParticle::List particles = group->particles(); for (int i = 0; i < particles.count(); ++i) { if (hasDuplicatedElementsInternal(particles.at(i), namePool, hash, conflictingElement)) return true; } } return false; }
static void internalParticleLookupMap(const XsdParticle::Ptr &particle, QHash<XsdTerm::Ptr, XsdParticle::Ptr> &hash) { hash.insert(particle->term(), particle); if (particle->term()->isModelGroup()) { const XsdModelGroup::Ptr group(particle->term()); const XsdParticle::List particles = group->particles(); for (int i = 0; i < particles.count(); ++i) internalParticleLookupMap(particles.at(i), hash); } }
bool XsdSchemaChecker::isValidParticleExtension(const XsdParticle::Ptr &extension, const XsdParticle::Ptr &base) const { // @see http://www.w3.org/TR/xmlschema11-1/#cos-particle-extend // 1 if (extension == base) return true; // 2 if (extension->minimumOccurs() == 1 && extension->maximumOccurs() == 1 && extension->maximumOccursUnbounded() == false) { if (extension->term()->isModelGroup()) { const XsdModelGroup::Ptr modelGroup = extension->term(); if (modelGroup->compositor() == XsdModelGroup::SequenceCompositor) { if (particleEqualsRecursively(modelGroup->particles().first(), base)) return true; } } } // 3 if (extension->minimumOccurs() == base->minimumOccurs()) { // 3.1 if (extension->term()->isModelGroup() && base->term()->isModelGroup()) { const XsdModelGroup::Ptr extensionGroup(extension->term()); const XsdModelGroup::Ptr baseGroup(base->term()); if (extensionGroup->compositor() == XsdModelGroup::AllCompositor && baseGroup->compositor() == XsdModelGroup::AllCompositor) { const XsdParticle::List extensionParticles = extensionGroup->particles(); const XsdParticle::List baseParticles = baseGroup->particles(); for (int i = 0; i < baseParticles.count() && i < extensionParticles.count(); ++i) { if (baseParticles.at(i) != extensionParticles.at(i)) return false; } } } } return false; }
/* * Create the FSA according to Algorithm Tt(S) from http://www.ltg.ed.ac.uk/~ht/XML_Europe_2003.html */ XsdStateMachine<XsdTerm::Ptr>::StateId XsdStateMachineBuilder::buildTerm(const XsdTerm::Ptr &term, XsdStateMachine<XsdTerm::Ptr>::StateId endState) { if (term->isWildcard()) { // 1 const XsdStateMachine<XsdTerm::Ptr>::StateId b = m_stateMachine->addState(XsdStateMachine<XsdTerm::Ptr>::InternalState); m_stateMachine->addTransition(b, term, endState); return b; } else if (term->isElement()) { // 2 const XsdStateMachine<XsdTerm::Ptr>::StateId b = m_stateMachine->addState(XsdStateMachine<XsdTerm::Ptr>::InternalState); m_stateMachine->addTransition(b, term, endState); const XsdElement::Ptr element(term); if (m_mode == CheckingMode) { const XsdElement::WeakList substGroups = element->substitutionGroups(); for (int i = 0; i < substGroups.count(); ++i) m_stateMachine->addTransition(b, XsdElement::Ptr(substGroups.at(i)), endState); } else if (m_mode == ValidatingMode) { const XsdElement::WeakList substGroups = element->substitutionGroups(); for (int i = 0; i < substGroups.count(); ++i) { if (XsdSchemaHelper::substitutionGroupOkTransitive(element, XsdElement::Ptr(substGroups.at(i)), m_namePool)) m_stateMachine->addTransition(b, XsdElement::Ptr(substGroups.at(i)), endState); } } return b; } else if (term->isModelGroup()) { const XsdModelGroup::Ptr group(term); if (group->compositor() == XsdModelGroup::ChoiceCompositor) { // 3 const XsdStateMachine<XsdTerm::Ptr>::StateId b = m_stateMachine->addState(XsdStateMachine<XsdTerm::Ptr>::InternalState); for (int i = 0; i < group->particles().count(); ++i) { const XsdParticle::Ptr particle(group->particles().at(i)); if (particle->maximumOccurs() != 0) { const XsdStateMachine<XsdTerm::Ptr>::StateId state = buildParticle(particle, endState); m_stateMachine->addEpsilonTransition(b, state); } } return b; } else if (group->compositor() == XsdModelGroup::SequenceCompositor) { // 4 XsdStateMachine<XsdTerm::Ptr>::StateId currentStartState = endState; XsdStateMachine<XsdTerm::Ptr>::StateId currentEndState = endState; for (int i = (group->particles().count() - 1); i >= 0; --i) { // iterate reverse const XsdParticle::Ptr particle(group->particles().at(i)); if (particle->maximumOccurs() != 0) { currentStartState = buildParticle(particle, currentEndState); currentEndState = currentStartState; } } return currentStartState; } else if (group->compositor() == XsdModelGroup::AllCompositor) { const XsdStateMachine<XsdTerm::Ptr>::StateId newStartState = m_stateMachine->addState(XsdStateMachine<XsdTerm::Ptr>::InternalState); const QList<XsdParticle::List> list = allCombinations(group->particles()); for (int i = 0; i < list.count(); ++i) { XsdStateMachine<XsdTerm::Ptr>::StateId currentStartState = endState; XsdStateMachine<XsdTerm::Ptr>::StateId currentEndState = endState; const XsdParticle::List particles = list.at(i); for (int j = (particles.count() - 1); j >= 0; --j) { // iterate reverse const XsdParticle::Ptr particle(particles.at(j)); if (particle->maximumOccurs() != 0) { currentStartState = buildParticle(particle, currentEndState); currentEndState = currentStartState; } } m_stateMachine->addEpsilonTransition(newStartState, currentStartState); } if (list.isEmpty()) return endState; else return newStartState; } } Q_ASSERT(false); return 0; }
bool XsdSchemaChecker::elementSequenceAccepted(const XsdModelGroup::Ptr &sequence, const XsdParticle::Ptr &particle) const { // @see http://www.w3.org/TR/xmlschema11-1/#cvc-accept if (particle->term()->isWildcard()) { // 1 const XsdWildcard::Ptr wildcard(particle->term()); // 1.1 if ((unsigned int)sequence->particles().count() < particle->minimumOccurs()) return false; // 1.2 if (!particle->maximumOccursUnbounded()) { if ((unsigned int)sequence->particles().count() > particle->maximumOccurs()) return false; } // 1.3 const XsdParticle::List particles(sequence->particles()); for (int i = 0; i < particles.count(); ++i) { if (particles.at(i)->term()->isElement()) { if (!XsdSchemaHelper::wildcardAllowsExpandedName(XsdElement::Ptr(particles.at(i)->term())->name(m_namePool), wildcard, m_namePool)) return false; } } } else if (particle->term()->isElement()) { // 2 const XsdElement::Ptr element(particle->term()); // 2.1 if ((unsigned int)sequence->particles().count() < particle->minimumOccurs()) return false; // 2.2 if (!particle->maximumOccursUnbounded()) { if ((unsigned int)sequence->particles().count() > particle->maximumOccurs()) return false; } // 2.3 const XsdParticle::List particles(sequence->particles()); for (int i = 0; i < particles.count(); ++i) { bool isValid = false; if (particles.at(i)->term()->isElement()) { const XsdElement::Ptr seqElement(particles.at(i)->term()); // 2.3.1 if (element->name(m_namePool) == seqElement->name(m_namePool)) isValid = true; // 2.3.2 if (element->scope() && element->scope()->variety() == XsdElement::Scope::Global) { if (!(element->disallowedSubstitutions() & NamedSchemaComponent::SubstitutionConstraint)) { //TODO: continue } } } } } return true; }