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