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