File indexing completed on 2024-11-24 04:49:52

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "xmlprintingscriptbuilder.h"
0008 #include "parser.h"
0009 
0010 #include "error.h"
0011 #include <QXmlStreamWriter>
0012 
0013 using namespace KSieveCore;
0014 XMLPrintingScriptBuilder::XMLPrintingScriptBuilder(int indent)
0015     : KSieve::ScriptBuilder()
0016 {
0017     initialize(indent);
0018 }
0019 
0020 XMLPrintingScriptBuilder::XMLPrintingScriptBuilder()
0021     : KSieve::ScriptBuilder()
0022 {
0023     initialize();
0024 }
0025 
0026 void XMLPrintingScriptBuilder::initialize(int indent)
0027 {
0028     mStream = new QXmlStreamWriter(&mResult);
0029     if (indent == 0) {
0030         mStream->setAutoFormatting(false);
0031     } else {
0032         mStream->setAutoFormatting(true);
0033         mStream->setAutoFormattingIndent(indent);
0034     }
0035     mStream->writeStartDocument();
0036     mStream->writeStartElement(QStringLiteral("script"));
0037 }
0038 
0039 XMLPrintingScriptBuilder::~XMLPrintingScriptBuilder()
0040 {
0041     delete mStream;
0042 }
0043 
0044 void XMLPrintingScriptBuilder::taggedArgument(const QString &tag)
0045 {
0046     write(QStringLiteral("tag"), tag);
0047 }
0048 
0049 void XMLPrintingScriptBuilder::stringArgument(const QString &string, bool multiLine, const QString & /*fixme*/)
0050 {
0051     if (multiLine) {
0052         write(QStringLiteral("str"), QStringLiteral("type"), QStringLiteral("multiline"), string);
0053     } else {
0054         write(QStringLiteral("str"), QStringLiteral("type"), QStringLiteral("quoted"), string);
0055     }
0056 }
0057 
0058 void XMLPrintingScriptBuilder::numberArgument(unsigned long number, char quantifier)
0059 {
0060     if (quantifier) {
0061         write(QStringLiteral("num"), QStringLiteral("quantifier"), QStringLiteral("%1").arg(quantifier), QString::number(number));
0062     } else {
0063         write(QStringLiteral("num"), QString(), QString(), QString::number(number));
0064     }
0065 }
0066 
0067 void XMLPrintingScriptBuilder::commandStart(const QString &identifier, int lineNumber)
0068 {
0069     Q_UNUSED(lineNumber)
0070     if (identifier == QLatin1StringView("else") || identifier == QLatin1StringView("break") || identifier == QLatin1StringView("require")
0071         || identifier == QLatin1StringView("foreverypart") || identifier == QLatin1StringView("if") || identifier == QLatin1StringView("elsif")) {
0072         mStream->writeStartElement(QStringLiteral("control"));
0073         mStream->writeAttribute(QStringLiteral("name"), identifier);
0074     } else {
0075         mStream->writeStartElement(QStringLiteral("action"));
0076         mStream->writeAttribute(QStringLiteral("name"), identifier);
0077     }
0078 }
0079 
0080 void XMLPrintingScriptBuilder::commandEnd(int lineNumber)
0081 {
0082     Q_UNUSED(lineNumber)
0083     mStream->writeEndElement();
0084 }
0085 
0086 void XMLPrintingScriptBuilder::testStart(const QString &identifier)
0087 {
0088     mStream->writeStartElement(QStringLiteral("test"));
0089     mStream->writeAttribute(QStringLiteral("name"), identifier);
0090 }
0091 
0092 void XMLPrintingScriptBuilder::testEnd()
0093 {
0094     mStream->writeEndElement();
0095 }
0096 
0097 void XMLPrintingScriptBuilder::testListStart()
0098 {
0099     mStream->writeStartElement(QStringLiteral("testlist"));
0100 }
0101 
0102 void XMLPrintingScriptBuilder::testListEnd()
0103 {
0104     mStream->writeEndElement();
0105 }
0106 
0107 void XMLPrintingScriptBuilder::blockStart(int lineNumber)
0108 {
0109     Q_UNUSED(lineNumber)
0110     mStream->writeStartElement(QStringLiteral("block"));
0111 }
0112 
0113 void XMLPrintingScriptBuilder::blockEnd(int lineNumber)
0114 {
0115     Q_UNUSED(lineNumber)
0116     mStream->writeEndElement();
0117 }
0118 
0119 void XMLPrintingScriptBuilder::stringListArgumentStart()
0120 {
0121     mStream->writeStartElement(QStringLiteral("list"));
0122 }
0123 
0124 void XMLPrintingScriptBuilder::stringListArgumentEnd()
0125 {
0126     mStream->writeEndElement();
0127 }
0128 
0129 void XMLPrintingScriptBuilder::stringListEntry(const QString &string, bool multiline, const QString &hashComment)
0130 {
0131     stringArgument(string, multiline, hashComment);
0132 }
0133 
0134 void XMLPrintingScriptBuilder::hashComment(const QString &comment)
0135 {
0136     write(QStringLiteral("comment"), QStringLiteral("type"), QStringLiteral("hash"), comment);
0137 }
0138 
0139 void XMLPrintingScriptBuilder::bracketComment(const QString &comment)
0140 {
0141     write(QStringLiteral("comment"), QStringLiteral("type"), QStringLiteral("bracket"), comment);
0142 }
0143 
0144 void XMLPrintingScriptBuilder::lineFeed()
0145 {
0146     mStream->writeEmptyElement(QStringLiteral("crlf"));
0147 }
0148 
0149 void XMLPrintingScriptBuilder::error(const KSieve::Error &error)
0150 {
0151     mError = QLatin1StringView("Error: ") + error.asString();
0152     mResult += mError;
0153 }
0154 
0155 void XMLPrintingScriptBuilder::finished()
0156 {
0157     mStream->writeEndElement();
0158 }
0159 
0160 void XMLPrintingScriptBuilder::write(const QString &key, const QString &value)
0161 {
0162     if (value.isEmpty()) {
0163         mStream->writeEmptyElement(key);
0164         return;
0165     }
0166     mStream->writeStartElement(key);
0167     mStream->writeCharacters(value);
0168     mStream->writeEndElement();
0169 }
0170 
0171 void XMLPrintingScriptBuilder::write(const QString &key, const QString &qualifiedName, const QString &attribute, const QString &value)
0172 {
0173     if (value.isEmpty()) {
0174         mStream->writeEmptyElement(key);
0175         return;
0176     }
0177     if (attribute.isEmpty()) {
0178         mStream->writeStartElement(key);
0179     } else {
0180         mStream->writeStartElement(key);
0181         mStream->writeAttribute(qualifiedName, attribute);
0182     }
0183     mStream->writeCharacters(value);
0184     mStream->writeEndElement();
0185 }
0186 
0187 QString XMLPrintingScriptBuilder::result() const
0188 {
0189     return mResult;
0190 }
0191 
0192 QString XMLPrintingScriptBuilder::error() const
0193 {
0194     return mError;
0195 }
0196 
0197 bool XMLPrintingScriptBuilder::hasError() const
0198 {
0199     return !mError.isEmpty();
0200 }
0201 
0202 void XMLPrintingScriptBuilder::clear()
0203 {
0204     mResult.clear();
0205     mError.clear();
0206 }