2003-03-25 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / bus / config-loader-libxml.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* config-loader-libxml.c  libxml2 XML loader
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
24 #include "config-parser.h"
25 #include <dbus/dbus-internals.h>
26 #include <libxml/xmlreader.h>
27 #include <libxml/parser.h>
28 #include <libxml/globals.h>
29 #include <errno.h>
30 #include <string.h>
31
32 static void
33 xml_text_reader_error (void                   *arg,
34                        const char             *msg,
35                        xmlParserSeverities     severity,
36                        xmlTextReaderLocatorPtr locator)
37 {
38   DBusError *error = arg;
39   
40   if (!dbus_error_is_set (error))
41     {
42       dbus_set_error (error, DBUS_ERROR_FAILED,
43                       "Error loading config file: %s",
44                       msg);
45     }
46 }
47
48 BusConfigParser*
49 bus_config_load (const DBusString *file,
50                  DBusError        *error)
51 {
52   xmlTextReader *reader;
53   const char *filename;
54   BusConfigParser *parser;
55   DBusError tmp_error;
56   
57   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
58   
59   _dbus_string_get_const_data (file, &filename);
60   
61   errno = 0;
62   reader = xmlNewTextReaderFilename (filename);
63
64   if (reader == NULL)
65     {
66       dbus_set_error (error, DBUS_ERROR_FAILED,
67                       "Failed to load configuration file %s: %s\n",
68                       filename,
69                       errno != 0 ? strerror (errno) : "Unknown error");
70         
71       return NULL;
72     }
73
74   dbus_error_init (&tmp_error);
75   xmlTextReaderSetErrorHandler (reader, xml_text_reader_error, &tmp_error);
76
77   while (xmlTextReaderRead(reader) == 1)
78     {
79       if (dbus_error_is_set (&tmp_error))
80         goto reader_out;
81
82       
83
84
85     }
86   
87  reader_out:
88   xmlFreeTextReader (reader);
89   if (dbus_error_is_set (&tmp_error))
90     {
91       dbus_move_error (&tmp_error, error);
92       goto failed;
93     }
94   
95   if (!bus_config_parser_finished (parser, error))
96     goto failed;
97
98   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
99   return parser;
100   
101  failed:
102   _DBUS_ASSERT_ERROR_IS_SET (error);
103   bus_config_parser_unref (parser);
104   return NULL;
105 }