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