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