libxml.c 7.69 KB
Newer Older
gbazin's avatar
gbazin committed
1
2
3
/*****************************************************************************
 * libxml.c: XML parser using libxml2
 *****************************************************************************
4
 * Copyright (C) 2004 the VideoLAN team
gbazin's avatar
gbazin committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * $Id$
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
gbazin's avatar
gbazin committed
22
23
 *****************************************************************************/

24
25
26
27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <vlc_common.h>
29
#include <vlc_plugin.h>
gbazin's avatar
gbazin committed
30

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
31
32
33
#include <vlc_block.h>
#include <vlc_stream.h>
#include <vlc_xml.h>
gbazin's avatar
gbazin committed
34
35
36
37
38
39
40
41
42
43

#include <libxml/xmlreader.h>
#include <libxml/catalog.h>

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

44
45
46
47
static int ReaderOpen( vlc_object_t * );
static void ReaderClose( vlc_object_t * );


48
49
50
51
vlc_module_begin ()
    set_description( N_("XML Parser (using libxml2)") )
    set_capability( "xml", 10 )
    set_callbacks( Open, Close )
52
53

#ifdef WIN32
54
    cannot_unload_broken_library()
55
56
#endif

57
58
59
    add_submodule()
    set_capability( "xml reader", 10 )
    set_callbacks( ReaderOpen, ReaderClose )
gbazin's avatar
gbazin committed
60

61
vlc_module_end ()
gbazin's avatar
gbazin committed
62
63
64
65
66
67
68

static int ReaderRead( xml_reader_t * );
static int ReaderNodeType( xml_reader_t * );
static char *ReaderName( xml_reader_t * );
static char *ReaderValue( xml_reader_t * );
static int ReaderNextAttr( xml_reader_t * );

69
static int ReaderUseDTD ( xml_reader_t * );
70

gbazin's avatar
gbazin committed
71
72
static void CatalogLoad( xml_t *, const char * );
static void CatalogAdd( xml_t *, const char *, const char *, const char * );
sigmunau's avatar
sigmunau committed
73
static int StreamRead( void *p_context, char *p_buffer, int i_buffer );
gbazin's avatar
gbazin committed
74

75
76
static vlc_mutex_t lock = VLC_STATIC_MUTEX;

gbazin's avatar
gbazin committed
77
78
79
80
81
82
83
/*****************************************************************************
 * Module initialization
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    xml_t *p_xml = (xml_t *)p_this;

84
85
86
87
    if( !xmlHasFeature( XML_WITH_THREAD ) )
        return VLC_EGENERIC;

    vlc_mutex_lock( &lock );
88
    xmlInitParser();
89
    vlc_mutex_unlock( &lock );
geal's avatar
geal committed
90

gbazin's avatar
gbazin committed
91
92
93
94
95
96
97
98
99
100
101
    p_xml->pf_catalog_load = CatalogLoad;
    p_xml->pf_catalog_add  = CatalogAdd;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Module deinitialization
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
102
#ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
103
    vlc_mutex_lock( &lock );
104
    xmlCleanupParser();
105
    vlc_mutex_unlock( &lock );
106
#endif
Rafaël Carré's avatar
Rafaël Carré committed
107
    VLC_UNUSED(p_this);
gbazin's avatar
gbazin committed
108
109
110
111
112
113
114
115
    return;
}

/*****************************************************************************
 * Catalogue functions
 *****************************************************************************/
static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
{
Rafaël Carré's avatar
Rafaël Carré committed
116
    VLC_UNUSED(p_xml);
gbazin's avatar
gbazin committed
117
118
119
120
121
122
123
    if( !psz_filename ) xmlInitializeCatalog();
    else xmlLoadCatalog( psz_filename );
}

static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
                          const char *psz_arg2, const char *psz_filename )
{
Rafaël Carré's avatar
Rafaël Carré committed
124
    VLC_UNUSED(p_xml);
125
    xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
126
        (unsigned char*)psz_filename );
gbazin's avatar
gbazin committed
127
128
129
130
131
132
133
134
135
}

/*****************************************************************************
 * Reader functions
 *****************************************************************************/
static void ReaderErrorHandler( void *p_arg, const char *p_msg,
                                xmlParserSeverities severity,
                                xmlTextReaderLocatorPtr locator)
{
Rafaël Carré's avatar
Rafaël Carré committed
136
    VLC_UNUSED(severity);
gbazin's avatar
gbazin committed
137
138
    xml_reader_t *p_reader = (xml_reader_t *)p_arg;
    int line = xmlTextReaderLocatorLineNumber( locator );
139
    msg_Err( p_reader, "XML parser error (line %d) : %s", line, p_msg );
gbazin's avatar
gbazin committed
140
141
}

142
static int ReaderOpen( vlc_object_t *p_this )
gbazin's avatar
gbazin committed
143
{
144
    xml_reader_t *p_reader = (xml_reader_t *)p_this;
gbazin's avatar
gbazin committed
145
    xmlTextReaderPtr p_libxml_reader;
sigmunau's avatar
sigmunau committed
146

147
148
149
150
151
152
153
154
    if( !xmlHasFeature( XML_WITH_THREAD ) )
        return VLC_EGENERIC;

    vlc_mutex_lock( &lock );
    xmlInitParser();
    vlc_mutex_unlock( &lock );

    p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_reader->p_stream,
sigmunau's avatar
sigmunau committed
155
                                      NULL, NULL, 0 );
gbazin's avatar
gbazin committed
156
157
    if( !p_libxml_reader )
    {
158
159
        msg_Err( p_this, "failed to create XML parser" );
        return VLC_ENOMEM;
gbazin's avatar
gbazin committed
160
161
    }

162
    p_reader->p_sys = (void *)p_libxml_reader;
gbazin's avatar
gbazin committed
163
164
165
166
167
168
169
170
171
172

    /* Set the error handler */
    xmlTextReaderSetErrorHandler( p_libxml_reader,
                                  ReaderErrorHandler, p_reader );

    p_reader->pf_read = ReaderRead;
    p_reader->pf_node_type = ReaderNodeType;
    p_reader->pf_name = ReaderName;
    p_reader->pf_value = ReaderValue;
    p_reader->pf_next_attr = ReaderNextAttr;
173
    p_reader->pf_use_dtd = ReaderUseDTD;
gbazin's avatar
gbazin committed
174

175
    return VLC_SUCCESS;
gbazin's avatar
gbazin committed
176
177
}

178
static void ReaderClose( vlc_object_t *p_this )
gbazin's avatar
gbazin committed
179
{
180
181
    xml_reader_t *p_reader = (xml_reader_t *)p_this;

182
    xmlFreeTextReader( (void *)p_reader->p_sys );
183
184
185
186
187
#ifdef LIBXML_GETS_A_CLUE_ABOUT_REENTRANCY_AND_MEMORY_LEAKS
    vlc_mutex_lock( &lock );
    xmlCleanupParser();
    vlc_mutex_unlock( &lock );
#endif
gbazin's avatar
gbazin committed
188
189
}

190
static int ReaderUseDTD ( xml_reader_t *p_reader )
191
192
{
    /* Activate DTD validation */
193
    xmlTextReaderSetParserProp( (void *)p_reader->p_sys,
194
                                XML_PARSER_DEFAULTATTRS, true );
195
    xmlTextReaderSetParserProp( (void *)p_reader->p_sys,
196
                                XML_PARSER_VALIDATE, true );
197
198
199
200

    return VLC_SUCCESS;
}

gbazin's avatar
gbazin committed
201
202
static int ReaderRead( xml_reader_t *p_reader )
{
203
    int i_ret = xmlTextReaderRead( (void *)p_reader->p_sys );
gbazin's avatar
gbazin committed
204
205
206
207
208
209
210
211
212
213
214
215
216

#if 0
    switch( i_ret )
    {
    default:
    }
#endif

    return i_ret;
}

static int ReaderNodeType( xml_reader_t *p_reader )
{
217
    int i_ret = xmlTextReaderNodeType( (void *)p_reader->p_sys );
gbazin's avatar
gbazin committed
218
219
220
221
222
223
224
225
226

    switch( i_ret )
    {
    case XML_READER_TYPE_ELEMENT:
        i_ret = XML_READER_STARTELEM;
        break;
    case XML_READER_TYPE_END_ELEMENT:
        i_ret = XML_READER_ENDELEM;
        break;
227
    case XML_READER_TYPE_CDATA:
228
229
230
    case XML_READER_TYPE_TEXT:
        i_ret = XML_READER_TEXT;
        break;
gbazin's avatar
gbazin committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
    case -1:
        i_ret = -1;
        break;
    default:
        i_ret = XML_READER_NONE;
        break;
    }

    return i_ret;
}

static char *ReaderName( xml_reader_t *p_reader )
{
    const xmlChar *psz_name =
245
        xmlTextReaderConstName( (void *)p_reader->p_sys );
gbazin's avatar
gbazin committed
246

247
    return psz_name ? strdup( (const char *)psz_name ) : NULL;
gbazin's avatar
gbazin committed
248
249
250
251
252
}

static char *ReaderValue( xml_reader_t *p_reader )
{
    const xmlChar *psz_value =
253
        xmlTextReaderConstValue( (void *)p_reader->p_sys );
gbazin's avatar
gbazin committed
254

255
    return psz_value ? strdup( (const char *)psz_value ) : NULL;
gbazin's avatar
gbazin committed
256
257
258
259
}

static int ReaderNextAttr( xml_reader_t *p_reader )
{
260
    return ( xmlTextReaderMoveToNextAttribute( (void *)p_reader->p_sys )
gbazin's avatar
gbazin committed
261
262
             == 1 ) ? VLC_SUCCESS : VLC_EGENERIC;
}
sigmunau's avatar
sigmunau committed
263
264
265
266

static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
{
    stream_t *s = (stream_t*)p_context;
267
    return stream_Read( s, p_buffer, i_buffer );
sigmunau's avatar
sigmunau committed
268
}