]> git.sesse.net Git - vlc/blob - modules/misc/xml/libxml.c
modules: factor Makefile statement
[vlc] / modules / misc / xml / libxml.c
1 /*****************************************************************************
2  * libxml.c: XML parser using libxml2
3  *****************************************************************************
4  * Copyright (C) 2004 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 const char *ReaderNextAttr( xml_reader_t *, const char ** );
65 static int ReaderIsEmptyElement( 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     if( !xmlHasFeature( XML_WITH_THREAD ) )
149         return VLC_EGENERIC;
150
151     xml_reader_t *p_reader = (xml_reader_t *)p_this;
152     xml_reader_sys_t *p_sys = malloc( sizeof( *p_sys ) );
153     xmlTextReaderPtr p_libxml_reader;
154
155     if( unlikely(!p_sys) )
156         return VLC_ENOMEM;
157
158     vlc_mutex_lock( &lock );
159     xmlInitParser();
160     vlc_mutex_unlock( &lock );
161
162     p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_reader->p_stream,
163                                       NULL, NULL, 0 );
164     if( !p_libxml_reader )
165     {
166         free( p_sys );
167         return VLC_ENOMEM;
168     }
169
170     /* Set the error handler */
171     xmlTextReaderSetErrorHandler( p_libxml_reader,
172                                   ReaderErrorHandler, p_reader );
173
174     p_sys->xml = p_libxml_reader;
175     p_sys->node = NULL;
176     p_reader->p_sys = p_sys;
177     p_reader->pf_next_node = ReaderNextNode;
178     p_reader->pf_next_attr = ReaderNextAttr;
179     p_reader->pf_is_empty = ReaderIsEmptyElement;
180     p_reader->pf_use_dtd = ReaderUseDTD;
181
182     return VLC_SUCCESS;
183 }
184
185 static void ReaderClose( vlc_object_t *p_this )
186 {
187     xml_reader_t *p_reader = (xml_reader_t *)p_this;
188     xml_reader_sys_t *p_sys = p_reader->p_sys;
189
190     xmlFreeTextReader( p_sys->xml );
191 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
192     vlc_mutex_lock( &lock );
193     xmlCleanupParser();
194     vlc_mutex_unlock( &lock );
195 #endif
196     free( p_sys->node );
197     free( p_sys );
198 }
199
200 static int ReaderUseDTD ( xml_reader_t *p_reader )
201 {
202     /* Activate DTD validation */
203     xmlTextReaderSetParserProp( p_reader->p_sys->xml,
204                                 XML_PARSER_DEFAULTATTRS, true );
205     xmlTextReaderSetParserProp( p_reader->p_sys->xml,
206                                 XML_PARSER_VALIDATE, true );
207
208     return VLC_SUCCESS;
209 }
210
211 static int ReaderNextNode( xml_reader_t *p_reader, const char **pval )
212 {
213     xml_reader_sys_t *p_sys = p_reader->p_sys;
214     const xmlChar *node;
215     int ret;
216
217     free( p_sys->node );
218     p_sys->node = NULL;
219
220 skip:
221     switch( xmlTextReaderRead( p_sys->xml ) )
222     {
223         case 0: /* EOF */
224             return 0;
225         case -1: /* error */
226             return -1;
227     }
228
229     switch( xmlTextReaderNodeType( p_sys->xml ) )
230     {
231         case XML_READER_TYPE_ELEMENT:
232             node = xmlTextReaderConstName( p_sys->xml );
233             ret = XML_READER_STARTELEM;
234             break;
235
236         case XML_READER_TYPE_END_ELEMENT:
237             node = xmlTextReaderConstName( p_sys->xml );
238             ret = XML_READER_ENDELEM;
239             break;
240
241         case XML_READER_TYPE_CDATA:
242         case XML_READER_TYPE_TEXT:
243             node = xmlTextReaderConstValue( p_sys->xml );
244             ret = XML_READER_TEXT;
245             break;
246
247         case -1:
248             return -1;
249
250         default:
251             goto skip;
252     }
253
254     if( unlikely(node == NULL) )
255         return -1;
256
257     p_sys->node = strdup( (const char *)node );
258     if( pval != NULL )
259         *pval = p_sys->node;
260     return likely(p_sys->node != NULL) ? ret : -1;
261 }
262
263 #if 0
264 static char *ReaderValue( xml_reader_t *p_reader )
265 {
266     const xmlChar *psz_value =
267         xmlTextReaderConstValue( p_reader->p_sys->xml );
268
269     return psz_value ? strdup( (const char *)psz_value ) : NULL;
270 }
271 #endif
272
273 static const char *ReaderNextAttr( xml_reader_t *p_reader, const char **pval )
274 {
275     xmlTextReaderPtr xml = p_reader->p_sys->xml;
276     const xmlChar *name, *value;
277
278     if( xmlTextReaderMoveToNextAttribute( xml ) != 1
279      || (name = xmlTextReaderConstName( xml )) == NULL
280      || (value = xmlTextReaderConstValue( xml )) == NULL )
281         return NULL;
282
283     *pval = (const char *)value;
284     return (const char *)name;
285 }
286
287 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
288 {
289     stream_t *s = (stream_t*)p_context;
290     return stream_Read( s, p_buffer, i_buffer );
291 }
292
293 static int ReaderIsEmptyElement( xml_reader_t *p_reader )
294 {
295     return xmlTextReaderIsEmptyElement( p_reader->p_sys->xml );
296 }