xspf.c 27.5 KB
Newer Older
zorglub's avatar
zorglub committed
1
/*******************************************************************************
2
3
4
5
6
7
8
 * xspf.c : XSPF playlist import functions
 *******************************************************************************
 * Copyright (C) 2006 the VideoLAN team
 * $Id$
 *
 * Authors: Daniel Stränger <vlc at schmaller dot de>
 *          Yoann Peronneau <yoann@videolan.org>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Rafaël Carré's avatar
Rafaël Carré committed
23
 ******************************************************************************/
24
25
26
27
28
/**
 * \file modules/demux/playlist/xspf.c
 * \brief XSPF playlist import functions
 */

29
30
31
32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
zorglub's avatar
zorglub committed
34
#include <vlc_demux.h>
35

36
37
38
#include <vlc_xml.h>
#include <vlc_strings.h>
#include <vlc_url.h>
39
#include "xspf.h"
40
#include "playlist.h"
41

42
struct demux_sys_t
zorglub's avatar
zorglub committed
43
{
44
45
    input_item_t **pp_tracklist;
    int i_tracklist_entries;
46
    int i_track_id;
Christophe Mutricy's avatar
Christophe Mutricy committed
47
    char * psz_base;
zorglub's avatar
zorglub committed
48
49
};

50
51
52
static int Control( demux_t *, int, va_list );
static int Demux( demux_t * );

53
54
55
/**
 * \brief XSPF submodule initialization function
 */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
56
int Import_xspf( vlc_object_t *p_this )
57
{
58
    DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
59
                                      "using XSPF playlist reader" );
60
61
62
    return VLC_SUCCESS;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
63
void Close_xspf( vlc_object_t *p_this )
gbazin's avatar
gbazin committed
64
65
{
    demux_t *p_demux = (demux_t *)p_this;
66
67
    demux_sys_t *p_sys = p_demux->p_sys;
    for( int i = 0; i < p_sys->i_tracklist_entries; i++ )
68
    {
69
70
        if( p_sys->pp_tracklist[i] )
            vlc_gc_decref( p_sys->pp_tracklist[i] );
71
    }
72
73
74
    free( p_sys->pp_tracklist );
    free( p_sys->psz_base );
    free( p_sys );
gbazin's avatar
gbazin committed
75
76
}

77
78
79
/**
 * \brief demuxer function for XSPF parsing
 */
80
int Demux( demux_t *p_demux )
81
{
ivoire's avatar
ivoire committed
82
    int i_ret = -1;
83
    xml_reader_t *p_xml_reader = NULL;
84
    const char *name = NULL;
85
    input_item_t *p_current_input = GetCurrentItem(p_demux);
86
87
    p_demux->p_sys->pp_tracklist = NULL;
    p_demux->p_sys->i_tracklist_entries = 0;
88
    p_demux->p_sys->i_track_id = -1;
Christophe Mutricy's avatar
Christophe Mutricy committed
89
    p_demux->p_sys->psz_base = NULL;
90
91

    /* create new xml parser from stream */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
92
    p_xml_reader = xml_ReaderCreate( p_demux, p_demux->s );
ivoire's avatar
ivoire committed
93
94
    if( !p_xml_reader )
        goto end;
95

96
    /* locating the root node */
97
    if( xml_ReaderNextNode( p_xml_reader, &name ) != XML_READER_STARTELEM )
98
    {
99
100
101
        msg_Err( p_demux, "can't read xml stream" );
        goto end;
    }
ivoire's avatar
ivoire committed
102

103
    /* checking root node name */
104
    if( strcmp( name, "playlist" ) )
105
    {
106
        msg_Err( p_demux, "invalid root node name <%s>", name );
ivoire's avatar
ivoire committed
107
        goto end;
108
109
    }

jpd's avatar
jpd committed
110
111
112
113
    input_item_node_t *p_subitems =
        input_item_node_Create( p_current_input );

    i_ret = parse_playlist_node( p_demux, p_subitems,
ivoire's avatar
ivoire committed
114
                                 p_xml_reader, "playlist" ) ? 0 : -1;
115

ivoire's avatar
ivoire committed
116
    for( int i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
Rafaël Carré's avatar
Rafaël Carré committed
117
118
119
    {
        input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
        if( p_new_input )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
120
        {
jpd's avatar
jpd committed
121
            input_item_node_AppendItem( p_subitems, p_new_input );
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
122
        }
Rafaël Carré's avatar
Rafaël Carré committed
123
    }
124

125
    input_item_node_PostAndDelete( p_subitems );
jpd's avatar
jpd committed
126

ivoire's avatar
ivoire committed
127
end:
128
    vlc_gc_decref(p_current_input);
129
    if( p_xml_reader )
130
        xml_ReaderDelete( p_xml_reader );
131
    return i_ret; /* Needed for correct operation of go back */
132
133
134
}

/** \brief dummy function for demux callback interface */
135
static int Control( demux_t *p_demux, int i_query, va_list args )
136
{
137
    VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
138
139
    return VLC_EGENERIC;
}
zorglub's avatar
zorglub committed
140

141
142
143
/**
 * \brief parse the root node of a XSPF playlist
 * \param p_demux demuxer instance
144
 * \param p_input_item current input item
145
146
147
 * \param p_xml_reader xml reader instance
 * \param psz_element name of element to parse
 */
148
static bool parse_playlist_node COMPLEX_INTERFACE
149
{
jpd's avatar
jpd committed
150
    input_item_t *p_input_item = p_input_node->p_item;
151
    char *psz_value = NULL;
152
    bool b_version_found = false;
153
    int i_node;
154
    bool b_ret = false;
155
    xml_elem_hnd_t *p_handler = NULL;
156
157
158
159

    xml_elem_hnd_t pl_elements[] =
        { {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
          {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
160
          {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
161
162
163
          {"info",         SIMPLE_CONTENT,  {NULL} },
          {"location",     SIMPLE_CONTENT,  {NULL} },
          {"identifier",   SIMPLE_CONTENT,  {NULL} },
164
          {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
165
166
167
168
169
          {"date",         SIMPLE_CONTENT,  {NULL} },
          {"license",      SIMPLE_CONTENT,  {NULL} },
          {"attribution",  COMPLEX_CONTENT, {.cmplx = skip_element} },
          {"link",         SIMPLE_CONTENT,  {NULL} },
          {"meta",         SIMPLE_CONTENT,  {NULL} },
170
          {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
171
172
173
174
175
          {"trackList",    COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
          {NULL,           UNKNOWN_CONTENT, {NULL} }
        };

    /* read all playlist attributes */
176
177
    const char *name, *value;
    while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
178
179
    {
        /* attribute: version */
180
        if( !strcmp( name, "version" ) )
zorglub's avatar
zorglub committed
181
        {
182
            b_version_found = true;
183
184
            if( strcmp( value, "0" ) && strcmp( value, "1" ) )
                msg_Warn( p_demux, "unsupported XSPF version %s", value );
185
186
        }
        /* attribute: xmlns */
187
        else if( !strcmp( name, "xmlns" ) || !strcmp( name, "xmlns:vlc" ) )
188
            ;
189
        else if( !strcmp( name, "xml:base" ) )
Christophe Mutricy's avatar
Christophe Mutricy committed
190
        {
191
            free( p_demux->p_sys->psz_base );
192
            p_demux->p_sys->psz_base = strdup( psz_value );
Christophe Mutricy's avatar
Christophe Mutricy committed
193
        }
194
195
        /* unknown attribute */
        else
196
            msg_Warn( p_demux, "invalid <playlist> attribute: \"%s\"", name);
197
198
    }
    /* attribute version is mandatory !!! */
199
    if( !b_version_found )
200
201
202
        msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );

    /* parse the child elements - we only take care of <trackList> */
203
    psz_value = NULL;
204
    while( (i_node = xml_ReaderNextNode( p_xml_reader, &name )) > 0 )
205
    {
206
        switch( i_node )
207
        {
208
            /*  element start tag  */
209
            case XML_READER_STARTELEM:
210
                if( !*name )
211
                {
212
                    msg_Err( p_demux, "invalid XML stream" );
213
                    goto end;
214
215
216
                }
                /* choose handler */
                for( p_handler = pl_elements;
217
                     p_handler->name && strcmp( name, p_handler->name );
218
                     p_handler++ );
219
                if( !p_handler->name )
220
                {
221
                    msg_Err( p_demux, "unexpected element <%s>", name );
222
                    goto end;
223
224
                }
                /* complex content is parsed in a separate function */
225
                if( p_handler->type == COMPLEX_CONTENT )
226
                {
227
                    FREE_VALUE();
228
                    if( p_handler->pf_handler.cmplx( p_demux,
jpd's avatar
jpd committed
229
                                                     p_input_node,
230
231
                                                     p_xml_reader,
                                                     p_handler->name ) )
232
233
234
235
236
                    {
                        p_handler = NULL;
                    }
                    else
                    {
237
                        return false;
238
239
240
241
                    }
                }
                break;

242
            /* simple element content */
243
            case XML_READER_TEXT:
244
245
                psz_value = strdup( name );
                if( unlikely(!name) )
246
                    goto end;
247
248
                break;

249
            /* element end tag */
250
251
            case XML_READER_ENDELEM:
                /* leave if the current parent node <playlist> is terminated */
252
                if( !strcmp( name, psz_element ) )
253
                {
254
255
                    b_ret = true;
                    goto end;
256
257
                }
                /* there MUST have been a start tag for that element name */
258
                if( !p_handler || !p_handler->name
259
                    || strcmp( p_handler->name, name ))
260
261
                {
                    msg_Err( p_demux, "there's no open element left for <%s>",
262
                             name );
263
                    goto end;
264
265
                }

266
                if( p_handler->pf_handler.smpl )
267
                {
268
                    p_handler->pf_handler.smpl( p_input_item, p_handler->name,
269
270
271
272
273
274
275
                                                psz_value );
                }
                FREE_ATT();
                p_handler = NULL;
                break;
        }
    }
276
277
278
279

end:
    free( psz_value );
    return b_ret;
280
281
282
283
284
}

/**
 * \brief parses the tracklist node which only may contain <track>s
 */
285
static bool parse_tracklist_node COMPLEX_INTERFACE
286
{
287
    VLC_UNUSED(psz_element);
288
289
    const char *name;
    unsigned i_ntracks = 0;
290
    int i_node;
291
292

    /* now parse the <track>s */
293
    while( (i_node = xml_ReaderNextNode( p_xml_reader, &name )) > 0 )
294
    {
295
        if( i_node == XML_READER_STARTELEM )
296
        {
297
            if( strcmp( name, "track") )
298
299
            {
                msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
300
                         name );
301
                return false;
302
303
304
            }

            /* parse the track data in a separate function */
jpd's avatar
jpd committed
305
306
            if( parse_track_node( p_demux, p_input_node,
                                  p_xml_reader, "track" ) )
307
308
                i_ntracks++;
        }
309
        else if( i_node == XML_READER_ENDELEM )
310
311
312
313
            break;
    }

    /* the <trackList> has to be terminated */
314
    if( i_node != XML_READER_ENDELEM )
315
316
    {
        msg_Err( p_demux, "there's a missing </trackList>" );
317
        return false;
318
    }
319
    if( strcmp( name, "trackList" ) )
320
    {
321
        msg_Err( p_demux, "expected: </trackList>, found: </%s>", name );
322
        return false;
323
324
    }

325
    msg_Dbg( p_demux, "parsed %u tracks successfully", i_ntracks );
326
    return true;
327
328
329
330
331
332
}

/**
 * \brief parse one track element
 * \param COMPLEX_INTERFACE
 */
333
static bool parse_track_node COMPLEX_INTERFACE
334
{
jpd's avatar
jpd committed
335
    input_item_t *p_input_item = p_input_node->p_item;
336
    const char *name;
337
338
    char *psz_value = NULL;
    xml_elem_hnd_t *p_handler = NULL;
ivoire's avatar
ivoire committed
339
    demux_sys_t *p_sys = p_demux->p_sys;
340
    int i_node;
341
    bool b_ret = false;
342
343
344
345
346
347

    xml_elem_hnd_t track_elements[] =
        { {"location",     SIMPLE_CONTENT,  {NULL} },
          {"identifier",   SIMPLE_CONTENT,  {NULL} },
          {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
          {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
348
          {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
349
          {"info",         SIMPLE_CONTENT,  {NULL} },
350
          {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
351
352
353
354
355
          {"album",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
          {"trackNum",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
          {"duration",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
          {"link",         SIMPLE_CONTENT,  {NULL} },
          {"meta",         SIMPLE_CONTENT,  {NULL} },
356
          {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
357
358
359
          {NULL,           UNKNOWN_CONTENT, {NULL} }
        };

hartman's avatar
hartman committed
360
    input_item_t *p_new_input = input_item_New( p_demux, NULL, NULL );
jpd's avatar
jpd committed
361
    input_item_node_t *p_new_node = input_item_node_Create( p_new_input );
362
363
364

    if( !p_new_input )
    {
365
        /* malloc has failed for input_item_New, so bailout early */
366
367
368
        return false;
    }

369
    /* reset i_track_id */
ivoire's avatar
ivoire committed
370
    p_sys->i_track_id = -1;
371

372
    while( (i_node = xml_ReaderNextNode( p_xml_reader, &name )) > 0 )
373
    {
374
        switch( i_node )
375
        {
376
            /*  element start tag  */
377
            case XML_READER_STARTELEM:
378
                if( !*name )
379
                {
380
                    msg_Err( p_demux, "invalid XML stream" );
381
                    goto end;
382
383
384
                }
                /* choose handler */
                for( p_handler = track_elements;
385
                     p_handler->name && strcmp( name, p_handler->name );
386
                     p_handler++ );
387
                if( !p_handler->name )
388
                {
389
                    msg_Err( p_demux, "unexpected element <%s>", name );
390
                    goto end;
391
392
                }
                /* complex content is parsed in a separate function */
393
                if( p_handler->type == COMPLEX_CONTENT )
394
                {
395
                    FREE_VALUE();
jpd's avatar
jpd committed
396
397
398
399
400
401

                    bool b_res = p_handler->pf_handler.cmplx( p_demux,
                                                        p_new_node,
                                                        p_xml_reader,
                                                        p_handler->name );
                    if( b_res )
402
403
                        p_handler = NULL;
                    else
404
                        return false;
405
406
407
                }
                break;

408
            /* simple element content */
409
            case XML_READER_TEXT:
410
                free( psz_value );
411
412
                psz_value = strdup( name );
                if( unlikely(!psz_value) )
413
                    goto end;
414
415
                break;

416
            /* element end tag */
417
418
            case XML_READER_ENDELEM:
                /* leave if the current parent node <track> is terminated */
419
                if( !strcmp( name, psz_element ) )
420
                {
421
                    free( psz_value );
422

hartman's avatar
hartman committed
423
424
425
426
                    /* Make sure we have a URI */
                    char *psz_uri = input_item_GetURI( p_new_input );
                    if( !psz_uri )
                        input_item_SetURI( p_new_input, "vlc://nop" );
427
428
                    else
                        free( psz_uri );
hartman's avatar
hartman committed
429

ivoire's avatar
ivoire committed
430
                    if( p_sys->i_track_id < 0 )
431
                    {
jpd's avatar
jpd committed
432
                        input_item_node_AppendNode( p_input_node, p_new_node );
433
                        vlc_gc_decref( p_new_input );
434
435
436
                        return true;
                    }

ivoire's avatar
ivoire committed
437
                    if( p_sys->i_track_id >= p_sys->i_tracklist_entries )
Pierre d'Herbemont's avatar
Pierre d'Herbemont committed
438
                    {
439
                        input_item_t **pp;
ivoire's avatar
ivoire committed
440
441
                        pp = realloc( p_sys->pp_tracklist,
                            (p_sys->i_track_id + 1) * sizeof(*pp) );
442
443
                        if( !pp )
                            return false;
ivoire's avatar
ivoire committed
444
445
446
                        p_sys->pp_tracklist = pp;
                        while( p_sys->i_track_id >= p_sys->i_tracklist_entries )
                            pp[p_sys->i_tracklist_entries++] = NULL;
447
                    }
448
449
450
451
452
453
454
                    else if( p_sys->pp_tracklist[p_sys->i_track_id] != NULL )
                    {
                        msg_Err( p_demux, "track ID %d collision",
                                 p_sys->i_track_id );
                        vlc_gc_decref( p_new_input );
                        return false;
                    }
455

ivoire's avatar
ivoire committed
456
                    p_sys->pp_tracklist[ p_sys->i_track_id ] = p_new_input;
457
                    return true;
458
459
                }
                /* there MUST have been a start tag for that element name */
460
                if( !p_handler || !p_handler->name
461
                    || strcmp( p_handler->name, name ))
462
463
                {
                    msg_Err( p_demux, "there's no open element left for <%s>",
464
                             name );
465
                    goto end;
466
467
468
                }

                /* special case: location */
469
                if( !strcmp( p_handler->name, "location" ) )
470
                {
471
472
473
                    if( psz_value == NULL )
                        input_item_SetURI( p_new_input, "vlc://nop" );
                    else
474
475
476
477
478
479
480
                    /* FIXME: This is broken. Scheme-relative (//...) locations
                     * and anchors (#...) are not resolved correctly. Also,
                     * host-relative (/...) and directory-relative locations
                     * ("relative path" in vernacular) should be resolved.
                     * Last, psz_base should default to the XSPF resource
                     * location if missing (not the current working directory).
                     * -- Courmisch */
ivoire's avatar
ivoire committed
481
                    if( p_sys->psz_base && !strstr( psz_value, "://" ) )
482
                    {
ivoire's avatar
ivoire committed
483
                        char* psz_tmp;
ivoire's avatar
ivoire committed
484
485
                        if( asprintf( &psz_tmp, "%s%s", p_sys->psz_base,
                                      psz_value ) == -1 )
ivoire's avatar
ivoire committed
486
                        {
487
                            goto end;
ivoire's avatar
ivoire committed
488
                        }
Rafaël Carré's avatar
Rafaël Carré committed
489
490
                        input_item_SetURI( p_new_input, psz_tmp );
                        free( psz_tmp );
491
                    }
Rafaël Carré's avatar
Rafaël Carré committed
492
                    else
493
                        input_item_SetURI( p_new_input, psz_value );
ivoire's avatar
ivoire committed
494
                    input_item_CopyOptions( p_input_item, p_new_input );
495
496
497
498
                }
                else
                {
                    /* there MUST be an item */
499
                    if( p_handler->pf_handler.smpl )
500
                    {
501
                        p_handler->pf_handler.smpl( p_new_input,
zorglub's avatar
zorglub committed
502
                                                    p_handler->name,
503
504
505
506
507
508
509
510
511
512
                                                    psz_value );
                        FREE_ATT();
                    }
                }
                FREE_ATT();
                p_handler = NULL;
                break;
        }
    }
    msg_Err( p_demux, "unexpected end of xml data" );
513
514
515
516

end:
    free( psz_value );
    return b_ret;
517
518
519
520
521
}

/**
 * \brief handles the supported <track> sub-elements
 */
522
static bool set_item_info SIMPLE_INTERFACE
523
524
{
    /* exit if setting is impossible */
525
    if( !psz_name || !psz_value || !p_input )
526
        return false;
527
528

    /* re-convert xml special characters inside psz_value */
529
    resolve_xml_special_chars( psz_value );
530
531

    /* handle each info element in a separate "if" clause */
532
    if( !strcmp( psz_name, "title" ) )
533
        input_item_SetTitle( p_input, psz_value );
534
    else if( !strcmp( psz_name, "creator" ) )
535
        input_item_SetArtist( p_input, psz_value );
536
    else if( !strcmp( psz_name, "album" ) )
537
        input_item_SetAlbum( p_input, psz_value );
538
    else if( !strcmp( psz_name, "trackNum" ) )
539
        input_item_SetTrackNum( p_input, psz_value );
540
    else if( !strcmp( psz_name, "duration" ) )
541
542
    {
        long i_num = atol( psz_value );
543
        input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
544
    }
545
    else if( !strcmp( psz_name, "annotation" ) )
546
        input_item_SetDescription( p_input, psz_value );
547
    else if( !strcmp( psz_name, "image" ) )
Rafaël Carré's avatar
Rafaël Carré committed
548
        input_item_SetArtURL( p_input, psz_value );
549
    return true;
550
551
}

552
/**
553
 * \brief handles the <vlc:option> elements
554
555
556
557
558
559
560
561
562
 */
static bool set_option SIMPLE_INTERFACE
{
    /* exit if setting is impossible */
    if( !psz_name || !psz_value || !p_input )
        return false;

    /* re-convert xml special characters inside psz_value */
    resolve_xml_special_chars( psz_value );
dionoea's avatar
dionoea committed
563

564
    input_item_AddOption( p_input, psz_value, 0 );
dionoea's avatar
dionoea committed
565

566
567
    return true;
}
568
569
570
571

/**
 * \brief parse the extension node of a XSPF playlist
 */
572
static bool parse_extension_node COMPLEX_INTERFACE
573
{
jpd's avatar
jpd committed
574
    input_item_t *p_input_item = p_input_node->p_item;
575
    char *psz_value = NULL;
576
    char *psz_title = NULL;
577
    char *psz_application = NULL;
578
    int i_node;
579
    bool b_release_input_item = false;
580
    xml_elem_hnd_t *p_handler = NULL;
yoann's avatar
yoann committed
581
    input_item_t *p_new_input = NULL;
582
583

    xml_elem_hnd_t pl_elements[] =
584
585
586
587
        { {"vlc:node",   COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
          {"vlc:item",   COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
          {"vlc:id",     SIMPLE_CONTENT, {NULL} },
          {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
588
589
590
          {NULL,    UNKNOWN_CONTENT, {NULL} }
        };

591
    /* read all extension node attributes */
592
593
    const char *name, *value;
    while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
594
595
    {
        /* attribute: title */
596
        if( !strcmp( name, "title" ) )
597
        {
598
            free( psz_title );
599
600
601
            psz_title = strdup( value );
            if( likely(psz_title != NULL) )
                resolve_xml_special_chars( psz_title );
602
        }
603
        /* extension attribute: application */
604
        else if( !strcmp( name, "application" ) )
605
        {
606
            free( psz_application );
607
            psz_application = strdup( value );
608
        }
609
610
        /* unknown attribute */
        else
611
            msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
612
                      name );
613
614
615
    }

    /* attribute title is mandatory except for <extension> */
616
    if( !strcmp( psz_element, "vlc:node" ) )
617
    {
yoann's avatar
yoann committed
618
619
        if( !psz_title )
        {
620
            msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
621
            return false;
yoann's avatar
yoann committed
622
        }
623
        p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
624
                          "vlc://nop", psz_title, 0, NULL, 0, -1,
625
                          ITEM_TYPE_DIRECTORY );
yoann's avatar
yoann committed
626
627
        if( p_new_input )
        {
jpd's avatar
jpd committed
628
629
            p_input_node =
                input_item_node_AppendItem( p_input_node, p_new_input );
yoann's avatar
yoann committed
630
            p_input_item = p_new_input;
631
            b_release_input_item = true;
yoann's avatar
yoann committed
632
633
634
635
        }
        free( psz_title );
    }
    else if( !strcmp( psz_element, "extension" ) )
636
637
638
639
    {
        if( !psz_application )
        {
            msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
640
            return false;
641
        }
642
643
        /* Skip the extension if the application is not vlc
           This will skip all children of the current node */
644
645
646
647
        else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
        {
            msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
            free( psz_application );
648
            /* Skip all children */
649
            for( unsigned lvl = 1; lvl; )
650
                switch( xml_ReaderNextNode( p_xml_reader, NULL ) )
651
                {
652
653
654
                    case XML_READER_STARTELEM: lvl++; break;
                    case XML_READER_ENDELEM:   lvl--; break;
                    case 0: case -1: return -1;
655
656
                }
            return true;
657
658
659
        }
    }
    free( psz_application );
660

jpd's avatar
jpd committed
661

662
    /* parse the child elements */
663
    while( (i_node = xml_ReaderNextNode( p_xml_reader, &name )) > 0 )
664
665
666
    {
        switch( i_node )
        {
667
            /*  element start tag  */
668
            case XML_READER_STARTELEM:
669
                if( !*name )
670
671
672
                {
                    msg_Err( p_demux, "invalid xml stream" );
                    FREE_ATT();
673
                    if( b_release_input_item ) vlc_gc_decref( p_new_input );
674
                    return false;
675
676
677
                }
                /* choose handler */
                for( p_handler = pl_elements;
678
                     p_handler->name && strcmp( name, p_handler->name );
679
680
681
                     p_handler++ );
                if( !p_handler->name )
                {
682
                    msg_Err( p_demux, "unexpected element <%s>", name );
683
                    FREE_ATT();
684
                    if( b_release_input_item ) vlc_gc_decref( p_new_input );
685
                    return false;
686
687
688
689
690
                }
                /* complex content is parsed in a separate function */
                if( p_handler->type == COMPLEX_CONTENT )
                {
                    if( p_handler->pf_handler.cmplx( p_demux,
jpd's avatar
jpd committed
691
                                                     p_input_node,
692
693
694
695
696
697
698
699
700
                                                     p_xml_reader,
                                                     p_handler->name ) )
                    {
                        p_handler = NULL;
                        FREE_ATT();
                    }
                    else
                    {
                        FREE_ATT();
701
                        if( b_release_input_item ) vlc_gc_decref( p_new_input );
702
                        return false;
703
704
705
706
707
708
709
                    }
                }
                break;

            case XML_READER_TEXT:
                /* simple element content */
                FREE_ATT();
710
711
                psz_value = strdup( name );
                if( unlikely(!psz_value) )
712
713
                {
                    FREE_ATT();
714
                    if( b_release_input_item ) vlc_gc_decref( p_new_input );
715
                    return false;
716
717
718
                }
                break;

719
            /* element end tag */
720
721
            case XML_READER_ENDELEM:
                /* leave if the current parent node is terminated */
722
                if( !strcmp( name, psz_element ) )
723
724
                {
                    FREE_ATT();
725
                    if( b_release_input_item ) vlc_gc_decref( p_new_input );
726
                    return true;
727
728
729
                }
                /* there MUST have been a start tag for that element name */
                if( !p_handler || !p_handler->name
730
                    || strcmp( p_handler->name, name ))
731
732
                {
                    msg_Err( p_demux, "there's no open element left for <%s>",
733
                             name );
734
                    FREE_ATT();
735
                    if( b_release_input_item ) vlc_gc_decref( p_new_input );
736
                    return false;
737
738
                }

739
740
741
742
743
744
                /* special tag <vlc:id> */
                if( !strcmp( p_handler->name, "vlc:id" ) )
                {
                    p_demux->p_sys->i_track_id = atoi( psz_value );
                }
                else if( p_handler->pf_handler.smpl )
745
                {
746
                    p_handler->pf_handler.smpl( p_input_item, p_handler->name,
747
748
749
750
751
752
753
                                                psz_value );
                }
                FREE_ATT();
                p_handler = NULL;
                break;
        }
    }
754
    if( b_release_input_item ) vlc_gc_decref( p_new_input );
755
    return false;
756
757
758
759
760
}

/**
 * \brief parse the extension item node of a XSPF playlist
 */
761
static bool parse_extitem_node COMPLEX_INTERFACE
762
{
763
    VLC_UNUSED(psz_element);
yoann's avatar
yoann committed
764
    input_item_t *p_new_input = NULL;
765
    int i_tid = -1;
766

767
    /* read all extension item attributes */
768
769
    const char *name, *value;
    while( (name = xml_ReaderNextAttr( p_xml_reader, &value )) != NULL )
770
771
    {
        /* attribute: href */
772
        if( !strcmp( name, "tid" ) )
773
            i_tid = atoi( value );
774
775
        /* unknown attribute */
        else
776
            msg_Warn( p_demux, "invalid <vlc:item> attribute: \"%s\"", name);
777
778
779
    }

    /* attribute href is mandatory */
780
    if( i_tid < 0 )
781
    {
782
        msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
783
        return false;
784
785
    }

786
    if( i_tid >= p_demux->p_sys->i_tracklist_entries )
787
    {
788
        msg_Warn( p_demux, "invalid \"tid\" attribute" );
789
        return false;
yoann's avatar
yoann committed
790
791
    }

792
    p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
yoann's avatar
yoann committed
793
794
    if( p_new_input )
    {
jpd's avatar
jpd committed
795
        input_item_node_AppendItem( p_input_node, p_new_input );
796
        vlc_gc_decref( p_new_input );