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

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 "repeatregexp.h"
0008 
0009 #include <KLocalizedString>
0010 #include <KMessageBox>
0011 
0012 RepeatRegExp::RepeatRegExp(bool selected, int lower, int upper, RegExp *child)
0013     : RegExp(selected)
0014 {
0015     _lower = lower;
0016     _upper = upper;
0017     _child = child;
0018     if (child) {
0019         addChild(child);
0020     }
0021 }
0022 
0023 bool RepeatRegExp::check(ErrorMap &map, bool first, bool last)
0024 {
0025     _child->check(map, first, last);
0026     return _lower == 0;
0027 }
0028 
0029 QDomNode RepeatRegExp::toXml(QDomDocument *doc) const
0030 {
0031     QDomElement top = doc->createElement(QStringLiteral("Repeat"));
0032     top.setAttribute(QStringLiteral("lower"), _lower);
0033     top.setAttribute(QStringLiteral("upper"), _upper);
0034     top.appendChild(_child->toXml(doc));
0035     return top;
0036 }
0037 
0038 bool RepeatRegExp::load(const QDomElement &top, const QString &version)
0039 {
0040     Q_ASSERT(top.tagName() == QStringLiteral("Repeat"));
0041     QString lower = top.attribute(QStringLiteral("lower"), QStringLiteral("0"));
0042     QString upper = top.attribute(QStringLiteral("upper"), QStringLiteral("0"));
0043     bool ok;
0044     _lower = lower.toInt(&ok);
0045     if (!ok) {
0046         KMessageBox::error(nullptr,
0047                            i18n("<p>Value for attribute <b>%1</b> was not an integer for element "
0048                                 "<b>%2</b></p><p>It contained the value <b>%3</b></p>",
0049                                 QStringLiteral("lower"),
0050                                 QStringLiteral("Repeat"),
0051                                 lower),
0052                            i18n("Error While Loading From XML File"));
0053         _lower = 0;
0054     }
0055     _upper = upper.toInt(&ok);
0056     if (!ok) {
0057         KMessageBox::error(nullptr,
0058                            i18n("<p>Value for attribute <b>%1</b> was not an integer for element "
0059                                 "<b>%2</b></p><p>It contained the value <b>%3</b></p>",
0060                                 QStringLiteral("upper"),
0061                                 QStringLiteral("Repeat"),
0062                                 upper),
0063                            i18n("Error While Loading From XML File"));
0064         _upper = -1;
0065     }
0066 
0067     _child = readRegExp(top, version);
0068     if (_child) {
0069         addChild(_child);
0070         return true;
0071     } else {
0072         return false;
0073     }
0074 }
0075 
0076 bool RepeatRegExp::operator==(const RegExp &other) const
0077 {
0078     if (type() != other.type()) {
0079         return false;
0080     }
0081 
0082     const RepeatRegExp &theOther = dynamic_cast<const RepeatRegExp &>(other);
0083     if (_lower != theOther._lower || _upper != theOther._upper) {
0084         return false;
0085     }
0086 
0087     return *_child == *(theOther._child);
0088 }