summaryrefslogtreecommitdiff
path: root/libmateweather/parser.c
blob: 9e4bb63460fdef55c8db85223a3a91fd570736ba (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* parser.c - Locations.xml parser
 *
 * Copyright 2008, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see
 * <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define MATEWEATHER_I_KNOW_THIS_IS_UNSTABLE
#include "weather-priv.h"

#include "parser.h"

#include <string.h>
#include <glib.h>
#include <libxml/xmlreader.h>

/**
 * mateweather_parser_get_value:
 * @parser: a #MateWeatherParser
 *
 * Gets the text of the element whose start tag @parser is pointing to.
 * Leaves @parser pointing at the next node after the element's end tag.
 *
 * Return value: the text of the current node, as a libxml-allocated
 * string, or %NULL if the node is empty.
 **/
char *
mateweather_parser_get_value (MateWeatherParser *parser)
{
    char *value;

    /* check for null node */
    if (xmlTextReaderIsEmptyElement (parser->xml))
	return NULL;

    /* the next "node" is the text node containing the value we want to get */
    if (xmlTextReaderRead (parser->xml) != 1)
	return NULL;

    value = (char *) xmlTextReaderValue (parser->xml);

    /* move on to the end of this node */
    while (xmlTextReaderNodeType (parser->xml) != XML_READER_TYPE_END_ELEMENT) {
	if (xmlTextReaderRead (parser->xml) != 1) {
	    xmlFree (value);
	    return NULL;
	}
    }

    /* consume the end element too */
    if (xmlTextReaderRead (parser->xml) != 1) {
	xmlFree (value);
	return NULL;
    }

    return value;
}

/**
 * mateweather_parser_get_localized_value:
 * @parser: a #MateWeatherParser
 *
 * Looks at the name of the element @parser is currently pointing to, and
 * returns the content of either that node, or a following node with
 * the same name but an "xml:lang" attribute naming one of the locale
 * languages. Leaves @parser pointing to the next node after the last
 * consecutive element with the same name as the original element.
 *
 * Return value: the localized (or unlocalized) text, as a
 * libxml-allocated string, or %NULL if the node is empty.
 **/
char *
mateweather_parser_get_localized_value (MateWeatherParser *parser)
{
    const char *this_language;
    int best_match = INT_MAX;
    const char *lang, *tagname, *next_tagname;
    gboolean keep_going;
    char *name = NULL;
    int i;

    tagname = (const char *) xmlTextReaderConstName (parser->xml);

    do {
	/* First let's get the language */
	lang = (const char *) xmlTextReaderConstXmlLang (parser->xml);

	if (lang == NULL)
	    this_language = "C";
	else
	    this_language = lang;

	/* the next "node" is text node containing the actual name */
	if (xmlTextReaderRead (parser->xml) != 1) {
	    if (name)
		xmlFree (name);
	    return NULL;
	}

	for (i = 0; parser->locales[i] && i < best_match; i++) {
	    if (!strcmp (parser->locales[i], this_language)) {
		/* if we've already encounted a less accurate
		   translation, then free it */
		g_free (name);

		name = (char *) xmlTextReaderValue (parser->xml);
		best_match = i;

		break;
	    }
	}

	/* Skip to close tag */
	while (xmlTextReaderNodeType (parser->xml) != XML_READER_TYPE_END_ELEMENT) {
	    if (xmlTextReaderRead (parser->xml) != 1) {
		xmlFree (name);
		return NULL;
	    }
	}

	/* Skip junk */
	do {
	    if (xmlTextReaderRead (parser->xml) != 1) {
		xmlFree (name);
		return NULL;
	    }
	} while (xmlTextReaderNodeType (parser->xml) != XML_READER_TYPE_ELEMENT &&
		 xmlTextReaderNodeType (parser->xml) != XML_READER_TYPE_END_ELEMENT);

	/* if the next tag has the same name then keep going */
	next_tagname = (const char *) xmlTextReaderConstName (parser->xml);
	keep_going = !strcmp (next_tagname, tagname);

    } while (keep_going);

    return name;
}

MateWeatherParser *
mateweather_parser_new (gboolean use_regions)
{
    MateWeatherParser *parser;
    int zlib_support;
    int i, keep_going;
    char *filename;
    char *tagname, *format;
    time_t now;
    struct tm tm;

    parser = g_slice_new0 (MateWeatherParser);
    parser->use_regions = use_regions;
    parser->locales = g_get_language_names ();

    zlib_support = xmlHasFeature (XML_WITH_ZLIB);

    /* First try to load a locale-specific XML. It's much faster. */
    filename = NULL;
    for (i = 0; parser->locales[i] != NULL; i++) {
	filename = g_strdup_printf ("%s/Locations.%s.xml",
				    MATEWEATHER_XML_LOCATION_DIR,
				    parser->locales[i]);

	if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
	    break;

	g_free (filename);
	filename = NULL;

        if (!zlib_support)
            continue;

	filename = g_strdup_printf ("%s/Locations.%s.xml.gz",
				    MATEWEATHER_XML_LOCATION_DIR,
				    parser->locales[i]);

	if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
	    break;

	g_free (filename);
	filename = NULL;
    }

    /* Fall back on the file containing either all translations, or only
     * the english names (depending on the configure flags).
     */
    if (!filename)
	filename = g_build_filename (MATEWEATHER_XML_LOCATION_DIR, "Locations.xml", NULL);

    if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR) && zlib_support) {
        g_free (filename);
	filename = g_build_filename (MATEWEATHER_XML_LOCATION_DIR, "Locations.xml.gz", NULL);
    }

    /* Open the xml file containing the different locations */
    parser->xml = xmlNewTextReaderFilename (filename);
    g_free (filename);

    if (parser->xml == NULL)
	goto error_out;

    /* fast forward to the first element */
    do {
	/* if we encounter a problem here, exit right away */
	if (xmlTextReaderRead (parser->xml) != 1)
	    goto error_out;
    } while (xmlTextReaderNodeType (parser->xml) != XML_READER_TYPE_ELEMENT);

    /* check the name and format */
    tagname = (char *) xmlTextReaderName (parser->xml);
    keep_going = tagname && !strcmp (tagname, "mateweather");
    xmlFree (tagname);

    if (!keep_going)
	goto error_out;

    format = (char *) xmlTextReaderGetAttribute (parser->xml, (xmlChar *) "format");
    keep_going = format && !strcmp (format, "1.0");
    xmlFree (format);

    if (!keep_going)
	goto error_out;

    /* Get timestamps for the start and end of this year */
    now = time (NULL);
    tm = *gmtime (&now);
    tm.tm_mon = 0;
    tm.tm_mday = 1;
    tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
    parser->year_start = mktime (&tm);
    tm.tm_year++;
    parser->year_end = mktime (&tm);

    return parser;

error_out:
    mateweather_parser_free (parser);
    return NULL;
}

void
mateweather_parser_free (MateWeatherParser *parser)
{
    if (parser->xml)
	xmlFreeTextReader (parser->xml);
    g_slice_free (MateWeatherParser, parser);
}