1
// SPDX-License-Identifier: LGPL-2.1-or-later
3
/***************************************************************************
4
* Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> *
6
* This file is part of FreeCAD. *
8
* FreeCAD is free software: you can redistribute it and/or modify it *
9
* under the terms of the GNU Lesser General Public License as *
10
* published by the Free Software Foundation, either version 2.1 of the *
11
* License, or (at your option) any later version. *
13
* FreeCAD is distributed in the hope that it will be useful, but *
14
* WITHOUT ANY WARRANTY; without even the implied warranty of *
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16
* Lesser General Public License for more details. *
18
* You should have received a copy of the GNU Lesser General Public *
19
* License along with FreeCAD. If not, see *
20
* <https://www.gnu.org/licenses/>. *
22
**************************************************************************/
24
#include <QCoreApplication>
26
#include <QDomDocument>
27
#include <QXmlStreamReader>
28
#include <QXmlStreamWriter>
31
int main(int argc, char* argv[])
33
QCoreApplication app(argc, argv);
34
QStringList args = QCoreApplication::arguments();
35
if (args.size() != 2) {
36
std::cerr << "Requires input file\n";
41
if (!file.open(QFile::ReadOnly)) {
42
std::cerr << "Failed to read file\n";
46
QXmlStreamReader reader(file.readAll());
49
if (!file.open(QFile::WriteOnly)) {
50
std::cerr << "Failed to write file\n";
54
QXmlStreamWriter writer(&file);
55
writer.setAutoFormatting(true);
56
writer.setAutoFormattingIndent(4);
58
auto findAttr = [](const QXmlStreamAttributes& attr, const QString& name) {
59
for (int i = 0; i < attr.size(); ++i) {
60
if (attr.at(i).name() == name) {
68
// ----------------------
70
auto sortAttr = [&findAttr](QXmlStreamAttributes& attr) {
71
QStringList list = {"Name",
80
QXmlStreamAttributes sorted;
81
for (const auto& it : list) {
82
int index = findAttr(attr, it);
84
sorted.append(attr.at(index));
90
for (const auto& it : qAsConst(attr)) {
97
// ----------------------
99
while (!reader.atEnd()) {
101
if (reader.isStartElement() && reader.name() == QLatin1String("PythonExport")) {
102
QXmlStreamAttributes attr = reader.attributes();
103
attr = sortAttr(attr);
105
writer.writeStartElement(QString("PythonExport"));
106
for (int i = 0; i < attr.size(); ++i) {
108
writer.writeAttribute(attr.at(i));
111
else if (reader.isStartElement() && reader.name() == QLatin1String("UserDocu")) {
112
QString text = reader.readElementText().trimmed();
113
text.replace("\t", " ");
114
writer.writeTextElement(QString("UserDocu"), text);
116
else if (!reader.isWhitespace()) {
117
writer.writeCurrentToken(reader);