]> git.sesse.net Git - vlc/blob - modules/demux/playlist/xspf.c
Merge branch 'master' of git@git.videolan.org:vlc
[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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_demux.h>
35
36 #include "playlist.h"
37 #include "vlc_xml.h"
38 #include "vlc_strings.h"
39 #include "vlc_url.h"
40 #include "xspf.h"
41
42 struct demux_sys_t
43 {
44     playlist_item_t *p_item_in_category;
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 E_(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 E_(Close_xspf)( vlc_object_t *p_this )
65 {
66     demux_t *p_demux = (demux_t *)p_this;
67     FREENULL( p_demux->p_sys->pp_tracklist );
68     FREENULL( p_demux->p_sys->psz_base );
69     free( p_demux->p_sys );
70 }
71
72 /**
73  * \brief demuxer function for XSPF parsing
74  */
75 int Demux( demux_t *p_demux )
76 {
77     int i_ret = 1;
78     xml_t *p_xml = NULL;
79     xml_reader_t *p_xml_reader = NULL;
80     char *psz_name = NULL;
81     INIT_PLAYLIST_STUFF;
82     p_demux->p_sys->pp_tracklist = NULL;
83     p_demux->p_sys->i_tracklist_entries = 0;
84     p_demux->p_sys->i_identifier = 0;
85     p_demux->p_sys->psz_base = NULL;
86
87     /* create new xml parser from stream */
88     p_xml = xml_Create( p_demux );
89     if( !p_xml )
90         i_ret = -1;
91     else
92     {
93         p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
94         if( !p_xml_reader )
95             i_ret = -1;
96     }
97
98     /* locating the root node */
99     if( i_ret == 1 )
100     {
101         do
102         {
103             if( xml_ReaderRead( p_xml_reader ) != 1 )
104             {
105                 msg_Err( p_demux, "can't read xml stream" );
106                 i_ret = -1;
107             }
108         } while( i_ret == VLC_SUCCESS &&
109                  xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
110     }
111     /* checking root node name */
112     if( i_ret == 1 )
113     {
114         psz_name = xml_ReaderName( p_xml_reader );
115         if( !psz_name || strcmp( psz_name, "playlist" ) )
116         {
117             msg_Err( p_demux, "invalid root node name: %s", psz_name );
118             i_ret = -1;
119         }
120         FREE_NAME();
121     }
122
123     if( i_ret == 1 )
124         i_ret = parse_playlist_node( p_demux, p_playlist, p_current_input,
125                                      p_xml_reader, "playlist" ) ? 0 : -1;
126
127     int i;
128     for( i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
129     {
130         input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
131         if( p_new_input )
132         {
133             input_ItemAddSubItem( p_current_input, p_new_input );
134         }
135     }
136
137     HANDLE_PLAY_AND_RELEASE;
138     if( p_xml_reader )
139         xml_ReaderDelete( p_xml, p_xml_reader );
140     if( p_xml )
141         xml_Delete( p_xml );
142     return i_ret; /* Needed for correct operation of go back */
143 }
144
145 /** \brief dummy function for demux callback interface */
146 static int Control( demux_t *p_demux, int i_query, va_list args )
147 {
148     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
149     return VLC_EGENERIC;
150 }
151
152 /**
153  * \brief parse the root node of a XSPF playlist
154  * \param p_demux demuxer instance
155  * \param p_playlist playlist instance
156  * \param p_input_item current input item
157  * \param p_xml_reader xml reader instance
158  * \param psz_element name of element to parse
159  */
160 static bool parse_playlist_node COMPLEX_INTERFACE
161 {
162     char *psz_name=NULL;
163     char *psz_value=NULL;
164     bool b_version_found = false;
165     int i_node;
166     xml_elem_hnd_t *p_handler=NULL;
167
168     xml_elem_hnd_t pl_elements[] =
169         { {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
170           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
171           {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
172           {"info",         SIMPLE_CONTENT,  {NULL} },
173           {"location",     SIMPLE_CONTENT,  {NULL} },
174           {"identifier",   SIMPLE_CONTENT,  {NULL} },
175           {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
176           {"date",         SIMPLE_CONTENT,  {NULL} },
177           {"license",      SIMPLE_CONTENT,  {NULL} },
178           {"attribution",  COMPLEX_CONTENT, {.cmplx = skip_element} },
179           {"link",         SIMPLE_CONTENT,  {NULL} },
180           {"meta",         SIMPLE_CONTENT,  {NULL} },
181           {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
182           {"trackList",    COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
183           {NULL,           UNKNOWN_CONTENT, {NULL} }
184         };
185
186     /* read all playlist attributes */
187     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
188     {
189         psz_name = xml_ReaderName( p_xml_reader );
190         psz_value = xml_ReaderValue( p_xml_reader );
191         if( !psz_name || !psz_value )
192         {
193             msg_Err( p_demux, "invalid xml stream @ <playlist>" );
194             FREE_ATT();
195             return false;
196         }
197         /* attribute: version */
198         if( !strcmp( psz_name, "version" ) )
199         {
200             b_version_found = true;
201             if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
202                 msg_Warn( p_demux, "unsupported XSPF version" );
203         }
204         /* attribute: xmlns */
205         else if( !strcmp( psz_name, "xmlns" ) )
206             ;
207         else if( !strcmp( psz_name, "xml:base" ) )
208         {
209             p_demux->p_sys->psz_base = decode_URI_duplicate( psz_value );
210         }
211         /* unknown attribute */
212         else
213             msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
214
215         FREE_ATT();
216     }
217     /* attribute version is mandatory !!! */
218     if( !b_version_found )
219         msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
220
221     /* parse the child elements - we only take care of <trackList> */
222     while( xml_ReaderRead( p_xml_reader ) == 1 )
223     {
224         i_node = xml_ReaderNodeType( p_xml_reader );
225         switch( i_node )
226         {
227             case XML_READER_NONE:
228                 break;
229             case XML_READER_STARTELEM:
230                 /*  element start tag  */
231                 psz_name = xml_ReaderName( p_xml_reader );
232                 if( !psz_name || !*psz_name )
233                 {
234                     msg_Err( p_demux, "invalid xml stream" );
235                     FREE_ATT();
236                     return false;
237                 }
238                 /* choose handler */
239                 for( p_handler = pl_elements;
240                      p_handler->name && strcmp( psz_name, p_handler->name );
241                      p_handler++ );
242                 if( !p_handler->name )
243                 {
244                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
245                     FREE_ATT();
246                     return false;
247                 }
248                 FREE_NAME();
249                 /* complex content is parsed in a separate function */
250                 if( p_handler->type == COMPLEX_CONTENT )
251                 {
252                     if( p_handler->pf_handler.cmplx( p_demux,
253                                                      p_playlist,
254                                                      p_input_item,
255                                                      p_xml_reader,
256                                                      p_handler->name ) )
257                     {
258                         p_handler = NULL;
259                         FREE_ATT();
260                     }
261                     else
262                     {
263                         FREE_ATT();
264                         return false;
265                     }
266                 }
267                 break;
268
269             case XML_READER_TEXT:
270                 /* simple element content */
271                 FREE_ATT();
272                 psz_value = xml_ReaderValue( p_xml_reader );
273                 if( !psz_value )
274                 {
275                     msg_Err( p_demux, "invalid xml stream" );
276                     FREE_ATT();
277                     return false;
278                 }
279                 break;
280
281             case XML_READER_ENDELEM:
282                 /* element end tag */
283                 psz_name = xml_ReaderName( p_xml_reader );
284                 if( !psz_name )
285                 {
286                     msg_Err( p_demux, "invalid xml stream" );
287                     FREE_ATT();
288                     return false;
289                 }
290                 /* leave if the current parent node <playlist> is terminated */
291                 if( !strcmp( psz_name, psz_element ) )
292                 {
293                     FREE_ATT();
294                     return true;
295                 }
296                 /* there MUST have been a start tag for that element name */
297                 if( !p_handler || !p_handler->name
298                     || strcmp( p_handler->name, psz_name ))
299                 {
300                     msg_Err( p_demux, "there's no open element left for <%s>",
301                              psz_name );
302                     FREE_ATT();
303                     return false;
304                 }
305
306                 if( p_handler->pf_handler.smpl )
307                 {
308                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
309                                                 psz_value );
310                 }
311                 FREE_ATT();
312                 p_handler = NULL;
313                 break;
314
315             default:
316                 /* unknown/unexpected xml node */
317                 msg_Err( p_demux, "unexpected xml node %i", i_node );
318                 FREE_ATT();
319                 return false;
320         }
321         FREE_NAME();
322     }
323     return false;
324 }
325
326 /**
327  * \brief parses the tracklist node which only may contain <track>s
328  */
329 static bool parse_tracklist_node COMPLEX_INTERFACE
330 {
331     VLC_UNUSED(psz_element);
332     char *psz_name=NULL;
333     int i_node;
334     int i_ntracks = 0;
335
336     /* now parse the <track>s */
337     while( xml_ReaderRead( p_xml_reader ) == 1 )
338     {
339         i_node = xml_ReaderNodeType( p_xml_reader );
340         if( i_node == XML_READER_STARTELEM )
341         {
342             psz_name = xml_ReaderName( p_xml_reader );
343             if( !psz_name )
344             {
345                 msg_Err( p_demux, "unexpected end of xml data" );
346                 FREE_NAME();
347                 return false;
348             }
349             if( strcmp( psz_name, "track") )
350             {
351                 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
352                          psz_name );
353                 FREE_NAME();
354                 return false;
355             }
356             FREE_NAME();
357
358             /* parse the track data in a separate function */
359             if( parse_track_node( p_demux, p_playlist, p_input_item,
360                                    p_xml_reader,"track" ) == true )
361                 i_ntracks++;
362         }
363         else if( i_node == XML_READER_ENDELEM )
364             break;
365     }
366
367     /* the <trackList> has to be terminated */
368     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
369     {
370         msg_Err( p_demux, "there's a missing </trackList>" );
371         FREE_NAME();
372         return false;
373     }
374     psz_name = xml_ReaderName( p_xml_reader );
375     if( !psz_name || strcmp( psz_name, "trackList" ) )
376     {
377         msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
378         FREE_NAME();
379         return false;
380     }
381     FREE_NAME();
382
383     msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
384
385     return true;
386 }
387
388 /**
389  * \brief parse one track element
390  * \param COMPLEX_INTERFACE
391  */
392 static bool parse_track_node COMPLEX_INTERFACE
393 {
394     input_item_t *p_new_input = NULL;
395     int i_node;
396     char *psz_name=NULL;
397     char *psz_value=NULL;
398     xml_elem_hnd_t *p_handler=NULL;
399
400     xml_elem_hnd_t track_elements[] =
401         { {"location",     SIMPLE_CONTENT,  {NULL} },
402           {"identifier",   SIMPLE_CONTENT,  {NULL} },
403           {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
404           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
405           {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
406           {"info",         SIMPLE_CONTENT,  {NULL} },
407           {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
408           {"album",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
409           {"trackNum",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
410           {"duration",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
411           {"link",         SIMPLE_CONTENT,  {NULL} },
412           {"meta",         SIMPLE_CONTENT,  {NULL} },
413           {"extension",    COMPLEX_CONTENT, {.cmplx = skip_element} },
414           {NULL,           UNKNOWN_CONTENT, {NULL} }
415         };
416
417     while( xml_ReaderRead( p_xml_reader ) == 1 )
418     {
419         i_node = xml_ReaderNodeType( p_xml_reader );
420         switch( i_node )
421         {
422             case XML_READER_NONE:
423                 break;
424
425             case XML_READER_STARTELEM:
426                 /*  element start tag  */
427                 psz_name = xml_ReaderName( p_xml_reader );
428                 if( !psz_name || !*psz_name )
429                 {
430                     msg_Err( p_demux, "invalid xml stream" );
431                     FREE_ATT();
432                     return false;
433                 }
434                 /* choose handler */
435                 for( p_handler = track_elements;
436                      p_handler->name && strcmp( psz_name, p_handler->name );
437                      p_handler++ );
438                 if( !p_handler->name )
439                 {
440                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
441                     FREE_ATT();
442                     return false;
443                 }
444                 FREE_NAME();
445                 /* complex content is parsed in a separate function */
446                 if( p_handler->type == COMPLEX_CONTENT )
447                 {
448                     if( !p_new_input )
449                     {
450                         msg_Err( p_demux,
451                                  "at <%s> level no new item has been allocated",
452                                  p_handler->name );
453                         FREE_ATT();
454                         return false;
455                     }
456                     if( p_handler->pf_handler.cmplx( p_demux,
457                                                      p_playlist,
458                                                      p_new_input,
459                                                      p_xml_reader,
460                                                      p_handler->name ) )
461                     {
462                         p_handler = NULL;
463                         FREE_ATT();
464                     }
465                     else
466                     {
467                         FREE_ATT();
468                         return false;
469                     }
470                 }
471                 break;
472
473             case XML_READER_TEXT:
474                 /* simple element content */
475                 FREE_ATT();
476                 psz_value = xml_ReaderValue( p_xml_reader );
477                 if( !psz_value )
478                 {
479                     msg_Err( p_demux, "invalid xml stream" );
480                     FREE_ATT();
481                     return false;
482                 }
483                 break;
484
485             case XML_READER_ENDELEM:
486                 /* element end tag */
487                 psz_name = xml_ReaderName( p_xml_reader );
488                 if( !psz_name )
489                 {
490                     msg_Err( p_demux, "invalid xml stream" );
491                     FREE_ATT();
492                     return false;
493                 }
494                 /* leave if the current parent node <track> is terminated */
495                 if( !strcmp( psz_name, psz_element ) )
496                 {
497                     FREE_ATT();
498                     if( p_demux->p_sys->i_identifier <
499                         p_demux->p_sys->i_tracklist_entries )
500                     {
501                         p_demux->p_sys->pp_tracklist[
502                             p_demux->p_sys->i_identifier ] = p_new_input;
503                     }
504                     else
505                     {
506                         if( p_demux->p_sys->i_identifier >
507                             p_demux->p_sys->i_tracklist_entries )
508                         {
509                             p_demux->p_sys->i_tracklist_entries =
510                                 p_demux->p_sys->i_identifier;
511                         }
512                         INSERT_ELEM( p_demux->p_sys->pp_tracklist,
513                                      p_demux->p_sys->i_tracklist_entries,
514                                      p_demux->p_sys->i_tracklist_entries,
515                                      p_new_input );
516                     }
517                     return true;
518                 }
519                 /* there MUST have been a start tag for that element name */
520                 if( !p_handler || !p_handler->name
521                     || strcmp( p_handler->name, psz_name ))
522                 {
523                     msg_Err( p_demux, "there's no open element left for <%s>",
524                              psz_name );
525                     FREE_ATT();
526                     return false;
527                 }
528
529                 /* special case: location */
530                 if( !strcmp( p_handler->name, "location" ) )
531                 {
532                     char *psz_uri=NULL;
533                     /* there MUST NOT be an item */
534                     if( p_new_input )
535                     {
536                         msg_Err( p_demux, "item <%s> already created",
537                                  psz_name );
538                         FREE_ATT();
539                         return false;
540                     }
541                     psz_uri = decode_URI_duplicate( psz_value );
542
543                     if( psz_uri )
544                     {
545                         if( p_demux->p_sys->psz_base &&
546                             !strstr( psz_uri, "://" ) )
547                         {
548                            char* psz_tmp = malloc(
549                                    strlen(p_demux->p_sys->psz_base) +
550                                    strlen(psz_uri) +1 );
551                            if( !psz_tmp )
552                            {
553                                msg_Err( p_demux, "out of memory");
554                                return false;
555                            }
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_ItemNewExt( p_playlist, psz_uri,
562                                                         NULL, 0, NULL, -1 );
563                         free( psz_uri );
564                         input_ItemCopyOptions( 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 /**
665  * \brief parse the extension node of a XSPF playlist
666  */
667 static bool parse_extension_node COMPLEX_INTERFACE
668 {
669     char *psz_name = NULL;
670     char *psz_value = NULL;
671     char *psz_title = NULL;
672     char *psz_application = NULL;
673     int i_node;
674     xml_elem_hnd_t *p_handler = NULL;
675     input_item_t *p_new_input = NULL;
676
677     xml_elem_hnd_t pl_elements[] =
678         { {"node",  COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
679           {"item",  COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
680           {NULL,    UNKNOWN_CONTENT, {NULL} }
681         };
682
683     /* read all extension node attributes */
684     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
685     {
686         psz_name = xml_ReaderName( p_xml_reader );
687         psz_value = xml_ReaderValue( p_xml_reader );
688         if( !psz_name || !psz_value )
689         {
690             msg_Err( p_demux, "invalid xml stream @ <node>" );
691             FREE_ATT();
692             return false;
693         }
694         /* attribute: title */
695         if( !strcmp( psz_name, "title" ) )
696         {
697             resolve_xml_special_chars( psz_value );
698             psz_title = strdup( psz_value );
699         }
700         /* extension attribute: application */
701         else if( !strcmp( psz_name, "application" ) )
702         {
703             psz_application = strdup( psz_value );
704         }
705         /* unknown attribute */
706         else
707             msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element, psz_name );
708
709         FREE_ATT();
710     }
711
712     /* attribute title is mandatory except for <extension> */
713     if( !strcmp( psz_element, "node" ) )
714     {
715         if( !psz_title )
716         {
717             msg_Warn( p_demux, "<node> requires \"title\" attribute" );
718             return false;
719         }
720         p_new_input = input_ItemNewWithType( VLC_OBJECT( p_playlist ), "vlc://nop",
721                                 psz_title, 0, NULL, -1, ITEM_TYPE_DIRECTORY );
722         if( p_new_input )
723         {
724             input_ItemAddSubItem( p_input_item, p_new_input );
725             p_input_item = p_new_input;
726             vlc_gc_decref( p_new_input );
727         }
728         free( psz_title );
729     }
730     else if( !strcmp( psz_element, "extension" ) )
731     {
732         if( !psz_application )
733         {
734             msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
735             return false;
736         }
737         else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
738         {
739             msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
740             free( psz_application );
741             return false;
742         }
743     }
744     free( psz_application );
745
746     /* parse the child elements */
747     while( xml_ReaderRead( p_xml_reader ) == 1 )
748     {
749         i_node = xml_ReaderNodeType( p_xml_reader );
750         switch( i_node )
751         {
752             case XML_READER_NONE:
753                 break;
754             case XML_READER_STARTELEM:
755                 /*  element start tag  */
756                 psz_name = xml_ReaderName( p_xml_reader );
757                 if( !psz_name || !*psz_name )
758                 {
759                     msg_Err( p_demux, "invalid xml stream" );
760                     FREE_ATT();
761                     return false;
762                 }
763                 /* choose handler */
764                 for( p_handler = pl_elements;
765                      p_handler->name && strcmp( psz_name, p_handler->name );
766                      p_handler++ );
767                 if( !p_handler->name )
768                 {
769                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
770                     FREE_ATT();
771                     return false;
772                 }
773                 FREE_NAME();
774                 /* complex content is parsed in a separate function */
775                 if( p_handler->type == COMPLEX_CONTENT )
776                 {
777                     if( p_handler->pf_handler.cmplx( p_demux,
778                                                      p_playlist,
779                                                      p_input_item,
780                                                      p_xml_reader,
781                                                      p_handler->name ) )
782                     {
783                         p_handler = NULL;
784                         FREE_ATT();
785                     }
786                     else
787                     {
788                         FREE_ATT();
789                         return false;
790                     }
791                 }
792                 break;
793
794             case XML_READER_TEXT:
795                 /* simple element content */
796                 FREE_ATT();
797                 psz_value = xml_ReaderValue( p_xml_reader );
798                 if( !psz_value )
799                 {
800                     msg_Err( p_demux, "invalid xml stream" );
801                     FREE_ATT();
802                     return false;
803                 }
804                 break;
805
806             case XML_READER_ENDELEM:
807                 /* element end tag */
808                 psz_name = xml_ReaderName( p_xml_reader );
809                 if( !psz_name )
810                 {
811                     msg_Err( p_demux, "invalid xml stream" );
812                     FREE_ATT();
813                     return false;
814                 }
815                 /* leave if the current parent node is terminated */
816                 if( !strcmp( psz_name, psz_element ) )
817                 {
818                     FREE_ATT();
819                     return true;
820                 }
821                 /* there MUST have been a start tag for that element name */
822                 if( !p_handler || !p_handler->name
823                     || strcmp( p_handler->name, psz_name ))
824                 {
825                     msg_Err( p_demux, "there's no open element left for <%s>",
826                              psz_name );
827                     FREE_ATT();
828                     return false;
829                 }
830
831                 if( p_handler->pf_handler.smpl )
832                 {
833                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
834                                                 psz_value );
835                 }
836                 FREE_ATT();
837                 p_handler = NULL;
838                 break;
839
840             default:
841                 /* unknown/unexpected xml node */
842                 msg_Err( p_demux, "unexpected xml node %i", i_node );
843                 FREE_ATT();
844                 return false;
845         }
846         FREE_NAME();
847     }
848     return false;
849 }
850
851 /**
852  * \brief parse the extension item node of a XSPF playlist
853  */
854 static bool parse_extitem_node COMPLEX_INTERFACE
855 {
856     VLC_UNUSED(p_playlist); VLC_UNUSED(psz_element);
857     input_item_t *p_new_input = NULL;
858     char *psz_name = NULL;
859     char *psz_value = NULL;
860     int i_href = -1;
861
862     /* read all extension item attributes */
863     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
864     {
865         psz_name = xml_ReaderName( p_xml_reader );
866         psz_value = xml_ReaderValue( p_xml_reader );
867         if( !psz_name || !psz_value )
868         {
869             msg_Err( p_demux, "invalid xml stream @ <item>" );
870             FREE_ATT();
871             return false;
872         }
873         /* attribute: href */
874         if( !strcmp( psz_name, "href" ) )
875         {
876             i_href = atoi( psz_value );
877         }
878         /* unknown attribute */
879         else
880             msg_Warn( p_demux, "invalid <item> attribute:\"%s\"", psz_name);
881
882         FREE_ATT();
883     }
884
885     /* attribute href is mandatory */
886     if( i_href < 0 )
887     {
888         msg_Warn( p_demux, "<item> requires \"href\" attribute" );
889         return false;
890     }
891
892     if( i_href >= p_demux->p_sys->i_tracklist_entries )
893     {
894         msg_Warn( p_demux, "invalid \"href\" attribute" );
895         return false;
896     }
897
898     p_new_input = p_demux->p_sys->pp_tracklist[ i_href ];
899     if( p_new_input )
900     {
901         input_ItemAddSubItem( p_input_item, p_new_input );
902         vlc_gc_decref( p_new_input );
903         p_demux->p_sys->pp_tracklist[i_href] = NULL;
904     }
905
906     /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
907     /* (libxml sends NONE) */
908     xml_ReaderRead( p_xml_reader );
909
910     return true;
911 }
912
913 /**
914  * \brief skips complex element content that we can't manage
915  */
916 static bool skip_element COMPLEX_INTERFACE
917 {
918     VLC_UNUSED(p_demux); VLC_UNUSED(p_playlist); VLC_UNUSED(p_input_item);
919     char *psz_endname;
920
921     while( xml_ReaderRead( p_xml_reader ) == 1 )
922     {
923         if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
924         {
925             psz_endname = xml_ReaderName( p_xml_reader );
926             if( !psz_endname )
927                 return false;
928             if( !strcmp( psz_element, psz_endname ) )
929             {
930                 free( psz_endname );
931                 return true;
932             }
933             else
934                 free( psz_endname );
935         }
936     }
937     return false;
938 }