summaryrefslogtreecommitdiffstats
path: root/src/lib/event_xml_parser.c
blob: 8bb8902719d3cae95cbdf1ee7098995b989b082b (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
    Copyright (C) 2011  ABRT Team
    Copyright (C) 2011  RedHat inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "abrtlib.h"
#include "event_config.h"

#define EVENT_ELEMENT           "event"
#define LABEL_ELEMENT           "label"
#define DESCRIPTION_ELEMENT     "description"
#define ALLOW_EMPTY_ELEMENT     "allow-empty"
#define OPTION_ELEMENT          "option"
//#define ACTION_ELEMENT        "action"
#define NAME_ELEMENT            "name"
#define DEFAULT_VALUE_ELEMENT   "default-value"

struct my_parse_data
{
    event_config_t *event_config;
    event_option_t *cur_option;
};

static char *elem_lang;
static char *locale;

static const char *const option_types[] =
{
    "text",
    "bool",
    "password",
    "number",
    NULL
};

static char * get_element_lang(const gchar **att_names, const gchar **att_values)
{
    char * short_locale_end = strchr(locale, '_');
    VERB2 log("locale: %s", locale);
    int i;
    for (i = 0; att_names[i] != NULL; ++i)
    {
        VERB2 log("attr: %s:%s", att_names[i], att_values[i]);
        if (strcmp(att_names[i], "xml:lang") == 0)
        {
            if (strcmp(att_values[i], locale) == 0)
            {
                VERB2 log("found translation for: %s", locale);
                return xstrdup(att_values[i]);
            }

            /* try to match shorter locale
             * e.g: "cs" with cs_CZ
            */
            if (strncmp(att_values[i], locale, short_locale_end-locale) == 0)
            {
                VERB2 log("found translation for shortlocale: %s", locale);
                return xstrndup(att_values[i], short_locale_end-locale);
            }
        }
    }
    /* if the element has no attribute then it's a default non-localized value */
    if (i == 0)
        return xstrdup("C");
    /* if the element is in different language then the current locale */
    return NULL;
}

static int cmp_event_option_name_with_string(gconstpointer a, gconstpointer b)
{
    return strcmp(((event_option_t *)a)->name, (char *)b);
}

static void consume_cur_option(struct my_parse_data *parse_data)
{
    event_option_t *opt = parse_data->cur_option;
    if (!opt)
        return;

    parse_data->cur_option = NULL;

    if (!opt->name)
    {
//TODO: "option w/o name" error msg?
        free_event_option(opt);
        return;
    }

    event_config_t *event_config = parse_data->event_config;
    GList *elem = g_list_find_custom(event_config->options, opt->name,
                                     &cmp_event_option_name_with_string);
    if (elem)
    {
        /* we already have option with such name */
        event_option_t *old_opt = elem->data;
        if (old_opt->value)
        {
            /* ...and it already has a value, which
             * overrides xml-defined default one:
             */
            free(opt->value);
            opt->value = old_opt->value;
            old_opt->value = NULL;
        }
        //log("xml: replacing '%s' value:'%s'->'%s'", opt->name, old_opt->value, opt->value);
        free_event_option(old_opt);
        elem->data = opt;
    }
    else
    {
        //log("xml: new value %s='%s'", opt->name, opt->value);
        event_config->options = g_list_append(event_config->options, opt);
    }
}

// Called for open tags <foo bar="baz">
static void start_element(GMarkupParseContext *context,
                  const gchar *element_name,
                  const gchar **attribute_names,
                  const gchar **attribute_values,
                  gpointer user_data,
                  GError **error)
{
    //log("start: %s", element_name);

    struct my_parse_data *parse_data = user_data;

    if (strcmp(element_name, OPTION_ELEMENT) == 0)
    {
        if (parse_data->cur_option)
        {
            error_msg("error, option nested in option");
            return;
        }

        event_option_t *opt = parse_data->cur_option = new_event_option();
        int i;

        for (i = 0; attribute_names[i] != NULL; ++i)
        {
            VERB2 log("attr: %s:%s", attribute_names[i], attribute_values[i]);
            if (strcmp(attribute_names[i], "name") == 0)
            {
                free(opt->name);
                opt->name = xstrdup(attribute_values[i]);
            }
            else if (strcmp(attribute_names[i], "type") == 0)
            {
                option_type_t type;
                for (type = OPTION_TYPE_TEXT; type < OPTION_TYPE_INVALID; ++type)
                {
                    if (strcmp(option_types[type], attribute_values[i]) == 0)
                        opt->type = type;
                }
            }
        }
    }
    else if (strcmp(element_name, LABEL_ELEMENT) == 0)
    {
        elem_lang = get_element_lang(attribute_names, attribute_values);
    }
    else if (strcmp(element_name, DESCRIPTION_ELEMENT) == 0)
    {
        elem_lang = get_element_lang(attribute_names, attribute_values);
    }

}

// Called for close tags </foo>
static void end_element(GMarkupParseContext *context,
                          const gchar         *element_name,
                          gpointer             user_data,
                          GError             **error)
{
    struct my_parse_data *parse_data = user_data;

    if (elem_lang != NULL)
    {
        free(elem_lang);
        elem_lang = NULL;
    }

    if (strcmp(element_name, OPTION_ELEMENT) == 0)
    {
        consume_cur_option(parse_data);
    }
    if (strcmp(element_name, EVENT_ELEMENT) == 0)
    {
        consume_cur_option(parse_data);
    }
}

// Called for character data
// text is not nul-terminated
static void text(GMarkupParseContext *context,
         const gchar         *text,
         gsize                text_len,
         gpointer             user_data,
         GError             **error)
{
    struct my_parse_data *parse_data = user_data;
    event_config_t *ui = parse_data->event_config;

    const gchar *inner_element = g_markup_parse_context_get_element(context);
    char *text_copy = xstrndup(text, text_len);
    event_option_t *opt = parse_data->cur_option;
    if (opt)
    {
        if (strcmp(inner_element, LABEL_ELEMENT) == 0)
        {
            VERB2 log("new label:'%s'", text_copy);
            /* set the value only if we found a value for the current locale (elem_lang != "C")
             * OR
             * the label is still NULL and we found the default "C" value
            */
            if (elem_lang != NULL && ((strcmp(elem_lang, "C") != 0)
            || (opt->label == NULL && (strcmp(elem_lang, "C") == 0))))
            {
                free(opt->label);
                opt->label = text_copy;
            }
            return;
        }
        /*
        * we can add a separate filed for the default value
        * in that case we can implement features like "reset to default value"
        * but for now using "value" should be enough and clients doesn't
        * have to know about the "defaul-value"
        */
        if (strcmp(inner_element, DEFAULT_VALUE_ELEMENT) == 0)
        {
            VERB2 log("default value:'%s'", text_copy);
            free(opt->value);
            opt->value = text_copy;
            return;
        }

        if (strcmp(inner_element, ALLOW_EMPTY_ELEMENT) == 0)
        {
            VERB2 log("allow-empty:'%s'", text_copy);
            opt->allow_empty = string_to_bool(text_copy);
            return;
        }
        /*
        if (strcmp(inner_element, DESCRIPTION_ELEMENT) == 0)
        {
            VERB2 log("tooltip:'%s'", text_copy);
            free(opt->description);
            opt->description = text_copy;
            return;
        }
        */
    }
    else
    {
        /* we're not in option, so the description is for the event */
        /*
        if (strcmp(inner_element, ACTION_ELEMENT) == 0)
        {
            VERB2 log("action description:'%s'", text_copy);
            free(ui->action);
            ui->action = text_copy;
            return;
        }
        */
        if (strcmp(inner_element, NAME_ELEMENT) == 0)
        {
            VERB2 log("event name:'%s'", text_copy);
            free(ui->screen_name);
            ui->screen_name = text_copy;
            return;
        }
        if (strcmp(inner_element, DESCRIPTION_ELEMENT) == 0)
        {
            VERB2 log("event description:'%s'", text_copy);
            if (elem_lang != NULL && ((strcmp(elem_lang, "C") != 0)
            || (ui->description == NULL && (strcmp(elem_lang, "C") == 0))))
            {
                free(ui->description);
                ui->description = text_copy;
            }
            return;
        }
    }
    free(text_copy);
}

  // Called for strings that should be re-saved verbatim in this same
  // position, but are not otherwise interpretable.  At the moment
  // this includes comments and processing instructions.
  // text is not nul-terminated
static void passthrough(GMarkupParseContext *context,
                const gchar *passthrough_text,
                gsize text_len,
                gpointer user_data,
                GError **error)
{
    VERB2 log("passthrough");
}

// Called on error, including one set by other
// methods in the vtable. The GError should not be freed.
static void error(GMarkupParseContext *context,
          GError *error,
          gpointer user_data)
{
    error_msg("error in XML parsing");
}

/* this function takes 2 parameters
 * ui -> pointer to event_config_t
 * filename -> filename to read
 * event_config_t contains list of options, which is malloced by hits function
 * and must be freed by the caller
 */

void load_event_description_from_file(event_config_t *event_config, const char* filename)
{
    struct my_parse_data parse_data = { event_config, NULL };
    locale = setlocale(LC_ALL, "");
    GMarkupParser parser;
    memset(&parser, 0, sizeof(parser)); /* just in case */
    parser.start_element = &start_element;
    parser.end_element = &end_element;
    parser.text = &text;
    parser.passthrough = &passthrough;
    parser.error = &error;

    GMarkupParseContext *context = g_markup_parse_context_new(
                    &parser, G_MARKUP_TREAT_CDATA_AS_TEXT,
                    &parse_data, /*GDestroyNotify:*/ NULL);

    FILE* fin = fopen(filename, "r");
    if (fin != NULL)
    {
        size_t read_bytes = 0;
        char buff[1024];
        while ((read_bytes = fread(buff, 1, 1024, fin)) != 0)
        {
            g_markup_parse_context_parse(context, buff, read_bytes, NULL);
        }
        fclose(fin);
    }

    g_markup_parse_context_free(context);

    consume_cur_option(&parse_data); /* just in case */
}