]> git.sesse.net Git - vlc/blob - modules/demux/playlist/xspf.c
xspf: simplify again.
[vlc] / modules / demux / playlist / xspf.c
1 /*******************************************************************************
2  * xspf.c : XSPF playlist import functions
3  *******************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Daniel Stränger <vlc at schmaller dot de>
8  *          Yoann Peronneau <yoann@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  ******************************************************************************/
24 /**
25  * \file modules/demux/playlist/xspf.c
26  * \brief XSPF playlist import functions
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
35
36 #include <vlc_xml.h>
37 #include <vlc_strings.h>
38 #include <vlc_url.h>
39 #include "xspf.h"
40 #include "playlist.h"
41
42 struct demux_sys_t
43 {
44     input_item_t **pp_tracklist;
45     int i_tracklist_entries;
46     int i_track_id;
47     char * psz_base;
48 };
49
50 static int Control( demux_t *, int, va_list );
51 static int Demux( demux_t * );
52
53 /**
54  * \brief XSPF submodule initialization function
55  */
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     return VLC_SUCCESS;
61 }
62
63 void Close_xspf( vlc_object_t *p_this )
64 {
65     demux_t *p_demux = (demux_t *)p_this;
66     int i;
67     for(i = 0; i < p_demux->p_sys->i_tracklist_entries; i++)
68     {
69         if(p_demux->p_sys->pp_tracklist[i])
70             vlc_gc_decref( p_demux->p_sys->pp_tracklist[i] );
71     }
72     free( p_demux->p_sys->pp_tracklist );
73     free( p_demux->p_sys->psz_base );
74     free( p_demux->p_sys );
75 }
76
77 /**
78  * \brief demuxer function for XSPF parsing
79  */
80 int Demux( demux_t *p_demux )
81 {
82     int i_ret = -1;
83     xml_t *p_xml = NULL;
84     xml_reader_t *p_xml_reader = NULL;
85     char *psz_name = NULL;
86     INIT_PLAYLIST_STUFF;
87     p_demux->p_sys->pp_tracklist = NULL;
88     p_demux->p_sys->i_tracklist_entries = 0;
89     p_demux->p_sys->i_track_id = -1;
90     p_demux->p_sys->psz_base = NULL;
91
92     /* create new xml parser from stream */
93     p_xml = xml_Create( p_demux );
94     if( !p_xml )
95         goto end;
96
97     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
98     if( !p_xml_reader )
99         goto end;
100
101     /* locating the root node */
102     do
103     {
104         if( xml_ReaderRead( p_xml_reader ) != 1 )
105         {
106             msg_Err( p_demux, "can't read xml stream" );
107             goto end;
108         }
109     } while( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
110
111     /* checking root node name */
112     psz_name = xml_ReaderName( p_xml_reader );
113     if( !psz_name || strcmp( psz_name, "playlist" ) )
114     {
115         msg_Err( p_demux, "invalid root node name: %s", psz_name );
116         goto end;
117     }
118     free( psz_name );
119
120     i_ret = parse_playlist_node( p_demux, p_current_input,
121                                  p_xml_reader, "playlist" ) ? 0 : -1;
122
123     for( int i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
124     {
125         input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
126         if( p_new_input )
127         {
128             input_item_AddSubItem( p_current_input, p_new_input );
129         }
130     }
131
132 end:
133     HANDLE_PLAY_AND_RELEASE;
134     if( p_xml_reader )
135         xml_ReaderDelete( p_xml, p_xml_reader );
136     if( p_xml )
137         xml_Delete( p_xml );
138     return i_ret; /* Needed for correct operation of go back */
139 }
140
141 /** \brief dummy function for demux callback interface */
142 static int Control( demux_t *p_demux, int i_query, va_list args )
143 {
144     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
145     return VLC_EGENERIC;
146 }
147
148 /**
149  * \brief parse the root node of a XSPF playlist
150  * \param p_demux demuxer instance
151  * \param p_input_item current input item
152  * \param p_xml_reader xml reader instance
153  * \param psz_element name of element to parse
154  */
155 static bool parse_playlist_node COMPLEX_INTERFACE
156 {
157     char *psz_name = NULL;
158     char *psz_value = NULL;
159     bool b_version_found = false;
160     int i_node;
161     xml_elem_hnd_t *p_handler = NULL;
162
163     xml_elem_hnd_t pl_elements[] =
164         { {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
165           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
166           {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
167           {"info",         SIMPLE_CONTENT,  {NULL} },
168           {"location",     SIMPLE_CONTENT,  {NULL} },
169           {"identifier",   SIMPLE_CONTENT,  {NULL} },
170           {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
171           {"date",         SIMPLE_CONTENT,  {NULL} },
172           {"license",      SIMPLE_CONTENT,  {NULL} },
173           {"attribution",  COMPLEX_CONTENT, {.cmplx = skip_element} },
174           {"link",         SIMPLE_CONTENT,  {NULL} },
175           {"meta",         SIMPLE_CONTENT,  {NULL} },
176           {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
177           {"trackList",    COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
178           {NULL,           UNKNOWN_CONTENT, {NULL} }
179         };
180
181     /* read all playlist attributes */
182     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
183     {
184         psz_name = xml_ReaderName( p_xml_reader );
185         psz_value = xml_ReaderValue( p_xml_reader );
186         if( !psz_name || !psz_value )
187         {
188             msg_Err( p_demux, "invalid xml stream @ <playlist>" );
189             FREE_ATT();
190             return false;
191         }
192         /* attribute: version */
193         if( !strcmp( psz_name, "version" ) )
194         {
195             b_version_found = true;
196             if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
197                 msg_Warn( p_demux, "unsupported XSPF version" );
198         }
199         /* attribute: xmlns */
200         else if( !strcmp( psz_name, "xmlns" ) || !strcmp( psz_name, "xmlns:vlc" ) )
201             ;
202         else if( !strcmp( psz_name, "xml:base" ) )
203         {
204             p_demux->p_sys->psz_base = strdup( psz_value );
205         }
206         /* unknown attribute */
207         else
208             msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
209
210         FREE_ATT();
211     }
212     /* attribute version is mandatory !!! */
213     if( !b_version_found )
214         msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
215
216     /* parse the child elements - we only take care of <trackList> */
217     while( xml_ReaderRead( p_xml_reader ) == 1 )
218     {
219         i_node = xml_ReaderNodeType( p_xml_reader );
220         switch( i_node )
221         {
222             case XML_READER_NONE:
223                 break;
224             case XML_READER_STARTELEM:
225                 /*  element start tag  */
226                 psz_name = xml_ReaderName( p_xml_reader );
227                 if( !psz_name || !*psz_name )
228                 {
229                     msg_Err( p_demux, "invalid xml stream" );
230                     FREE_ATT();
231                     return false;
232                 }
233                 /* choose handler */
234                 for( p_handler = pl_elements;
235                      p_handler->name && strcmp( psz_name, p_handler->name );
236                      p_handler++ );
237                 if( !p_handler->name )
238                 {
239                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
240                     FREE_ATT();
241                     return false;
242                 }
243                 FREE_NAME();
244                 /* complex content is parsed in a separate function */
245                 if( p_handler->type == COMPLEX_CONTENT )
246                 {
247                     if( p_handler->pf_handler.cmplx( p_demux,
248                                                      p_input_item,
249                                                      p_xml_reader,
250                                                      p_handler->name ) )
251                     {
252                         p_handler = NULL;
253                         FREE_ATT();
254                     }
255                     else
256                     {
257                         FREE_ATT();
258                         return false;
259                     }
260                 }
261                 break;
262
263             case XML_READER_TEXT:
264                 /* simple element content */
265                 FREE_ATT();
266                 psz_value = xml_ReaderValue( p_xml_reader );
267                 if( !psz_value )
268                 {
269                     msg_Err( p_demux, "invalid xml stream" );
270                     FREE_ATT();
271                     return false;
272                 }
273                 break;
274
275             case XML_READER_ENDELEM:
276                 /* element end tag */
277                 psz_name = xml_ReaderName( p_xml_reader );
278                 if( !psz_name )
279                 {
280                     msg_Err( p_demux, "invalid xml stream" );
281                     FREE_ATT();
282                     return false;
283                 }
284                 /* leave if the current parent node <playlist> is terminated */
285                 if( !strcmp( psz_name, psz_element ) )
286                 {
287                     FREE_ATT();
288                     return true;
289                 }
290                 /* there MUST have been a start tag for that element name */
291                 if( !p_handler || !p_handler->name
292                     || strcmp( p_handler->name, psz_name ))
293                 {
294                     msg_Err( p_demux, "there's no open element left for <%s>",
295                              psz_name );
296                     FREE_ATT();
297                     return false;
298                 }
299
300                 if( p_handler->pf_handler.smpl )
301                 {
302                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
303                                                 psz_value );
304                 }
305                 FREE_ATT();
306                 p_handler = NULL;
307                 break;
308
309             default:
310                 /* unknown/unexpected xml node */
311                 msg_Err( p_demux, "unexpected xml node %i", i_node );
312                 FREE_ATT();
313                 return false;
314         }
315         FREE_NAME();
316     }
317     return false;
318 }
319
320 /**
321  * \brief parses the tracklist node which only may contain <track>s
322  */
323 static bool parse_tracklist_node COMPLEX_INTERFACE
324 {
325     VLC_UNUSED(psz_element);
326     char *psz_name;
327     int i_ntracks = 0;
328
329     /* now parse the <track>s */
330     while( xml_ReaderRead( p_xml_reader ) == 1 )
331     {
332         int i_node = xml_ReaderNodeType( p_xml_reader );
333         if( i_node == XML_READER_STARTELEM )
334         {
335             char *psz_eltname = xml_ReaderName( p_xml_reader );
336             if( !psz_eltname )
337             {
338                 msg_Err( p_demux, "unexpected end of xml data" );
339                 free( psz_eltname );
340                 return false;
341             }
342             if( strcmp( psz_eltname, "track") )
343             {
344                 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
345                          psz_eltname );
346                 free( psz_eltname );
347                 return false;
348             }
349             free( psz_eltname );
350
351             /* parse the track data in a separate function */
352             if( parse_track_node( p_demux, p_input_item, p_xml_reader,
353                                   "track" ) )
354                 i_ntracks++;
355         }
356         else if( i_node == XML_READER_ENDELEM )
357             break;
358     }
359
360     /* the <trackList> has to be terminated */
361     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
362     {
363         msg_Err( p_demux, "there's a missing </trackList>" );
364         return false;
365     }
366     psz_name = xml_ReaderName( p_xml_reader );
367     if( !psz_name || strcmp( psz_name, "trackList" ) )
368     {
369         msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
370         free( psz_name );
371         return false;
372     }
373     free( psz_name );
374
375     msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
376     return true;
377 }
378
379 /**
380  * \brief parse one track element
381  * \param COMPLEX_INTERFACE
382  */
383 static bool parse_track_node COMPLEX_INTERFACE
384 {
385     int i_node;
386     char *psz_name = NULL;
387     char *psz_value = NULL;
388     xml_elem_hnd_t *p_handler = NULL;
389
390     xml_elem_hnd_t track_elements[] =
391         { {"location",     SIMPLE_CONTENT,  {NULL} },
392           {"identifier",   SIMPLE_CONTENT,  {NULL} },
393           {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
394           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
395           {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
396           {"info",         SIMPLE_CONTENT,  {NULL} },
397           {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
398           {"album",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
399           {"trackNum",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
400           {"duration",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
401           {"link",         SIMPLE_CONTENT,  {NULL} },
402           {"meta",         SIMPLE_CONTENT,  {NULL} },
403           {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
404           {NULL,           UNKNOWN_CONTENT, {NULL} }
405         };
406
407     input_item_t *p_new_input = input_item_New( p_demux, NULL, NULL );
408
409     if( !p_new_input )
410     {
411         /* malloc has failed for input_item_New, so bailout early */
412         return false;
413     }
414
415     /* reset i_track_id */
416     p_demux->p_sys->i_track_id = -1;
417
418     while( xml_ReaderRead( p_xml_reader ) == 1 )
419     {
420         i_node = xml_ReaderNodeType( p_xml_reader );
421         switch( i_node )
422         {
423             case XML_READER_NONE:
424                 break;
425
426             case XML_READER_STARTELEM:
427                 /*  element start tag  */
428                 psz_name = xml_ReaderName( p_xml_reader );
429                 if( !psz_name || !*psz_name )
430                 {
431                     msg_Err( p_demux, "invalid xml stream" );
432                     FREE_ATT();
433                     return false;
434                 }
435                 /* choose handler */
436                 for( p_handler = track_elements;
437                      p_handler->name && strcmp( psz_name, p_handler->name );
438                      p_handler++ );
439                 if( !p_handler->name )
440                 {
441                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
442                     FREE_ATT();
443                     return false;
444                 }
445                 FREE_NAME();
446                 /* complex content is parsed in a separate function */
447                 if( p_handler->type == COMPLEX_CONTENT )
448                 {
449                     if( !p_new_input )
450                     {
451                         msg_Err( p_demux,
452                                  "at <%s> level no new item has been allocated",
453                                  p_handler->name );
454                         FREE_ATT();
455                         return false;
456                     }
457                     if( p_handler->pf_handler.cmplx( p_demux,
458                                                      p_new_input,
459                                                      p_xml_reader,
460                                                      p_handler->name ) )
461                     {
462                         p_handler = NULL;
463                         FREE_ATT();
464                     }
465                     else
466                     {
467                         FREE_ATT();
468                         return false;
469                     }
470                 }
471                 break;
472
473             case XML_READER_TEXT:
474                 /* simple element content */
475                 FREE_ATT();
476                 psz_value = xml_ReaderValue( p_xml_reader );
477                 if( !psz_value )
478                 {
479                     msg_Err( p_demux, "invalid xml stream" );
480                     FREE_ATT();
481                     return false;
482                 }
483                 break;
484
485             case XML_READER_ENDELEM:
486                 /* element end tag */
487                 psz_name = xml_ReaderName( p_xml_reader );
488                 if( !psz_name )
489                 {
490                     msg_Err( p_demux, "invalid xml stream" );
491                     FREE_ATT();
492                     return false;
493                 }
494
495                 /* leave if the current parent node <track> is terminated */
496                 if( !strcmp( psz_name, psz_element ) )
497                 {
498                     FREE_ATT();
499
500                     /* Make sure we have a URI */
501                     char *psz_uri = input_item_GetURI( p_new_input );
502                     if( !psz_uri )
503                     {
504                         input_item_SetURI( p_new_input, "vlc://nop" );
505                     }
506                     free( psz_uri );
507
508                     if( p_demux->p_sys->i_track_id < 0 )
509                     {
510                         input_item_AddSubItem( p_input_item, p_new_input );
511                         vlc_gc_decref( p_new_input );
512                         return true;
513                     }
514
515                     if( p_demux->p_sys->i_track_id >=
516                            p_demux->p_sys->i_tracklist_entries )
517                     {
518                         input_item_t **pp;
519                         pp = realloc( p_demux->p_sys->pp_tracklist,
520                             (p_demux->p_sys->i_track_id + 1) * sizeof(*pp) );
521                         if( !pp )
522                             return false;
523                         p_demux->p_sys->pp_tracklist = pp;
524                         while( p_demux->p_sys->i_track_id >=
525                                p_demux->p_sys->i_tracklist_entries )
526                             pp[p_demux->p_sys->i_tracklist_entries++] = NULL;
527                     }
528
529                     p_demux->p_sys->pp_tracklist[
530                             p_demux->p_sys->i_track_id ] = p_new_input;
531                     return true;
532                 }
533                 /* there MUST have been a start tag for that element name */
534                 if( !p_handler || !p_handler->name
535                     || strcmp( p_handler->name, psz_name ))
536                 {
537                     msg_Err( p_demux, "there's no open element left for <%s>",
538                              psz_name );
539                     FREE_ATT();
540                     return false;
541                 }
542
543                 /* special case: location */
544                 if( !strcmp( p_handler->name, "location" ) )
545                 {
546                     /* FIXME: This is broken. Scheme-relative (//...) locations
547                      * and anchors (#...) are not resolved correctly. Also,
548                      * host-relative (/...) and directory-relative locations
549                      * ("relative path" in vernacular) should be resolved.
550                      * Last, psz_base should default to the XSPF resource
551                      * location if missing (not the current working directory).
552                      * -- Courmisch */
553                     if( p_demux->p_sys->psz_base && !strstr( psz_value, "://" ) )
554                     {
555                         char* psz_tmp;
556                         if( asprintf( &psz_tmp, "%s%s",
557                                 p_demux->p_sys->psz_base, psz_value ) == -1 )
558                         {
559                             FREE_ATT();
560                             return NULL;
561                         }
562                         input_item_SetURI( p_new_input, psz_tmp );
563                         free( psz_tmp );
564                     }
565                     else
566                         input_item_SetURI( p_new_input, psz_value );
567                     input_item_CopyOptions( p_input_item, p_new_input );
568                     FREE_ATT();
569                     p_handler = NULL;
570                 }
571                 else
572                 {
573                     /* there MUST be an item */
574                     if( !p_new_input )
575                     {
576                         msg_Err( p_demux, "item not yet created at <%s>",
577                                  psz_name );
578                         FREE_ATT();
579                         return false;
580                     }
581                     if( p_handler->pf_handler.smpl )
582                     {
583                         p_handler->pf_handler.smpl( p_new_input,
584                                                     p_handler->name,
585                                                     psz_value );
586                         FREE_ATT();
587                     }
588                 }
589                 FREE_ATT();
590                 p_handler = NULL;
591                 break;
592
593             default:
594                 /* unknown/unexpected xml node */
595                 msg_Err( p_demux, "unexpected xml node %i", i_node );
596                 FREE_ATT();
597                 return false;
598         }
599         FREE_NAME();
600     }
601     msg_Err( p_demux, "unexpected end of xml data" );
602     FREE_ATT();
603     return false;
604 }
605
606 /**
607  * \brief handles the supported <track> sub-elements
608  */
609 static bool set_item_info SIMPLE_INTERFACE
610 {
611     /* exit if setting is impossible */
612     if( !psz_name || !psz_value || !p_input )
613         return false;
614
615
616     /* re-convert xml special characters inside psz_value */
617     resolve_xml_special_chars( psz_value );
618
619     /* handle each info element in a separate "if" clause */
620     if( !strcmp( psz_name, "title" ) )
621     {
622         input_item_SetTitle( p_input, psz_value );
623     }
624     else if( !strcmp( psz_name, "creator" ) )
625     {
626         input_item_SetArtist( p_input, psz_value );
627     }
628     else if( !strcmp( psz_name, "album" ) )
629     {
630         input_item_SetAlbum( p_input, psz_value );
631
632     }
633     else if( !strcmp( psz_name, "trackNum" ) )
634     {
635         input_item_SetTrackNum( p_input, psz_value );
636     }
637     else if( !strcmp( psz_name, "duration" ) )
638     {
639         long i_num = atol( psz_value );
640         input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
641     }
642     else if( !strcmp( psz_name, "annotation" ) )
643     {
644         input_item_SetDescription( p_input, psz_value );
645     }
646     else if( !strcmp( psz_name, "image" ) )
647     {
648         input_item_SetArtURL( p_input, psz_value );
649     }
650     return true;
651 }
652
653 /**
654  * \brief handles the <vlc:option> elements
655  */
656 static bool set_option SIMPLE_INTERFACE
657 {
658     /* exit if setting is impossible */
659     if( !psz_name || !psz_value || !p_input )
660         return false;
661
662     /* re-convert xml special characters inside psz_value */
663     resolve_xml_special_chars( psz_value );
664
665     input_item_AddOption( p_input, psz_value, 0 );
666
667     return true;
668 }
669
670 /**
671  * \brief parse the extension node of a XSPF playlist
672  */
673 static bool parse_extension_node COMPLEX_INTERFACE
674 {
675     char *psz_name = NULL;
676     char *psz_value = NULL;
677     char *psz_title = NULL;
678     char *psz_application = NULL;
679     int i_node;
680     bool b_release_input_item = false;
681     xml_elem_hnd_t *p_handler = NULL;
682     input_item_t *p_new_input = NULL;
683
684     xml_elem_hnd_t pl_elements[] =
685         { {"vlc:node",   COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
686           {"vlc:item",   COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
687           {"vlc:id",     SIMPLE_CONTENT, {NULL} },
688           {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
689           {NULL,    UNKNOWN_CONTENT, {NULL} }
690         };
691
692     /* read all extension node attributes */
693     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
694     {
695         psz_name = xml_ReaderName( p_xml_reader );
696         psz_value = xml_ReaderValue( p_xml_reader );
697         if( !psz_name || !psz_value )
698         {
699             msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
700             FREE_ATT();
701             return false;
702         }
703         /* attribute: title */
704         if( !strcmp( psz_name, "title" ) )
705         {
706             resolve_xml_special_chars( psz_value );
707             psz_title = psz_value;
708         }
709         /* extension attribute: application */
710         else if( !strcmp( psz_name, "application" ) )
711         {
712             psz_application = psz_value;
713         }
714         /* unknown attribute */
715         else
716         {
717             msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
718                       psz_name );
719             FREE_VALUE();
720         }
721         FREE_NAME();
722         psz_value = NULL;
723     }
724
725     /* attribute title is mandatory except for <extension> */
726     if( !strcmp( psz_element, "vlc:node" ) )
727     {
728         if( !psz_title )
729         {
730             msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
731             return false;
732         }
733         p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
734                           "vlc://nop", psz_title, 0, NULL, 0, -1,
735                           ITEM_TYPE_DIRECTORY );
736         if( p_new_input )
737         {
738             input_item_AddSubItem( p_input_item, p_new_input );
739             p_input_item = p_new_input;
740             b_release_input_item = true;
741         }
742         free( psz_title );
743     }
744     else if( !strcmp( psz_element, "extension" ) )
745     {
746         if( !psz_application )
747         {
748             msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
749             return false;
750         }
751         else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
752         {
753             msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
754             free( psz_application );
755             return false;
756         }
757     }
758     free( psz_application );
759
760     /* parse the child elements */
761     while( xml_ReaderRead( p_xml_reader ) == 1 )
762     {
763         i_node = xml_ReaderNodeType( p_xml_reader );
764         switch( i_node )
765         {
766             case XML_READER_NONE:
767                 break;
768             case XML_READER_STARTELEM:
769                 /*  element start tag  */
770                 psz_name = xml_ReaderName( p_xml_reader );
771                 if( !psz_name || !*psz_name )
772                 {
773                     msg_Err( p_demux, "invalid xml stream" );
774                     FREE_ATT();
775                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
776                     return false;
777                 }
778                 /* choose handler */
779                 for( p_handler = pl_elements;
780                      p_handler->name && strcmp( psz_name, p_handler->name );
781                      p_handler++ );
782                 if( !p_handler->name )
783                 {
784                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
785                     FREE_ATT();
786                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
787                     return false;
788                 }
789                 FREE_NAME();
790                 /* complex content is parsed in a separate function */
791                 if( p_handler->type == COMPLEX_CONTENT )
792                 {
793                     if( p_handler->pf_handler.cmplx( p_demux,
794                                                      p_input_item,
795                                                      p_xml_reader,
796                                                      p_handler->name ) )
797                     {
798                         p_handler = NULL;
799                         FREE_ATT();
800                     }
801                     else
802                     {
803                         FREE_ATT();
804                         if( b_release_input_item ) vlc_gc_decref( p_new_input );
805                         return false;
806                     }
807                 }
808                 break;
809
810             case XML_READER_TEXT:
811                 /* simple element content */
812                 FREE_ATT();
813                 psz_value = xml_ReaderValue( p_xml_reader );
814                 if( !psz_value )
815                 {
816                     msg_Err( p_demux, "invalid xml stream" );
817                     FREE_ATT();
818                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
819                     return false;
820                 }
821                 break;
822
823             case XML_READER_ENDELEM:
824                 /* element end tag */
825                 psz_name = xml_ReaderName( p_xml_reader );
826                 if( !psz_name )
827                 {
828                     msg_Err( p_demux, "invalid xml stream" );
829                     FREE_ATT();
830                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
831                     return false;
832                 }
833                 /* leave if the current parent node is terminated */
834                 if( !strcmp( psz_name, psz_element ) )
835                 {
836                     FREE_ATT();
837                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
838                     return true;
839                 }
840                 /* there MUST have been a start tag for that element name */
841                 if( !p_handler || !p_handler->name
842                     || strcmp( p_handler->name, psz_name ))
843                 {
844                     msg_Err( p_demux, "there's no open element left for <%s>",
845                              psz_name );
846                     FREE_ATT();
847                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
848                     return false;
849                 }
850
851                 /* special tag <vlc:id> */
852                 if( !strcmp( p_handler->name, "vlc:id" ) )
853                 {
854                     p_demux->p_sys->i_track_id = atoi( psz_value );
855                 }
856                 else if( p_handler->pf_handler.smpl )
857                 {
858                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
859                                                 psz_value );
860                 }
861                 FREE_ATT();
862                 p_handler = NULL;
863                 break;
864
865             default:
866                 /* unknown/unexpected xml node */
867                 msg_Err( p_demux, "unexpected xml node %i", i_node );
868                 FREE_ATT();
869                 if( b_release_input_item ) vlc_gc_decref( p_new_input );
870                 return false;
871         }
872         FREE_NAME();
873     }
874     if( b_release_input_item ) vlc_gc_decref( p_new_input );
875     return false;
876 }
877
878 /**
879  * \brief parse the extension item node of a XSPF playlist
880  */
881 static bool parse_extitem_node COMPLEX_INTERFACE
882 {
883     VLC_UNUSED(psz_element);
884     input_item_t *p_new_input = NULL;
885     char *psz_name = NULL;
886     char *psz_value = NULL;
887     int i_tid = -1;
888
889     /* read all extension item attributes */
890     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
891     {
892         psz_name = xml_ReaderName( p_xml_reader );
893         psz_value = xml_ReaderValue( p_xml_reader );
894         if( !psz_name || !psz_value )
895         {
896             msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
897             FREE_ATT();
898             return false;
899         }
900         /* attribute: href */
901         if( !strcmp( psz_name, "tid" ) )
902         {
903             i_tid = atoi( psz_value );
904         }
905         /* unknown attribute */
906         else
907             msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
908
909         FREE_ATT();
910     }
911
912     /* attribute href is mandatory */
913     if( i_tid < 0 )
914     {
915         msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
916         return false;
917     }
918
919     if( i_tid >= p_demux->p_sys->i_tracklist_entries )
920     {
921         msg_Warn( p_demux, "invalid \"tid\" attribute" );
922         return false;
923     }
924
925     p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
926     if( p_new_input )
927     {
928         input_item_AddSubItem( p_input_item, p_new_input );
929         vlc_gc_decref( p_new_input );
930         p_demux->p_sys->pp_tracklist[i_tid] = NULL;
931     }
932
933     /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
934     /* (libxml sends NONE) */
935     xml_ReaderRead( p_xml_reader );
936
937     return true;
938 }
939
940 /**
941  * \brief skips complex element content that we can't manage
942  */
943 static bool skip_element COMPLEX_INTERFACE
944 {
945     VLC_UNUSED(p_demux); VLC_UNUSED(p_input_item);
946     char *psz_endname;
947
948     while( xml_ReaderRead( p_xml_reader ) == 1 )
949     {
950         if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
951         {
952             psz_endname = xml_ReaderName( p_xml_reader );
953             if( !psz_endname )
954                 return false;
955             if( !strcmp( psz_element, psz_endname ) )
956             {
957                 free( psz_endname );
958                 return true;
959             }
960             else
961                 free( psz_endname );
962         }
963     }
964     return false;
965 }