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