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 #if !defined (LIBXML_VERSION) || (LIBXML_VERSION > 20700)
39 # error Stale config.cache detected. Erase it and re-run configure.
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
49 set_description( N_("XML Parser (using libxml2)") )
50 set_capability( "xml", 10 )
51 set_callbacks( Open, Close )
54 struct xml_reader_sys_t
56 /* libxml2 reader context */
57 xmlTextReaderPtr p_reader;
60 static xml_reader_t *ReaderCreate( xml_t *, stream_t * );
61 static void ReaderDelete( xml_reader_t * );
62 static int ReaderRead( xml_reader_t * );
63 static int ReaderNodeType( xml_reader_t * );
64 static char *ReaderName( xml_reader_t * );
65 static char *ReaderValue( xml_reader_t * );
66 static int ReaderNextAttr( xml_reader_t * );
68 static int ReaderUseDTD ( xml_reader_t *, bool );
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 unsigned refs = 0;
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 );
90 vlc_mutex_unlock( &lock );
92 p_xml->pf_reader_create = ReaderCreate;
93 p_xml->pf_reader_delete = ReaderDelete;
95 p_xml->pf_catalog_load = CatalogLoad;
96 p_xml->pf_catalog_add = CatalogAdd;
101 /*****************************************************************************
102 * Module deinitialization
103 *****************************************************************************/
104 static void Close( vlc_object_t *p_this )
106 vlc_mutex_lock( &lock );
109 vlc_mutex_unlock( &lock );
115 /*****************************************************************************
116 * Catalogue functions
117 *****************************************************************************/
118 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
121 if( !psz_filename ) xmlInitializeCatalog();
122 else xmlLoadCatalog( psz_filename );
125 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
126 const char *psz_arg2, const char *psz_filename )
129 xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
130 (unsigned char*)psz_filename );
133 /*****************************************************************************
135 *****************************************************************************/
136 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
137 xmlParserSeverities severity,
138 xmlTextReaderLocatorPtr locator)
140 VLC_UNUSED(severity);
141 xml_reader_t *p_reader = (xml_reader_t *)p_arg;
142 int line = xmlTextReaderLocatorLineNumber( locator );
143 msg_Err( p_reader->p_xml, "XML parser error (line %d) : %s", line, p_msg );
146 static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream )
148 xml_reader_t *p_reader;
149 xml_reader_sys_t *p_sys;
150 xmlTextReaderPtr p_libxml_reader;
152 p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_stream,
154 if( !p_libxml_reader )
156 msg_Err( p_xml, "failed to create XML parser" );
160 p_reader = malloc( sizeof(xml_reader_t) );
163 xmlFreeTextReader( p_libxml_reader );
166 p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
169 xmlFreeTextReader( p_libxml_reader );
173 p_reader->p_sys->p_reader = p_libxml_reader;
174 p_reader->p_xml = p_xml;
176 /* Set the error handler */
177 xmlTextReaderSetErrorHandler( p_libxml_reader,
178 ReaderErrorHandler, p_reader );
181 p_reader->pf_read = ReaderRead;
182 p_reader->pf_node_type = ReaderNodeType;
183 p_reader->pf_name = ReaderName;
184 p_reader->pf_value = ReaderValue;
185 p_reader->pf_next_attr = ReaderNextAttr;
186 p_reader->pf_use_dtd = ReaderUseDTD;
191 static void ReaderDelete( xml_reader_t *p_reader )
193 xmlFreeTextReader( p_reader->p_sys->p_reader );
194 free( p_reader->p_sys );
198 static int ReaderUseDTD ( xml_reader_t *p_reader, bool b_use )
200 /* Activate DTD validation */
201 xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
202 XML_PARSER_DEFAULTATTRS, b_use );
203 xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
204 XML_PARSER_VALIDATE, b_use );
209 static int ReaderRead( xml_reader_t *p_reader )
211 int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
223 static int ReaderNodeType( xml_reader_t *p_reader )
225 int i_ret = xmlTextReaderNodeType( p_reader->p_sys->p_reader );
229 case XML_READER_TYPE_ELEMENT:
230 i_ret = XML_READER_STARTELEM;
232 case XML_READER_TYPE_END_ELEMENT:
233 i_ret = XML_READER_ENDELEM;
235 case XML_READER_TYPE_CDATA:
236 case XML_READER_TYPE_TEXT:
237 i_ret = XML_READER_TEXT;
243 i_ret = XML_READER_NONE;
250 static char *ReaderName( xml_reader_t *p_reader )
252 const xmlChar *psz_name =
253 xmlTextReaderConstName( p_reader->p_sys->p_reader );
255 if( psz_name ) return strdup( (const char *)psz_name );
259 static char *ReaderValue( xml_reader_t *p_reader )
261 const xmlChar *psz_value =
262 xmlTextReaderConstValue( p_reader->p_sys->p_reader );
264 if( psz_value ) return strdup( (const char *)psz_value );
268 static int ReaderNextAttr( xml_reader_t *p_reader )
270 return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
271 == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
274 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
276 stream_t *s = (stream_t*)p_context;
277 return stream_Read( s, p_buffer, i_buffer );