summaryrefslogtreecommitdiff
path: root/libgupnp-av/gupnp-feature-list-parser.c
blob: 16208b594336842aa676f5dcb966ab6d5d4e6277 (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
/*
 * Copyright (C) 2012 Intel Corporation.
 *
 * Authors: Regis Merlino <regis.merlino@intel.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 */

/**
 * SECTION:gupnp-feature-list-parser
 * @short_description: FeatureList state variable XML parser
 *
 * #GUPnPFeatureListParser parses XML strings from ContentDirectory
 * FeatureList state variable.
 *
 */

#include <config.h>

#include <string.h>
#include "gupnp-feature-list-parser.h"
#include "gupnp-av.h"
#include "xml-util.h"

struct _GUPnPFeatureListParser {
        GObject parent;
};

/* GUPnPFeatureListParser */
G_DEFINE_TYPE (GUPnPFeatureListParser,
               gupnp_feature_list_parser,
               G_TYPE_OBJECT)

static void
gupnp_feature_list_parser_init (G_GNUC_UNUSED GUPnPFeatureListParser *parser)
{
        /* Nothing to do here */
}

static void
gupnp_feature_list_parser_finalize (GObject *object)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (gupnp_feature_list_parser_parent_class);
        object_class->finalize (object);
}

static void
gupnp_feature_list_parser_class_init (GUPnPFeatureListParserClass *klass)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (klass);

        object_class->finalize = gupnp_feature_list_parser_finalize;
}

/**
 * gupnp_feature_list_parser_new:
 *
 * Return value: A new #GUPnPFeatureListParser object.
 **/
GUPnPFeatureListParser *
gupnp_feature_list_parser_new (void)
{
        return g_object_new (GUPNP_TYPE_FEATURE_LIST_PARSER, NULL);
}

static char *
get_feature_object_ids (xmlNode *feature)
{
        xmlNode    *element;
        const char *content;
        GString *object_ids = g_string_new ("");

        for (element = feature->children; element; element = element->next) {
                const char *name = (const char *) element->name;
                if (g_ascii_strcasecmp (name, "objectIDs") == 0) {
                        content = (const char *) xmlNodeGetContent (element);
                        if (strlen (content) == 0)
                                continue;

                        if (object_ids->len > 0)
                                g_string_append_c (object_ids, ',');

                        g_string_append (object_ids, content);
                }
        }

        return g_string_free (object_ids, FALSE);
}

/**
 * gupnp_feature_list_parser_parse_text:
 * @parser: A #GUPnPFeatureListParser
 * @text: The feature list string to be parsed
 * @error: The location where to store the error information if any, or NULL
 *
 * Parses @text and returns the list of available features.
 * If an error occured @error will be set.
 *
 * Return value: (transfer full) (element-type GUPnPFeature)(nullable): The list of
 * features or %NULL if an error occured.
 **/
GList *
gupnp_feature_list_parser_parse_text
                                 (G_GNUC_UNUSED GUPnPFeatureListParser *parser,
                                  const char             *text,
                                  GError                **error)
{
        xmlDoc       *doc;
        xmlNode      *element;
        GList        *feature_list = NULL;

        doc = xmlRecoverMemory (text, strlen (text));
        if (doc == NULL) {
                g_set_error (error,
                             G_MARKUP_ERROR,
                             G_MARKUP_ERROR_PARSE,
                             "Could not parse FeatureList XML:\n%s", text);

                return NULL;
        }

        /* Get a pointer to root element */
        element = av_xml_util_get_element ((xmlNode *) doc, "Features", NULL);
        if (element == NULL) {
                g_set_error (error,
                             G_MARKUP_ERROR,
                             G_MARKUP_ERROR_PARSE,
                             "No 'Features' node in the XML:\n%s",
                             text);
                xmlFreeDoc (doc);

                return NULL;
        }

        for (element = element->children; element; element = element->next) {
                GUPnPFeature *feature;
                const char   *name;
                const char   *version;
                char         *object_ids;

                if (g_ascii_strcasecmp ((char *) element->name,
                                        "Feature") == 0) {
                        name = av_xml_util_get_attribute_content (element,
                                                                  "name");
                        version = av_xml_util_get_attribute_content (element,
                                                                     "version");
                        if (!name || !version) {
                                g_set_error (error,
                                             G_MARKUP_ERROR,
                                             G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
                                             "Invalid attributes in 'Feature' "
                                             "node in the XML:\n%s",
                                             text);

                                xmlFreeDoc (doc);
                                if (feature_list)
                                        g_list_free_full (feature_list,
                                                          g_object_unref);

                                return NULL;
                        }

                        object_ids = get_feature_object_ids (element);

                        feature = g_object_new (GUPNP_TYPE_FEATURE,
                                                "name", name,
                                                "version", version,
                                                "object-ids", object_ids,
                                                NULL);

                        feature_list = g_list_append (feature_list, feature);

                        g_free (object_ids);
                }
        }

        xmlFreeDoc (doc);

        return feature_list;
}