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