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