]> git.sesse.net Git - vlc/blob - modules/misc/xml/libxml.c
Require libxml2 < 2.7
[vlc] / modules / misc / xml / libxml.c
1 /*****************************************************************************
2  * libxml.c: XML parser using libxml2
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30
31 #include "vlc_block.h"
32 #include "vlc_stream.h"
33 #include "vlc_xml.h"
34
35 #include <libxml/xmlreader.h>
36 #include <libxml/catalog.h>
37
38 #if !defined (LIBXML_VERSION) || (LIBXML_VERSION > 20700)
39 # error Stale config.cache detected. Erase it and re-run configure.
40 #endif
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 static int  Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
47
48 vlc_module_begin ()
49     set_description( N_("XML Parser (using libxml2)") )
50     set_capability( "xml", 10 )
51     set_callbacks( Open, Close )
52 vlc_module_end ()
53
54 struct xml_reader_sys_t
55 {
56     /* libxml2 reader context */
57     xmlTextReaderPtr p_reader;
58 };
59
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 * );
67
68 static int ReaderUseDTD ( xml_reader_t *, bool );
69
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 );
73
74 static unsigned refs = 0;
75 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
76
77 /*****************************************************************************
78  * Module initialization
79  *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
81 {
82     xml_t *p_xml = (xml_t *)p_this;
83
84     if( !xmlHasFeature( XML_WITH_THREAD ) )
85         return VLC_EGENERIC;
86
87     vlc_mutex_lock( &lock );
88     if( refs++ == 0 )
89         xmlInitParser();
90     vlc_mutex_unlock( &lock );
91
92     p_xml->pf_reader_create = ReaderCreate;
93     p_xml->pf_reader_delete = ReaderDelete;
94
95     p_xml->pf_catalog_load = CatalogLoad;
96     p_xml->pf_catalog_add  = CatalogAdd;
97
98     return VLC_SUCCESS;
99 }
100
101 /*****************************************************************************
102  * Module deinitialization
103  *****************************************************************************/
104 static void Close( vlc_object_t *p_this )
105 {
106     vlc_mutex_lock( &lock );
107     if( --refs == 0 )
108         xmlCleanupParser();
109     vlc_mutex_unlock( &lock );
110
111     VLC_UNUSED(p_this);
112     return;
113 }
114
115 /*****************************************************************************
116  * Catalogue functions
117  *****************************************************************************/
118 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
119 {
120     VLC_UNUSED(p_xml);
121     if( !psz_filename ) xmlInitializeCatalog();
122     else xmlLoadCatalog( psz_filename );
123 }
124
125 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
126                           const char *psz_arg2, const char *psz_filename )
127 {
128     VLC_UNUSED(p_xml);
129     xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
130         (unsigned char*)psz_filename );
131 }
132
133 /*****************************************************************************
134  * Reader functions
135  *****************************************************************************/
136 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
137                                 xmlParserSeverities severity,
138                                 xmlTextReaderLocatorPtr locator)
139 {
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 );
144 }
145
146 static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream )
147 {
148     xml_reader_t *p_reader;
149     xml_reader_sys_t *p_sys;
150     xmlTextReaderPtr p_libxml_reader;
151
152     p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_stream,
153                                       NULL, NULL, 0 );
154     if( !p_libxml_reader )
155     {
156         msg_Err( p_xml, "failed to create XML parser" );
157         return NULL;
158     }
159
160     p_reader = malloc( sizeof(xml_reader_t) );
161     if( !p_reader )
162     {
163         xmlFreeTextReader( p_libxml_reader );
164         return NULL;
165     }
166     p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
167     if( !p_sys )
168     {
169         xmlFreeTextReader( p_libxml_reader );
170         free( p_reader );
171         return NULL;
172     }
173     p_reader->p_sys->p_reader = p_libxml_reader;
174     p_reader->p_xml = p_xml;
175
176     /* Set the error handler */
177     xmlTextReaderSetErrorHandler( p_libxml_reader,
178                                   ReaderErrorHandler, p_reader );
179
180
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;
187
188     return p_reader;
189 }
190
191 static void ReaderDelete( xml_reader_t *p_reader )
192 {
193     xmlFreeTextReader( p_reader->p_sys->p_reader );
194     free( p_reader->p_sys );
195     free( p_reader );
196 }
197
198 static int ReaderUseDTD ( xml_reader_t *p_reader, bool b_use )
199 {
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 );
205
206     return VLC_SUCCESS;
207 }
208
209 static int ReaderRead( xml_reader_t *p_reader )
210 {
211     int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
212
213 #if 0
214     switch( i_ret )
215     {
216     default:
217     }
218 #endif
219
220     return i_ret;
221 }
222
223 static int ReaderNodeType( xml_reader_t *p_reader )
224 {
225     int i_ret = xmlTextReaderNodeType( p_reader->p_sys->p_reader );
226
227     switch( i_ret )
228     {
229     case XML_READER_TYPE_ELEMENT:
230         i_ret = XML_READER_STARTELEM;
231         break;
232     case XML_READER_TYPE_END_ELEMENT:
233         i_ret = XML_READER_ENDELEM;
234         break;
235     case XML_READER_TYPE_CDATA:
236     case XML_READER_TYPE_TEXT:
237         i_ret = XML_READER_TEXT;
238         break;
239     case -1:
240         i_ret = -1;
241         break;
242     default:
243         i_ret = XML_READER_NONE;
244         break;
245     }
246
247     return i_ret;
248 }
249
250 static char *ReaderName( xml_reader_t *p_reader )
251 {
252     const xmlChar *psz_name =
253         xmlTextReaderConstName( p_reader->p_sys->p_reader );
254
255     if( psz_name ) return strdup( (const char *)psz_name );
256     else return 0;
257 }
258
259 static char *ReaderValue( xml_reader_t *p_reader )
260 {
261     const xmlChar *psz_value =
262         xmlTextReaderConstValue( p_reader->p_sys->p_reader );
263
264     if( psz_value ) return strdup( (const char *)psz_value );
265     else return 0;
266 }
267
268 static int ReaderNextAttr( xml_reader_t *p_reader )
269 {
270     return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
271              == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
272 }
273
274 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
275 {
276     stream_t *s = (stream_t*)p_context;
277     return stream_Read( s, p_buffer, i_buffer );
278 }