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