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 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 * );
69 static int ReaderUseDTD ( xml_reader_t * );
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 );
75 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
77 /*****************************************************************************
78 * Module initialization
79 *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
82 xml_t *p_xml = (xml_t *)p_this;
84 if( !xmlHasFeature( XML_WITH_THREAD ) )
87 vlc_mutex_lock( &lock );
89 vlc_mutex_unlock( &lock );
91 p_xml->pf_catalog_load = CatalogLoad;
92 p_xml->pf_catalog_add = CatalogAdd;
97 /*****************************************************************************
98 * Module deinitialization
99 *****************************************************************************/
100 static void Close( vlc_object_t *p_this )
102 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
103 vlc_mutex_lock( &lock );
105 vlc_mutex_unlock( &lock );
111 /*****************************************************************************
112 * Catalogue functions
113 *****************************************************************************/
114 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
117 if( !psz_filename ) xmlInitializeCatalog();
118 else xmlLoadCatalog( psz_filename );
121 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
122 const char *psz_arg2, const char *psz_filename )
125 xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
126 (unsigned char*)psz_filename );
129 /*****************************************************************************
131 *****************************************************************************/
132 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
133 xmlParserSeverities severity,
134 xmlTextReaderLocatorPtr locator)
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 );
142 static int ReaderOpen( vlc_object_t *p_this )
144 xml_reader_t *p_reader = (xml_reader_t *)p_this;
145 xmlTextReaderPtr p_libxml_reader;
147 if( !xmlHasFeature( XML_WITH_THREAD ) )
150 vlc_mutex_lock( &lock );
152 vlc_mutex_unlock( &lock );
154 p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_reader->p_stream,
156 if( !p_libxml_reader )
158 msg_Err( p_this, "failed to create XML parser" );
162 p_reader->p_sys = (void *)p_libxml_reader;
164 /* Set the error handler */
165 xmlTextReaderSetErrorHandler( p_libxml_reader,
166 ReaderErrorHandler, p_reader );
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;
178 static void ReaderClose( vlc_object_t *p_this )
180 xml_reader_t *p_reader = (xml_reader_t *)p_this;
182 xmlFreeTextReader( (void *)p_reader->p_sys );
183 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
184 vlc_mutex_lock( &lock );
186 vlc_mutex_unlock( &lock );
190 static int ReaderUseDTD ( xml_reader_t *p_reader )
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 );
201 static int ReaderRead( xml_reader_t *p_reader )
203 int i_ret = xmlTextReaderRead( (void *)p_reader->p_sys );
215 static int ReaderNodeType( xml_reader_t *p_reader )
217 int i_ret = xmlTextReaderNodeType( (void *)p_reader->p_sys );
221 case XML_READER_TYPE_ELEMENT:
222 i_ret = XML_READER_STARTELEM;
224 case XML_READER_TYPE_END_ELEMENT:
225 i_ret = XML_READER_ENDELEM;
227 case XML_READER_TYPE_CDATA:
228 case XML_READER_TYPE_TEXT:
229 i_ret = XML_READER_TEXT;
235 i_ret = XML_READER_NONE;
242 static char *ReaderName( xml_reader_t *p_reader )
244 const xmlChar *psz_name =
245 xmlTextReaderConstName( (void *)p_reader->p_sys );
247 return psz_name ? strdup( (const char *)psz_name ) : NULL;
250 static char *ReaderValue( xml_reader_t *p_reader )
252 const xmlChar *psz_value =
253 xmlTextReaderConstValue( (void *)p_reader->p_sys );
255 return psz_value ? strdup( (const char *)psz_value ) : NULL;
258 static int ReaderNextAttr( xml_reader_t *p_reader )
260 return ( xmlTextReaderMoveToNextAttribute( (void *)p_reader->p_sys )
261 == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
264 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
266 stream_t *s = (stream_t*)p_context;
267 return stream_Read( s, p_buffer, i_buffer );