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