]> git.sesse.net Git - vlc/blob - modules/demux/playlist/xspf.c
A bit of headers cleanup
[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->psz_base);
64     free( p_demux->p_sys );
65 }
66
67 /**
68  * \brief demuxer function for XSPF parsing
69  */
70 int Demux( demux_t *p_demux )
71 {
72     int i_ret = VLC_SUCCESS;
73     xml_t *p_xml = NULL;
74     xml_reader_t *p_xml_reader = NULL;
75     char *psz_name = NULL;
76     INIT_PLAYLIST_STUFF;
77     p_demux->p_sys->p_item_in_category = p_item_in_category;
78     p_demux->p_sys->pp_tracklist = NULL;
79     p_demux->p_sys->i_tracklist_entries = 0;
80     p_demux->p_sys->i_identifier = -1;
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, NULL,
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_item current playlist item
140  * \param p_input current input item
141  * \param p_xml_reader xml reader instance
142  * \param psz_element name of element to parse
143  */
144 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
145 {
146     char *psz_name=NULL;
147     char *psz_value=NULL;
148     vlc_bool_t b_version_found = VLC_FALSE;
149     int i_node;
150     xml_elem_hnd_t *p_handler=NULL;
151
152     xml_elem_hnd_t pl_elements[] =
153         { {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
154           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
155           {"annotation",   SIMPLE_CONTENT,  {NULL} },
156           {"info",         SIMPLE_CONTENT,  {NULL} },
157           {"location",     SIMPLE_CONTENT,  {NULL} },
158           {"identifier",   SIMPLE_CONTENT,  {NULL} },
159           {"image",        SIMPLE_CONTENT,  {NULL} },
160           {"date",         SIMPLE_CONTENT,  {NULL} },
161           {"license",      SIMPLE_CONTENT,  {NULL} },
162           {"attribution",  COMPLEX_CONTENT, {.cmplx = skip_element} },
163           {"link",         SIMPLE_CONTENT,  {NULL} },
164           {"meta",         SIMPLE_CONTENT,  {NULL} },
165           {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
166           {"trackList",    COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
167           {NULL,           UNKNOWN_CONTENT, {NULL} }
168         };
169
170     /* read all playlist attributes */
171     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
172     {
173         psz_name = xml_ReaderName( p_xml_reader );
174         psz_value = xml_ReaderValue( p_xml_reader );
175         if( !psz_name || !psz_value )
176         {
177             msg_Err( p_demux, "invalid xml stream @ <playlist>" );
178             FREE_ATT();
179             return VLC_FALSE;
180         }
181         /* attribute: version */
182         if( !strcmp( psz_name, "version" ) )
183         {
184             b_version_found = VLC_TRUE;
185             if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
186                 msg_Warn( p_demux, "unsupported XSPF version" );
187         }
188         /* attribute: xmlns */
189         else if( !strcmp( psz_name, "xmlns" ) )
190             ;
191         else if( !strcmp( psz_name, "xml:base" ) )
192         {
193             p_demux->p_sys->psz_base = decode_URI_duplicate( psz_value );
194         }
195         /* unknown attribute */
196         else
197             msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
198
199         FREE_ATT();
200     }
201     /* attribute version is mandatory !!! */
202     if( !b_version_found )
203         msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
204
205     /* parse the child elements - we only take care of <trackList> */
206     while( xml_ReaderRead( p_xml_reader ) == 1 )
207     {
208         i_node = xml_ReaderNodeType( p_xml_reader );
209         switch( i_node )
210         {
211             case XML_READER_NONE:
212                 break;
213             case XML_READER_STARTELEM:
214                 /*  element start tag  */
215                 psz_name = xml_ReaderName( p_xml_reader );
216                 if( !psz_name || !*psz_name )
217                 {
218                     msg_Err( p_demux, "invalid xml stream" );
219                     FREE_ATT();
220                     return VLC_FALSE;
221                 }
222                 /* choose handler */
223                 for( p_handler = pl_elements;
224                      p_handler->name && strcmp( psz_name, p_handler->name );
225                      p_handler++ );
226                 if( !p_handler->name )
227                 {
228                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
229                     FREE_ATT();
230                     return VLC_FALSE;
231                 }
232                 FREE_NAME();
233                 /* complex content is parsed in a separate function */
234                 if( p_handler->type == COMPLEX_CONTENT )
235                 {
236                     if( p_handler->pf_handler.cmplx( p_demux,
237                                                      p_playlist,
238                                                      p_item,NULL,
239                                                      p_xml_reader,
240                                                      p_handler->name ) )
241                     {
242                         p_handler = NULL;
243                         FREE_ATT();
244                     }
245                     else
246                     {
247                         FREE_ATT();
248                         return VLC_FALSE;
249                     }
250                 }
251                 break;
252
253             case XML_READER_TEXT:
254                 /* simple element content */
255                 FREE_ATT();
256                 psz_value = xml_ReaderValue( p_xml_reader );
257                 if( !psz_value )
258                 {
259                     msg_Err( p_demux, "invalid xml stream" );
260                     FREE_ATT();
261                     return VLC_FALSE;
262                 }
263                 break;
264
265             case XML_READER_ENDELEM:
266                 /* element end tag */
267                 psz_name = xml_ReaderName( p_xml_reader );
268                 if( !psz_name )
269                 {
270                     msg_Err( p_demux, "invalid xml stream" );
271                     FREE_ATT();
272                     return VLC_FALSE;
273                 }
274                 /* leave if the current parent node <playlist> is terminated */
275                 if( !strcmp( psz_name, psz_element ) )
276                 {
277                     FREE_ATT();
278                     return VLC_TRUE;
279                 }
280                 /* there MUST have been a start tag for that element name */
281                 if( !p_handler || !p_handler->name
282                     || strcmp( p_handler->name, psz_name ))
283                 {
284                     msg_Err( p_demux, "there's no open element left for <%s>",
285                              psz_name );
286                     FREE_ATT();
287                     return VLC_FALSE;
288                 }
289
290                 if( p_handler->pf_handler.smpl )
291                 {
292                     p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
293                                                 psz_value );
294                 }
295                 FREE_ATT();
296                 p_handler = NULL;
297                 break;
298
299             default:
300                 /* unknown/unexpected xml node */
301                 msg_Err( p_demux, "unexpected xml node %i", i_node );
302                 FREE_ATT();
303                 return VLC_FALSE;
304         }
305         FREE_NAME();
306     }
307     return VLC_FALSE;
308 }
309
310 /**
311  * \brief parses the tracklist node which only may contain <track>s
312  */
313 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
314 {
315     char *psz_name=NULL;
316     int i_node;
317     int i_ntracks = 0;
318
319     /* now parse the <track>s */
320     while( xml_ReaderRead( p_xml_reader ) == 1 )
321     {
322         i_node = xml_ReaderNodeType( p_xml_reader );
323         if( i_node == XML_READER_STARTELEM )
324         {
325             psz_name = xml_ReaderName( p_xml_reader );
326             if( !psz_name )
327             {
328                 msg_Err( p_demux, "unexpected end of xml data" );
329                 FREE_NAME();
330                 return VLC_FALSE;
331             }
332             if( strcmp( psz_name, "track") )
333             {
334                 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
335                          psz_name );
336                 FREE_NAME();
337                 return VLC_FALSE;
338             }
339             FREE_NAME();
340
341             /* parse the track data in a separate function */
342             if( parse_track_node( p_demux, p_playlist, p_item, NULL,
343                                    p_xml_reader,"track" ) == VLC_TRUE )
344                 i_ntracks++;
345         }
346         else if( i_node == XML_READER_ENDELEM )
347             break;
348     }
349
350     /* the <trackList> has to be terminated */
351     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
352     {
353         msg_Err( p_demux, "there's a missing </trackList>" );
354         FREE_NAME();
355         return VLC_FALSE;
356     }
357     psz_name = xml_ReaderName( p_xml_reader );
358     if( !psz_name || strcmp( psz_name, "trackList" ) )
359     {
360         msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
361         FREE_NAME();
362         return VLC_FALSE;
363     }
364     FREE_NAME();
365
366     msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
367
368     return VLC_TRUE;
369 }
370
371 /**
372  * \brief parse one track element
373  * \param COMPLEX_INTERFACE
374  */
375 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
376 {
377     input_item_t *p_new_input = NULL;
378     int i_node;
379     char *psz_name=NULL;
380     char *psz_value=NULL;
381     xml_elem_hnd_t *p_handler=NULL;
382
383     xml_elem_hnd_t track_elements[] =
384         { {"location",     SIMPLE_CONTENT,  {NULL} },
385           {"identifier",   SIMPLE_CONTENT,  {NULL} },
386           {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
387           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
388           {"annotation",   SIMPLE_CONTENT,  {NULL} },
389           {"info",         SIMPLE_CONTENT,  {NULL} },
390           {"image",        SIMPLE_CONTENT,  {NULL} },
391           {"album",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
392           {"trackNum",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
393           {"duration",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
394           {"link",         SIMPLE_CONTENT,  {NULL} },
395           {"meta",         SIMPLE_CONTENT,  {NULL} },
396           {"extension",    COMPLEX_CONTENT, {.cmplx = skip_element} },
397           {NULL,           UNKNOWN_CONTENT, {NULL} }
398         };
399
400     while( xml_ReaderRead( p_xml_reader ) == 1 )
401     {
402         i_node = xml_ReaderNodeType( p_xml_reader );
403         switch( i_node )
404         {
405             case XML_READER_NONE:
406                 break;
407
408             case XML_READER_STARTELEM:
409                 /*  element start tag  */
410                 psz_name = xml_ReaderName( p_xml_reader );
411                 if( !psz_name || !*psz_name )
412                 {
413                     msg_Err( p_demux, "invalid xml stream" );
414                     FREE_ATT();
415                     return VLC_FALSE;
416                 }
417                 /* choose handler */
418                 for( p_handler = track_elements;
419                      p_handler->name && strcmp( psz_name, p_handler->name );
420                      p_handler++ );
421                 if( !p_handler->name )
422                 {
423                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
424                     FREE_ATT();
425                     return VLC_FALSE;
426                 }
427                 FREE_NAME();
428                 /* complex content is parsed in a separate function */
429                 if( p_handler->type == COMPLEX_CONTENT )
430                 {
431                     if( !p_new_input )
432                     {
433                         msg_Err( p_demux,
434                                  "at <%s> level no new item has been allocated",
435                                  p_handler->name );
436                         FREE_ATT();
437                         return VLC_FALSE;
438                     }
439                     if( p_handler->pf_handler.cmplx( p_demux,
440                                                      p_playlist,
441                                                      NULL, p_new_input,
442                                                      p_xml_reader,
443                                                      p_handler->name ) )
444                     {
445                         p_handler = NULL;
446                         FREE_ATT();
447                     }
448                     else
449                     {
450                         FREE_ATT();
451                         return VLC_FALSE;
452                     }
453                 }
454                 break;
455
456             case XML_READER_TEXT:
457                 /* simple element content */
458                 FREE_ATT();
459                 psz_value = xml_ReaderValue( p_xml_reader );
460                 if( !psz_value )
461                 {
462                     msg_Err( p_demux, "invalid xml stream" );
463                     FREE_ATT();
464                     return VLC_FALSE;
465                 }
466                 break;
467
468             case XML_READER_ENDELEM:
469                 /* element end tag */
470                 psz_name = xml_ReaderName( p_xml_reader );
471                 if( !psz_name )
472                 {
473                     msg_Err( p_demux, "invalid xml stream" );
474                     FREE_ATT();
475                     return VLC_FALSE;
476                 }
477                 /* leave if the current parent node <track> is terminated */
478                 if( !strcmp( psz_name, psz_element ) )
479                 {
480                     FREE_ATT();
481                     playlist_BothAddInput( p_playlist, p_new_input,
482                                            p_demux->p_sys->p_item_in_category,
483                                            PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
484                                            PLAYLIST_END, NULL, NULL );
485                     if( p_demux->p_sys->i_identifier <
486                         p_demux->p_sys->i_tracklist_entries )
487                     {
488                         p_demux->p_sys->pp_tracklist[
489                             p_demux->p_sys->i_identifier ] = p_new_input;
490                     }
491                     else
492                     {
493                         if( p_demux->p_sys->i_identifier >
494                             p_demux->p_sys->i_tracklist_entries )
495                         {
496                             p_demux->p_sys->i_tracklist_entries =
497                                 p_demux->p_sys->i_identifier;
498                         }
499                         INSERT_ELEM( p_demux->p_sys->pp_tracklist,
500                                      p_demux->p_sys->i_tracklist_entries,
501                                      p_demux->p_sys->i_tracklist_entries,
502                                      p_new_input );
503                     }
504                     return VLC_TRUE;
505                 }
506                 /* there MUST have been a start tag for that element name */
507                 if( !p_handler || !p_handler->name
508                     || strcmp( p_handler->name, psz_name ))
509                 {
510                     msg_Err( p_demux, "there's no open element left for <%s>",
511                              psz_name );
512                     FREE_ATT();
513                     return VLC_FALSE;
514                 }
515
516                 /* special case: location */
517                 if( !strcmp( p_handler->name, "location" ) )
518                 {
519                     char *psz_uri=NULL;
520                     /* there MUST NOT be an item */
521                     if( p_new_input )
522                     {
523                         msg_Err( p_demux, "item <%s> already created",
524                                  psz_name );
525                         FREE_ATT();
526                         return VLC_FALSE;
527                     }
528                     psz_uri = decode_URI_duplicate( psz_value );
529
530                     if( psz_uri )
531                     {
532                         if( p_demux->p_sys->psz_base && 
533                             !strstr( psz_uri, "://" ) )
534                         {
535                            char* psz_tmp = malloc( 
536                                    strlen(p_demux->p_sys->psz_base) + 
537                                    strlen(psz_uri) +1 );
538                            if( !psz_tmp )
539                            {
540                                msg_Err( p_demux, "out of memory");
541                                return VLC_FALSE;
542                            }
543                            sprintf( psz_tmp, "%s%s", 
544                                     p_demux->p_sys->psz_base, psz_uri );
545                            free( psz_uri );
546                            psz_uri = psz_tmp;
547                         }
548                         p_new_input = input_ItemNewExt( p_playlist, psz_uri,
549                                                         NULL, 0, NULL, -1 );
550                         p_new_input->p_meta = vlc_meta_New();
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         vlc_meta_SetArtist( p_input->p_meta, psz_value );
623     }
624     else if( !strcmp( psz_name, "album" ) )
625     {
626         vlc_meta_SetAlbum( p_input->p_meta, psz_value );
627
628     }
629     else if( !strcmp( psz_name, "trackNum" ) )
630     {
631         vlc_meta_SetTracknum( p_input->p_meta, 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     return VLC_TRUE;
639 }
640
641
642 /**
643  * \brief parse the extension node of a XSPF playlist
644  */
645 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
646 {
647     char *psz_name = NULL;
648     char *psz_value = NULL;
649     char *psz_title = NULL;
650     int i_node;
651     xml_elem_hnd_t *p_handler = NULL;
652
653     xml_elem_hnd_t pl_elements[] =
654         { {"node",  COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
655           {"item",  COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
656           {NULL,    UNKNOWN_CONTENT, {NULL} }
657         };
658
659     /* read all extension node attributes */
660     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
661     {
662         psz_name = xml_ReaderName( p_xml_reader );
663         psz_value = xml_ReaderValue( p_xml_reader );
664         if( !psz_name || !psz_value )
665         {
666             msg_Err( p_demux, "invalid xml stream @ <node>" );
667             FREE_ATT();
668             return VLC_FALSE;
669         }
670         /* attribute: title */
671         if( !strcmp( psz_name, "title" ) )
672         {
673             psz_title = unescape_URI_duplicate( psz_value );
674         }
675         /* unknown attribute */
676         else
677             msg_Warn( p_demux, "invalid <node> attribute:\"%s\"", psz_name);
678
679         FREE_ATT();
680     }
681
682     /* attribute title is mandatory except for <extension> */
683     if( !strcmp( psz_element, "node" ) && !psz_title )
684     {
685         msg_Warn( p_demux, "<node> requires \"title\" attribute" );
686         return VLC_FALSE;
687     }
688     if( psz_title ) free( psz_title );
689
690     /* parse the child elements */
691     while( xml_ReaderRead( p_xml_reader ) == 1 )
692     {
693         i_node = xml_ReaderNodeType( p_xml_reader );
694         switch( i_node )
695         {
696             case XML_READER_NONE:
697                 break;
698             case XML_READER_STARTELEM:
699                 /*  element start tag  */
700                 psz_name = xml_ReaderName( p_xml_reader );
701                 if( !psz_name || !*psz_name )
702                 {
703                     msg_Err( p_demux, "invalid xml stream" );
704                     FREE_ATT();
705                     return VLC_FALSE;
706                 }
707                 /* choose handler */
708                 for( p_handler = pl_elements;
709                      p_handler->name && strcmp( psz_name, p_handler->name );
710                      p_handler++ );
711                 if( !p_handler->name )
712                 {
713                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
714                     FREE_ATT();
715                     return VLC_FALSE;
716                 }
717                 FREE_NAME();
718                 /* complex content is parsed in a separate function */
719                 if( p_handler->type == COMPLEX_CONTENT )
720                 {
721                     if( p_handler->pf_handler.cmplx( p_demux,
722                                                      p_playlist,
723                                                      p_item, NULL,
724                                                      p_xml_reader,
725                                                      p_handler->name ) )
726                     {
727                         p_handler = NULL;
728                         FREE_ATT();
729                     }
730                     else
731                     {
732                         FREE_ATT();
733                         return VLC_FALSE;
734                     }
735                 }
736                 break;
737
738             case XML_READER_TEXT:
739                 /* simple element content */
740                 FREE_ATT();
741                 psz_value = xml_ReaderValue( p_xml_reader );
742                 if( !psz_value )
743                 {
744                     msg_Err( p_demux, "invalid xml stream" );
745                     FREE_ATT();
746                     return VLC_FALSE;
747                 }
748                 break;
749
750             case XML_READER_ENDELEM:
751                 /* element end tag */
752                 psz_name = xml_ReaderName( p_xml_reader );
753                 if( !psz_name )
754                 {
755                     msg_Err( p_demux, "invalid xml stream" );
756                     FREE_ATT();
757                     return VLC_FALSE;
758                 }
759                 /* leave if the current parent node is terminated */
760                 if( !strcmp( psz_name, psz_element ) )
761                 {
762                     FREE_ATT();
763                     return VLC_TRUE;
764                 }
765                 /* there MUST have been a start tag for that element name */
766                 if( !p_handler || !p_handler->name
767                     || strcmp( p_handler->name, psz_name ))
768                 {
769                     msg_Err( p_demux, "there's no open element left for <%s>",
770                              psz_name );
771                     FREE_ATT();
772                     return VLC_FALSE;
773                 }
774
775                 if( p_handler->pf_handler.smpl )
776                 {
777                     p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
778                                                 psz_value );
779                 }
780                 FREE_ATT();
781                 p_handler = NULL;
782                 break;
783
784             default:
785                 /* unknown/unexpected xml node */
786                 msg_Err( p_demux, "unexpected xml node %i", i_node );
787                 FREE_ATT();
788                 return VLC_FALSE;
789         }
790         FREE_NAME();
791     }
792     return VLC_FALSE;
793 }
794
795 /**
796  * \brief parse the extension item node of a XSPF playlist
797  */
798 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
799 {
800     char *psz_name = NULL;
801     char *psz_value = NULL;
802     int i_href = -1;
803
804     /* read all extension item attributes */
805     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
806     {
807         psz_name = xml_ReaderName( p_xml_reader );
808         psz_value = xml_ReaderValue( p_xml_reader );
809         if( !psz_name || !psz_value )
810         {
811             msg_Err( p_demux, "invalid xml stream @ <item>" );
812             FREE_ATT();
813             return VLC_FALSE;
814         }
815         /* attribute: href */
816         if( !strcmp( psz_name, "href" ) )
817         {
818             i_href = atoi( psz_value );
819         }
820         /* unknown attribute */
821         else
822             msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
823
824         FREE_ATT();
825     }
826
827     /* attribute href is mandatory */
828     if( i_href < 0 )
829     {
830         msg_Warn( p_demux, "<item> requires \"href\" attribute" );
831         return VLC_FALSE;
832     }
833
834     if( i_href > p_demux->p_sys->i_tracklist_entries )
835     {
836         msg_Warn( p_demux, "invalid \"href\" attribute" );
837         return VLC_FALSE;
838     }
839     return VLC_TRUE;
840 }
841
842 /**
843  * \brief skips complex element content that we can't manage
844  */
845 static vlc_bool_t skip_element COMPLEX_INTERFACE
846 {
847     char *psz_endname;
848
849     while( xml_ReaderRead( p_xml_reader ) == 1 )
850     {
851         if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
852         {
853             psz_endname = xml_ReaderName( p_xml_reader );
854             if( !psz_endname )
855                 return VLC_FALSE;
856             if( !strcmp( psz_element, psz_endname ) )
857             {
858                 free( psz_endname );
859                 return VLC_TRUE;
860             }
861             else
862                 free( psz_endname );
863         }
864     }
865     return VLC_FALSE;
866 }