Libksieve

xmlprintingscriptbuilder.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "xmlprintingscriptbuilder.h"
8#include "parser.h"
9
10#include "error.h"
11#include <QXmlStreamWriter>
12
13using namespace KSieveCore;
14XMLPrintingScriptBuilder::XMLPrintingScriptBuilder(int indent)
15 : KSieve::ScriptBuilder()
16{
17 initialize(indent);
18}
19
20XMLPrintingScriptBuilder::XMLPrintingScriptBuilder()
21 : KSieve::ScriptBuilder()
22{
23 initialize();
24}
25
26void XMLPrintingScriptBuilder::initialize(int indent)
27{
28 mStream = new QXmlStreamWriter(&mResult);
29 if (indent == 0) {
30 mStream->setAutoFormatting(false);
31 } else {
32 mStream->setAutoFormatting(true);
33 mStream->setAutoFormattingIndent(indent);
34 }
35 mStream->writeStartDocument();
36 mStream->writeStartElement(QStringLiteral("script"));
37}
38
39XMLPrintingScriptBuilder::~XMLPrintingScriptBuilder()
40{
41 delete mStream;
42}
43
44void XMLPrintingScriptBuilder::taggedArgument(const QString &tag)
45{
46 write(QStringLiteral("tag"), tag);
47}
48
49void XMLPrintingScriptBuilder::stringArgument(const QString &string, bool multiLine, const QString & /*fixme*/)
50{
51 if (multiLine) {
52 write(QStringLiteral("str"), QStringLiteral("type"), QStringLiteral("multiline"), string);
53 } else {
54 write(QStringLiteral("str"), QStringLiteral("type"), QStringLiteral("quoted"), string);
55 }
56}
57
58void XMLPrintingScriptBuilder::numberArgument(unsigned long number, char quantifier)
59{
60 if (quantifier) {
61 write(QStringLiteral("num"), QStringLiteral("quantifier"), QStringLiteral("%1").arg(quantifier), QString::number(number));
62 } else {
63 write(QStringLiteral("num"), QString(), QString(), QString::number(number));
64 }
65}
66
67void XMLPrintingScriptBuilder::commandStart(const QString &identifier, int lineNumber)
68{
69 Q_UNUSED(lineNumber)
70 if (identifier == QLatin1StringView("else") || identifier == QLatin1StringView("break") || identifier == QLatin1StringView("require")
71 || identifier == QLatin1StringView("foreverypart") || identifier == QLatin1StringView("if") || identifier == QLatin1StringView("elsif")) {
72 mStream->writeStartElement(QStringLiteral("control"));
73 mStream->writeAttribute(QStringLiteral("name"), identifier);
74 } else {
75 mStream->writeStartElement(QStringLiteral("action"));
76 mStream->writeAttribute(QStringLiteral("name"), identifier);
77 }
78}
79
80void XMLPrintingScriptBuilder::commandEnd(int lineNumber)
81{
82 Q_UNUSED(lineNumber)
83 mStream->writeEndElement();
84}
85
86void XMLPrintingScriptBuilder::testStart(const QString &identifier)
87{
88 mStream->writeStartElement(QStringLiteral("test"));
89 mStream->writeAttribute(QStringLiteral("name"), identifier);
90}
91
92void XMLPrintingScriptBuilder::testEnd()
93{
94 mStream->writeEndElement();
95}
96
97void XMLPrintingScriptBuilder::testListStart()
98{
99 mStream->writeStartElement(QStringLiteral("testlist"));
100}
101
102void XMLPrintingScriptBuilder::testListEnd()
103{
104 mStream->writeEndElement();
105}
106
107void XMLPrintingScriptBuilder::blockStart(int lineNumber)
108{
109 Q_UNUSED(lineNumber)
110 mStream->writeStartElement(QStringLiteral("block"));
111}
112
113void XMLPrintingScriptBuilder::blockEnd(int lineNumber)
114{
115 Q_UNUSED(lineNumber)
116 mStream->writeEndElement();
117}
118
119void XMLPrintingScriptBuilder::stringListArgumentStart()
120{
121 mStream->writeStartElement(QStringLiteral("list"));
122}
123
124void XMLPrintingScriptBuilder::stringListArgumentEnd()
125{
126 mStream->writeEndElement();
127}
128
129void XMLPrintingScriptBuilder::stringListEntry(const QString &string, bool multiline, const QString &hashComment)
130{
131 stringArgument(string, multiline, hashComment);
132}
133
134void XMLPrintingScriptBuilder::hashComment(const QString &comment)
135{
136 write(QStringLiteral("comment"), QStringLiteral("type"), QStringLiteral("hash"), comment);
137}
138
139void XMLPrintingScriptBuilder::bracketComment(const QString &comment)
140{
141 write(QStringLiteral("comment"), QStringLiteral("type"), QStringLiteral("bracket"), comment);
142}
143
144void XMLPrintingScriptBuilder::lineFeed()
145{
146 mStream->writeEmptyElement(QStringLiteral("crlf"));
147}
148
149void XMLPrintingScriptBuilder::error(const KSieve::Error &error)
150{
151 mError = QLatin1StringView("Error: ") + error.asString();
152 mResult += mError;
153}
154
155void XMLPrintingScriptBuilder::finished()
156{
157 mStream->writeEndElement();
158}
159
160void XMLPrintingScriptBuilder::write(const QString &key, const QString &value)
161{
162 if (value.isEmpty()) {
163 mStream->writeEmptyElement(key);
164 return;
165 }
166 mStream->writeStartElement(key);
167 mStream->writeCharacters(value);
168 mStream->writeEndElement();
169}
170
171void XMLPrintingScriptBuilder::write(const QString &key, const QString &qualifiedName, const QString &attribute, const QString &value)
172{
173 if (value.isEmpty()) {
174 mStream->writeEmptyElement(key);
175 return;
176 }
177 if (attribute.isEmpty()) {
178 mStream->writeStartElement(key);
179 } else {
180 mStream->writeStartElement(key);
181 mStream->writeAttribute(qualifiedName, attribute);
182 }
183 mStream->writeCharacters(value);
184 mStream->writeEndElement();
185}
186
187QString XMLPrintingScriptBuilder::result() const
188{
189 return mResult;
190}
191
192QString XMLPrintingScriptBuilder::error() const
193{
194 return mError;
195}
196
197bool XMLPrintingScriptBuilder::hasError() const
198{
199 return !mError.isEmpty();
200}
201
202void XMLPrintingScriptBuilder::clear()
203{
204 mResult.clear();
205 mError.clear();
206}
KCRASH_EXPORT void initialize()
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
void clear()
bool isEmpty() const const
QString number(double n, char format, int precision)
void setAutoFormattingIndent(int spacesOrTabs)
void setAutoFormatting(bool enable)
void writeAttribute(QAnyStringView namespaceUri, QAnyStringView name, QAnyStringView value)
void writeCharacters(QAnyStringView text)
void writeEmptyElement(QAnyStringView namespaceUri, QAnyStringView name)
void writeEndElement()
void writeStartDocument()
void writeStartElement(QAnyStringView namespaceUri, QAnyStringView name)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.