]> git.sesse.net Git - vlc/blob - modules/misc/xml/libxml.c
7cd55a4a13d35fa8712bfc65d568c15256a21ffc
[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 ReaderNextNode( xml_reader_t *, const char ** );
64 static char *ReaderValue( xml_reader_t * );
65 static const char *ReaderNextAttr( xml_reader_t * );
66
67 static int ReaderUseDTD ( xml_reader_t * );
68
69 static void CatalogLoad( xml_t *, const char * );
70 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
71 static int StreamRead( void *p_context, char *p_buffer, int i_buffer );
72
73 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
74
75 /*****************************************************************************
76  * Module initialization
77  *****************************************************************************/
78 static int Open( vlc_object_t *p_this )
79 {
80     xml_t *p_xml = (xml_t *)p_this;
81
82     if( !xmlHasFeature( XML_WITH_THREAD ) )
83         return VLC_EGENERIC;
84
85     vlc_mutex_lock( &lock );
86     xmlInitParser();
87     vlc_mutex_unlock( &lock );
88
89     p_xml->pf_catalog_load = CatalogLoad;
90     p_xml->pf_catalog_add  = CatalogAdd;
91
92     return VLC_SUCCESS;
93 }
94
95 /*****************************************************************************
96  * Module deinitialization
97  *****************************************************************************/
98 static void Close( vlc_object_t *p_this )
99 {
100 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
101     vlc_mutex_lock( &lock );
102     xmlCleanupParser();
103     vlc_mutex_unlock( &lock );
104 #endif
105     VLC_UNUSED(p_this);
106     return;
107 }
108
109 /*****************************************************************************
110  * Catalogue functions
111  *****************************************************************************/
112 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
113 {
114     VLC_UNUSED(p_xml);
115     if( !psz_filename ) xmlInitializeCatalog();
116     else xmlLoadCatalog( psz_filename );
117 }
118
119 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
120                           const char *psz_arg2, const char *psz_filename )
121 {
122     VLC_UNUSED(p_xml);
123     xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
124         (unsigned char*)psz_filename );
125 }
126
127 /*****************************************************************************
128  * Reader functions
129  *****************************************************************************/
130 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
131                                 xmlParserSeverities severity,
132                                 xmlTextReaderLocatorPtr locator)
133 {
134     VLC_UNUSED(severity);
135     xml_reader_t *p_reader = (xml_reader_t *)p_arg;
136     int line = xmlTextReaderLocatorLineNumber( locator );
137     msg_Err( p_reader, "XML parser error (line %d) : %s", line, p_msg );
138 }
139
140 struct xml_reader_sys_t
141 {
142     xmlTextReaderPtr xml;
143     char *node;
144 };
145
146 static int ReaderOpen( vlc_object_t *p_this )
147 {
148     xml_reader_t *p_reader = (xml_reader_t *)p_this;
149     xml_reader_sys_t *p_sys = malloc( sizeof( *p_sys ) );
150     xmlTextReaderPtr p_libxml_reader;
151
152     if( unlikely(!p_sys) )
153         return VLC_ENOMEM;
154
155     if( !xmlHasFeature( XML_WITH_THREAD ) )
156     {
157         free( p_sys );
158         return VLC_EGENERIC;
159     }
160
161     vlc_mutex_lock( &lock );
162     xmlInitParser();
163     vlc_mutex_unlock( &lock );
164
165     p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_reader->p_stream,
166                                       NULL, NULL, 0 );
167     if( !p_libxml_reader )
168     {
169         free( p_sys );
170         return VLC_ENOMEM;
171     }
172
173     /* Set the error handler */
174     xmlTextReaderSetErrorHandler( p_libxml_reader,
175                                   ReaderErrorHandler, p_reader );
176
177     p_sys->xml = p_libxml_reader;
178     p_sys->node = NULL;
179     p_reader->p_sys = p_sys;
180     p_reader->pf_next_node = ReaderNextNode;
181     p_reader->pf_value = ReaderValue;
182     p_reader->pf_next_attr = ReaderNextAttr;
183     p_reader->pf_use_dtd = ReaderUseDTD;
184
185     return VLC_SUCCESS;
186 }
187
188 static void ReaderClose( vlc_object_t *p_this )
189 {
190     xml_reader_t *p_reader = (xml_reader_t *)p_this;
191     xml_reader_sys_t *p_sys = p_reader->p_sys;
192
193     xmlFreeTextReader( p_sys->xml );
194 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
195     vlc_mutex_lock( &lock );
196     xmlCleanupParser();
197     vlc_mutex_unlock( &lock );
198 #endif
199     free( p_sys->node );
200     free( p_sys );
201 }
202
203 static int ReaderUseDTD ( xml_reader_t *p_reader )
204 {
205     /* Activate DTD validation */
206     xmlTextReaderSetParserProp( p_reader->p_sys->xml,
207                                 XML_PARSER_DEFAULTATTRS, true );
208     xmlTextReaderSetParserProp( p_reader->p_sys->xml,
209                                 XML_PARSER_VALIDATE, true );
210
211     return VLC_SUCCESS;
212 }
213
214 static int ReaderNextNode( xml_reader_t *p_reader, const char **pval )
215 {
216     xml_reader_sys_t *p_sys = p_reader->p_sys;
217     const xmlChar *node;
218     int ret;
219
220     free( p_sys->node );
221     p_sys->node = NULL;
222
223 skip:
224     switch( xmlTextReaderRead( p_sys->xml ) )
225     {
226         case 0: /* EOF */
227             return 0;
228         case -1: /* error */
229             return -1;
230     }
231
232     switch( xmlTextReaderNodeType( p_sys->xml ) )
233     {
234         case XML_READER_TYPE_ELEMENT:
235             node = xmlTextReaderConstName( p_sys->xml );
236             ret = XML_READER_STARTELEM;
237             break;
238
239         case XML_READER_TYPE_END_ELEMENT:
240             node = xmlTextReaderConstName( p_sys->xml );
241             ret = XML_READER_ENDELEM;
242             break;
243
244         case XML_READER_TYPE_CDATA:
245         case XML_READER_TYPE_TEXT:
246             node = xmlTextReaderConstValue( p_sys->xml );
247             ret = XML_READER_TEXT;
248             break;
249
250         case -1:
251             return -1;
252
253         default:
254             goto skip;
255     }
256
257     if( unlikely(node == NULL) )
258         return -1;
259
260     p_sys->node = strdup( (const char *)node );
261     if( pval != NULL )
262         *pval = p_sys->node;
263     return likely(p_sys->node != NULL) ? ret : -1;
264 }
265
266 static char *ReaderValue( xml_reader_t *p_reader )
267 {
268     const xmlChar *psz_value =
269         xmlTextReaderConstValue( p_reader->p_sys->xml );
270
271     return psz_value ? strdup( (const char *)psz_value ) : NULL;
272 }
273
274 static const char *ReaderNextAttr( xml_reader_t *p_reader )
275 {
276     if( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->xml ) != 1 )
277         return NULL;
278     return (const char *)xmlTextReaderConstName( p_reader->p_sys->xml );
279 }
280
281 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
282 {
283     stream_t *s = (stream_t*)p_context;
284     return stream_Read( s, p_buffer, i_buffer );
285 }