summaryrefslogtreecommitdiff
path: root/src/rexmpp_xml.h
blob: 0a3cb63818c105102ce04b324840ff544b85243e (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
   @file rexmpp_xml.h
   @brief XML structures and functions for rexmpp
   @author defanor <defanor@uberspace.net>
   @date 2023
   @copyright MIT license.
*/

#ifndef REXMPP_XML_H
#define REXMPP_XML_H

#include <libxml/tree.h>

typedef struct rexmpp_xml_qname rexmpp_xml_qname_t;
typedef struct rexmpp_xml_attribute rexmpp_xml_attr_t;
typedef struct rexmpp_xml_node rexmpp_xml_t;

struct rexmpp_xml_qname {
  char *name;
  char *namespace;
};

struct rexmpp_xml_attribute {
  rexmpp_xml_qname_t qname;
  char *value;
  rexmpp_xml_attr_t *next;
};

enum rexmpp_xml_node_type {
  REXMPP_XML_ELEMENT,
  REXMPP_XML_TEXT
};

typedef enum rexmpp_xml_node_type rexmpp_xml_node_type_t;

struct rexmpp_xml_node {
  rexmpp_xml_node_type_t type;
  union {
    struct {
      rexmpp_xml_qname_t qname;
      rexmpp_xml_attr_t *attributes;
      rexmpp_xml_t *children;
    } elem;
    char *text;
  } alt;
  rexmpp_xml_t *next;
};


void rexmpp_xml_qname_free (rexmpp_xml_qname_t *qname);
void rexmpp_xml_attribute_free (rexmpp_xml_attr_t *attr);
void rexmpp_xml_attribute_free_list (rexmpp_xml_attr_t *attr);

/**
   @brief Frees a single XML node. Does not free its siblings.
*/
void rexmpp_xml_free (rexmpp_xml_t *node);

/**
   @brief Frees an XML node and its siblings.
*/
void rexmpp_xml_free_list (rexmpp_xml_t *node);

/**
   @brief Clones a single XML node, without its siblings.
*/
rexmpp_xml_t *rexmpp_xml_clone (rexmpp_xml_t *node);

/**
   @brief Clones an XML node, together with its siblings.
*/
rexmpp_xml_t *rexmpp_xml_clone_list (rexmpp_xml_t *node);

/**
   @brief Creates a single ::rexmpp_xml_t XML node out of libxml2's
   xmlNode, without siblings.
*/
rexmpp_xml_t *rexmpp_xml_from_libxml2 (xmlNodePtr from);

/**
   @brief Creates a ::rexmpp_xml_t XML node out of libxml2's xmlNode,
   with siblings.
*/
rexmpp_xml_t *rexmpp_xml_from_libxml2_list (xmlNodePtr from);

xmlNodePtr rexmpp_xml_to_libxml2 (rexmpp_xml_t *from);

xmlNodePtr rexmpp_xml_to_libxml2_list (rexmpp_xml_t *from);

/**
   @brief Creates a textual ::rexmpp_xml_t XML node (with type =
   ::REXMPP_XML_TEXT).
*/
rexmpp_xml_t *rexmpp_xml_new_text (const char *str);

/**
   @brief Creates an element ::rexmpp_xml_t XML node (with type =
   ::REXMPP_XML_ELEMENT).
*/
rexmpp_xml_t *rexmpp_xml_new_elem (const char *name,
                                   const char *namespace);

/**
   @brief Adds a child node.
*/
void rexmpp_xml_add_child (rexmpp_xml_t *node,
                           rexmpp_xml_t *child);

/**
   @brief Creates a text node, and adds it as a child.
*/
int rexmpp_xml_add_text (rexmpp_xml_t *node,
                         const char *str);

rexmpp_xml_attr_t *rexmpp_xml_attr_new (const char *name,
                                        const char *namespace,
                                        const char *value);

int rexmpp_xml_add_attr (rexmpp_xml_t *node,
                         const char *name,
                         const char *value);

int rexmpp_xml_remove_attr_ns (rexmpp_xml_t *node,
                               const char *name,
                               const char *namespace);

int rexmpp_xml_remove_attr (rexmpp_xml_t *node,
                            const char *name);

int rexmpp_xml_add_attr_ns (rexmpp_xml_t *node,
                            const char *name,
                            const char *namespace,
                            const char *value);

/**
   @brief Adds an "id" attribute to an XML stanza.
   @param[in,out] s ::rexmpp
   @param[in] node A pointer to an XML stanza.
   @returns The same pointer as on input, for more convenient
   composition.
*/
rexmpp_xml_t *
rexmpp_xml_add_id (rexmpp_t *s,
                   rexmpp_xml_t *node);

/**
   @brief A helper function for XML serialisation.
   @param[in] node An XML node.
   @returns A string (must be freed by the caller).
*/
char *rexmpp_xml_serialize (const rexmpp_xml_t *node, int pretty);

/**
   @brief Count the number of siblings after a given node.
*/
unsigned int rexmpp_xml_siblings_count (rexmpp_xml_t *node);

/**
   @brief Compares the node's name and namespace to given ones.
*/
int rexmpp_xml_match (rexmpp_xml_t *node,
                      const char *namespace,
                      const char *name);

int rexmpp_xml_is_stanza (rexmpp_xml_t *node);

/**
   @brief Compose an 'error' element.
*/
rexmpp_xml_t *rexmpp_xml_error (const char *type, const char *condition);

/**
   @brief Matches an XML node against a namespace and an element name.
   @param[in] node An XML node to match.
   @param[in] namespace An XML namespace. Can be NULL (matches
   anything), and it is assumed that the default namespace is
   "jabber:client" (so if it is "jabber:client" and an element doesn't
   have a namespace defined, this function counts that as a match).
   @param[in] name Element name. Can be NULL (matches anything).
   @returns 1 on a successful match, 0 otherwise.
*/
int rexmpp_xml_attr_match (rexmpp_xml_attr_t *attr,
                           const char *namespace,
                           const char *name);

rexmpp_xml_attr_t *rexmpp_xml_find_attr (rexmpp_xml_t *node,
                                         const char *name,
                                         const char *namespace);

const char *rexmpp_xml_find_attr_val_ns (rexmpp_xml_t *node,
                                         const char *name,
                                         const char *namespace);

const char *rexmpp_xml_find_attr_val (rexmpp_xml_t *node,
                                      const char *name);

/**
   @brief Finds a child element of an XML node, which matches the
   given namespace and name.
   @param[in] node The node containing child nodes.
   @param[in] namespace The namespace to look for.
   @param[in] name The element name to look for.
   @returns A pointer to the first matching child node, or NULL if no
   matching child elements found.
*/
rexmpp_xml_t *rexmpp_xml_find_child (rexmpp_xml_t *node,
                                     const char *namespace,
                                     const char *name);

rexmpp_xml_t *rexmpp_xml_children (const rexmpp_xml_t *node);

char *rexmpp_xml_text (rexmpp_xml_t *node);

char *rexmpp_xml_text_child (rexmpp_xml_t *node);

rexmpp_xml_t *rexmpp_xml_first_elem_child (rexmpp_xml_t *node);

rexmpp_xml_t *rexmpp_xml_next_elem_sibling (rexmpp_xml_t *node);

/**
   @brief Compares two XML elements.
*/
int rexmpp_xml_eq (rexmpp_xml_t *n1, rexmpp_xml_t *n2);

/**
   @brief A helper function for XML parsing.
   @param[in] str A string to parse.
   @param[in] str_len String length.
   @returns Parsed XML, or NULL on failure.
*/
rexmpp_xml_t *rexmpp_xml_parse (const char *str, int str_len);

rexmpp_xml_t *rexmpp_xml_read_file (const char *path);
int rexmpp_xml_write_file (const char *path, rexmpp_xml_t* node);

#endif