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