101bd729b25fff3d9363d5e547405639bc68f602
[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                               strrchr( p_demux->psz_path, DIR_SEP_CHAR ) ) &&
542                             !strstr( psz_uri, "://" ) )
543                         {
544                            char *psz_baseref = p_demux->p_sys->psz_base;
545                            size_t i_baselen;
546                            if( psz_baseref )
547                                i_baselen = strlen( psz_baseref );
548                            else
549                            {
550                                psz_baseref = p_demux->psz_path;
551                                i_baselen = strrchr( psz_baseref, DIR_SEP_CHAR )
552                                             - psz_baseref + 1;
553                            }
554                            char* psz_tmp = malloc( i_baselen +
555                                    strlen( psz_uri ) + 1 );
556                            if( !psz_tmp )
557                            {
558                                msg_Err( p_demux, "out of memory");
559                                return VLC_FALSE;
560                            }
561                            strncpy( psz_tmp, psz_baseref, i_baselen );
562                            strcpy( psz_tmp + i_baselen, psz_uri );
563                            free( psz_uri );
564                            psz_uri = psz_tmp;
565                         }
566                         /* FIXME: We are leaking that one */
567                         p_new_input = input_ItemNewExt( p_playlist, psz_uri,
568                                                         NULL, 0, NULL, -1 );
569                         free( psz_uri );
570                         input_ItemCopyOptions( p_input_item, p_new_input );
571                         psz_uri = NULL;
572                         FREE_ATT();
573                         p_handler = NULL;
574                     }
575                     else
576                     {
577                         FREE_ATT();
578                         return VLC_FALSE;
579                     }
580                 }
581                 else if( !strcmp( p_handler->name, "identifier" ) )
582                 {
583                     p_demux->p_sys->i_identifier = atoi( psz_value );
584                 }
585                 else
586                 {
587                     /* there MUST be an item */
588                     if( !p_new_input )
589                     {
590                         msg_Err( p_demux, "item not yet created at <%s>",
591                                  psz_name );
592                         FREE_ATT();
593                         return VLC_FALSE;
594                     }
595                     if( p_handler->pf_handler.smpl )
596                     {
597                         p_handler->pf_handler.smpl( p_new_input,
598                                                     p_handler->name,
599                                                     psz_value );
600                         FREE_ATT();
601                     }
602                 }
603                 FREE_ATT();
604                 p_handler = NULL;
605                 break;
606
607             default:
608                 /* unknown/unexpected xml node */
609                 msg_Err( p_demux, "unexpected xml node %i", i_node );
610                 FREE_ATT();
611                 return VLC_FALSE;
612         }
613         FREE_NAME();
614     }
615     msg_Err( p_demux, "unexpected end of xml data" );
616     FREE_ATT();
617     return VLC_FALSE;
618 }
619
620 /**
621  * \brief handles the supported <track> sub-elements
622  */
623 static vlc_bool_t set_item_info SIMPLE_INTERFACE
624 {
625     /* exit if setting is impossible */
626     if( !psz_name || !psz_value || !p_input )
627         return VLC_FALSE;
628
629
630     /* re-convert xml special characters inside psz_value */
631     resolve_xml_special_chars( psz_value );
632
633     /* handle each info element in a separate "if" clause */
634     if( !strcmp( psz_name, "title" ) )
635     {
636         input_item_SetTitle( p_input, psz_value );
637     }
638     else if( !strcmp( psz_name, "creator" ) )
639     {
640         input_item_SetArtist( p_input, psz_value );
641     }
642     else if( !strcmp( psz_name, "album" ) )
643     {
644         input_item_SetAlbum( p_input, psz_value );
645
646     }
647     else if( !strcmp( psz_name, "trackNum" ) )
648     {
649         input_item_SetTrackNum( p_input, psz_value );
650     }
651     else if( !strcmp( psz_name, "duration" ) )
652     {
653         long i_num = atol( psz_value );
654         input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
655     }
656     else if( !strcmp( psz_name, "annotation" ) )
657     {
658         input_item_SetDescription( p_input, psz_value );
659     }
660     else if( !strcmp( psz_name, "image" ) )
661     {
662         char *psz_uri = decode_URI_duplicate( psz_value );
663         input_item_SetArtURL( p_input, psz_uri );
664         free( psz_uri );
665     }
666     return VLC_TRUE;
667 }
668
669
670 /**
671  * \brief parse the extension node of a XSPF playlist
672  */
673 static vlc_bool_t parse_extension_node COMPLEX_INTERFACE
674 {
675     char *psz_name = NULL;
676     char *psz_value = NULL;
677     char *psz_title = NULL;
678     char *psz_application = NULL;
679     int i_node;
680     xml_elem_hnd_t *p_handler = NULL;
681     input_item_t *p_new_input = NULL;
682
683     xml_elem_hnd_t pl_elements[] =
684         { {"node",  COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
685           {"item",  COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
686           {NULL,    UNKNOWN_CONTENT, {NULL} }
687         };
688
689     /* read all extension node attributes */
690     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
691     {
692         psz_name = xml_ReaderName( p_xml_reader );
693         psz_value = xml_ReaderValue( p_xml_reader );
694         if( !psz_name || !psz_value )
695         {
696             msg_Err( p_demux, "invalid xml stream @ <node>" );
697             FREE_ATT();
698             return VLC_FALSE;
699         }
700         /* attribute: title */
701         if( !strcmp( psz_name, "title" ) )
702         {
703             resolve_xml_special_chars( psz_value );
704             psz_title = strdup( psz_value );
705         }
706         /* extension attribute: application */
707         else if( !strcmp( psz_name, "application" ) )
708         {
709             psz_application = strdup( psz_value );
710         }
711         /* unknown attribute */
712         else
713             msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element, psz_name );
714
715         FREE_ATT();
716     }
717
718     /* attribute title is mandatory except for <extension> */
719     if( !strcmp( psz_element, "node" ) )
720     {
721         if( !psz_title )
722         {
723             msg_Warn( p_demux, "<node> requires \"title\" attribute" );
724             return VLC_FALSE;
725         }
726         p_new_input = input_ItemNewWithType( VLC_OBJECT(p_playlist), "vlc:nop",
727                                 psz_title, 0, NULL, -1, ITEM_TYPE_DIRECTORY );
728         if( p_new_input )
729         {
730             input_ItemAddSubItem( p_input_item, p_new_input, VLC_TRUE );
731             p_input_item = p_new_input;
732         }
733         free( psz_title );
734     }
735     else if( !strcmp( psz_element, "extension" ) )
736     {
737         if( !psz_application )
738         {
739             msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
740             return VLC_FALSE;
741         }
742         else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
743         {
744             msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
745             free( psz_application );
746             return VLC_FALSE;
747         }
748     }
749     free( psz_application );
750
751     /* parse the child elements */
752     while( xml_ReaderRead( p_xml_reader ) == 1 )
753     {
754         i_node = xml_ReaderNodeType( p_xml_reader );
755         switch( i_node )
756         {
757             case XML_READER_NONE:
758                 break;
759             case XML_READER_STARTELEM:
760                 /*  element start tag  */
761                 psz_name = xml_ReaderName( p_xml_reader );
762                 if( !psz_name || !*psz_name )
763                 {
764                     msg_Err( p_demux, "invalid xml stream" );
765                     FREE_ATT();
766                     return VLC_FALSE;
767                 }
768                 /* choose handler */
769                 for( p_handler = pl_elements;
770                      p_handler->name && strcmp( psz_name, p_handler->name );
771                      p_handler++ );
772                 if( !p_handler->name )
773                 {
774                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
775                     FREE_ATT();
776                     return VLC_FALSE;
777                 }
778                 FREE_NAME();
779                 /* complex content is parsed in a separate function */
780                 if( p_handler->type == COMPLEX_CONTENT )
781                 {
782                     if( p_handler->pf_handler.cmplx( p_demux,
783                                                      p_playlist,
784                                                      p_input_item,
785                                                      p_xml_reader,
786                                                      p_handler->name ) )
787                     {
788                         p_handler = NULL;
789                         FREE_ATT();
790                     }
791                     else
792                     {
793                         FREE_ATT();
794                         return VLC_FALSE;
795                     }
796                 }
797                 break;
798
799             case XML_READER_TEXT:
800                 /* simple element content */
801                 FREE_ATT();
802                 psz_value = xml_ReaderValue( p_xml_reader );
803                 if( !psz_value )
804                 {
805                     msg_Err( p_demux, "invalid xml stream" );
806                     FREE_ATT();
807                     return VLC_FALSE;
808                 }
809                 break;
810
811             case XML_READER_ENDELEM:
812                 /* element end tag */
813                 psz_name = xml_ReaderName( p_xml_reader );
814                 if( !psz_name )
815                 {
816                     msg_Err( p_demux, "invalid xml stream" );
817                     FREE_ATT();
818                     return VLC_FALSE;
819                 }
820                 /* leave if the current parent node is terminated */
821                 if( !strcmp( psz_name, psz_element ) )
822                 {
823                     FREE_ATT();
824                     return VLC_TRUE;
825                 }
826                 /* there MUST have been a start tag for that element name */
827                 if( !p_handler || !p_handler->name
828                     || strcmp( p_handler->name, psz_name ))
829                 {
830                     msg_Err( p_demux, "there's no open element left for <%s>",
831                              psz_name );
832                     FREE_ATT();
833                     return VLC_FALSE;
834                 }
835
836                 if( p_handler->pf_handler.smpl )
837                 {
838                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
839                                                 psz_value );
840                 }
841                 FREE_ATT();
842                 p_handler = NULL;
843                 break;
844
845             default:
846                 /* unknown/unexpected xml node */
847                 msg_Err( p_demux, "unexpected xml node %i", i_node );
848                 FREE_ATT();
849                 return VLC_FALSE;
850         }
851         FREE_NAME();
852     }
853     return VLC_FALSE;
854 }
855
856 /**
857  * \brief parse the extension item node of a XSPF playlist
858  */
859 static vlc_bool_t parse_extitem_node COMPLEX_INTERFACE
860 {
861     input_item_t *p_new_input = NULL;
862     char *psz_name = NULL;
863     char *psz_value = NULL;
864     int i_href = -1;
865
866     /* read all extension item attributes */
867     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
868     {
869         psz_name = xml_ReaderName( p_xml_reader );
870         psz_value = xml_ReaderValue( p_xml_reader );
871         if( !psz_name || !psz_value )
872         {
873             msg_Err( p_demux, "invalid xml stream @ <item>" );
874             FREE_ATT();
875             return VLC_FALSE;
876         }
877         /* attribute: href */
878         if( !strcmp( psz_name, "href" ) )
879         {
880             i_href = atoi( psz_value );
881         }
882         /* unknown attribute */
883         else
884             msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
885
886         FREE_ATT();
887     }
888
889     /* attribute href is mandatory */
890     if( i_href < 0 )
891     {
892         msg_Warn( p_demux, "<item> requires \"href\" attribute" );
893         return VLC_FALSE;
894     }
895
896     if( i_href >= p_demux->p_sys->i_tracklist_entries )
897     {
898         msg_Warn( p_demux, "invalid \"href\" attribute" );
899         return VLC_FALSE;
900     }
901
902     p_new_input = p_demux->p_sys->pp_tracklist[ i_href ];
903     if( p_new_input )
904     {
905         input_ItemAddSubItem( p_input_item, p_new_input, VLC_FALSE );
906         p_demux->p_sys->pp_tracklist[i_href] = NULL;
907         vlc_gc_decref( p_new_input );
908     }
909
910     /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
911     /* (libxml sends NONE) */
912     xml_ReaderRead( p_xml_reader );
913
914     return VLC_TRUE;
915 }
916
917 /**
918  * \brief skips complex element content that we can't manage
919  */
920 static vlc_bool_t skip_element COMPLEX_INTERFACE
921 {
922     char *psz_endname;
923
924     while( xml_ReaderRead( p_xml_reader ) == 1 )
925     {
926         if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
927         {
928             psz_endname = xml_ReaderName( p_xml_reader );
929             if( !psz_endname )
930                 return VLC_FALSE;
931             if( !strcmp( psz_element, psz_endname ) )
932             {
933                 free( psz_endname );
934                 return VLC_TRUE;
935             }
936             else
937                 free( psz_endname );
938         }
939     }
940     return VLC_FALSE;
941 }