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 "textrangeregexp.h" 0008 0009 #include <KLocalizedString> 0010 #include <KMessageBox> 0011 0012 #include "regexpconverter.h" 0013 0014 TextRangeRegExp::TextRangeRegExp(bool selected) 0015 : RegExp(selected) 0016 , _negate(false) 0017 , _digit(false) 0018 , _nonDigit(false) 0019 , _space(false) 0020 , _nonSpace(false) 0021 , _wordChar(false) 0022 , _nonWordChar(false) 0023 { 0024 } 0025 0026 TextRangeRegExp::~TextRangeRegExp() 0027 { 0028 } 0029 0030 void TextRangeRegExp::addCharacter(const QString &str) 0031 { 0032 _chars.append(str); 0033 } 0034 0035 void TextRangeRegExp::addRange(const QString &from, const QString &to) 0036 { 0037 _ranges.append(StringPair(from, to)); 0038 } 0039 0040 bool TextRangeRegExp::check(ErrorMap &, bool, bool) 0041 { 0042 return false; 0043 } 0044 0045 QDomNode TextRangeRegExp::toXml(QDomDocument *doc) const 0046 { 0047 QDomElement top = doc->createElement(QStringLiteral("TextRange")); 0048 0049 if (_negate) { 0050 top.setAttribute(QStringLiteral("negate"), true); 0051 } 0052 0053 if (_digit) { 0054 top.setAttribute(QStringLiteral("digit"), true); 0055 } 0056 0057 if (_nonDigit) { 0058 top.setAttribute(QStringLiteral("nonDigit"), true); 0059 } 0060 0061 if (_space) { 0062 top.setAttribute(QStringLiteral("space"), true); 0063 } 0064 0065 if (_nonSpace) { 0066 top.setAttribute(QStringLiteral("nonSpace"), true); 0067 } 0068 0069 if (_wordChar) { 0070 top.setAttribute(QStringLiteral("wordChar"), true); 0071 } 0072 0073 if (_nonWordChar) { 0074 top.setAttribute(QStringLiteral("nonWordChar"), true); 0075 } 0076 0077 for (QStringList::ConstIterator it = _chars.begin(); it != _chars.end(); ++it) { 0078 QDomElement elm = doc->createElement(QStringLiteral("Character")); 0079 elm.setAttribute(QStringLiteral("char"), *it); 0080 top.appendChild(elm); 0081 } 0082 0083 for (const StringPair &pair : _ranges) { 0084 QDomElement elm = doc->createElement(QStringLiteral("Range")); 0085 elm.setAttribute(QStringLiteral("from"), pair.first); 0086 elm.setAttribute(QStringLiteral("to"), pair.second); 0087 top.appendChild(elm); 0088 } 0089 return top; 0090 } 0091 0092 bool TextRangeRegExp::load(const QDomElement &top, const QString & /*version*/) 0093 { 0094 Q_ASSERT(top.tagName() == QStringLiteral("TextRange")); 0095 QString str; 0096 QString one = QStringLiteral("1"); 0097 QString zero = QStringLiteral("0"); 0098 0099 str = top.attribute(QStringLiteral("negate"), zero); 0100 _negate = (str == one); 0101 0102 str = top.attribute(QStringLiteral("digit"), zero); 0103 _digit = (str == one); 0104 0105 str = top.attribute(QStringLiteral("nonDigit"), zero); 0106 _nonDigit = (str == one); 0107 0108 str = top.attribute(QStringLiteral("space"), zero); 0109 _space = (str == one); 0110 0111 str = top.attribute(QStringLiteral("nonSpace"), zero); 0112 _nonSpace = (str == one); 0113 0114 str = top.attribute(QStringLiteral("wordChar"), zero); 0115 _wordChar = (str == one); 0116 0117 str = top.attribute(QStringLiteral("nonWordChar"), zero); 0118 _nonWordChar = (str == one); 0119 0120 for (QDomNode node = top.firstChild(); !node.isNull(); node = node.nextSibling()) { 0121 if (!node.isElement()) { 0122 continue; // Skip comments. 0123 } 0124 QDomElement child = node.toElement(); 0125 0126 if (child.tagName() == QStringLiteral("Character")) { 0127 QString ch = child.attribute(QStringLiteral("char")); 0128 addCharacter(ch); 0129 } else if (child.tagName() == QStringLiteral("Range")) { 0130 QString from = child.attribute(QStringLiteral("from")); 0131 QString to = child.attribute(QStringLiteral("to")); 0132 addRange(from, to); 0133 } else { 0134 KMessageBox::error(nullptr, 0135 i18n("<p>Invalid sub element to element <b>TextRange</b>. Tag was <b>%1</b></p>", child.tagName()), 0136 i18n("Error While Loading From XML File")); 0137 return false; 0138 } 0139 } 0140 return true; 0141 } 0142 0143 bool TextRangeRegExp::operator==(const RegExp &other) const 0144 { 0145 return RegExpConverter::current()->toStr(const_cast<TextRangeRegExp *>(this), false) 0146 == RegExpConverter::current()->toStr(const_cast<RegExp *>(&other), false); 0147 }