]> git.sesse.net Git - vlc/blob - modules/misc/xml/libxml.c
d448cfcf487bbbf4b337117a3715026310d35db0
[vlc] / modules / misc / xml / libxml.c
1 /*****************************************************************************
2  * libxml.c: XML parser using libxml2
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30
31 #include <vlc_block.h>
32 #include <vlc_stream.h>
33 #include <vlc_xml.h>
34
35 #include <libxml/xmlreader.h>
36 #include <libxml/catalog.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
44 static int ReaderOpen( vlc_object_t * );
45 static void ReaderClose( vlc_object_t * );
46
47
48 vlc_module_begin ()
49     set_description( N_("XML Parser (using libxml2)") )
50     set_capability( "xml", 10 )
51     set_callbacks( Open, Close )
52
53 #ifdef WIN32
54     cannot_unload_broken_library()
55 #endif
56
57     add_submodule()
58     set_capability( "xml reader", 10 )
59     set_callbacks( ReaderOpen, ReaderClose )
60
61 vlc_module_end ()
62
63 static int ReaderRead( xml_reader_t * );
64 static int ReaderNodeType( xml_reader_t * );
65 static char *ReaderName( xml_reader_t * );
66 static char *ReaderValue( xml_reader_t * );
67 static int ReaderNextAttr( xml_reader_t * );
68
69 static int ReaderUseDTD ( xml_reader_t * );
70
71 static void CatalogLoad( xml_t *, const char * );
72 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
73 static int StreamRead( void *p_context, char *p_buffer, int i_buffer );
74
75 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
76
77 /*****************************************************************************
78  * Module initialization
79  *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
81 {
82     xml_t *p_xml = (xml_t *)p_this;
83
84     if( !xmlHasFeature( XML_WITH_THREAD ) )
85         return VLC_EGENERIC;
86
87     vlc_mutex_lock( &lock );
88     xmlInitParser();
89     vlc_mutex_unlock( &lock );
90
91     p_xml->pf_catalog_load = CatalogLoad;
92     p_xml->pf_catalog_add  = CatalogAdd;
93
94     return VLC_SUCCESS;
95 }
96
97 /*****************************************************************************
98  * Module deinitialization
99  *****************************************************************************/
100 static void Close( vlc_object_t *p_this )
101 {
102 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
103     vlc_mutex_lock( &lock );
104     xmlCleanupParser();
105     vlc_mutex_unlock( &lock );
106 #endif
107     VLC_UNUSED(p_this);
108     return;
109 }
110
111 /*****************************************************************************
112  * Catalogue functions
113  *****************************************************************************/
114 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
115 {
116     VLC_UNUSED(p_xml);
117     if( !psz_filename ) xmlInitializeCatalog();
118     else xmlLoadCatalog( psz_filename );
119 }
120
121 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
122                           const char *psz_arg2, const char *psz_filename )
123 {
124     VLC_UNUSED(p_xml);
125     xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
126         (unsigned char*)psz_filename );
127 }
128
129 /*****************************************************************************
130  * Reader functions
131  *****************************************************************************/
132 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
133                                 xmlParserSeverities severity,
134                                 xmlTextReaderLocatorPtr locator)
135 {
136     VLC_UNUSED(severity);
137     xml_reader_t *p_reader = (xml_reader_t *)p_arg;
138     int line = xmlTextReaderLocatorLineNumber( locator );
139     msg_Err( p_reader, "XML parser error (line %d) : %s", line, p_msg );
140 }
141
142 static int ReaderOpen( vlc_object_t *p_this )
143 {
144     xml_reader_t *p_reader = (xml_reader_t *)p_this;
145     xmlTextReaderPtr p_libxml_reader;
146
147     if( !xmlHasFeature( XML_WITH_THREAD ) )
148         return VLC_EGENERIC;
149
150     vlc_mutex_lock( &lock );
151     xmlInitParser();
152     vlc_mutex_unlock( &lock );
153
154     p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_reader->p_stream,
155                                       NULL, NULL, 0 );
156     if( !p_libxml_reader )
157     {
158         msg_Err( p_this, "failed to create XML parser" );
159         return VLC_ENOMEM;
160     }
161
162     p_reader->p_sys = (void *)p_libxml_reader;
163
164     /* Set the error handler */
165     xmlTextReaderSetErrorHandler( p_libxml_reader,
166                                   ReaderErrorHandler, p_reader );
167
168     p_reader->pf_read = ReaderRead;
169     p_reader->pf_node_type = ReaderNodeType;
170     p_reader->pf_name = ReaderName;
171     p_reader->pf_value = ReaderValue;
172     p_reader->pf_next_attr = ReaderNextAttr;
173     p_reader->pf_use_dtd = ReaderUseDTD;
174
175     return VLC_SUCCESS;
176 }
177
178 static void ReaderClose( vlc_object_t *p_this )
179 {
180     xml_reader_t *p_reader = (xml_reader_t *)p_this;
181
182 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
183     vlc_mutex_lock( &lock );
184     xmlCleanupParser();
185     vlc_mutex_unlock( &lock );
186 #endif
187     xmlFreeTextReader( (void *)p_reader->p_sys );
188 }
189
190 static int ReaderUseDTD ( xml_reader_t *p_reader )
191 {
192     /* Activate DTD validation */
193     xmlTextReaderSetParserProp( (void *)p_reader->p_sys,
194                                 XML_PARSER_DEFAULTATTRS, true );
195     xmlTextReaderSetParserProp( (void *)p_reader->p_sys,
196                                 XML_PARSER_VALIDATE, true );
197
198     return VLC_SUCCESS;
199 }
200
201 static int ReaderRead( xml_reader_t *p_reader )
202 {
203     int i_ret = xmlTextReaderRead( (void *)p_reader->p_sys );
204
205 #if 0
206     switch( i_ret )
207     {
208     default:
209     }
210 #endif
211
212     return i_ret;
213 }
214
215 static int ReaderNodeType( xml_reader_t *p_reader )
216 {
217     int i_ret = xmlTextReaderNodeType( (void *)p_reader->p_sys );
218
219     switch( i_ret )
220     {
221     case XML_READER_TYPE_ELEMENT:
222         i_ret = XML_READER_STARTELEM;
223         break;
224     case XML_READER_TYPE_END_ELEMENT:
225         i_ret = XML_READER_ENDELEM;
226         break;
227     case XML_READER_TYPE_CDATA:
228     case XML_READER_TYPE_TEXT:
229         i_ret = XML_READER_TEXT;
230         break;
231     case -1:
232         i_ret = -1;
233         break;
234     default:
235         i_ret = XML_READER_NONE;
236         break;
237     }
238
239     return i_ret;
240 }
241
242 static char *ReaderName( xml_reader_t *p_reader )
243 {
244     const xmlChar *psz_name =
245         xmlTextReaderConstName( (void *)p_reader->p_sys );
246
247     return psz_name ? strdup( (const char *)psz_name ) : NULL;
248 }
249
250 static char *ReaderValue( xml_reader_t *p_reader )
251 {
252     const xmlChar *psz_value =
253         xmlTextReaderConstValue( (void *)p_reader->p_sys );
254
255     return psz_value ? strdup( (const char *)psz_value ) : NULL;
256 }
257
258 static int ReaderNextAttr( xml_reader_t *p_reader )
259 {
260     return ( xmlTextReaderMoveToNextAttribute( (void *)p_reader->p_sys )
261              == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
262 }
263
264 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
265 {
266     stream_t *s = (stream_t*)p_context;
267     return stream_Read( s, p_buffer, i_buffer );
268 }