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 * );
45 set_description( N_("XML Parser (using libxml2)") )
46 set_capability( "xml", 10 )
47 set_callbacks( Open, Close )
50 struct xml_reader_sys_t
52 /* libxml2 reader context */
53 xmlTextReaderPtr p_reader;
56 static xml_reader_t *ReaderCreate( xml_t *, stream_t * );
57 static void ReaderDelete( xml_reader_t * );
58 static int ReaderRead( xml_reader_t * );
59 static int ReaderNodeType( xml_reader_t * );
60 static char *ReaderName( xml_reader_t * );
61 static char *ReaderValue( xml_reader_t * );
62 static int ReaderNextAttr( xml_reader_t * );
64 static int ReaderUseDTD ( xml_reader_t *, bool );
66 static void CatalogLoad( xml_t *, const char * );
67 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
68 static int StreamRead( void *p_context, char *p_buffer, int i_buffer );
70 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
72 /*****************************************************************************
73 * Module initialization
74 *****************************************************************************/
75 static int Open( vlc_object_t *p_this )
77 xml_t *p_xml = (xml_t *)p_this;
79 if( !xmlHasFeature( XML_WITH_THREAD ) )
82 vlc_mutex_lock( &lock );
84 vlc_mutex_unlock( &lock );
86 p_xml->pf_reader_create = ReaderCreate;
87 p_xml->pf_reader_delete = ReaderDelete;
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->p_xml, "XML parser error (line %d) : %s", line, p_msg );
140 static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream )
142 xml_reader_t *p_reader;
143 xml_reader_sys_t *p_sys;
144 xmlTextReaderPtr p_libxml_reader;
146 p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_stream,
148 if( !p_libxml_reader )
150 msg_Err( p_xml, "failed to create XML parser" );
154 p_reader = malloc( sizeof(xml_reader_t) );
157 xmlFreeTextReader( p_libxml_reader );
160 p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
163 xmlFreeTextReader( p_libxml_reader );
167 p_reader->p_sys->p_reader = p_libxml_reader;
168 p_reader->p_xml = p_xml;
170 /* Set the error handler */
171 xmlTextReaderSetErrorHandler( p_libxml_reader,
172 ReaderErrorHandler, p_reader );
175 p_reader->pf_read = ReaderRead;
176 p_reader->pf_node_type = ReaderNodeType;
177 p_reader->pf_name = ReaderName;
178 p_reader->pf_value = ReaderValue;
179 p_reader->pf_next_attr = ReaderNextAttr;
180 p_reader->pf_use_dtd = ReaderUseDTD;
185 static void ReaderDelete( xml_reader_t *p_reader )
187 xmlFreeTextReader( p_reader->p_sys->p_reader );
188 free( p_reader->p_sys );
192 static int ReaderUseDTD ( xml_reader_t *p_reader, bool b_use )
194 /* Activate DTD validation */
195 xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
196 XML_PARSER_DEFAULTATTRS, b_use );
197 xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
198 XML_PARSER_VALIDATE, b_use );
203 static int ReaderRead( xml_reader_t *p_reader )
205 int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
217 static int ReaderNodeType( xml_reader_t *p_reader )
219 int i_ret = xmlTextReaderNodeType( p_reader->p_sys->p_reader );
223 case XML_READER_TYPE_ELEMENT:
224 i_ret = XML_READER_STARTELEM;
226 case XML_READER_TYPE_END_ELEMENT:
227 i_ret = XML_READER_ENDELEM;
229 case XML_READER_TYPE_CDATA:
230 case XML_READER_TYPE_TEXT:
231 i_ret = XML_READER_TEXT;
237 i_ret = XML_READER_NONE;
244 static char *ReaderName( xml_reader_t *p_reader )
246 const xmlChar *psz_name =
247 xmlTextReaderConstName( p_reader->p_sys->p_reader );
249 return psz_name ? strdup( (const char *)psz_name ) : NULL;
252 static char *ReaderValue( xml_reader_t *p_reader )
254 const xmlChar *psz_value =
255 xmlTextReaderConstValue( p_reader->p_sys->p_reader );
257 return psz_value ? strdup( (const char *)psz_value ) : NULL;
260 static int ReaderNextAttr( xml_reader_t *p_reader )
262 return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
263 == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
266 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
268 stream_t *s = (stream_t*)p_context;
269 return stream_Read( s, p_buffer, i_buffer );