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