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 * );
64 static char *ReaderName( xml_reader_t * );
65 static char *ReaderValue( xml_reader_t * );
66 static const char *ReaderNextAttr( xml_reader_t * );
68 static int ReaderUseDTD ( xml_reader_t * );
70 static void CatalogLoad( xml_t *, const char * );
71 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
72 static int StreamRead( void *p_context, char *p_buffer, int i_buffer );
74 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
76 /*****************************************************************************
77 * Module initialization
78 *****************************************************************************/
79 static int Open( vlc_object_t *p_this )
81 xml_t *p_xml = (xml_t *)p_this;
83 if( !xmlHasFeature( XML_WITH_THREAD ) )
86 vlc_mutex_lock( &lock );
88 vlc_mutex_unlock( &lock );
90 p_xml->pf_catalog_load = CatalogLoad;
91 p_xml->pf_catalog_add = CatalogAdd;
96 /*****************************************************************************
97 * Module deinitialization
98 *****************************************************************************/
99 static void Close( vlc_object_t *p_this )
101 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
102 vlc_mutex_lock( &lock );
104 vlc_mutex_unlock( &lock );
110 /*****************************************************************************
111 * Catalogue functions
112 *****************************************************************************/
113 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
116 if( !psz_filename ) xmlInitializeCatalog();
117 else xmlLoadCatalog( psz_filename );
120 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
121 const char *psz_arg2, const char *psz_filename )
124 xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
125 (unsigned char*)psz_filename );
128 /*****************************************************************************
130 *****************************************************************************/
131 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
132 xmlParserSeverities severity,
133 xmlTextReaderLocatorPtr locator)
135 VLC_UNUSED(severity);
136 xml_reader_t *p_reader = (xml_reader_t *)p_arg;
137 int line = xmlTextReaderLocatorLineNumber( locator );
138 msg_Err( p_reader, "XML parser error (line %d) : %s", line, p_msg );
141 static int ReaderOpen( vlc_object_t *p_this )
143 xml_reader_t *p_reader = (xml_reader_t *)p_this;
144 xmlTextReaderPtr p_libxml_reader;
146 if( !xmlHasFeature( XML_WITH_THREAD ) )
149 vlc_mutex_lock( &lock );
151 vlc_mutex_unlock( &lock );
153 p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_reader->p_stream,
155 if( !p_libxml_reader )
157 msg_Err( p_this, "failed to create XML parser" );
161 p_reader->p_sys = (void *)p_libxml_reader;
163 /* Set the error handler */
164 xmlTextReaderSetErrorHandler( p_libxml_reader,
165 ReaderErrorHandler, p_reader );
167 p_reader->pf_next_node = ReaderNextNode;
168 p_reader->pf_name = ReaderName;
169 p_reader->pf_value = ReaderValue;
170 p_reader->pf_next_attr = ReaderNextAttr;
171 p_reader->pf_use_dtd = ReaderUseDTD;
176 static void ReaderClose( vlc_object_t *p_this )
178 xml_reader_t *p_reader = (xml_reader_t *)p_this;
180 xmlFreeTextReader( (void *)p_reader->p_sys );
181 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
182 vlc_mutex_lock( &lock );
184 vlc_mutex_unlock( &lock );
188 static int ReaderUseDTD ( xml_reader_t *p_reader )
190 /* Activate DTD validation */
191 xmlTextReaderSetParserProp( (void *)p_reader->p_sys,
192 XML_PARSER_DEFAULTATTRS, true );
193 xmlTextReaderSetParserProp( (void *)p_reader->p_sys,
194 XML_PARSER_VALIDATE, true );
199 static int ReaderNextNode( xml_reader_t *p_reader )
202 switch( xmlTextReaderRead( (void *)p_reader->p_sys ) )
210 switch( xmlTextReaderNodeType( (void *)p_reader->p_sys ) )
212 case XML_READER_TYPE_ELEMENT:
213 return XML_READER_STARTELEM;
214 case XML_READER_TYPE_END_ELEMENT:
215 return XML_READER_ENDELEM;
216 case XML_READER_TYPE_CDATA:
217 case XML_READER_TYPE_TEXT:
218 return XML_READER_TEXT;
226 static char *ReaderName( xml_reader_t *p_reader )
228 const xmlChar *psz_name =
229 xmlTextReaderConstName( (void *)p_reader->p_sys );
231 return psz_name ? strdup( (const char *)psz_name ) : NULL;
234 static char *ReaderValue( xml_reader_t *p_reader )
236 const xmlChar *psz_value =
237 xmlTextReaderConstValue( (void *)p_reader->p_sys );
239 return psz_value ? strdup( (const char *)psz_value ) : NULL;
242 static const char *ReaderNextAttr( xml_reader_t *p_reader )
244 if( xmlTextReaderMoveToNextAttribute( (void *)p_reader->p_sys ) != 1 )
246 return (const char *)xmlTextReaderConstValue( (void *)p_reader->p_sys );
249 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
251 stream_t *s = (stream_t*)p_context;
252 return stream_Read( s, p_buffer, i_buffer );