]> git.sesse.net Git - vlc/blob - modules/demux/playlist/xspf.c
a35923e31cc06ca4ee82a879750c308d6d4861d3
[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     bool b_ret = false;
162     xml_elem_hnd_t *p_handler = NULL;
163
164     xml_elem_hnd_t pl_elements[] =
165         { {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
166           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
167           {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
168           {"info",         SIMPLE_CONTENT,  {NULL} },
169           {"location",     SIMPLE_CONTENT,  {NULL} },
170           {"identifier",   SIMPLE_CONTENT,  {NULL} },
171           {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
172           {"date",         SIMPLE_CONTENT,  {NULL} },
173           {"license",      SIMPLE_CONTENT,  {NULL} },
174           {"attribution",  COMPLEX_CONTENT, {.cmplx = skip_element} },
175           {"link",         SIMPLE_CONTENT,  {NULL} },
176           {"meta",         SIMPLE_CONTENT,  {NULL} },
177           {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
178           {"trackList",    COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
179           {NULL,           UNKNOWN_CONTENT, {NULL} }
180         };
181
182     /* read all playlist attributes */
183     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
184     {
185         psz_name = xml_ReaderName( p_xml_reader );
186         psz_value = xml_ReaderValue( p_xml_reader );
187         if( !psz_name || !psz_value )
188         {
189             msg_Err( p_demux, "invalid xml stream @ <playlist>" );
190             goto end;
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( psz_name );
211         free( psz_value );
212     }
213     /* attribute version is mandatory !!! */
214     if( !b_version_found )
215         msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
216
217     /* parse the child elements - we only take care of <trackList> */
218     psz_name = NULL;
219     psz_value = NULL;
220     while( xml_ReaderRead( p_xml_reader ) == 1 )
221     {
222         i_node = xml_ReaderNodeType( p_xml_reader );
223         switch( i_node )
224         {
225             case XML_READER_NONE:
226                 break;
227             case XML_READER_STARTELEM:
228                 /*  element start tag  */
229                 psz_name = xml_ReaderName( p_xml_reader );
230                 if( !psz_name || !*psz_name )
231                 {
232                     msg_Err( p_demux, "invalid xml stream" );
233                     goto end;
234                 }
235                 /* choose handler */
236                 for( p_handler = pl_elements;
237                      p_handler->name && strcmp( psz_name, p_handler->name );
238                      p_handler++ );
239                 if( !p_handler->name )
240                 {
241                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
242                     goto end;
243                 }
244                 FREE_NAME();
245                 /* complex content is parsed in a separate function */
246                 if( p_handler->type == COMPLEX_CONTENT )
247                 {
248                     FREE_VALUE();
249                     if( p_handler->pf_handler.cmplx( p_demux,
250                                                      p_input_item,
251                                                      p_xml_reader,
252                                                      p_handler->name ) )
253                     {
254                         p_handler = NULL;
255                     }
256                     else
257                     {
258                         return false;
259                     }
260                 }
261                 break;
262
263             case XML_READER_TEXT:
264                 /* simple element content */
265                 free( psz_value );
266                 psz_value = xml_ReaderValue( p_xml_reader );
267                 if( !psz_value )
268                 {
269                     msg_Err( p_demux, "invalid xml stream" );
270                     goto end;
271                 }
272                 break;
273
274             case XML_READER_ENDELEM:
275                 /* element end tag */
276                 psz_name = xml_ReaderName( p_xml_reader );
277                 if( !psz_name )
278                 {
279                     msg_Err( p_demux, "invalid xml stream" );
280                     goto end;
281                 }
282                 /* leave if the current parent node <playlist> is terminated */
283                 if( !strcmp( psz_name, psz_element ) )
284                 {
285                     b_ret = true;
286                     goto end;
287                 }
288                 /* there MUST have been a start tag for that element name */
289                 if( !p_handler || !p_handler->name
290                     || strcmp( p_handler->name, psz_name ))
291                 {
292                     msg_Err( p_demux, "there's no open element left for <%s>",
293                              psz_name );
294                     goto end;
295                 }
296
297                 if( p_handler->pf_handler.smpl )
298                 {
299                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
300                                                 psz_value );
301                 }
302                 FREE_ATT();
303                 p_handler = NULL;
304                 break;
305
306             default:
307                 /* unknown/unexpected xml node */
308                 msg_Err( p_demux, "unexpected xml node %i", i_node );
309                 goto end;
310         }
311     }
312
313 end:
314     free( psz_name );
315     free( psz_value );
316     return b_ret;
317 }
318
319 /**
320  * \brief parses the tracklist node which only may contain <track>s
321  */
322 static bool parse_tracklist_node COMPLEX_INTERFACE
323 {
324     VLC_UNUSED(psz_element);
325     char *psz_name;
326     int i_ntracks = 0;
327
328     /* now parse the <track>s */
329     while( xml_ReaderRead( p_xml_reader ) == 1 )
330     {
331         int i_node = xml_ReaderNodeType( p_xml_reader );
332         if( i_node == XML_READER_STARTELEM )
333         {
334             char *psz_eltname = xml_ReaderName( p_xml_reader );
335             if( !psz_eltname )
336             {
337                 msg_Err( p_demux, "unexpected end of xml data" );
338                 free( psz_eltname );
339                 return false;
340             }
341             if( strcmp( psz_eltname, "track") )
342             {
343                 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
344                          psz_eltname );
345                 free( psz_eltname );
346                 return false;
347             }
348             free( psz_eltname );
349
350             /* parse the track data in a separate function */
351             if( parse_track_node( p_demux, p_input_item, p_xml_reader,
352                                   "track" ) )
353                 i_ntracks++;
354         }
355         else if( i_node == XML_READER_ENDELEM )
356             break;
357     }
358
359     /* the <trackList> has to be terminated */
360     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
361     {
362         msg_Err( p_demux, "there's a missing </trackList>" );
363         return false;
364     }
365     psz_name = xml_ReaderName( p_xml_reader );
366     if( !psz_name || strcmp( psz_name, "trackList" ) )
367     {
368         msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
369         free( psz_name );
370         return false;
371     }
372     free( psz_name );
373
374     msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
375     return true;
376 }
377
378 /**
379  * \brief parse one track element
380  * \param COMPLEX_INTERFACE
381  */
382 static bool parse_track_node COMPLEX_INTERFACE
383 {
384     int i_node;
385     char *psz_name = NULL;
386     char *psz_value = NULL;
387     xml_elem_hnd_t *p_handler = NULL;
388
389     xml_elem_hnd_t track_elements[] =
390         { {"location",     SIMPLE_CONTENT,  {NULL} },
391           {"identifier",   SIMPLE_CONTENT,  {NULL} },
392           {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
393           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
394           {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
395           {"info",         SIMPLE_CONTENT,  {NULL} },
396           {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
397           {"album",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
398           {"trackNum",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
399           {"duration",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
400           {"link",         SIMPLE_CONTENT,  {NULL} },
401           {"meta",         SIMPLE_CONTENT,  {NULL} },
402           {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
403           {NULL,           UNKNOWN_CONTENT, {NULL} }
404         };
405
406     input_item_t *p_new_input = input_item_New( p_demux, NULL, NULL );
407
408     if( !p_new_input )
409     {
410         /* malloc has failed for input_item_New, so bailout early */
411         return false;
412     }
413
414     /* reset i_track_id */
415     p_demux->p_sys->i_track_id = -1;
416
417     while( xml_ReaderRead( p_xml_reader ) == 1 )
418     {
419         i_node = xml_ReaderNodeType( p_xml_reader );
420         switch( i_node )
421         {
422             case XML_READER_NONE:
423                 break;
424
425             case XML_READER_STARTELEM:
426                 /*  element start tag  */
427                 psz_name = xml_ReaderName( p_xml_reader );
428                 if( !psz_name || !*psz_name )
429                 {
430                     msg_Err( p_demux, "invalid xml stream" );
431                     FREE_ATT();
432                     return false;
433                 }
434                 /* choose handler */
435                 for( p_handler = track_elements;
436                      p_handler->name && strcmp( psz_name, p_handler->name );
437                      p_handler++ );
438                 if( !p_handler->name )
439                 {
440                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
441                     FREE_ATT();
442                     return false;
443                 }
444                 FREE_NAME();
445                 /* complex content is parsed in a separate function */
446                 if( p_handler->type == COMPLEX_CONTENT )
447                 {
448                     if( p_handler->pf_handler.cmplx( p_demux,
449                                                      p_new_input,
450                                                      p_xml_reader,
451                                                      p_handler->name ) )
452                     {
453                         p_handler = NULL;
454                         FREE_ATT();
455                     }
456                     else
457                     {
458                         FREE_ATT();
459                         return false;
460                     }
461                 }
462                 break;
463
464             case XML_READER_TEXT:
465                 /* simple element content */
466                 FREE_ATT();
467                 psz_value = xml_ReaderValue( p_xml_reader );
468                 if( !psz_value )
469                 {
470                     msg_Err( p_demux, "invalid xml stream" );
471                     FREE_ATT();
472                     return false;
473                 }
474                 break;
475
476             case XML_READER_ENDELEM:
477                 /* element end tag */
478                 psz_name = xml_ReaderName( p_xml_reader );
479                 if( !psz_name )
480                 {
481                     msg_Err( p_demux, "invalid xml stream" );
482                     FREE_ATT();
483                     return false;
484                 }
485
486                 /* leave if the current parent node <track> is terminated */
487                 if( !strcmp( psz_name, psz_element ) )
488                 {
489                     FREE_ATT();
490
491                     /* Make sure we have a URI */
492                     char *psz_uri = input_item_GetURI( p_new_input );
493                     if( !psz_uri )
494                     {
495                         input_item_SetURI( p_new_input, "vlc://nop" );
496                     }
497                     free( psz_uri );
498
499                     if( p_demux->p_sys->i_track_id < 0 )
500                     {
501                         input_item_AddSubItem( p_input_item, p_new_input );
502                         vlc_gc_decref( p_new_input );
503                         return true;
504                     }
505
506                     if( p_demux->p_sys->i_track_id >=
507                            p_demux->p_sys->i_tracklist_entries )
508                     {
509                         input_item_t **pp;
510                         pp = realloc( p_demux->p_sys->pp_tracklist,
511                             (p_demux->p_sys->i_track_id + 1) * sizeof(*pp) );
512                         if( !pp )
513                             return false;
514                         p_demux->p_sys->pp_tracklist = pp;
515                         while( p_demux->p_sys->i_track_id >=
516                                p_demux->p_sys->i_tracklist_entries )
517                             pp[p_demux->p_sys->i_tracklist_entries++] = NULL;
518                     }
519
520                     p_demux->p_sys->pp_tracklist[
521                             p_demux->p_sys->i_track_id ] = p_new_input;
522                     return true;
523                 }
524                 /* there MUST have been a start tag for that element name */
525                 if( !p_handler || !p_handler->name
526                     || strcmp( p_handler->name, psz_name ))
527                 {
528                     msg_Err( p_demux, "there's no open element left for <%s>",
529                              psz_name );
530                     FREE_ATT();
531                     return false;
532                 }
533
534                 /* special case: location */
535                 if( !strcmp( p_handler->name, "location" ) )
536                 {
537                     /* FIXME: This is broken. Scheme-relative (//...) locations
538                      * and anchors (#...) are not resolved correctly. Also,
539                      * host-relative (/...) and directory-relative locations
540                      * ("relative path" in vernacular) should be resolved.
541                      * Last, psz_base should default to the XSPF resource
542                      * location if missing (not the current working directory).
543                      * -- Courmisch */
544                     if( p_demux->p_sys->psz_base && !strstr( psz_value, "://" ) )
545                     {
546                         char* psz_tmp;
547                         if( asprintf( &psz_tmp, "%s%s",
548                                 p_demux->p_sys->psz_base, psz_value ) == -1 )
549                         {
550                             FREE_ATT();
551                             return NULL;
552                         }
553                         input_item_SetURI( p_new_input, psz_tmp );
554                         free( psz_tmp );
555                     }
556                     else
557                         input_item_SetURI( p_new_input, psz_value );
558                     input_item_CopyOptions( p_input_item, p_new_input );
559                     FREE_ATT();
560                     p_handler = NULL;
561                 }
562                 else
563                 {
564                     /* there MUST be an item */
565                     if( p_handler->pf_handler.smpl )
566                     {
567                         p_handler->pf_handler.smpl( p_new_input,
568                                                     p_handler->name,
569                                                     psz_value );
570                         FREE_ATT();
571                     }
572                 }
573                 FREE_ATT();
574                 p_handler = NULL;
575                 break;
576
577             default:
578                 /* unknown/unexpected xml node */
579                 msg_Err( p_demux, "unexpected xml node %i", i_node );
580                 FREE_ATT();
581                 return false;
582         }
583         FREE_NAME();
584     }
585     msg_Err( p_demux, "unexpected end of xml data" );
586     FREE_ATT();
587     return false;
588 }
589
590 /**
591  * \brief handles the supported <track> sub-elements
592  */
593 static bool set_item_info SIMPLE_INTERFACE
594 {
595     /* exit if setting is impossible */
596     if( !psz_name || !psz_value || !p_input )
597         return false;
598
599     /* re-convert xml special characters inside psz_value */
600     resolve_xml_special_chars( psz_value );
601
602     /* handle each info element in a separate "if" clause */
603     if( !strcmp( psz_name, "title" ) )
604     {
605         input_item_SetTitle( p_input, psz_value );
606     }
607     else if( !strcmp( psz_name, "creator" ) )
608     {
609         input_item_SetArtist( p_input, psz_value );
610     }
611     else if( !strcmp( psz_name, "album" ) )
612     {
613         input_item_SetAlbum( p_input, psz_value );
614
615     }
616     else if( !strcmp( psz_name, "trackNum" ) )
617     {
618         input_item_SetTrackNum( p_input, psz_value );
619     }
620     else if( !strcmp( psz_name, "duration" ) )
621     {
622         long i_num = atol( psz_value );
623         input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
624     }
625     else if( !strcmp( psz_name, "annotation" ) )
626     {
627         input_item_SetDescription( p_input, psz_value );
628     }
629     else if( !strcmp( psz_name, "image" ) )
630     {
631         input_item_SetArtURL( p_input, psz_value );
632     }
633     return true;
634 }
635
636 /**
637  * \brief handles the <vlc:option> elements
638  */
639 static bool set_option SIMPLE_INTERFACE
640 {
641     /* exit if setting is impossible */
642     if( !psz_name || !psz_value || !p_input )
643         return false;
644
645     /* re-convert xml special characters inside psz_value */
646     resolve_xml_special_chars( psz_value );
647
648     input_item_AddOption( p_input, psz_value, 0 );
649
650     return true;
651 }
652
653 /**
654  * \brief parse the extension node of a XSPF playlist
655  */
656 static bool parse_extension_node COMPLEX_INTERFACE
657 {
658     char *psz_name = NULL;
659     char *psz_value = NULL;
660     char *psz_title = NULL;
661     char *psz_application = NULL;
662     int i_node;
663     bool b_release_input_item = false;
664     xml_elem_hnd_t *p_handler = NULL;
665     input_item_t *p_new_input = NULL;
666
667     xml_elem_hnd_t pl_elements[] =
668         { {"vlc:node",   COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
669           {"vlc:item",   COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
670           {"vlc:id",     SIMPLE_CONTENT, {NULL} },
671           {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
672           {NULL,    UNKNOWN_CONTENT, {NULL} }
673         };
674
675     /* read all extension node attributes */
676     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
677     {
678         psz_name = xml_ReaderName( p_xml_reader );
679         psz_value = xml_ReaderValue( p_xml_reader );
680         if( !psz_name || !psz_value )
681         {
682             msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
683             FREE_ATT();
684             return false;
685         }
686         /* attribute: title */
687         if( !strcmp( psz_name, "title" ) )
688         {
689             resolve_xml_special_chars( psz_value );
690             psz_title = psz_value;
691         }
692         /* extension attribute: application */
693         else if( !strcmp( psz_name, "application" ) )
694         {
695             psz_application = psz_value;
696         }
697         /* unknown attribute */
698         else
699         {
700             msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
701                       psz_name );
702             FREE_VALUE();
703         }
704         FREE_NAME();
705         psz_value = NULL;
706     }
707
708     /* attribute title is mandatory except for <extension> */
709     if( !strcmp( psz_element, "vlc:node" ) )
710     {
711         if( !psz_title )
712         {
713             msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
714             return false;
715         }
716         p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
717                           "vlc://nop", psz_title, 0, NULL, 0, -1,
718                           ITEM_TYPE_DIRECTORY );
719         if( p_new_input )
720         {
721             input_item_AddSubItem( p_input_item, p_new_input );
722             p_input_item = p_new_input;
723             b_release_input_item = true;
724         }
725         free( psz_title );
726     }
727     else if( !strcmp( psz_element, "extension" ) )
728     {
729         if( !psz_application )
730         {
731             msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
732             return false;
733         }
734         else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
735         {
736             msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
737             free( psz_application );
738             return false;
739         }
740     }
741     free( psz_application );
742
743     /* parse the child elements */
744     while( xml_ReaderRead( p_xml_reader ) == 1 )
745     {
746         i_node = xml_ReaderNodeType( p_xml_reader );
747         switch( i_node )
748         {
749             case XML_READER_NONE:
750                 break;
751             case XML_READER_STARTELEM:
752                 /*  element start tag  */
753                 psz_name = xml_ReaderName( p_xml_reader );
754                 if( !psz_name || !*psz_name )
755                 {
756                     msg_Err( p_demux, "invalid xml stream" );
757                     FREE_ATT();
758                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
759                     return false;
760                 }
761                 /* choose handler */
762                 for( p_handler = pl_elements;
763                      p_handler->name && strcmp( psz_name, p_handler->name );
764                      p_handler++ );
765                 if( !p_handler->name )
766                 {
767                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
768                     FREE_ATT();
769                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
770                     return false;
771                 }
772                 FREE_NAME();
773                 /* complex content is parsed in a separate function */
774                 if( p_handler->type == COMPLEX_CONTENT )
775                 {
776                     if( p_handler->pf_handler.cmplx( p_demux,
777                                                      p_input_item,
778                                                      p_xml_reader,
779                                                      p_handler->name ) )
780                     {
781                         p_handler = NULL;
782                         FREE_ATT();
783                     }
784                     else
785                     {
786                         FREE_ATT();
787                         if( b_release_input_item ) vlc_gc_decref( p_new_input );
788                         return false;
789                     }
790                 }
791                 break;
792
793             case XML_READER_TEXT:
794                 /* simple element content */
795                 FREE_ATT();
796                 psz_value = xml_ReaderValue( p_xml_reader );
797                 if( !psz_value )
798                 {
799                     msg_Err( p_demux, "invalid xml stream" );
800                     FREE_ATT();
801                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
802                     return false;
803                 }
804                 break;
805
806             case XML_READER_ENDELEM:
807                 /* element end tag */
808                 psz_name = xml_ReaderName( p_xml_reader );
809                 if( !psz_name )
810                 {
811                     msg_Err( p_demux, "invalid xml stream" );
812                     FREE_ATT();
813                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
814                     return false;
815                 }
816                 /* leave if the current parent node is terminated */
817                 if( !strcmp( psz_name, psz_element ) )
818                 {
819                     FREE_ATT();
820                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
821                     return true;
822                 }
823                 /* there MUST have been a start tag for that element name */
824                 if( !p_handler || !p_handler->name
825                     || strcmp( p_handler->name, psz_name ))
826                 {
827                     msg_Err( p_demux, "there's no open element left for <%s>",
828                              psz_name );
829                     FREE_ATT();
830                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
831                     return false;
832                 }
833
834                 /* special tag <vlc:id> */
835                 if( !strcmp( p_handler->name, "vlc:id" ) )
836                 {
837                     p_demux->p_sys->i_track_id = atoi( psz_value );
838                 }
839                 else if( p_handler->pf_handler.smpl )
840                 {
841                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
842                                                 psz_value );
843                 }
844                 FREE_ATT();
845                 p_handler = NULL;
846                 break;
847
848             default:
849                 /* unknown/unexpected xml node */
850                 msg_Err( p_demux, "unexpected xml node %i", i_node );
851                 FREE_ATT();
852                 if( b_release_input_item ) vlc_gc_decref( p_new_input );
853                 return false;
854         }
855         FREE_NAME();
856     }
857     if( b_release_input_item ) vlc_gc_decref( p_new_input );
858     return false;
859 }
860
861 /**
862  * \brief parse the extension item node of a XSPF playlist
863  */
864 static bool parse_extitem_node COMPLEX_INTERFACE
865 {
866     VLC_UNUSED(psz_element);
867     input_item_t *p_new_input = NULL;
868     int i_tid = -1;
869
870     /* read all extension item attributes */
871     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
872     {
873         char *psz_name = xml_ReaderName( p_xml_reader );
874         char *psz_value = xml_ReaderValue( p_xml_reader );
875         if( !psz_name || !psz_value )
876         {
877             msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
878             free( psz_name );
879             free( psz_value );
880             return false;
881         }
882         /* attribute: href */
883         if( !strcmp( psz_name, "tid" ) )
884         {
885             i_tid = atoi( psz_value );
886         }
887         /* unknown attribute */
888         else
889             msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
890
891         free( psz_name );
892         free( psz_value );
893     }
894
895     /* attribute href is mandatory */
896     if( i_tid < 0 )
897     {
898         msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
899         return false;
900     }
901
902     if( i_tid >= p_demux->p_sys->i_tracklist_entries )
903     {
904         msg_Warn( p_demux, "invalid \"tid\" attribute" );
905         return false;
906     }
907
908     p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
909     if( p_new_input )
910     {
911         input_item_AddSubItem( p_input_item, p_new_input );
912         vlc_gc_decref( p_new_input );
913         p_demux->p_sys->pp_tracklist[i_tid] = NULL;
914     }
915
916     /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
917     /* (libxml sends NONE) */
918     xml_ReaderRead( p_xml_reader );
919
920     return true;
921 }
922
923 /**
924  * \brief skips complex element content that we can't manage
925  */
926 static bool skip_element COMPLEX_INTERFACE
927 {
928     VLC_UNUSED(p_demux); VLC_UNUSED(p_input_item);
929
930     while( xml_ReaderRead( p_xml_reader ) == 1 )
931     {
932         if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
933         {
934             char *psz_endname = xml_ReaderName( p_xml_reader );
935             if( !psz_endname )
936                 return false;
937             if( !strcmp( psz_element, psz_endname ) )
938             {
939                 free( psz_endname );
940                 return true;
941             }
942             else
943                 free( psz_endname );
944         }
945     }
946     return false;
947 }