File: XmlGenerator.cpp

package info (click to toggle)
bluez-qt 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,932 kB
  • sloc: cpp: 16,676; xml: 554; ansic: 337; sh: 13; makefile: 8
file content (139 lines) | stat: -rw-r--r-- 4,063 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * BluezQt - Asynchronous BlueZ wrapper library
 *
 * SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <mincequi@web.de>
 *
 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
 */

#include "XmlGenerator.h"

#include <QDebug>
#include <QFile>

#include "BluezApiParser.h"
#include "TypeAnnotation.h"

XmlGenerator::XmlGenerator(const Config &config)
    : m_config(config)
{
}

bool XmlGenerator::generate(const BluezApiParser &parser)
{
    // Iterate interfaces
    for (const auto &interface : parser.interfaces()) {
        // Only consider interfaces with methods
        if (interface.methods().methods().empty()) {
            continue;
        }

        // Create file
        QFile file(interface.name() + QStringLiteral(".xml"));
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            qWarning() << "Error opening file for writing:" << file.fileName();
            return false;
        }

        // Write content
        QTextStream stream(&file);
        writeHeader(stream);
        writeInterface(stream, interface.name());

        // Iterate methods
        for (const auto &method : interface.methods().methods()) {
            // Respect config
            if ((method.tags().isDeprecated && !m_config.useDeprecated) || (method.tags().isOptional && !m_config.useOptional)
                || (method.tags().isExperimental && !m_config.useExperimental)) {
                continue;
            }

            writeMethod(stream, method);
        }

        closeInterface(stream);
        writeFooter(stream);
        file.close();
    }

    return true;
}

void XmlGenerator::writeHeader(QTextStream &stream)
{
    stream << "<?xml version=\"1.0\"?>\n";
    stream << "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" "
              "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n";
    stream << "<node>\n";
}

void XmlGenerator::writeFooter(QTextStream &stream)
{
    stream << "</node>\n";
}

void XmlGenerator::writeInterface(QTextStream &stream, const QString &name)
{
    stream << "  <interface name=\"" << name << "\">\n";
}

void XmlGenerator::closeInterface(QTextStream &stream)
{
    stream << "  </interface>\n";
}

bool XmlGenerator::writeMethod(QTextStream &stream, const Method &method)
{
    stream << "    <method name=\"" << method.name() << "\"";

    // Some beautification
    if (method.inParameters().empty() && method.outParameters().empty()) {
        stream << "/>\n";
        return true;
    }

    stream << ">\n";

    for (const auto &param : method.inParameters()) {
        if (!writeArg(stream, param, QStringLiteral("in"))) {
            return false;
        }
    }
    for (const auto &param : method.outParameters()) {
        if (!writeArg(stream, param, QStringLiteral("out"))) {
            return false;
        }
    }
    for (int i = 0; i < method.inParameters().size(); ++i) {
        writeAnnotation(stream, method.inParameters().at(i), QStringLiteral("In"), i);
    }
    for (int i = 0; i < method.outParameters().size(); ++i) {
        writeAnnotation(stream, method.outParameters().at(i), QStringLiteral("Out"), i);
    }

    stream << "    </method>\n";

    return true;
}

bool XmlGenerator::writeArg(QTextStream &stream, const Parameter &param, const QString &dir)
{
    auto dbusType = annotateType(AnnotationType::Bluez, AnnotationType::Dbus, param.type());
    if (dbusType.isEmpty()) {
        return false;
    }
    stream << "      <arg name=\"" << param.name() << "\" type=\"" << dbusType << "\" direction=\"" << dir << "\"/>\n";

    return true;
}

void XmlGenerator::writeAnnotation(QTextStream &stream, const Parameter &param, const QString &dir, int i)
{
    auto qtType = annotateType(AnnotationType::Bluez, AnnotationType::Qt, param.type());
    if (qtType.isEmpty()) {
        return;
    }
    stream << "      <annotation name=\"org.qtproject.QtDBus.QtTypeName." << dir << QString::number(i) << "\" value=\"" << qtType << "\"/>\n";

    return;
}