~thomas-voss/dbus-cpp/add_support_for_get_connection_app_armor_security_context

« back to all changes in this revision

Viewing changes to src/core/dbus/introspection_parser.cpp

  • Committer: Tarmac
  • Author(s): thomas-voss, Thomas Voß
  • Date: 2013-12-12 13:25:20 UTC
  • mfrom: (11.2.42 dbus-cpp-refactoring)
  • Revision ID: tarmac-20131212132520-gi9pig97oo57tke5
 * Add introspect function to autogenerated protocol header files.
 * Add a code generator for protocol header files.
 * Split up files and move the boost::asio executor implementation into src.
 * Force definitions inline.

Approved by Timo Jyrinki, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
 
 
19
#include <core/dbus/introspection_parser.h>
 
20
 
 
21
#include <libxml/xmlreader.h>
 
22
 
 
23
#include <iostream>
 
24
#include <map>
 
25
 
 
26
namespace core
 
27
{
 
28
namespace dbus
 
29
{
 
30
namespace
 
31
{
 
32
std::string safe_string_construct(const char* c)
 
33
{
 
34
    return std::move(std::string((c ? c : "")));
 
35
}
 
36
}
 
37
struct IntrospectionParser::Private
 
38
{
 
39
    std::function<void(const Node&)> on_node;
 
40
    std::function<void()> on_node_done;
 
41
    std::function<void(const Interface&)> on_interface;
 
42
    std::function<void()> on_interface_done;
 
43
    std::function<void(const Method&)> on_method;
 
44
    std::function<void()> on_method_done;
 
45
    std::function<void(const Property&)> on_property;
 
46
    std::function<void(const Signal&)> on_signal;
 
47
    std::function<void()> on_signal_done;
 
48
    std::function<void(const Argument&)> on_argument;
 
49
    std::function<void()> on_argument_done;
 
50
    std::function<void(const Annotation&)> on_annotation;
 
51
    std::function<void()> on_annotation_done;
 
52
 
 
53
};
 
54
 
 
55
IntrospectionParser::IntrospectionParser() : d(new Private())
 
56
{
 
57
}
 
58
 
 
59
IntrospectionParser::~IntrospectionParser()
 
60
{
 
61
}
 
62
 
 
63
#define NAMED_ENUMERATION_ELEMENT(element) {element, #element}
 
64
 
 
65
bool IntrospectionParser::invoke_for(const std::string& filename)
 
66
{
 
67
    static const char* empty_encoding = nullptr;
 
68
    // We consciously do not check the dtd here
 
69
    static const int parser_options = 0;
 
70
 
 
71
    auto reader = std::shared_ptr<xmlTextReader>(
 
72
        xmlReaderForFile(filename.c_str(),
 
73
                         empty_encoding,
 
74
                         parser_options),
 
75
        [](xmlTextReaderPtr reader)
 
76
        {
 
77
            if (reader == nullptr)
 
78
                return;
 
79
 
 
80
            xmlFreeTextReader(reader);
 
81
        });
 
82
 
 
83
    if (!reader)
 
84
        return false;
 
85
 
 
86
    const std::map<std::string, std::function<void()>> vtable_done =
 
87
    {
 
88
        {Node::element_name(), d->on_node_done},
 
89
        {Interface::element_name(), d->on_interface_done},
 
90
        {Method::element_name(), d->on_method_done},
 
91
        {Argument::element_name(), d->on_argument_done},
 
92
        {Annotation::element_name(), d->on_annotation_done},
 
93
        {Signal::element_name(), d->on_signal_done}
 
94
    };
 
95
 
 
96
    const std::map<std::string, std::function<void(xmlTextReaderPtr)>> vtable =
 
97
    {
 
98
        {
 
99
            Annotation::element_name(),
 
100
            [this](xmlTextReaderPtr reader)
 
101
            {
 
102
                if (!d->on_annotation)
 
103
                    return;
 
104
 
 
105
                d->on_annotation(
 
106
                    Annotation{
 
107
                        safe_string_construct(reinterpret_cast<const char*>(xmlTextReaderGetAttribute(reader, BAD_CAST Annotation::name_attribute_name()))),
 
108
                        safe_string_construct(reinterpret_cast<const char*>(xmlTextReaderGetAttribute(reader, BAD_CAST Annotation::value_attribute_name())))});
 
109
            }
 
110
        },
 
111
        {
 
112
            Node::element_name(),
 
113
            [this](xmlTextReaderPtr reader)
 
114
            {
 
115
                if (!d->on_node)
 
116
                    return;
 
117
 
 
118
                auto attribute_name = reinterpret_cast<const char*>(
 
119
                            xmlTextReaderGetAttribute(reader,
 
120
                                                      BAD_CAST Node::name_attribute_name()));
 
121
 
 
122
                d->on_node(Node{safe_string_construct(attribute_name)});
 
123
            }
 
124
        },
 
125
        {
 
126
            Interface::element_name(),
 
127
            [this](xmlTextReaderPtr reader)
 
128
            {
 
129
                if (!d->on_interface)
 
130
                    return;
 
131
 
 
132
                d->on_interface(
 
133
                            Interface{
 
134
                                safe_string_construct(reinterpret_cast<const char*>(
 
135
                                    xmlTextReaderGetAttribute(
 
136
                                    reader,
 
137
                                    BAD_CAST Interface::name_attribute_name())))});
 
138
            }
 
139
        },
 
140
        {
 
141
            Argument::element_name(),
 
142
            [this](xmlTextReaderPtr reader)
 
143
            {
 
144
                if (!d->on_argument)
 
145
                    return;
 
146
 
 
147
                static const std::map<std::string, Argument::Direction> direction_lut =
 
148
                {
 
149
                    {"", Argument::Direction::context},
 
150
                    {"in", Argument::Direction::in},
 
151
                    {"out", Argument::Direction::out}
 
152
                };
 
153
 
 
154
                auto name_attribute = reinterpret_cast<const char*>(
 
155
                            xmlTextReaderGetAttribute(
 
156
                                reader,
 
157
                                BAD_CAST Argument::name_attribute_name()));
 
158
 
 
159
                Argument::Direction dir = Argument::Direction::context;
 
160
                try
 
161
                {
 
162
                    dir = direction_lut.at(
 
163
                                safe_string_construct(reinterpret_cast<const char*>(
 
164
                                    xmlTextReaderGetAttribute(
 
165
                                        reader,
 
166
                                        BAD_CAST Argument::direction_attribute_name()))));
 
167
                } catch(const std::out_of_range& e)
 
168
                {
 
169
                    throw std::runtime_error(
 
170
                                std::string("Could not interpret direction for argument with name: ") +
 
171
                                safe_string_construct(name_attribute));
 
172
                }
 
173
 
 
174
                d->on_argument(
 
175
                    Argument{
 
176
                        safe_string_construct(name_attribute),
 
177
                        safe_string_construct(reinterpret_cast<const char*>(
 
178
                                xmlTextReaderGetAttribute(
 
179
                                    reader,
 
180
                                    BAD_CAST Argument::type_attribute_name()))),
 
181
                        dir});
 
182
            }
 
183
        },
 
184
        {
 
185
            Method::element_name(),
 
186
            [this](xmlTextReaderPtr reader)
 
187
            {
 
188
                if (!d->on_method)
 
189
                    return;
 
190
 
 
191
                d->on_method(
 
192
                    Method{
 
193
                        safe_string_construct(
 
194
                            reinterpret_cast<const char*>(
 
195
                                xmlTextReaderGetAttribute(
 
196
                                    reader,
 
197
                                    BAD_CAST Method::name_attribute_name())))});
 
198
            }
 
199
        },
 
200
        {
 
201
            Signal::element_name(),
 
202
            [this](xmlTextReaderPtr reader)
 
203
            {
 
204
                if (!d->on_signal)
 
205
                    return;
 
206
 
 
207
                d->on_signal(
 
208
                    Signal{
 
209
                        safe_string_construct(
 
210
                            reinterpret_cast<const char*>(
 
211
                                xmlTextReaderGetAttribute(
 
212
                                    reader,
 
213
                                    BAD_CAST Signal::name_attribute_name())))});
 
214
            }
 
215
        },
 
216
        {
 
217
            Property::element_name(),
 
218
            [this](xmlTextReaderPtr reader)
 
219
            {
 
220
                if (!d->on_property)
 
221
                    return;
 
222
 
 
223
                static const std::map<std::string, Property::Access> access_lut =
 
224
                {
 
225
                    {"read", Property::Access::read},
 
226
                    {"write", Property::Access::write},
 
227
                    {"readwrite", Property::Access::read_write}
 
228
                };
 
229
 
 
230
                auto property_name = reinterpret_cast<const char*>(
 
231
                            xmlTextReaderGetAttribute(
 
232
                                reader,
 
233
                                BAD_CAST Property::name_attribute_name()));
 
234
 
 
235
                Property::Access a;
 
236
                try
 
237
                {
 
238
                     a = access_lut.at(
 
239
                                safe_string_construct(reinterpret_cast<const char*>(
 
240
                                    xmlTextReaderGetAttribute(
 
241
                                        reader,
 
242
                                        BAD_CAST Property::access_attribute_name()))));
 
243
                } catch(const std::out_of_range& e)
 
244
                {
 
245
                    throw std::runtime_error(
 
246
                                std::string("Could not determine read/write setup of property ") + property_name);
 
247
                }
 
248
 
 
249
                d->on_property(
 
250
                    Property{
 
251
                        safe_string_construct(property_name),
 
252
                        safe_string_construct(reinterpret_cast<const char*>(
 
253
                                xmlTextReaderGetAttribute(
 
254
                                    reader,
 
255
                                    BAD_CAST Property::type_attribute_name()))),
 
256
                        a});
 
257
            }
 
258
        }
 
259
    };
 
260
 
 
261
    enum class NodeType : int
 
262
    {
 
263
        attribute = 2,
 
264
        cdata = 4,
 
265
        comment = 8,
 
266
        document = 9,
 
267
        document_fragment = 11,
 
268
        document_type = 10,
 
269
        element = 1,
 
270
        end_element = 15,
 
271
        entity = 6,
 
272
        end_entity = 16,
 
273
        entity_reference = 5,
 
274
        none = 0,
 
275
        notation = 12,
 
276
        processing_instruction = 7,
 
277
        significant_whitespace = 14,
 
278
        text = 3,
 
279
        whitespace = 13,
 
280
        xml_declaration = 17
 
281
    };
 
282
 
 
283
    static std::map<NodeType, std::string> node_type_lut=
 
284
    {
 
285
        NAMED_ENUMERATION_ELEMENT(NodeType::attribute),
 
286
        NAMED_ENUMERATION_ELEMENT(NodeType::cdata),
 
287
        NAMED_ENUMERATION_ELEMENT(NodeType::comment),
 
288
        NAMED_ENUMERATION_ELEMENT(NodeType::document),
 
289
        NAMED_ENUMERATION_ELEMENT(NodeType::document_fragment),
 
290
        NAMED_ENUMERATION_ELEMENT(NodeType::document_type),
 
291
        NAMED_ENUMERATION_ELEMENT(NodeType::element),
 
292
        NAMED_ENUMERATION_ELEMENT(NodeType::end_element),
 
293
        NAMED_ENUMERATION_ELEMENT(NodeType::entity),
 
294
        NAMED_ENUMERATION_ELEMENT(NodeType::end_entity),
 
295
        NAMED_ENUMERATION_ELEMENT(NodeType::entity_reference),
 
296
        NAMED_ENUMERATION_ELEMENT(NodeType::none),
 
297
        NAMED_ENUMERATION_ELEMENT(NodeType::notation),
 
298
        NAMED_ENUMERATION_ELEMENT(NodeType::processing_instruction),
 
299
        NAMED_ENUMERATION_ELEMENT(NodeType::significant_whitespace),
 
300
        NAMED_ENUMERATION_ELEMENT(NodeType::text),
 
301
        NAMED_ENUMERATION_ELEMENT(NodeType::whitespace),
 
302
        NAMED_ENUMERATION_ELEMENT(NodeType::xml_declaration)
 
303
    };
 
304
 
 
305
    enum class Status : int
 
306
    {
 
307
        has_more = 1,
 
308
        done = 0,
 
309
        error = -1,
 
310
    };
 
311
 
 
312
    int status = xmlTextReaderRead(reader.get());
 
313
 
 
314
    if (Status::error == static_cast<Status>(status))
 
315
        return false;
 
316
 
 
317
    while (Status::has_more == static_cast<Status>(status))
 
318
    {
 
319
        auto node_name = reinterpret_cast<const char*>(
 
320
                xmlTextReaderConstName(reader.get()));
 
321
        switch(static_cast<NodeType>(xmlTextReaderNodeType(reader.get())))
 
322
        {
 
323
            case NodeType::element:
 
324
                if (vtable.count(safe_string_construct(node_name)) > 0)
 
325
                {
 
326
                    vtable.at(safe_string_construct(node_name))(reader.get());
 
327
                }
 
328
 
 
329
                if (xmlTextReaderIsEmptyElement(reader.get()))
 
330
                {
 
331
                    if (vtable_done.count(safe_string_construct(node_name)) > 0)
 
332
                    {
 
333
                        vtable_done.at(safe_string_construct(node_name))();
 
334
                    }
 
335
                }
 
336
                break;
 
337
            case NodeType::end_element:
 
338
                if (vtable_done.count(
 
339
                            safe_string_construct((const char*) xmlTextReaderConstName(reader.get()))) > 0)
 
340
                {
 
341
                    vtable_done.at(safe_string_construct((const char*) xmlTextReaderConstName(reader.get())))();
 
342
                }
 
343
                break;
 
344
            default:
 
345
                break;
 
346
        }
 
347
        status = xmlTextReaderRead(reader.get());
 
348
    }
 
349
 
 
350
    if (Status::error == static_cast<Status>(status))
 
351
        return false;
 
352
 
 
353
    return Status::done == static_cast<Status>(status);
 
354
}
 
355
 
 
356
void IntrospectionParser::on_node(const std::function<void(const Node&)>& f)
 
357
{
 
358
    d->on_node = f;
 
359
}
 
360
 
 
361
void IntrospectionParser::on_node_done(const std::function<void()>& f)
 
362
{
 
363
    d->on_node_done = f;
 
364
}
 
365
 
 
366
void IntrospectionParser::on_interface(const std::function<void(const Interface&)>& f)
 
367
{
 
368
    d->on_interface = f;
 
369
}
 
370
 
 
371
void IntrospectionParser::on_interface_done(const std::function<void()>& f)
 
372
{
 
373
    d->on_interface_done = f;
 
374
}
 
375
 
 
376
void IntrospectionParser::on_method(const std::function<void(const Method&)>& f)
 
377
{
 
378
    d->on_method = f;
 
379
}
 
380
 
 
381
void IntrospectionParser::on_method_done(const std::function<void()>& f)
 
382
{
 
383
    d->on_method_done = f;
 
384
}
 
385
 
 
386
void IntrospectionParser::on_property(const std::function<void(const Property&)>& f)
 
387
{
 
388
    d->on_property = f;
 
389
}
 
390
 
 
391
void IntrospectionParser::on_signal(const std::function<void(const Signal&)>& f)
 
392
{
 
393
    d->on_signal = f;
 
394
}
 
395
 
 
396
void IntrospectionParser::on_signal_done(const std::function<void()>& f)
 
397
{
 
398
    d->on_signal_done = f;
 
399
}
 
400
 
 
401
void IntrospectionParser::on_argument(const std::function<void(const Argument&)>& f)
 
402
{
 
403
    d->on_argument = f;
 
404
}
 
405
 
 
406
void IntrospectionParser::on_argument_done(const std::function<void()>& f)
 
407
{
 
408
    d->on_argument_done = f;
 
409
}
 
410
 
 
411
void IntrospectionParser::on_annotation(const std::function<void(const Annotation&)>& f)
 
412
{
 
413
    d->on_annotation = f;
 
414
}
 
415
 
 
416
void IntrospectionParser::on_annotation_done(const std::function<void()>& f)
 
417
{
 
418
    d->on_annotation_done = f;
 
419
}
 
420
}
 
421
}