Libksieve

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

KDE's Doxygen guidelines are available online.