/**
 * 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;
}
    QString XsdStateMachine<XsdTerm::Ptr>::transitionTypeToString(XsdTerm::Ptr term) const
    {
        if (!term)
            return QLatin1String("(empty)");

        if (term->isElement()) {
            return XsdElement::Ptr(term)->displayName(m_namePool);
        } else if (term->isWildcard()) {
            const XsdWildcard::Ptr wildcard(term);
            return QLatin1String("(wildcard)");
        } else {
            return QString();
        }
    }
QSet<XsdElement::Ptr> collectAllElements(const XsdParticle::Ptr &particle)
{
    QSet<XsdElement::Ptr> elements;

    const XsdTerm::Ptr term(particle->term());
    if (term->isElement()) {
        elements.insert(XsdElement::Ptr(term));
    } else if (term->isModelGroup()) {
        const XsdModelGroup::Ptr group(term);

        for (int i = 0; i < group->particles().count(); ++i)
            elements.unite(collectAllElements(group->particles().at(i)));
    }

    return elements;
}
/**
 * This method is used by the isUPAConform method to check whether @p term and @p otherTerm
 * are the same resp. match each other.
 */
static bool termMatches(const XsdTerm::Ptr &term, const XsdTerm::Ptr &otherTerm, const NamePool::Ptr &namePool)
{
    if (term->isElement()) {
        const XsdElement::Ptr element(term);

        if (otherTerm->isElement()) {
            // both, the term and the other term are elements

            const XsdElement::Ptr otherElement(otherTerm);

            // if they have the same name they match
            if (element->name(namePool) == otherElement->name(namePool))
                return true;

        } else if (otherTerm->isWildcard()) {
            // the term is an element and the other term a wildcard

            const XsdWildcard::Ptr wildcard(otherTerm);

            // wildcards using XsdWildcard::absentNamespace, so we have to fix that here
            QXmlName name = element->name(namePool);
            if (name.namespaceURI() == StandardNamespaces::empty)
                name.setNamespaceURI(namePool->allocateNamespace(XsdWildcard::absentNamespace()));

            // if the wildcards namespace constraint allows the elements name, they match
            if (XsdSchemaHelper::wildcardAllowsExpandedName(name, wildcard, namePool))
                return true;
        }
    } else if (term->isWildcard()) {
        const XsdWildcard::Ptr wildcard(term);

        if (otherTerm->isElement()) {
            // the term is a wildcard and the other term an element

            const XsdElement::Ptr otherElement(otherTerm);

            // wildcards using XsdWildcard::absentNamespace, so we have to fix that here
            QXmlName name = otherElement->name(namePool);
            if (name.namespaceURI() == StandardNamespaces::empty)
                name.setNamespaceURI(namePool->allocateNamespace(XsdWildcard::absentNamespace()));

            // if the wildcards namespace constraint allows the elements name, they match
            if (XsdSchemaHelper::wildcardAllowsExpandedName(name, wildcard, namePool))
                return true;

        } else if (otherTerm->isWildcard()) {
            // both, the term and the other term are wildcards

            const XsdWildcard::Ptr otherWildcard(otherTerm);

            // check if the range of the wildcard overlaps.
            const XsdWildcard::Ptr intersectionWildcard = XsdSchemaHelper::wildcardIntersection(wildcard, otherWildcard);
            if (!intersectionWildcard ||
                (intersectionWildcard && !(intersectionWildcard->namespaceConstraint()->variety() != XsdWildcard::NamespaceConstraint::Not && intersectionWildcard->namespaceConstraint()->namespaces().isEmpty())))
                return true;
        }
    }

    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;
}
/**
 * This method is used by the subsumes algorithm to check whether the @p derivedTerm is validly derived from the @p baseTerm.
 *
 * @param baseTerm The term of the base component (type or group).
 * @param derivedTerm The term of the derived component (type or group).
 * @param particles A hash to map the passed base and derived term to the particles they belong to.
 * @param context The schema context.
 * @param errorMsg The error message in the case that an error occurs.
 */
static bool derivedTermValid(const XsdTerm::Ptr &baseTerm, const XsdTerm::Ptr &derivedTerm, const QHash<XsdTerm::Ptr, XsdParticle::Ptr> &particles, const XsdSchemaContext::Ptr &context, QString &errorMsg)
{
    const NamePool::Ptr namePool(context->namePool());

    // find the particles where the base and derived term belongs to
    const XsdParticle::Ptr baseParticle = particles.value(baseTerm);
    const XsdParticle::Ptr derivedParticle = particles.value(derivedTerm);

    // check that an empty particle can not be derived from a non-empty particle
    if (derivedParticle && baseParticle) {
        if (XsdSchemaHelper::isParticleEmptiable(derivedParticle) && !XsdSchemaHelper::isParticleEmptiable(baseParticle)) {
            errorMsg = QtXmlPatterns::tr("Empty particle cannot be derived from non-empty particle.");
            return false;
        }
    }

    if (baseTerm->isElement()) {
        const XsdElement::Ptr element(baseTerm);

        if (derivedTerm->isElement()) {
            // if both terms are elements

            const XsdElement::Ptr derivedElement(derivedTerm);

            // check names are equal
            if (element->name(namePool) != derivedElement->name(namePool)) {
                errorMsg = QtXmlPatterns::tr("Derived particle is missing element %1.").arg(formatKeyword(element->displayName(namePool)));
                return false;
            }

            // check value constraints are equal (if available)
            if (element->valueConstraint() && element->valueConstraint()->variety() == XsdElement::ValueConstraint::Fixed) {
                if (!derivedElement->valueConstraint()) {
                    errorMsg = QtXmlPatterns::tr("Derived element %1 is missing value constraint as defined in base particle.").arg(formatKeyword(derivedElement->displayName(namePool)));
                    return false;
                }

                if (derivedElement->valueConstraint()->variety() != XsdElement::ValueConstraint::Fixed) {
                    errorMsg = QtXmlPatterns::tr("Derived element %1 has weaker value constraint than base particle.").arg(formatKeyword(derivedElement->displayName(namePool)));
                    return false;
                }

                const QSourceLocation dummyLocation(QUrl(QLatin1String("http://dummy.org")), 1, 1);
                const XsdTypeChecker checker(context, QVector<QXmlName>(), dummyLocation);
                if (!checker.valuesAreEqual(element->valueConstraint()->value(), derivedElement->valueConstraint()->value(), derivedElement->type())) {
                    errorMsg = QtXmlPatterns::tr("Fixed value constraint of element %1 differs from value constraint in base particle.").arg(formatKeyword(derivedElement->displayName(namePool)));
                    return false;
                }
            }

            // check that a derived element can not be nillable if the base element is not nillable
            if (!element->isNillable() && derivedElement->isNillable()) {
                errorMsg = QtXmlPatterns::tr("Derived element %1 cannot be nillable as base element is not nillable.").arg(formatKeyword(derivedElement->displayName(namePool)));
                return false;
            }

            // check that the constraints of the derived element are more strict then the constraints of the base element
            const XsdElement::BlockingConstraints baseConstraints = element->disallowedSubstitutions();
            const XsdElement::BlockingConstraints derivedConstraints = derivedElement->disallowedSubstitutions();
            if (((baseConstraints & XsdElement::RestrictionConstraint) && !(derivedConstraints & XsdElement::RestrictionConstraint)) ||
                ((baseConstraints & XsdElement::ExtensionConstraint) && !(derivedConstraints & XsdElement::ExtensionConstraint)) ||
                ((baseConstraints & XsdElement::SubstitutionConstraint) && !(derivedConstraints & XsdElement::SubstitutionConstraint))) {
                errorMsg = QtXmlPatterns::tr("Block constraints of derived element %1 must not be more weaker than in the base element.").arg(formatKeyword(derivedElement->displayName(namePool)));
                return false;
            }

            // if the type of both elements is the same we can stop testing here
            if (element->type()->name(namePool) == derivedElement->type()->name(namePool))
                return true;

            // check that the type of the derived element can validly derived from the type of the base element
            if (derivedElement->type()->isSimpleType()) {
                if (!XsdSchemaHelper::isSimpleDerivationOk(derivedElement->type(), element->type(), SchemaType::DerivationConstraints())) {
                    errorMsg = QtXmlPatterns::tr("Simple type of derived element %1 cannot be validly derived from base element.").arg(formatKeyword(derivedElement->displayName(namePool)));
                    return false;
                }
            } else if (derivedElement->type()->isComplexType()) {
                if (!XsdSchemaHelper::isComplexDerivationOk(derivedElement->type(), element->type(), SchemaType::DerivationConstraints())) {
                    errorMsg = QtXmlPatterns::tr("Complex type of derived element %1 cannot be validly derived from base element.").arg(formatKeyword(derivedElement->displayName(namePool)));
                    return false;
                }
            }

            // if both, derived and base element, have a complex type that contains a particle itself, apply the subsumes algorithm
            // recursive on their particles
            if (element->type()->isComplexType() && derivedElement->type()->isComplexType()) {
                if (element->type()->isDefinedBySchema() && derivedElement->type()->isDefinedBySchema()) {
                    const XsdComplexType::Ptr baseType(element->type());
                    const XsdComplexType::Ptr derivedType(derivedElement->type());
                    if ((baseType->contentType()->variety() == XsdComplexType::ContentType::ElementOnly ||
                        baseType->contentType()->variety() == XsdComplexType::ContentType::Mixed) &&
                        (derivedType->contentType()->variety() == XsdComplexType::ContentType::ElementOnly ||
                         derivedType->contentType()->variety() == XsdComplexType::ContentType::Mixed)) {

                        return XsdParticleChecker::subsumes(baseType->contentType()->particle(), derivedType->contentType()->particle(), context, errorMsg);
                    }
                }
            }

            return true;
        } else if (derivedTerm->isWildcard()) {
            // derive a wildcard from an element is not allowed
            errorMsg = QtXmlPatterns::tr("Element %1 is missing in derived particle.").arg(formatKeyword(element->displayName(namePool)));
            return false;
        }
    } else if (baseTerm->isWildcard()) {
        const XsdWildcard::Ptr wildcard(baseTerm);

        if (derivedTerm->isElement()) {
            // the base term is a wildcard and derived term an element

            const XsdElement::Ptr derivedElement(derivedTerm);

            // wildcards using XsdWildcard::absentNamespace, so we have to fix that here
            QXmlName name = derivedElement->name(namePool);
            if (name.namespaceURI() == StandardNamespaces::empty)
                name.setNamespaceURI(namePool->allocateNamespace(XsdWildcard::absentNamespace()));

            // check that name of the element is allowed by the wildcards namespace constraint
            if (!XsdSchemaHelper::wildcardAllowsExpandedName(name, wildcard, namePool)) {
                errorMsg = QtXmlPatterns::tr("Element %1 does not match namespace constraint of wildcard in base particle.").arg(formatKeyword(derivedElement->displayName(namePool)));
                return false;
            }

        } else if (derivedTerm->isWildcard()) {
            // both, derived and base term are wildcards

            const XsdWildcard::Ptr derivedWildcard(derivedTerm);

            // check that the derived wildcard is a valid subset of the base wildcard
            if (!XsdSchemaHelper::isWildcardSubset(derivedWildcard, wildcard)) {
                errorMsg = QtXmlPatterns::tr("Wildcard in derived particle is not a valid subset of wildcard in base particle.");
                return false;
            }

            if (!XsdSchemaHelper::checkWildcardProcessContents(wildcard, derivedWildcard)) {
                errorMsg = QtXmlPatterns::tr("processContent of wildcard in derived particle is weaker than wildcard in base particle.");
                return false;
            }
        }

        return true;
    }

    return false;
}
bool XsdSchemaChecker::particleEqualsRecursively(const XsdParticle::Ptr &particle, const XsdParticle::Ptr &otherParticle) const
{
    // @see http://www.w3.org/TR/xmlschema11-1/#cos-particle-extend
    //TODO: find out what 'properties' of a particle should be checked here...

    if (particle->minimumOccurs() != otherParticle->minimumOccurs())
        return false;

    if (particle->maximumOccursUnbounded() != otherParticle->maximumOccursUnbounded())
        return false;

    if (particle->maximumOccurs() != otherParticle->maximumOccurs())
        return false;

    const XsdTerm::Ptr term = particle->term();
    const XsdTerm::Ptr otherTerm = otherParticle->term();

    if (term->isElement() && !(otherTerm->isElement()))
        return false;

    if (term->isModelGroup() && !(otherTerm->isModelGroup()))
        return false;

    if (term->isWildcard() && !(otherTerm->isWildcard()))
        return false;

    if (term->isElement()) {
        const XsdElement::Ptr element = term;
        const XsdElement::Ptr otherElement = otherTerm;

        if (element->name(m_namePool) != otherElement->name(m_namePool))
            return false;

        if (element->type()->name(m_namePool) != otherElement->type()->name(m_namePool))
            return false;
    }

    if (term->isModelGroup()) {
        const XsdModelGroup::Ptr group = term;
        const XsdModelGroup::Ptr otherGroup = otherTerm;

        if (group->particles().count() != otherGroup->particles().count())
            return false;

        for (int i = 0; i < group->particles().count(); ++i) {
            if (!particleEqualsRecursively(group->particles().at(i), otherGroup->particles().at(i)))
                return false;
        }
    }

    if (term->isWildcard()) {
    }

    return true;
}