summaryrefslogtreecommitdiff
path: root/libgupnp/xml-util.c
blob: cdef2eddc00ab015278d3e7bf4bb62ab42547bc1 (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
237
238
239
240
241
242
243
244
245
/*
 * Copyright (C) 2006, 2007 OpenedHand Ltd.
 *
 * Author: Jorn Baayen <jorn@openedhand.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 */

#include <config.h>
#include <string.h>

#include "gvalue-util.h"
#include "xml-util.h"

/* libxml DOM interface helpers */
xmlNode *
xml_util_get_element (xmlNode *node,
                      ...)
{
        va_list var_args;

        va_start (var_args, node);

        while (TRUE) {
                const char *arg;

                arg = va_arg (var_args, const char *);
                if (!arg)
                        break;

                for (node = node->children; node; node = node->next)
                        if (!g_strcmp0 (arg, (char *) node->name))
                                break;

                if (!node)
                        break;
        }

        va_end (var_args);

        return node;
}

xmlChar *
xml_util_get_child_element_content (xmlNode    *node,
                                    const char *child_name)
{
        xmlNode *child_node;

        child_node = xml_util_get_element (node,
                                           child_name,
                                           NULL);
        if (!child_node)
                return NULL;

        return xmlNodeGetContent (child_node);
}

int
xml_util_get_child_element_content_int (xmlNode    *node,
                                        const char *child_name)
{
        xmlChar *content;
        int i;

        content = xml_util_get_child_element_content (node, child_name);
        if (!content)
                return -1;

        i = atoi ((char *) content);

        xmlFree (content);

        return i;
}

char *
xml_util_get_child_element_content_glib (xmlNode    *node,
                                         const char *child_name)
{
        xmlChar *content;
        char *copy;

        content = xml_util_get_child_element_content (node, child_name);
        if (!content)
                return NULL;

        copy = g_strdup ((char *) content);

        xmlFree (content);

        return copy;
}

GUri *
xml_util_get_child_element_content_uri (xmlNode *node,
                                        const char *child_name,
                                        GUri *base)
{
        xmlChar *content;
        GUri *uri;

        content = xml_util_get_child_element_content (node, child_name);
        if (!content)
                return NULL;

        if (base != NULL)
                uri = g_uri_parse_relative (base,
                                            (const char *) content,
                                            G_URI_FLAGS_NONE,
                                            NULL);
        else
                uri = g_uri_parse ((const char *) content,
                                   G_URI_FLAGS_NONE,
                                   NULL);

        xmlFree (content);

        return uri;
}

char *
xml_util_get_child_element_content_url (xmlNode *node,
                                        const char *child_name,
                                        GUri *base)
{
        GUri *uri;
        char *url;

        uri = xml_util_get_child_element_content_uri (node, child_name, base);
        if (!uri)
                return NULL;

        url = g_uri_to_string_partial (uri, G_URI_HIDE_PASSWORD);
        g_uri_unref (uri);

        return url;
}

xmlChar *
xml_util_get_attribute_contents (xmlNode    *node,
                                 const char *attribute_name)
{
        xmlAttr *attribute;

        for (attribute = node->properties;
             attribute;
             attribute = attribute->next) {
                if (strcmp (attribute_name, (char *) attribute->name) == 0)
                        break;
        }

        if (attribute)
                return xmlNodeGetContent (attribute->children);
        else
                return NULL;
}

/**
 * xml_util_real_node:
 * @node: a %xmlNodePtr
 *
 * Finds the first "real" node (ie, not a comment or whitespace) at or
 * after @node at its level in the tree.
 *
 * Return: a node, or %NULL
 *
 * (Taken from libsoup)
 **/
xmlNode *
xml_util_real_node (xmlNode *node)
{
        while (node &&
               (node->type == XML_COMMENT_NODE || xmlIsBlankNode (node)))
                node = node->next;
        return node;
}

/* XML string creation helpers */

#define INITIAL_XML_STR_SIZE 100 /* Initial xml string size in bytes */

GString *
xml_util_new_string (void)
{
        return g_string_sized_new (INITIAL_XML_STR_SIZE);
}

void
xml_util_start_element (GString    *xml_str,
                        const char *element_name)
{
        g_string_append_c (xml_str, '<');
        g_string_append (xml_str, element_name);
        g_string_append_c (xml_str, '>');
}

void
xml_util_end_element (GString    *xml_str,
                      const char *element_name)
{
        g_string_append (xml_str, "</");
        g_string_append (xml_str, element_name);
        g_string_append_c (xml_str, '>');
}

void
xml_util_add_content (GString    *xml_str,
                      const char *content)
{
        /* Modified from GLib gmarkup.c */
        const gchar *p;

        p = content;

        while (*p) {
                const gchar *next;
                next = g_utf8_next_char (p);

                switch (*p) {
                case '&':
                        g_string_append (xml_str, "&amp;");
                        break;

                case '<':
                        g_string_append (xml_str, "&lt;");
                        break;

                case '>':
                        g_string_append (xml_str, "&gt;");
                        break;

                case '"':
                        g_string_append (xml_str, "&quot;");
                        break;

                default:
                        g_string_append_len (xml_str, p, next - p);
                        break;
                }

                p = next;
        }
}