]> git.sesse.net Git - vlc/blob - modules/misc/xml/libxml.c
28a6696d3acf86f250cc3e2613d39399cb3bdb4c
[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 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
44 vlc_module_begin ()
45     set_description( N_("XML Parser (using libxml2)") )
46     set_capability( "xml", 10 )
47     set_callbacks( Open, Close )
48 vlc_module_end ()
49
50 struct xml_reader_sys_t
51 {
52     /* libxml2 reader context */
53     xmlTextReaderPtr p_reader;
54 };
55
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 * );
63
64 static int ReaderUseDTD ( xml_reader_t *, bool );
65
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 );
69
70 static unsigned refs = 0;
71 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
72
73 /*****************************************************************************
74  * Module initialization
75  *****************************************************************************/
76 static int Open( vlc_object_t *p_this )
77 {
78     xml_t *p_xml = (xml_t *)p_this;
79
80     if( !xmlHasFeature( XML_WITH_THREAD ) )
81         return VLC_EGENERIC;
82
83     vlc_mutex_lock( &lock );
84     if( refs++ == 0 )
85         xmlInitParser();
86     vlc_mutex_unlock( &lock );
87
88     p_xml->pf_reader_create = ReaderCreate;
89     p_xml->pf_reader_delete = ReaderDelete;
90
91     p_xml->pf_catalog_load = CatalogLoad;
92     p_xml->pf_catalog_add  = CatalogAdd;
93
94     return VLC_SUCCESS;
95 }
96
97 /*****************************************************************************
98  * Module deinitialization
99  *****************************************************************************/
100 static void Close( vlc_object_t *p_this )
101 {
102     vlc_mutex_lock( &lock );
103     if( --refs == 0 )
104         xmlCleanupParser();
105     vlc_mutex_unlock( &lock );
106
107     VLC_UNUSED(p_this);
108     return;
109 }
110
111 /*****************************************************************************
112  * Catalogue functions
113  *****************************************************************************/
114 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
115 {
116     VLC_UNUSED(p_xml);
117     if( !psz_filename ) xmlInitializeCatalog();
118     else xmlLoadCatalog( psz_filename );
119 }
120
121 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
122                           const char *psz_arg2, const char *psz_filename )
123 {
124     VLC_UNUSED(p_xml);
125     xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
126         (unsigned char*)psz_filename );
127 }
128
129 /*****************************************************************************
130  * Reader functions
131  *****************************************************************************/
132 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
133                                 xmlParserSeverities severity,
134                                 xmlTextReaderLocatorPtr locator)
135 {
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->p_xml, "XML parser error (line %d) : %s", line, p_msg );
140 }
141
142 static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream )
143 {
144     xml_reader_t *p_reader;
145     xml_reader_sys_t *p_sys;
146     xmlTextReaderPtr p_libxml_reader;
147
148     p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_stream,
149                                       NULL, NULL, 0 );
150     if( !p_libxml_reader )
151     {
152         msg_Err( p_xml, "failed to create XML parser" );
153         return NULL;
154     }
155
156     p_reader = malloc( sizeof(xml_reader_t) );
157     if( !p_reader )
158     {
159         xmlFreeTextReader( p_libxml_reader );
160         return NULL;
161     }
162     p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
163     if( !p_sys )
164     {
165         xmlFreeTextReader( p_libxml_reader );
166         free( p_reader );
167         return NULL;
168     }
169     p_reader->p_sys->p_reader = p_libxml_reader;
170     p_reader->p_xml = p_xml;
171
172     /* Set the error handler */
173     xmlTextReaderSetErrorHandler( p_libxml_reader,
174                                   ReaderErrorHandler, p_reader );
175
176
177     p_reader->pf_read = ReaderRead;
178     p_reader->pf_node_type = ReaderNodeType;
179     p_reader->pf_name = ReaderName;
180     p_reader->pf_value = ReaderValue;
181     p_reader->pf_next_attr = ReaderNextAttr;
182     p_reader->pf_use_dtd = ReaderUseDTD;
183
184     return p_reader;
185 }
186
187 static void ReaderDelete( xml_reader_t *p_reader )
188 {
189     xmlFreeTextReader( p_reader->p_sys->p_reader );
190     free( p_reader->p_sys );
191     free( p_reader );
192 }
193
194 static int ReaderUseDTD ( xml_reader_t *p_reader, bool b_use )
195 {
196     /* Activate DTD validation */
197     xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
198                                 XML_PARSER_DEFAULTATTRS, b_use );
199     xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
200                                 XML_PARSER_VALIDATE, b_use );
201
202     return VLC_SUCCESS;
203 }
204
205 static int ReaderRead( xml_reader_t *p_reader )
206 {
207     int i_ret = xmlTextReaderRead( p_reader->p_sys->p_reader );
208
209 #if 0
210     switch( i_ret )
211     {
212     default:
213     }
214 #endif
215
216     return i_ret;
217 }
218
219 static int ReaderNodeType( xml_reader_t *p_reader )
220 {
221     int i_ret = xmlTextReaderNodeType( p_reader->p_sys->p_reader );
222
223     switch( i_ret )
224     {
225     case XML_READER_TYPE_ELEMENT:
226         i_ret = XML_READER_STARTELEM;
227         break;
228     case XML_READER_TYPE_END_ELEMENT:
229         i_ret = XML_READER_ENDELEM;
230         break;
231     case XML_READER_TYPE_CDATA:
232     case XML_READER_TYPE_TEXT:
233         i_ret = XML_READER_TEXT;
234         break;
235     case -1:
236         i_ret = -1;
237         break;
238     default:
239         i_ret = XML_READER_NONE;
240         break;
241     }
242
243     return i_ret;
244 }
245
246 static char *ReaderName( xml_reader_t *p_reader )
247 {
248     const xmlChar *psz_name =
249         xmlTextReaderConstName( p_reader->p_sys->p_reader );
250
251     if( psz_name ) return strdup( (const char *)psz_name );
252     else return 0;
253 }
254
255 static char *ReaderValue( xml_reader_t *p_reader )
256 {
257     const xmlChar *psz_value =
258         xmlTextReaderConstValue( p_reader->p_sys->p_reader );
259
260     if( psz_value ) return strdup( (const char *)psz_value );
261     else return 0;
262 }
263
264 static int ReaderNextAttr( xml_reader_t *p_reader )
265 {
266     return ( xmlTextReaderMoveToNextAttribute( p_reader->p_sys->p_reader )
267              == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
268 }
269
270 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
271 {
272     stream_t *s = (stream_t*)p_context;
273     return stream_Read( s, p_buffer, i_buffer );
274 }