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