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