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