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