131e6cfb4414c369b8b0b61de522bc6d61fe4241
[vlc.git] / modules / demux / playlist / xspf.c
1 /*******************************************************************************
2  * xspf.c : XSPF playlist import functions
3  *******************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Daniel Stränger <vlc at schmaller dot de>
8  *          Yoann Peronneau <yoann@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  ******************************************************************************/
24 /**
25  * \file modules/demux/playlist/xspf.c
26  * \brief XSPF playlist import functions
27  */
28
29 #include <vlc/vlc.h>
30 #include <vlc_demux.h>
31
32 #include "playlist.h"
33 #include "vlc_xml.h"
34 #include "vlc_strings.h"
35 #include "vlc_url.h"
36 #include "xspf.h"
37
38 struct demux_sys_t
39 {
40     playlist_item_t *p_item_in_category;
41     input_item_t **pp_tracklist;
42     int i_tracklist_entries;
43     int i_identifier;
44     char * psz_base;
45 };
46
47 static int Control( demux_t *, int, va_list );
48 static int Demux( demux_t * );
49
50 /**
51  * \brief XSPF submodule initialization function
52  */
53 int E_(Import_xspf)( vlc_object_t *p_this )
54 {
55     DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
56                                       "using XSPF playlist reader" );
57     return VLC_SUCCESS;
58 }
59
60 void E_(Close_xspf)( vlc_object_t *p_this )
61 {
62     demux_t *p_demux = (demux_t *)p_this;
63     FREENULL( p_demux->p_sys->pp_tracklist );
64     FREENULL( p_demux->p_sys->psz_base );
65     free( p_demux->p_sys );
66 }
67
68 /**
69  * \brief demuxer function for XSPF parsing
70  */
71 int Demux( demux_t *p_demux )
72 {
73     int i_ret = 1;
74     xml_t *p_xml = NULL;
75     xml_reader_t *p_xml_reader = NULL;
76     char *psz_name = NULL;
77     INIT_PLAYLIST_STUFF;
78     p_demux->p_sys->pp_tracklist = NULL;
79     p_demux->p_sys->i_tracklist_entries = 0;
80     p_demux->p_sys->i_identifier = 0;
81     p_demux->p_sys->psz_base = NULL;
82
83     /* create new xml parser from stream */
84     p_xml = xml_Create( p_demux );
85     if( !p_xml )
86         i_ret = -1;
87     else
88     {
89         p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
90         if( !p_xml_reader )
91             i_ret = -1;
92     }
93
94     /* locating the root node */
95     if( i_ret == 1 )
96     {
97         do
98         {
99             if( xml_ReaderRead( p_xml_reader ) != 1 )
100             {
101                 msg_Err( p_demux, "can't read xml stream" );
102                 i_ret = -1;
103             }
104         } while( i_ret == VLC_SUCCESS &&
105                  xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
106     }
107     /* checking root node name */
108     if( i_ret == 1 )
109     {
110         psz_name = xml_ReaderName( p_xml_reader );
111         if( !psz_name || strcmp( psz_name, "playlist" ) )
112         {
113             msg_Err( p_demux, "invalid root node name: %s", psz_name );
114             i_ret = -1;
115         }
116         FREE_NAME();
117     }
118
119     if( i_ret == 1 )
120         i_ret = parse_playlist_node( p_demux, p_playlist, p_current_input,
121                                      p_xml_reader, "playlist" ) ? 0 : -1;
122
123     int i;
124     for( i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
125     {
126         input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
127         if( p_new_input )
128         {
129             input_ItemAddSubItem( p_current_input, p_new_input, VLC_FALSE );
130         }
131         vlc_gc_decref( p_new_input );
132     }
133
134     HANDLE_PLAY_AND_RELEASE;
135     if( p_xml_reader )
136         xml_ReaderDelete( p_xml, p_xml_reader );
137     if( p_xml )
138         xml_Delete( p_xml );
139     return i_ret; /* Needed for correct operation of go back */
140 }
141
142 /** \brief dummy function for demux callback interface */
143 static int Control( demux_t *p_demux, int i_query, va_list args )
144 {
145     return VLC_EGENERIC;
146 }
147
148 /**
149  * \brief parse the root node of a XSPF playlist
150  * \param p_demux demuxer instance
151  * \param p_playlist playlist instance
152  * \param p_input_item current input item
153  * \param p_xml_reader xml reader instance
154  * \param psz_element name of element to parse
155  */
156 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
157 {
158     char *psz_name=NULL;
159     char *psz_value=NULL;
160     vlc_bool_t b_version_found = VLC_FALSE;
161     int i_node;
162     xml_elem_hnd_t *p_handler=NULL;
163
164     xml_elem_hnd_t pl_elements[] =
165         { {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
166           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
167           {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
168           {"info",         SIMPLE_CONTENT,  {NULL} },
169           {"location",     SIMPLE_CONTENT,  {NULL} },
170           {"identifier",   SIMPLE_CONTENT,  {NULL} },
171           {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
172           {"date",         SIMPLE_CONTENT,  {NULL} },
173           {"license",      SIMPLE_CONTENT,  {NULL} },
174           {"attribution",  COMPLEX_CONTENT, {.cmplx = skip_element} },
175           {"link",         SIMPLE_CONTENT,  {NULL} },
176           {"meta",         SIMPLE_CONTENT,  {NULL} },
177           {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
178           {"trackList",    COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
179           {NULL,           UNKNOWN_CONTENT, {NULL} }
180         };
181
182     /* read all playlist attributes */
183     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
184     {
185         psz_name = xml_ReaderName( p_xml_reader );
186         psz_value = xml_ReaderValue( p_xml_reader );
187         if( !psz_name || !psz_value )
188         {
189             msg_Err( p_demux, "invalid xml stream @ <playlist>" );
190             FREE_ATT();
191             return VLC_FALSE;
192         }
193         /* attribute: version */
194         if( !strcmp( psz_name, "version" ) )
195         {
196             b_version_found = VLC_TRUE;
197             if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
198                 msg_Warn( p_demux, "unsupported XSPF version" );
199         }
200         /* attribute: xmlns */
201         else if( !strcmp( psz_name, "xmlns" ) )
202             ;
203         else if( !strcmp( psz_name, "xml:base" ) )
204         {
205             p_demux->p_sys->psz_base = decode_URI_duplicate( psz_value );
206         }
207         /* unknown attribute */
208         else
209             msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
210
211         FREE_ATT();
212     }
213     /* attribute version is mandatory !!! */
214     if( !b_version_found )
215         msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
216
217     /* parse the child elements - we only take care of <trackList> */
218     while( xml_ReaderRead( p_xml_reader ) == 1 )
219     {
220         i_node = xml_ReaderNodeType( p_xml_reader );
221         switch( i_node )
222         {
223             case XML_READER_NONE:
224                 break;
225             case XML_READER_STARTELEM:
226                 /*  element start tag  */
227                 psz_name = xml_ReaderName( p_xml_reader );
228                 if( !psz_name || !*psz_name )
229                 {
230                     msg_Err( p_demux, "invalid xml stream" );
231                     FREE_ATT();
232                     return VLC_FALSE;
233                 }
234                 /* choose handler */
235                 for( p_handler = pl_elements;
236                      p_handler->name && strcmp( psz_name, p_handler->name );
237                      p_handler++ );
238                 if( !p_handler->name )
239                 {
240                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
241                     FREE_ATT();
242                     return VLC_FALSE;
243                 }
244                 FREE_NAME();
245                 /* complex content is parsed in a separate function */
246                 if( p_handler->type == COMPLEX_CONTENT )
247                 {
248                     if( p_handler->pf_handler.cmplx( p_demux,
249                                                      p_playlist,
250                                                      p_input_item,
251                                                      p_xml_reader,
252                                                      p_handler->name ) )
253                     {
254                         p_handler = NULL;
255                         FREE_ATT();
256                     }
257                     else
258                     {
259                         FREE_ATT();
260                         return VLC_FALSE;
261                     }
262                 }
263                 break;
264
265             case XML_READER_TEXT:
266                 /* simple element content */
267                 FREE_ATT();
268                 psz_value = xml_ReaderValue( p_xml_reader );
269                 if( !psz_value )
270                 {
271                     msg_Err( p_demux, "invalid xml stream" );
272                     FREE_ATT();
273                     return VLC_FALSE;
274                 }
275                 break;
276
277             case XML_READER_ENDELEM:
278                 /* element end tag */
279                 psz_name = xml_ReaderName( p_xml_reader );
280                 if( !psz_name )
281                 {
282                     msg_Err( p_demux, "invalid xml stream" );
283                     FREE_ATT();
284                     return VLC_FALSE;
285                 }
286                 /* leave if the current parent node <playlist> is terminated */
287                 if( !strcmp( psz_name, psz_element ) )
288                 {
289                     FREE_ATT();
290                     return VLC_TRUE;
291                 }
292                 /* there MUST have been a start tag for that element name */
293                 if( !p_handler || !p_handler->name
294                     || strcmp( p_handler->name, psz_name ))
295                 {
296                     msg_Err( p_demux, "there's no open element left for <%s>",
297                              psz_name );
298                     FREE_ATT();
299                     return VLC_FALSE;
300                 }
301
302                 if( p_handler->pf_handler.smpl )
303                 {
304                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
305                                                 psz_value );
306                 }
307                 FREE_ATT();
308                 p_handler = NULL;
309                 break;
310
311             default:
312                 /* unknown/unexpected xml node */
313                 msg_Err( p_demux, "unexpected xml node %i", i_node );
314                 FREE_ATT();
315                 return VLC_FALSE;
316         }
317         FREE_NAME();
318     }
319     return VLC_FALSE;
320 }
321
322 /**
323  * \brief parses the tracklist node which only may contain <track>s
324  */
325 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
326 {
327     char *psz_name=NULL;
328     int i_node;
329     int i_ntracks = 0;
330
331     /* now parse the <track>s */
332     while( xml_ReaderRead( p_xml_reader ) == 1 )
333     {
334         i_node = xml_ReaderNodeType( p_xml_reader );
335         if( i_node == XML_READER_STARTELEM )
336         {
337             psz_name = xml_ReaderName( p_xml_reader );
338             if( !psz_name )
339             {
340                 msg_Err( p_demux, "unexpected end of xml data" );
341                 FREE_NAME();
342                 return VLC_FALSE;
343             }
344             if( strcmp( psz_name, "track") )
345             {
346                 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
347                          psz_name );
348                 FREE_NAME();
349                 return VLC_FALSE;
350             }
351             FREE_NAME();
352
353             /* parse the track data in a separate function */
354             if( parse_track_node( p_demux, p_playlist, p_input_item,
355                                    p_xml_reader,"track" ) == VLC_TRUE )
356                 i_ntracks++;
357         }
358         else if( i_node == XML_READER_ENDELEM )
359             break;
360     }
361
362     /* the <trackList> has to be terminated */
363     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
364     {
365         msg_Err( p_demux, "there's a missing </trackList>" );
366         FREE_NAME();
367         return VLC_FALSE;
368     }
369     psz_name = xml_ReaderName( p_xml_reader );
370     if( !psz_name || strcmp( psz_name, "trackList" ) )
371     {
372         msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
373         FREE_NAME();
374         return VLC_FALSE;
375     }
376     FREE_NAME();
377
378     msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
379
380     return VLC_TRUE;
381 }
382
383 /**
384  * \brief parse one track element
385  * \param COMPLEX_INTERFACE
386  */
387 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
388 {
389     input_item_t *p_new_input = NULL;
390     int i_node;
391     char *psz_name=NULL;
392     char *psz_value=NULL;
393     xml_elem_hnd_t *p_handler=NULL;
394
395     xml_elem_hnd_t track_elements[] =
396         { {"location",     SIMPLE_CONTENT,  {NULL} },
397           {"identifier",   SIMPLE_CONTENT,  {NULL} },
398           {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
399           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
400           {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
401           {"info",         SIMPLE_CONTENT,  {NULL} },
402           {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
403           {"album",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
404           {"trackNum",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
405           {"duration",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
406           {"link",         SIMPLE_CONTENT,  {NULL} },
407           {"meta",         SIMPLE_CONTENT,  {NULL} },
408           {"extension",    COMPLEX_CONTENT, {.cmplx = skip_element} },
409           {NULL,           UNKNOWN_CONTENT, {NULL} }
410         };
411
412     while( xml_ReaderRead( p_xml_reader ) == 1 )
413     {
414         i_node = xml_ReaderNodeType( p_xml_reader );
415         switch( i_node )
416         {
417             case XML_READER_NONE:
418                 break;
419
420             case XML_READER_STARTELEM:
421                 /*  element start tag  */
422                 psz_name = xml_ReaderName( p_xml_reader );
423                 if( !psz_name || !*psz_name )
424                 {
425                     msg_Err( p_demux, "invalid xml stream" );
426                     FREE_ATT();
427                     return VLC_FALSE;
428                 }
429                 /* choose handler */
430                 for( p_handler = track_elements;
431                      p_handler->name && strcmp( psz_name, p_handler->name );
432                      p_handler++ );
433                 if( !p_handler->name )
434                 {
435                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
436                     FREE_ATT();
437                     return VLC_FALSE;
438                 }
439                 FREE_NAME();
440                 /* complex content is parsed in a separate function */
441                 if( p_handler->type == COMPLEX_CONTENT )
442                 {
443                     if( !p_new_input )
444                     {
445                         msg_Err( p_demux,
446                                  "at <%s> level no new item has been allocated",
447                                  p_handler->name );
448                         FREE_ATT();
449                         return VLC_FALSE;
450                     }
451                     if( p_handler->pf_handler.cmplx( p_demux,
452                                                      p_playlist,
453                                                      p_new_input,
454                                                      p_xml_reader,
455                                                      p_handler->name ) )
456                     {
457                         p_handler = NULL;
458                         FREE_ATT();
459                     }
460                     else
461                     {
462                         FREE_ATT();
463                         return VLC_FALSE;
464                     }
465                 }
466                 break;
467
468             case XML_READER_TEXT:
469                 /* simple element content */
470                 FREE_ATT();
471                 psz_value = xml_ReaderValue( p_xml_reader );
472                 if( !psz_value )
473                 {
474                     msg_Err( p_demux, "invalid xml stream" );
475                     FREE_ATT();
476                     return VLC_FALSE;
477                 }
478                 break;
479
480             case XML_READER_ENDELEM:
481                 /* element end tag */
482                 psz_name = xml_ReaderName( p_xml_reader );
483                 if( !psz_name )
484                 {
485                     msg_Err( p_demux, "invalid xml stream" );
486                     FREE_ATT();
487                     return VLC_FALSE;
488                 }
489                 /* leave if the current parent node <track> is terminated */
490                 if( !strcmp( psz_name, psz_element ) )
491                 {
492                     FREE_ATT();
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 = decode_URI_duplicate( psz_value );
537
538                     if( psz_uri )
539                     {
540                         if( p_demux->p_sys->psz_base &&
541                             !strstr( psz_uri, "://" ) )
542                         {
543                            char* psz_tmp = malloc(
544                                    strlen(p_demux->p_sys->psz_base) +
545                                    strlen(psz_uri) +1 );
546                            if( !psz_tmp )
547                            {
548                                msg_Err( p_demux, "out of memory");
549                                return VLC_FALSE;
550                            }
551                            sprintf( psz_tmp, "%s%s",
552                                     p_demux->p_sys->psz_base, psz_uri );
553                            free( psz_uri );
554                            psz_uri = psz_tmp;
555                         }
556                         /* FIXME: We are leaking that one */
557                         p_new_input = input_ItemNewExt( p_playlist, psz_uri,
558                                                         NULL, 0, NULL, -1 );
559                         free( psz_uri );
560                         input_ItemCopyOptions( p_input_item, p_new_input );
561                         psz_uri = NULL;
562                         FREE_ATT();
563                         p_handler = NULL;
564                     }
565                     else
566                     {
567                         FREE_ATT();
568                         return VLC_FALSE;
569                     }
570                 }
571                 else if( !strcmp( p_handler->name, "identifier" ) )
572                 {
573                     p_demux->p_sys->i_identifier = atoi( psz_value );
574                 }
575                 else
576                 {
577                     /* there MUST be an item */
578                     if( !p_new_input )
579                     {
580                         msg_Err( p_demux, "item not yet created at <%s>",
581                                  psz_name );
582                         FREE_ATT();
583                         return VLC_FALSE;
584                     }
585                     if( p_handler->pf_handler.smpl )
586                     {
587                         p_handler->pf_handler.smpl( p_new_input,
588                                                     p_handler->name,
589                                                     psz_value );
590                         FREE_ATT();
591                     }
592                 }
593                 FREE_ATT();
594                 p_handler = NULL;
595                 break;
596
597             default:
598                 /* unknown/unexpected xml node */
599                 msg_Err( p_demux, "unexpected xml node %i", i_node );
600                 FREE_ATT();
601                 return VLC_FALSE;
602         }
603         FREE_NAME();
604     }
605     msg_Err( p_demux, "unexpected end of xml data" );
606     FREE_ATT();
607     return VLC_FALSE;
608 }
609
610 /**
611  * \brief handles the supported <track> sub-elements
612  */
613 static vlc_bool_t set_item_info SIMPLE_INTERFACE
614 {
615     /* exit if setting is impossible */
616     if( !psz_name || !psz_value || !p_input )
617         return VLC_FALSE;
618
619
620     /* re-convert xml special characters inside psz_value */
621     resolve_xml_special_chars( psz_value );
622
623     /* handle each info element in a separate "if" clause */
624     if( !strcmp( psz_name, "title" ) )
625     {
626         input_item_SetTitle( p_input, psz_value );
627     }
628     else if( !strcmp( psz_name, "creator" ) )
629     {
630         input_item_SetArtist( p_input, psz_value );
631     }
632     else if( !strcmp( psz_name, "album" ) )
633     {
634         input_item_SetAlbum( p_input, psz_value );
635
636     }
637     else if( !strcmp( psz_name, "trackNum" ) )
638     {
639         input_item_SetTrackNum( p_input, psz_value );
640     }
641     else if( !strcmp( psz_name, "duration" ) )
642     {
643         long i_num = atol( psz_value );
644         input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
645     }
646     else if( !strcmp( psz_name, "annotation" ) )
647     {
648         input_item_SetDescription( p_input, psz_value );
649     }
650     else if( !strcmp( psz_name, "image" ) )
651     {
652         char *psz_uri = decode_URI_duplicate( psz_value );
653         input_item_SetArtURL( p_input, psz_uri );
654         free( psz_uri );
655     }
656     return VLC_TRUE;
657 }
658
659
660 /**
661  * \brief parse the extension node of a XSPF playlist
662  */
663 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
664 {
665     char *psz_name = NULL;
666     char *psz_value = NULL;
667     char *psz_title = NULL;
668     char *psz_application = NULL;
669     int i_node;
670     xml_elem_hnd_t *p_handler = NULL;
671     input_item_t *p_new_input = NULL;
672
673     xml_elem_hnd_t pl_elements[] =
674         { {"node",  COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
675           {"item",  COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
676           {NULL,    UNKNOWN_CONTENT, {NULL} }
677         };
678
679     /* read all extension node attributes */
680     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
681     {
682         psz_name = xml_ReaderName( p_xml_reader );
683         psz_value = xml_ReaderValue( p_xml_reader );
684         if( !psz_name || !psz_value )
685         {
686             msg_Err( p_demux, "invalid xml stream @ <node>" );
687             FREE_ATT();
688             return VLC_FALSE;
689         }
690         /* attribute: title */
691         if( !strcmp( psz_name, "title" ) )
692         {
693             resolve_xml_special_chars( psz_value );
694             psz_title = strdup( psz_value );
695         }
696         /* extension attribute: application */
697         else if( !strcmp( psz_name, "application" ) )
698         {
699             psz_application = strdup( psz_value );
700         }
701         /* unknown attribute */
702         else
703             msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element, psz_name );
704
705         FREE_ATT();
706     }
707
708     /* attribute title is mandatory except for <extension> */
709     if( !strcmp( psz_element, "node" ) )
710     {
711         if( !psz_title )
712         {
713             msg_Warn( p_demux, "<node> requires \"title\" attribute" );
714             return VLC_FALSE;
715         }
716         p_new_input = input_ItemNewWithType( VLC_OBJECT(p_playlist), "vlc:nop",
717                                 psz_title, 0, NULL, -1, ITEM_TYPE_DIRECTORY );
718         if( p_new_input )
719         {
720             input_ItemAddSubItem( p_input_item, p_new_input, VLC_TRUE );
721             p_input_item = p_new_input;
722         }
723         free( psz_title );
724     }
725     else if( !strcmp( psz_element, "extension" ) )
726     {
727         if( !psz_application )
728         {
729             msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
730             return VLC_FALSE;
731         }
732         else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
733         {
734             msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
735             free( psz_application );
736             return VLC_FALSE;
737         }
738     }
739     free( psz_application );
740
741     /* parse the child elements */
742     while( xml_ReaderRead( p_xml_reader ) == 1 )
743     {
744         i_node = xml_ReaderNodeType( p_xml_reader );
745         switch( i_node )
746         {
747             case XML_READER_NONE:
748                 break;
749             case XML_READER_STARTELEM:
750                 /*  element start tag  */
751                 psz_name = xml_ReaderName( p_xml_reader );
752                 if( !psz_name || !*psz_name )
753                 {
754                     msg_Err( p_demux, "invalid xml stream" );
755                     FREE_ATT();
756                     return VLC_FALSE;
757                 }
758                 /* choose handler */
759                 for( p_handler = pl_elements;
760                      p_handler->name && strcmp( psz_name, p_handler->name );
761                      p_handler++ );
762                 if( !p_handler->name )
763                 {
764                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
765                     FREE_ATT();
766                     return VLC_FALSE;
767                 }
768                 FREE_NAME();
769                 /* complex content is parsed in a separate function */
770                 if( p_handler->type == COMPLEX_CONTENT )
771                 {
772                     if( p_handler->pf_handler.cmplx( p_demux,
773                                                      p_playlist,
774                                                      p_input_item,
775                                                      p_xml_reader,
776                                                      p_handler->name ) )
777                     {
778                         p_handler = NULL;
779                         FREE_ATT();
780                     }
781                     else
782                     {
783                         FREE_ATT();
784                         return VLC_FALSE;
785                     }
786                 }
787                 break;
788
789             case XML_READER_TEXT:
790                 /* simple element content */
791                 FREE_ATT();
792                 psz_value = xml_ReaderValue( p_xml_reader );
793                 if( !psz_value )
794                 {
795                     msg_Err( p_demux, "invalid xml stream" );
796                     FREE_ATT();
797                     return VLC_FALSE;
798                 }
799                 break;
800
801             case XML_READER_ENDELEM:
802                 /* element end tag */
803                 psz_name = xml_ReaderName( p_xml_reader );
804                 if( !psz_name )
805                 {
806                     msg_Err( p_demux, "invalid xml stream" );
807                     FREE_ATT();
808                     return VLC_FALSE;
809                 }
810                 /* leave if the current parent node is terminated */
811                 if( !strcmp( psz_name, psz_element ) )
812                 {
813                     FREE_ATT();
814                     return VLC_TRUE;
815                 }
816                 /* there MUST have been a start tag for that element name */
817                 if( !p_handler || !p_handler->name
818                     || strcmp( p_handler->name, psz_name ))
819                 {
820                     msg_Err( p_demux, "there's no open element left for <%s>",
821                              psz_name );
822                     FREE_ATT();
823                     return VLC_FALSE;
824                 }
825
826                 if( p_handler->pf_handler.smpl )
827                 {
828                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
829                                                 psz_value );
830                 }
831                 FREE_ATT();
832                 p_handler = NULL;
833                 break;
834
835             default:
836                 /* unknown/unexpected xml node */
837                 msg_Err( p_demux, "unexpected xml node %i", i_node );
838                 FREE_ATT();
839                 return VLC_FALSE;
840         }
841         FREE_NAME();
842     }
843     return VLC_FALSE;
844 }
845
846 /**
847  * \brief parse the extension item node of a XSPF playlist
848  */
849 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
850 {
851     input_item_t *p_new_input = NULL;
852     char *psz_name = NULL;
853     char *psz_value = NULL;
854     int i_href = -1;
855
856     /* read all extension item attributes */
857     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
858     {
859         psz_name = xml_ReaderName( p_xml_reader );
860         psz_value = xml_ReaderValue( p_xml_reader );
861         if( !psz_name || !psz_value )
862         {
863             msg_Err( p_demux, "invalid xml stream @ <item>" );
864             FREE_ATT();
865             return VLC_FALSE;
866         }
867         /* attribute: href */
868         if( !strcmp( psz_name, "href" ) )
869         {
870             i_href = atoi( psz_value );
871         }
872         /* unknown attribute */
873         else
874             msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
875
876         FREE_ATT();
877     }
878
879     /* attribute href is mandatory */
880     if( i_href < 0 )
881     {
882         msg_Warn( p_demux, "<item> requires \"href\" attribute" );
883         return VLC_FALSE;
884     }
885
886     if( i_href >= p_demux->p_sys->i_tracklist_entries )
887     {
888         msg_Warn( p_demux, "invalid \"href\" attribute" );
889         return VLC_FALSE;
890     }
891
892     p_new_input = p_demux->p_sys->pp_tracklist[ i_href ];
893     if( p_new_input )
894     {
895         input_ItemAddSubItem( p_input_item, p_new_input, VLC_FALSE );
896         p_demux->p_sys->pp_tracklist[i_href] = NULL;
897         vlc_gc_decref( p_new_input );
898     }
899
900     /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
901     /* (libxml sends NONE) */
902     xml_ReaderRead( p_xml_reader );
903
904     return VLC_TRUE;
905 }
906
907 /**
908  * \brief skips complex element content that we can't manage
909  */
910 static vlc_bool_t skip_element COMPLEX_INTERFACE
911 {
912     char *psz_endname;
913
914     while( xml_ReaderRead( p_xml_reader ) == 1 )
915     {
916         if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
917         {
918             psz_endname = xml_ReaderName( p_xml_reader );
919             if( !psz_endname )
920                 return VLC_FALSE;
921             if( !strcmp( psz_element, psz_endname ) )
922             {
923                 free( psz_endname );
924                 return VLC_TRUE;
925             }
926             else
927                 free( psz_endname );
928         }
929     }
930     return VLC_FALSE;
931 }