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