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

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 "widgetfactory.h"
0008 
0009 #include <KMessageBox>
0010 #include <QDebug>
0011 
0012 #include "altnwidget.h"
0013 #include "characterswidget.h"
0014 #include "compoundwidget.h"
0015 #include "concwidget.h"
0016 #include "lookaheadwidget.h"
0017 #include "repeatwidget.h"
0018 #include "textwidget.h"
0019 #include "zerowidgets.h"
0020 
0021 #include "altnregexp.h"
0022 #include "compoundregexp.h"
0023 #include "concregexp.h"
0024 #include "dotregexp.h"
0025 #include "kregexpeditorwindow.h"
0026 #include "lookaheadregexp.h"
0027 #include "positionregexp.h"
0028 #include "repeatregexp.h"
0029 #include "textrangeregexp.h"
0030 #include "textregexp.h"
0031 
0032 bool WidgetFactory::isContainer(RegExpType tp)
0033 {
0034     return tp == REPEAT || tp == ALTN || tp == COMPOUND;
0035 }
0036 
0037 RegExpWidget *WidgetFactory::createWidget(RegExpEditorWindow *win, QWidget *parent, RegExpType type)
0038 {
0039     RegExpWidget *widget = nullptr;
0040 
0041     switch (type) {
0042     case TEXT:
0043         return new TextWidget(win, parent);
0044     case ALTN:
0045         return new AltnWidget(win, parent);
0046     case DOT:
0047         return new AnyCharWidget(win, parent);
0048     case BEGLINE:
0049         return new BegLineWidget(win, parent);
0050     case ENDLINE:
0051         return new EndLineWidget(win, parent);
0052     case WORDBOUNDARY:
0053         return new WordBoundaryWidget(win, parent);
0054     case NONWORDBOUNDARY:
0055         return new NonWordBoundaryWidget(win, parent);
0056     case POSLOOKAHEAD:
0057     case NEGLOOKAHEAD:
0058         return new LookAheadWidget(win, type, parent);
0059     case REPEAT:
0060         widget = new RepeatWidget(win, parent);
0061         break;
0062     case CHARSET:
0063         widget = new CharactersWidget(win, parent);
0064         break;
0065     case COMPOUND:
0066         widget = new CompoundWidget(win, parent);
0067         break;
0068     default:
0069         qFatal("It should not be possible to get here!");
0070         return nullptr;
0071     }
0072 
0073     if (widget->edit() == QDialog::Rejected) {
0074         delete widget;
0075         return nullptr;
0076     }
0077     return widget;
0078 }
0079 
0080 RegExpWidget *WidgetFactory::createWidget(RegExp *regexp, RegExpEditorWindow *editorWindow, QWidget *parent)
0081 {
0082     if (regexp == nullptr) {
0083         qFatal("%s:%d Regexp is 0", __FILE__, __LINE__);
0084     } else if (TextRegExp *reg = dynamic_cast<TextRegExp *>(regexp)) {
0085         return new TextWidget(reg, editorWindow, parent);
0086     } else if (TextRangeRegExp *reg = dynamic_cast<TextRangeRegExp *>(regexp)) {
0087         return new CharactersWidget(reg, editorWindow, parent);
0088     } else if (RepeatRegExp *reg = dynamic_cast<RepeatRegExp *>(regexp)) {
0089         return new RepeatWidget(reg, editorWindow, parent);
0090     } else if (LookAheadRegExp *reg = dynamic_cast<LookAheadRegExp *>(regexp)) {
0091         if (reg->lookAheadType() == LookAheadRegExp::POSITIVE) {
0092             return new LookAheadWidget(reg, editorWindow, POSLOOKAHEAD, parent);
0093         } else {
0094             return new LookAheadWidget(reg, editorWindow, NEGLOOKAHEAD, parent);
0095         }
0096     } else if (ConcRegExp *reg = dynamic_cast<ConcRegExp *>(regexp)) {
0097         return new ConcWidget(reg, editorWindow, parent);
0098     } else if (AltnRegExp *reg = dynamic_cast<AltnRegExp *>(regexp)) {
0099         return new AltnWidget(reg, editorWindow, parent);
0100     } else if (PositionRegExp *reg = dynamic_cast<PositionRegExp *>(regexp)) {
0101         switch (reg->position()) {
0102         case PositionRegExp::BEGLINE:
0103             return new BegLineWidget(editorWindow, parent);
0104         case PositionRegExp::ENDLINE:
0105             return new EndLineWidget(editorWindow, parent);
0106         case PositionRegExp::WORDBOUNDARY:
0107             return new WordBoundaryWidget(editorWindow, parent);
0108         case PositionRegExp::NONWORDBOUNDARY:
0109             return new NonWordBoundaryWidget(editorWindow, parent);
0110         }
0111     } else if (dynamic_cast<DotRegExp *>(regexp)) {
0112         return new AnyCharWidget(editorWindow, parent);
0113     } else if (CompoundRegExp *reg = dynamic_cast<CompoundRegExp *>(regexp)) {
0114         return new CompoundWidget(reg, editorWindow, parent);
0115     } else {
0116         qFatal("%s:%d Internal Error: Unknown RegExp type", __FILE__, __LINE__);
0117     }
0118     return nullptr;
0119 }
0120 
0121 RegExp *WidgetFactory::createRegExp(const QDomElement &node, const QString &version)
0122 {
0123     QString tag = node.tagName();
0124     RegExp *regexp;
0125     if (tag == QStringLiteral("TextRange")) {
0126         regexp = new TextRangeRegExp(false);
0127     } else if (tag == QStringLiteral("Text")) {
0128         regexp = new TextRegExp(false);
0129     } else if (tag == QStringLiteral("Concatenation")) {
0130         regexp = new ConcRegExp(false);
0131     } else if (tag == QStringLiteral("Alternatives")) {
0132         regexp = new AltnRegExp(false);
0133     } else if (tag == QStringLiteral("BegLine")) {
0134         regexp = new PositionRegExp(false, PositionRegExp::BEGLINE);
0135     } else if (tag == QStringLiteral("EndLine")) {
0136         regexp = new PositionRegExp(false, PositionRegExp::ENDLINE);
0137     } else if (tag == QStringLiteral("WordBoundary")) {
0138         regexp = new PositionRegExp(false, PositionRegExp::WORDBOUNDARY);
0139     } else if (tag == QStringLiteral("NonWordBoundary")) {
0140         regexp = new PositionRegExp(false, PositionRegExp::NONWORDBOUNDARY);
0141     } else if (tag == QStringLiteral("PositiveLookAhead")) {
0142         regexp = new LookAheadRegExp(false, LookAheadRegExp::POSITIVE);
0143     } else if (tag == QStringLiteral("NegativeLookAhead")) {
0144         regexp = new LookAheadRegExp(false, LookAheadRegExp::NEGATIVE);
0145     } else if (tag == QStringLiteral("Compound")) {
0146         regexp = new CompoundRegExp(false);
0147     } else if (tag == QStringLiteral("AnyChar")) {
0148         regexp = new DotRegExp(false);
0149     } else if (tag == QStringLiteral("Repeat")) {
0150         regexp = new RepeatRegExp(false);
0151     } else {
0152         KMessageBox::error(nullptr, i18n("<p>Unknown tag while reading XML. Tag was <b>%1</b></p>", tag), i18n("Error While Loading From XML File"));
0153 
0154         return nullptr;
0155     }
0156 
0157     bool ok = regexp->load(node, version);
0158     if (ok) {
0159         return regexp;
0160     }
0161 
0162     delete regexp;
0163     return nullptr;
0164 }
0165 
0166 RegExp *WidgetFactory::createRegExp(const QString &str)
0167 {
0168     QDomDocument doc;
0169     QString error;
0170     int errorLine, errorCol;
0171     bool ok = doc.setContent(str, &error, &errorLine, &errorCol);
0172     if (!ok) {
0173         qDebug() << error << "at line" << errorLine << "xml was:";
0174         qDebug() << str;
0175         KMessageBox::error(nullptr,
0176                            i18n("Error while loading regular expression from XML.") + QLatin1Char('\n') + error,
0177                            i18n("Error While Loading Regular Expression From XML"));
0178     }
0179 
0180     // Read the RegularExpression element, and extract the version.
0181     QDomElement top = doc.documentElement();
0182     if (!(top.tagName() == QStringLiteral("RegularExpression"))) {
0183         KMessageBox::error(nullptr,
0184                            i18n("<p>XML file did not contain a <b>%1</b> tag.</p>", QStringLiteral("RegularExpression")),
0185                            i18n("Error While Loading From XML File"));
0186     }
0187     QString version = top.attribute(QStringLiteral("version"), KRegExpEditorWindow::version);
0188     QDomNode child = top.firstChild();
0189     if (!child.isElement()) {
0190         KMessageBox::error(nullptr,
0191                            i18n("<p>Error while reading XML file. The element just below the tag "
0192                                 "<b>%1</b> was not an element.</p>",
0193                                 QStringLiteral("RegularExpression")),
0194                            i18n("Error While Loading From XML File"));
0195     }
0196 
0197     RegExp *regexp = WidgetFactory::createRegExp(child.toElement(), version);
0198 
0199     return regexp;
0200 }