1 /*****************************************************************************
2 * libxml.c: XML parser using libxml2
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
31 #include <vlc_block.h>
32 #include <vlc_stream.h>
35 #include <libxml/xmlreader.h>
36 #include <libxml/catalog.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
44 static int ReaderOpen( vlc_object_t * );
45 static void ReaderClose( vlc_object_t * );
49 set_description( N_("XML Parser (using libxml2)") )
50 set_capability( "xml", 10 )
51 set_callbacks( Open, Close )
54 cannot_unload_broken_library()
58 set_capability( "xml reader", 10 )
59 set_callbacks( ReaderOpen, ReaderClose )
63 static int ReaderNextNode( xml_reader_t *, const char ** );
64 static char *ReaderValue( xml_reader_t * );
65 static const char *ReaderNextAttr( xml_reader_t * );
67 static int ReaderUseDTD ( xml_reader_t * );
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 );
73 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
75 /*****************************************************************************
76 * Module initialization
77 *****************************************************************************/
78 static int Open( vlc_object_t *p_this )
80 xml_t *p_xml = (xml_t *)p_this;
82 if( !xmlHasFeature( XML_WITH_THREAD ) )
85 vlc_mutex_lock( &lock );
87 vlc_mutex_unlock( &lock );
89 p_xml->pf_catalog_load = CatalogLoad;
90 p_xml->pf_catalog_add = CatalogAdd;
95 /*****************************************************************************
96 * Module deinitialization
97 *****************************************************************************/
98 static void Close( vlc_object_t *p_this )
100 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
101 vlc_mutex_lock( &lock );
103 vlc_mutex_unlock( &lock );
109 /*****************************************************************************
110 * Catalogue functions
111 *****************************************************************************/
112 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
115 if( !psz_filename ) xmlInitializeCatalog();
116 else xmlLoadCatalog( psz_filename );
119 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
120 const char *psz_arg2, const char *psz_filename )
123 xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
124 (unsigned char*)psz_filename );
127 /*****************************************************************************
129 *****************************************************************************/
130 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
131 xmlParserSeverities severity,
132 xmlTextReaderLocatorPtr locator)
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 );
140 struct xml_reader_sys_t
142 xmlTextReaderPtr xml;
146 static int ReaderOpen( vlc_object_t *p_this )
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;
152 if( !xmlHasFeature( XML_WITH_THREAD ) )
155 vlc_mutex_lock( &lock );
157 vlc_mutex_unlock( &lock );
159 if( unlikely(!p_sys) )
162 p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_reader->p_stream,
164 if( !p_libxml_reader )
170 /* Set the error handler */
171 xmlTextReaderSetErrorHandler( p_libxml_reader,
172 ReaderErrorHandler, p_reader );
174 p_sys->xml = p_libxml_reader;
176 p_reader->p_sys = p_sys;
177 p_reader->pf_next_node = ReaderNextNode;
178 p_reader->pf_value = ReaderValue;
179 p_reader->pf_next_attr = ReaderNextAttr;
180 p_reader->pf_use_dtd = ReaderUseDTD;
185 static void ReaderClose( vlc_object_t *p_this )
187 xml_reader_t *p_reader = (xml_reader_t *)p_this;
188 xml_reader_sys_t *p_sys = p_reader->p_sys;
190 xmlFreeTextReader( p_sys->xml );
191 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
192 vlc_mutex_lock( &lock );
194 vlc_mutex_unlock( &lock );
200 static int ReaderUseDTD ( xml_reader_t *p_reader )
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 );
211 static int ReaderNextNode( xml_reader_t *p_reader, const char **pval )
213 xml_reader_sys_t *p_sys = p_reader->p_sys;
221 switch( xmlTextReaderRead( p_sys->xml ) )
229 switch( xmlTextReaderNodeType( p_sys->xml ) )
231 case XML_READER_TYPE_ELEMENT:
232 node = xmlTextReaderConstName( p_sys->xml );
233 ret = XML_READER_STARTELEM;
236 case XML_READER_TYPE_END_ELEMENT:
237 node = xmlTextReaderConstName( p_sys->xml );
238 ret = XML_READER_ENDELEM;
241 case XML_READER_TYPE_CDATA:
242 case XML_READER_TYPE_TEXT:
243 node = xmlTextReaderConstValue( p_sys->xml );
244 ret = XML_READER_TEXT;
254 if( unlikely(node == NULL) )
257 p_sys->node = strdup( (const char *)node );
260 return likely(p_sys->node != NULL) ? ret : -1;
263 static char *ReaderValue( xml_reader_t *p_reader )
265 const xmlChar *psz_value =
266 xmlTextReaderConstValue( p_reader->p_sys->xml );
268 return psz_value ? strdup( (const char *)psz_value ) : NULL;
271 static const char *ReaderNextAttr( xml_reader_t *p_reader )
273 if( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->xml ) != 1 )
275 return (const char *)xmlTextReaderConstName( p_reader->p_sys->xml );
278 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
280 stream_t *s = (stream_t*)p_context;
281 return stream_Read( s, p_buffer, i_buffer );