File indexing completed on 2024-04-21 05:51:38

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 "regexp.h"
0008 
0009 #include "errormap.h"
0010 #include "kregexpeditorwindow.h"
0011 #include "widgetfactory.h"
0012 
0013 RegExp::RegExp(bool selected)
0014     : _parent(nullptr)
0015     , _destructing(false)
0016     , _selected(selected)
0017 {
0018     // Nothing to do
0019 }
0020 
0021 RegExp::~RegExp()
0022 {
0023     _destructing = true;
0024     qDeleteAll(_children);
0025     if (_parent) {
0026         _parent->removeChild(this);
0027     }
0028     _parent = nullptr;
0029 }
0030 
0031 void RegExp::addChild(RegExp *child)
0032 {
0033     _children.append(child);
0034     child->setParent(this);
0035 }
0036 
0037 void RegExp::removeChild(RegExp *child)
0038 {
0039     if (!_destructing) {
0040         _children.removeOne(child);
0041     }
0042 }
0043 
0044 void RegExp::setParent(RegExp *parent)
0045 {
0046     _parent = parent;
0047 }
0048 
0049 RegExp *RegExp::readRegExp(const QDomElement &top, const QString &version)
0050 {
0051     for (QDomNode node = top.firstChild(); !node.isNull(); node = node.nextSibling()) {
0052         if (!node.isElement()) {
0053             continue; // skip past comments
0054         }
0055 
0056         RegExp *regexp = WidgetFactory::createRegExp(node.toElement(), version);
0057         return regexp;
0058     }
0059     return nullptr;
0060 }
0061 
0062 QString RegExp::toXmlString() const
0063 {
0064     QDomDocument doc;
0065     doc.setContent(QStringLiteral("<RegularExpression/>"));
0066     QDomNode top = doc.documentElement();
0067     top.toElement().setAttribute(QStringLiteral("version"), KRegExpEditorWindow::version);
0068 
0069     QDomNode elm = toXml(&doc);
0070 
0071     top.appendChild(elm);
0072     QString xmlString = QStringLiteral(
0073                             "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<!DOCTYPE RegularExpression PUBLIC \"-//KDE//KRegexpEditor DTD 1.0//EN\" "
0074                             "\"http://www.blackie.dk/kreg.dtd\">\n")
0075         + doc.toString();
0076 
0077     return xmlString;
0078 }
0079 
0080 RegExp *RegExp::clone() const
0081 {
0082     return WidgetFactory::createRegExp(toXmlString());
0083 }
0084 
0085 void RegExp::check(ErrorMap &map)
0086 {
0087     map.start();
0088     check(map, true, true);
0089     map.end();
0090 }