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