File: xml.h

package info (click to toggle)
sdbus-cpp 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,296 kB
  • sloc: cpp: 9,859; xml: 170; ansic: 135; makefile: 27
file content (113 lines) | stat: -rw-r--r-- 2,020 bytes parent folder | download | duplicates (3)
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
/**
 * Inspired by: http://dbus-cplusplus.sourceforge.net/
 */

#ifndef __SDBUSCPP_TOOLS_XML_H
#define __SDBUSCPP_TOOLS_XML_H


#include <exception>
#include <string>
#include <vector>
#include <map>
#include <memory>
#include <iostream>
#include <sstream>

namespace sdbuscpp
{
namespace xml
{

class Error : public std::exception
{
public:

    Error(const char *error, int line, int column);

    ~Error() {}

    const char *what() const noexcept { return m_error.c_str(); }

private:
    std::string m_error;
};


class Node;

class Nodes : public std::vector<Node *>
{
public:
    Nodes operator[](const std::string &key) const;
    Nodes select(const std::string &attr, const std::string &value) const;
};

class Node
{
public:

        using Attributes = std::map<std::string, std::string>;
        using Children = std::vector<Node>;

        std::string name;
        std::string cdata;
        Children children;

        Node(std::string n, Attributes a) :
            name(n),
            m_attrs(a)
        {}

        Node(const char* n, const char** a = nullptr);

        Nodes operator[](const std::string& key);

        std::string get(const std::string& attribute) const;

        void set(const std::string &attribute, std::string value);

        std::string to_xml() const;

        Node& add(Node child)
        {
            children.push_back(child);
            return children.back();
        }

private:

        void _raw_xml(std::string& xml, int& depth) const;

        Attributes m_attrs;
};

class Document
{
public:
        struct Expat;

        Node* root;

        Document();
        ~Document();

        Document(const std::string& xml);

        void from_xml(const std::string& xml);

        std::string to_xml() const;

private:

        int m_depth;
};

} // namespace xml

} // namespace sdbuscpp

std::istream &operator >> (std::istream &, sdbuscpp::xml::Document &);
std::ostream &operator << (std::ostream &, sdbuscpp::xml::Document &);

#endif//__SDBUSCPP_TOOLS_XML_H