File indexing completed on 2024-04-14 05:44:22

0001 /*
0002  *  SPDX-FileCopyrightText: 2002-2003 Jesper K. Pedersen <blackie@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-only
0005  **/
0006 
0007 #include "altnregexp.h"
0008 #include "widgetfactory.h"
0009 
0010 AltnRegExp::AltnRegExp(bool selected)
0011     : RegExp(selected)
0012 {
0013     // Nothing to do
0014 }
0015 
0016 void AltnRegExp::addRegExp(RegExp *elm)
0017 {
0018     list.append(elm);
0019     addChild(elm);
0020 }
0021 
0022 RegExpList AltnRegExp::children() const
0023 {
0024     return list;
0025 }
0026 
0027 bool AltnRegExp::check(ErrorMap &map, bool first, bool last)
0028 {
0029     bool possibleEmpty = false;
0030     for (RegExp *r : std::as_const(list)) {
0031         possibleEmpty = r->check(map, first, last) || possibleEmpty;
0032     }
0033     return possibleEmpty;
0034 }
0035 
0036 QDomNode AltnRegExp::toXml(QDomDocument *doc) const
0037 {
0038     QDomElement top = doc->createElement(QStringLiteral("Alternatives"));
0039     for (RegExp *r : std::as_const(list)) {
0040         top.appendChild(r->toXml(doc));
0041     }
0042     return top;
0043 }
0044 
0045 bool AltnRegExp::load(const QDomElement &top, const QString &version)
0046 {
0047     Q_ASSERT(top.tagName() == QStringLiteral("Alternatives"));
0048 
0049     for (QDomNode child = top.firstChild(); !child.isNull(); child = child.nextSibling()) {
0050         if (!child.isElement()) {
0051             continue; // User might have added a comment.
0052         }
0053 
0054         RegExp *regexp = WidgetFactory::createRegExp(child.toElement(), version);
0055         if (regexp == nullptr) {
0056             return false;
0057         }
0058         addRegExp(regexp);
0059     }
0060     return true;
0061 }
0062 
0063 bool AltnRegExp::operator==(const RegExp &other) const
0064 {
0065     // TODO: merge with ConcRegExp::operator==
0066 
0067     if (other.type() != type()) {
0068         return false;
0069     }
0070 
0071     const AltnRegExp &theOther = dynamic_cast<const AltnRegExp &>(other);
0072 
0073     if (list.count() != theOther.list.count()) {
0074         return false;
0075     }
0076 
0077     RegExpListIt it1(list);
0078     RegExpListIt it2(theOther.list);
0079 
0080     while (it1.hasNext() && it2.hasNext()) {
0081         if (!(it1.next() == it2.next())) {
0082             return false;
0083         }
0084     }
0085     return true;
0086 }
0087 
0088 void AltnRegExp::replacePart(CompoundRegExp *replacement)
0089 {
0090     for (RegExp *r : std::as_const(list)) {
0091         r->replacePart(replacement);
0092     }
0093 }