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