]> git.sesse.net Git - vlc/blob - modules/misc/xml/libxml.c
Return XML attribute as const from NextAttr
[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 static int ReaderOpen( vlc_object_t * );
45 static void ReaderClose( vlc_object_t * );
46
47
48 vlc_module_begin ()
49     set_description( N_("XML Parser (using libxml2)") )
50     set_capability( "xml", 10 )
51     set_callbacks( Open, Close )
52
53 #ifdef WIN32
54     cannot_unload_broken_library()
55 #endif
56
57     add_submodule()
58     set_capability( "xml reader", 10 )
59     set_callbacks( ReaderOpen, ReaderClose )
60
61 vlc_module_end ()
62
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 * );
67
68 static int ReaderUseDTD ( xml_reader_t * );
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 vlc_mutex_t lock = VLC_STATIC_MUTEX;
75
76 /*****************************************************************************
77  * Module initialization
78  *****************************************************************************/
79 static int Open( vlc_object_t *p_this )
80 {
81     xml_t *p_xml = (xml_t *)p_this;
82
83     if( !xmlHasFeature( XML_WITH_THREAD ) )
84         return VLC_EGENERIC;
85
86     vlc_mutex_lock( &lock );
87     xmlInitParser();
88     vlc_mutex_unlock( &lock );
89
90     p_xml->pf_catalog_load = CatalogLoad;
91     p_xml->pf_catalog_add  = CatalogAdd;
92
93     return VLC_SUCCESS;
94 }
95
96 /*****************************************************************************
97  * Module deinitialization
98  *****************************************************************************/
99 static void Close( vlc_object_t *p_this )
100 {
101 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
102     vlc_mutex_lock( &lock );
103     xmlCleanupParser();
104     vlc_mutex_unlock( &lock );
105 #endif
106     VLC_UNUSED(p_this);
107     return;
108 }
109
110 /*****************************************************************************
111  * Catalogue functions
112  *****************************************************************************/
113 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
114 {
115     VLC_UNUSED(p_xml);
116     if( !psz_filename ) xmlInitializeCatalog();
117     else xmlLoadCatalog( psz_filename );
118 }
119
120 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
121                           const char *psz_arg2, const char *psz_filename )
122 {
123     VLC_UNUSED(p_xml);
124     xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
125         (unsigned char*)psz_filename );
126 }
127
128 /*****************************************************************************
129  * Reader functions
130  *****************************************************************************/
131 static void ReaderErrorHandler( void *p_arg, const char *p_msg,
132                                 xmlParserSeverities severity,
133                                 xmlTextReaderLocatorPtr locator)
134 {
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 );
139 }
140
141 static int ReaderOpen( vlc_object_t *p_this )
142 {
143     xml_reader_t *p_reader = (xml_reader_t *)p_this;
144     xmlTextReaderPtr p_libxml_reader;
145
146     if( !xmlHasFeature( XML_WITH_THREAD ) )
147         return VLC_EGENERIC;
148
149     vlc_mutex_lock( &lock );
150     xmlInitParser();
151     vlc_mutex_unlock( &lock );
152
153     p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_reader->p_stream,
154                                       NULL, NULL, 0 );
155     if( !p_libxml_reader )
156     {
157         msg_Err( p_this, "failed to create XML parser" );
158         return VLC_ENOMEM;
159     }
160
161     p_reader->p_sys = (void *)p_libxml_reader;
162
163     /* Set the error handler */
164     xmlTextReaderSetErrorHandler( p_libxml_reader,
165                                   ReaderErrorHandler, p_reader );
166
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;
172
173     return VLC_SUCCESS;
174 }
175
176 static void ReaderClose( vlc_object_t *p_this )
177 {
178     xml_reader_t *p_reader = (xml_reader_t *)p_this;
179
180     xmlFreeTextReader( (void *)p_reader->p_sys );
181 #ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
182     vlc_mutex_lock( &lock );
183     xmlCleanupParser();
184     vlc_mutex_unlock( &lock );
185 #endif
186 }
187
188 static int ReaderUseDTD ( xml_reader_t *p_reader )
189 {
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 );
195
196     return VLC_SUCCESS;
197 }
198
199 static int ReaderNextNode( xml_reader_t *p_reader )
200 {
201 skip:
202     switch( xmlTextReaderRead( (void *)p_reader->p_sys ) )
203     {
204         case 0: /* EOF */
205             return 0;
206         case -1: /* error */
207             return -1;
208     }
209
210     switch( xmlTextReaderNodeType( (void *)p_reader->p_sys ) )
211     {
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;
219         case -1:
220             return -1;
221         default:
222             goto skip;
223     }
224 }
225
226 static char *ReaderName( xml_reader_t *p_reader )
227 {
228     const xmlChar *psz_name =
229         xmlTextReaderConstName( (void *)p_reader->p_sys );
230
231     return psz_name ? strdup( (const char *)psz_name ) : NULL;
232 }
233
234 static char *ReaderValue( xml_reader_t *p_reader )
235 {
236     const xmlChar *psz_value =
237         xmlTextReaderConstValue( (void *)p_reader->p_sys );
238
239     return psz_value ? strdup( (const char *)psz_value ) : NULL;
240 }
241
242 static const char *ReaderNextAttr( xml_reader_t *p_reader )
243 {
244     if( xmlTextReaderMoveToNextAttribute( (void *)p_reader->p_sys ) != 1 )
245         return NULL;
246     return (const char *)xmlTextReaderConstValue( (void *)p_reader->p_sys );
247 }
248
249 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
250 {
251     stream_t *s = (stream_t*)p_context;
252     return stream_Read( s, p_buffer, i_buffer );
253 }