1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* config-loader-expat.c expat XML loader
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config-parser.h"
25 #include <dbus/dbus-internals.h>
28 static XML_Memory_Handling_Suite memsuite =
37 BusConfigParser *parser;
45 process_content (ExpatParseContext *context)
50 if (_dbus_string_get_length (&context->content) > 0)
52 if (!bus_config_parser_content (context->parser,
56 context->failed = TRUE;
59 _dbus_string_set_length (&context->content, 0);
66 expat_StartElementHandler (void *userData,
68 const XML_Char **atts)
70 ExpatParseContext *context = userData;
75 /* Expat seems to suck and can't abort the parse if we
76 * throw an error. Expat 2.0 is supposed to fix this.
81 if (!process_content (context))
84 /* "atts" is key, value, key, value, NULL */
85 for (i = 0; atts[i] != NULL; ++i)
88 _dbus_assert (i % 2 == 0);
89 names = dbus_new0 (char *, i / 2 + 1);
90 values = dbus_new0 (char *, i / 2 + 1);
92 if (names == NULL || values == NULL)
94 dbus_set_error (context->error, DBUS_ERROR_NO_MEMORY, NULL);
95 context->failed = TRUE;
102 while (atts[i] != NULL)
104 _dbus_assert (i % 2 == 0);
105 names [i / 2] = (char*) atts[i];
106 values[i / 2] = (char*) atts[i+1];
111 if (!bus_config_parser_start_element (context->parser,
113 (const char **) names,
114 (const char **) values,
119 context->failed = TRUE;
128 expat_EndElementHandler (void *userData,
129 const XML_Char *name)
131 ExpatParseContext *context = userData;
133 if (!process_content (context))
136 if (!bus_config_parser_end_element (context->parser,
140 context->failed = TRUE;
145 /* s is not 0 terminated. */
147 expat_CharacterDataHandler (void *userData,
151 ExpatParseContext *context = userData;
155 if (!_dbus_string_append_len (&context->content,
158 dbus_set_error (context->error, DBUS_ERROR_NO_MEMORY, NULL);
159 context->failed = TRUE;
166 bus_config_load (const DBusString *file,
167 dbus_bool_t is_toplevel,
171 const char *filename;
172 BusConfigParser *parser;
173 ExpatParseContext context;
176 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
180 context.error = error;
181 context.failed = FALSE;
183 filename = _dbus_string_get_const_data (file);
185 if (!_dbus_string_init (&context.content))
187 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
191 if (!_dbus_string_init (&dirname))
193 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
194 _dbus_string_free (&context.content);
198 expat = XML_ParserCreate_MM ("UTF-8", &memsuite, NULL);
201 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
205 if (!_dbus_string_get_dirname (file, &dirname))
207 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
211 parser = bus_config_parser_new (&dirname, is_toplevel);
214 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
217 context.parser = parser;
219 XML_SetUserData (expat, &context);
220 XML_SetElementHandler (expat,
221 expat_StartElementHandler,
222 expat_EndElementHandler);
223 XML_SetCharacterDataHandler (expat,
224 expat_CharacterDataHandler);
228 const char *data_str;
230 if (!_dbus_string_init (&data))
232 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
236 if (!_dbus_file_get_contents (&data, file, error))
238 _dbus_string_free (&data);
242 data_str = _dbus_string_get_const_data (&data);
244 if (!XML_Parse (expat, data_str, _dbus_string_get_length (&data), TRUE))
246 if (context.error != NULL &&
247 !dbus_error_is_set (context.error))
251 e = XML_GetErrorCode (expat);
252 if (e == XML_ERROR_NO_MEMORY)
253 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
255 dbus_set_error (error, DBUS_ERROR_FAILED,
256 "Error in file %s, line %d, column %d: %s\n",
258 XML_GetCurrentLineNumber (expat),
259 XML_GetCurrentColumnNumber (expat),
260 XML_ErrorString (e));
263 _dbus_string_free (&data);
267 _dbus_string_free (&data);
273 if (!bus_config_parser_finished (parser, error))
276 _dbus_string_free (&dirname);
277 _dbus_string_free (&context.content);
278 XML_ParserFree (expat);
280 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
284 _DBUS_ASSERT_ERROR_IS_SET (error);
286 _dbus_string_free (&dirname);
287 _dbus_string_free (&context.content);
289 XML_ParserFree (expat);
291 bus_config_parser_unref (parser);