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