]> git.sesse.net Git - vlc/blob - modules/demux/playlist/xspf.c
* Protect input item's meta through setters and getters. That allows tracking of...
[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->p_item_in_category = p_item_in_category;
79     p_demux->p_sys->pp_tracklist = NULL;
80     p_demux->p_sys->i_tracklist_entries = 0;
81     p_demux->p_sys->i_identifier = 0;
82     p_demux->p_sys->psz_base = NULL;
83
84     /* create new xml parser from stream */
85     p_xml = xml_Create( p_demux );
86     if( !p_xml )
87         i_ret = VLC_ENOMOD;
88     else
89     {
90         p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
91         if( !p_xml_reader )
92             i_ret = VLC_EGENERIC;
93     }
94
95     /* locating the root node */
96     if( i_ret == VLC_SUCCESS )
97     {
98         do
99         {
100             if( xml_ReaderRead( p_xml_reader ) != 1 )
101             {
102                 msg_Err( p_demux, "can't read xml stream" );
103                 i_ret = VLC_EGENERIC;
104             }
105         } while( i_ret == VLC_SUCCESS &&
106                  xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
107     }
108     /* checking root node name */
109     if( i_ret == VLC_SUCCESS )
110     {
111         psz_name = xml_ReaderName( p_xml_reader );
112         if( !psz_name || strcmp( psz_name, "playlist" ) )
113         {
114             msg_Err( p_demux, "invalid root node name: %s", psz_name );
115             i_ret = VLC_EGENERIC;
116         }
117         FREE_NAME();
118     }
119
120     i_ret = parse_playlist_node( p_demux, p_playlist, p_current, NULL,
121                                  p_xml_reader, "playlist" );
122     HANDLE_PLAY_AND_RELEASE;
123     if( p_xml_reader )
124         xml_ReaderDelete( p_xml, p_xml_reader );
125     if( p_xml )
126         xml_Delete( p_xml );
127     return -1; /* Needed for correct operation of go back */
128 }
129
130 /** \brief dummy function for demux callback interface */
131 static int Control( demux_t *p_demux, int i_query, va_list args )
132 {
133     return VLC_EGENERIC;
134 }
135
136 /**
137  * \brief parse the root node of a XSPF playlist
138  * \param p_demux demuxer instance
139  * \param p_playlist playlist instance
140  * \param p_item current playlist item
141  * \param p_input 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,  {NULL} },
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_item,NULL,
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_item, NULL, 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_item, NULL,
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,  {NULL} },
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                                                      NULL, 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                     playlist_BothAddInput( p_playlist, p_new_input,
483                                            p_demux->p_sys->p_item_in_category,
484                                            PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
485                                            PLAYLIST_END, NULL, NULL, VLC_FALSE );
486                     if( p_demux->p_sys->i_identifier <
487                         p_demux->p_sys->i_tracklist_entries )
488                     {
489                         p_demux->p_sys->pp_tracklist[
490                             p_demux->p_sys->i_identifier ] = p_new_input;
491                     }
492                     else
493                     {
494                         if( p_demux->p_sys->i_identifier >
495                             p_demux->p_sys->i_tracklist_entries )
496                         {
497                             p_demux->p_sys->i_tracklist_entries =
498                                 p_demux->p_sys->i_identifier;
499                         }
500                         INSERT_ELEM( p_demux->p_sys->pp_tracklist,
501                                      p_demux->p_sys->i_tracklist_entries,
502                                      p_demux->p_sys->i_tracklist_entries,
503                                      p_new_input );
504                     }
505                     return VLC_TRUE;
506                 }
507                 /* there MUST have been a start tag for that element name */
508                 if( !p_handler || !p_handler->name
509                     || strcmp( p_handler->name, psz_name ))
510                 {
511                     msg_Err( p_demux, "there's no open element left for <%s>",
512                              psz_name );
513                     FREE_ATT();
514                     return VLC_FALSE;
515                 }
516
517                 /* special case: location */
518                 if( !strcmp( p_handler->name, "location" ) )
519                 {
520                     char *psz_uri=NULL;
521                     /* there MUST NOT be an item */
522                     if( p_new_input )
523                     {
524                         msg_Err( p_demux, "item <%s> already created",
525                                  psz_name );
526                         FREE_ATT();
527                         return VLC_FALSE;
528                     }
529                     psz_uri = decode_URI_duplicate( psz_value );
530
531                     if( psz_uri )
532                     {
533                         if( p_demux->p_sys->psz_base &&
534                             !strstr( psz_uri, "://" ) )
535                         {
536                            char* psz_tmp = malloc(
537                                    strlen(p_demux->p_sys->psz_base) +
538                                    strlen(psz_uri) +1 );
539                            if( !psz_tmp )
540                            {
541                                msg_Err( p_demux, "out of memory");
542                                return VLC_FALSE;
543                            }
544                            sprintf( psz_tmp, "%s%s",
545                                     p_demux->p_sys->psz_base, psz_uri );
546                            free( psz_uri );
547                            psz_uri = psz_tmp;
548                         }
549                         p_new_input = input_ItemNewExt( p_playlist, psz_uri,
550                                                         NULL, 0, NULL, -1 );
551                         free( psz_uri );
552                         input_ItemCopyOptions( p_item->p_input, p_new_input );
553                         psz_uri = NULL;
554                         FREE_ATT();
555                         p_handler = NULL;
556                     }
557                     else
558                     {
559                         FREE_ATT();
560                         return VLC_FALSE;
561                     }
562                 }
563                 else if( !strcmp( p_handler->name, "identifier" ) )
564                 {
565                     p_demux->p_sys->i_identifier = atoi( psz_value );
566                 }
567                 else
568                 {
569                     /* there MUST be an item */
570                     if( !p_new_input )
571                     {
572                         msg_Err( p_demux, "item not yet created at <%s>",
573                                  psz_name );
574                         FREE_ATT();
575                         return VLC_FALSE;
576                     }
577                     if( p_handler->pf_handler.smpl )
578                     {
579                         p_handler->pf_handler.smpl( NULL, p_new_input,
580                                                     p_handler->name,
581                                                     psz_value );
582                         FREE_ATT();
583                     }
584                 }
585                 FREE_ATT();
586                 p_handler = NULL;
587                 break;
588
589             default:
590                 /* unknown/unexpected xml node */
591                 msg_Err( p_demux, "unexpected xml node %i", i_node );
592                 FREE_ATT();
593                 return VLC_FALSE;
594         }
595         FREE_NAME();
596     }
597     msg_Err( p_demux, "unexpected end of xml data" );
598     FREE_ATT();
599     return VLC_FALSE;
600 }
601
602 /**
603  * \brief handles the supported <track> sub-elements
604  */
605 static vlc_bool_t set_item_info SIMPLE_INTERFACE
606 {
607     /* exit if setting is impossible */
608     if( !psz_name || !psz_value || !p_input )
609         return VLC_FALSE;
610
611
612     /* re-convert xml special characters inside psz_value */
613     resolve_xml_special_chars( psz_value );
614
615     /* handle each info element in a separate "if" clause */
616     if( !strcmp( psz_name, "title" ) )
617     {
618         p_input->psz_name = strdup( (char*)psz_value );
619     }
620     else if( !strcmp( psz_name, "creator" ) )
621     {
622         input_item_SetArtist( p_input, psz_value );
623     }
624     else if( !strcmp( psz_name, "album" ) )
625     {
626         input_item_SetAlbum( p_input, psz_value );
627
628     }
629     else if( !strcmp( psz_name, "trackNum" ) )
630     {
631         input_item_SetTrackNum( p_input, psz_value );
632     }
633     else if( !strcmp( psz_name, "duration" ) )
634     {
635         long i_num = atol( psz_value );
636         p_input->i_duration = i_num*1000;
637     }
638     else if( !strcmp( psz_name, "annotation" ) )
639     {
640         input_item_SetDescription( p_input, psz_value );
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     int i_node;
655     xml_elem_hnd_t *p_handler = NULL;
656
657     xml_elem_hnd_t pl_elements[] =
658         { {"node",  COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
659           {"item",  COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
660           {NULL,    UNKNOWN_CONTENT, {NULL} }
661         };
662
663     /* read all extension node attributes */
664     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
665     {
666         psz_name = xml_ReaderName( p_xml_reader );
667         psz_value = xml_ReaderValue( p_xml_reader );
668         if( !psz_name || !psz_value )
669         {
670             msg_Err( p_demux, "invalid xml stream @ <node>" );
671             FREE_ATT();
672             return VLC_FALSE;
673         }
674         /* attribute: title */
675         if( !strcmp( psz_name, "title" ) )
676         {
677             psz_title = unescape_URI_duplicate( psz_value );
678         }
679         /* unknown attribute */
680         else
681             msg_Warn( p_demux, "invalid <node> attribute:\"%s\"", psz_name);
682
683         FREE_ATT();
684     }
685
686     /* attribute title is mandatory except for <extension> */
687     if( !strcmp( psz_element, "node" ) && !psz_title )
688     {
689         msg_Warn( p_demux, "<node> requires \"title\" attribute" );
690         return VLC_FALSE;
691     }
692     if( psz_title ) free( psz_title );
693
694     /* parse the child elements */
695     while( xml_ReaderRead( p_xml_reader ) == 1 )
696     {
697         i_node = xml_ReaderNodeType( p_xml_reader );
698         switch( i_node )
699         {
700             case XML_READER_NONE:
701                 break;
702             case XML_READER_STARTELEM:
703                 /*  element start tag  */
704                 psz_name = xml_ReaderName( p_xml_reader );
705                 if( !psz_name || !*psz_name )
706                 {
707                     msg_Err( p_demux, "invalid xml stream" );
708                     FREE_ATT();
709                     return VLC_FALSE;
710                 }
711                 /* choose handler */
712                 for( p_handler = pl_elements;
713                      p_handler->name && strcmp( psz_name, p_handler->name );
714                      p_handler++ );
715                 if( !p_handler->name )
716                 {
717                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
718                     FREE_ATT();
719                     return VLC_FALSE;
720                 }
721                 FREE_NAME();
722                 /* complex content is parsed in a separate function */
723                 if( p_handler->type == COMPLEX_CONTENT )
724                 {
725                     if( p_handler->pf_handler.cmplx( p_demux,
726                                                      p_playlist,
727                                                      p_item, NULL,
728                                                      p_xml_reader,
729                                                      p_handler->name ) )
730                     {
731                         p_handler = NULL;
732                         FREE_ATT();
733                     }
734                     else
735                     {
736                         FREE_ATT();
737                         return VLC_FALSE;
738                     }
739                 }
740                 break;
741
742             case XML_READER_TEXT:
743                 /* simple element content */
744                 FREE_ATT();
745                 psz_value = xml_ReaderValue( p_xml_reader );
746                 if( !psz_value )
747                 {
748                     msg_Err( p_demux, "invalid xml stream" );
749                     FREE_ATT();
750                     return VLC_FALSE;
751                 }
752                 break;
753
754             case XML_READER_ENDELEM:
755                 /* element end tag */
756                 psz_name = xml_ReaderName( p_xml_reader );
757                 if( !psz_name )
758                 {
759                     msg_Err( p_demux, "invalid xml stream" );
760                     FREE_ATT();
761                     return VLC_FALSE;
762                 }
763                 /* leave if the current parent node is terminated */
764                 if( !strcmp( psz_name, psz_element ) )
765                 {
766                     FREE_ATT();
767                     return VLC_TRUE;
768                 }
769                 /* there MUST have been a start tag for that element name */
770                 if( !p_handler || !p_handler->name
771                     || strcmp( p_handler->name, psz_name ))
772                 {
773                     msg_Err( p_demux, "there's no open element left for <%s>",
774                              psz_name );
775                     FREE_ATT();
776                     return VLC_FALSE;
777                 }
778
779                 if( p_handler->pf_handler.smpl )
780                 {
781                     p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
782                                                 psz_value );
783                 }
784                 FREE_ATT();
785                 p_handler = NULL;
786                 break;
787
788             default:
789                 /* unknown/unexpected xml node */
790                 msg_Err( p_demux, "unexpected xml node %i", i_node );
791                 FREE_ATT();
792                 return VLC_FALSE;
793         }
794         FREE_NAME();
795     }
796     return VLC_FALSE;
797 }
798
799 /**
800  * \brief parse the extension item node of a XSPF playlist
801  */
802 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
803 {
804     char *psz_name = NULL;
805     char *psz_value = NULL;
806     int i_href = -1;
807
808     /* read all extension item attributes */
809     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
810     {
811         psz_name = xml_ReaderName( p_xml_reader );
812         psz_value = xml_ReaderValue( p_xml_reader );
813         if( !psz_name || !psz_value )
814         {
815             msg_Err( p_demux, "invalid xml stream @ <item>" );
816             FREE_ATT();
817             return VLC_FALSE;
818         }
819         /* attribute: href */
820         if( !strcmp( psz_name, "href" ) )
821         {
822             i_href = atoi( psz_value );
823         }
824         /* unknown attribute */
825         else
826             msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
827
828         FREE_ATT();
829     }
830
831     /* attribute href is mandatory */
832     if( i_href < 0 )
833     {
834         msg_Warn( p_demux, "<item> requires \"href\" attribute" );
835         return VLC_FALSE;
836     }
837
838     if( i_href > p_demux->p_sys->i_tracklist_entries )
839     {
840         msg_Warn( p_demux, "invalid \"href\" attribute" );
841         return VLC_FALSE;
842     }
843     return VLC_TRUE;
844 }
845
846 /**
847  * \brief skips complex element content that we can't manage
848  */
849 static vlc_bool_t skip_element COMPLEX_INTERFACE
850 {
851     char *psz_endname;
852
853     while( xml_ReaderRead( p_xml_reader ) == 1 )
854     {
855         if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
856         {
857             psz_endname = xml_ReaderName( p_xml_reader );
858             if( !psz_endname )
859                 return VLC_FALSE;
860             if( !strcmp( psz_element, psz_endname ) )
861             {
862                 free( psz_endname );
863                 return VLC_TRUE;
864             }
865             else
866                 free( psz_endname );
867         }
868     }
869     return VLC_FALSE;
870 }