From 026ff5edea100d08bc1d1af9c3519219551d9505 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 28 Oct 2023 22:36:37 +0200 Subject: [PATCH] Tools: improve XML formatter * Avoid adding ' ' to the output * Sort attributes of PythonExport element * Remove trailing whitespaces from UserDocu text and replace tabs with 4 spaces --- src/Tools/xmlformat/main.cpp | 76 +++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 6 deletions(-) diff --git a/src/Tools/xmlformat/main.cpp b/src/Tools/xmlformat/main.cpp index 527a7a9132..3948a27899 100644 --- a/src/Tools/xmlformat/main.cpp +++ b/src/Tools/xmlformat/main.cpp @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include int main(int argc, char* argv[]) @@ -41,11 +43,7 @@ int main(int argc, char* argv[]) return -1; } - QDomDocument xml; - if (!xml.setContent(&file)) { - std::cerr << "Invalid XML content\n"; - return -1; - } + QXmlStreamReader reader(file.readAll()); file.close(); if (!file.open(QFile::WriteOnly)) { @@ -53,7 +51,73 @@ int main(int argc, char* argv[]) return -1; } - file.write(xml.toByteArray(4)); + QXmlStreamWriter writer(&file); + writer.setAutoFormatting(true); + writer.setAutoFormattingIndent(4); + + auto findAttr = [](const QXmlStreamAttributes& attr, const QString& name) { + for (int i = 0; i < attr.size(); ++i) { + if (attr.at(i).name() == name) { + return i; + } + } + + return -1; + }; + + // ---------------------- + + auto sortAttr = [&findAttr](QXmlStreamAttributes& attr) { + QStringList list = {"Name", + "Namespace", + "Twin", + "TwinPointer", + "PythonName", + "FatherInclude", + "Include", + "Father", + "FatherNamespace"}; + QXmlStreamAttributes sorted; + for (const auto& it : list) { + int index = findAttr(attr, it); + if (index > -1) { + sorted.append(attr.at(index)); + attr.remove(index); + } + } + + // add the rest + for (const auto& it : qAsConst(attr)) { + sorted.append(it); + } + + return sorted; + }; + + // ---------------------- + + while (!reader.atEnd()) { + reader.readNext(); + if (reader.isStartElement() && reader.name() == QLatin1String("PythonExport")) { + QXmlStreamAttributes attr = reader.attributes(); + attr = sortAttr(attr); + + writer.writeStartElement(QString("PythonExport")); + for (int i = 0; i < attr.size(); ++i) { + file.write("\n "); + writer.writeAttribute(attr.at(i)); + } + } + else if (reader.isStartElement() && reader.name() == QLatin1String("UserDocu")) { + QString text = reader.readElementText().trimmed(); + text.replace("\t", " "); + writer.writeTextElement(QString("UserDocu"), text); + } + else if (!reader.isWhitespace()) { + writer.writeCurrentToken(reader); + } + } + file.close(); return 0;