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