1 /******************************************************************************
2 * Copyright (C) 2006 Daniel Stränger <vlc at schmaller dot de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
17 *******************************************************************************/
19 * \file modules/demux/playlist/xspf.c
20 * \brief XSPF playlist import functions
24 #include <vlc/input.h>
29 #include "vlc_strings.h"
33 * \brief XSPF submodule initialization function
35 int E_(xspf_import_Activate)( vlc_object_t *p_this )
37 demux_t *p_demux = (demux_t *)p_this;
40 psz_ext = strrchr ( p_demux->psz_path, '.' );
42 if( ( psz_ext && !strcasecmp( psz_ext, ".xspf") ) ||
43 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "xspf-open") ) )
51 msg_Dbg( p_demux, "using xspf playlist import");
53 p_demux->pf_control = xspf_import_Control;
54 p_demux->pf_demux = xspf_import_Demux;
60 * \brief demuxer function for XSPF parsing
62 int xspf_import_Demux( demux_t *p_demux )
64 playlist_t *p_playlist = NULL;
65 playlist_item_t *p_current = NULL;
68 int i_ret = VLC_SUCCESS;
71 xml_reader_t *p_xml_reader = NULL;
72 char *psz_name = NULL;
74 /* create new xml parser from stream */
75 p_xml = xml_Create( p_demux );
80 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
85 /* start with parsing the root node */
86 if ( i_ret == VLC_SUCCESS )
87 if ( xml_ReaderRead( p_xml_reader ) != 1 )
89 msg_Err( p_demux, "can't read xml stream" );
92 /* checking root nody type */
93 if ( i_ret == VLC_SUCCESS )
94 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM )
96 msg_Err( p_demux, "invalid root node type: %i", xml_ReaderNodeType( p_xml_reader ) );
99 /* checking root node name */
100 if ( i_ret == VLC_SUCCESS )
101 psz_name = xml_ReaderName( p_xml_reader );
102 if ( !psz_name || strcmp( psz_name, "playlist" ) )
104 msg_Err( p_demux, "invalid root node name: %s", psz_name );
105 i_ret = VLC_EGENERIC;
109 /* get the playlist ... */
110 if ( i_ret == VLC_SUCCESS )
112 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST, FIND_PARENT );
115 msg_Err( p_demux, "can't find playlist" );
119 /* ... and its current item (to convert it to a node) */
120 if ( i_ret == VLC_SUCCESS )
122 b_play = E_(FindItem)( p_demux, p_playlist, &p_current );
123 playlist_ItemToNode( p_playlist, p_current );
124 p_current->input.i_type = ITEM_TYPE_PLAYLIST;
125 /* parse the playlist node */
126 i_ret = parse_playlist_node( p_demux, p_playlist, p_current,
127 p_xml_reader, "playlist" );
128 /* true/false - success/egeneric mapping */
129 i_ret = ( i_ret==VLC_TRUE ? VLC_SUCCESS : VLC_EGENERIC );
133 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
134 p_playlist->status.i_view,
135 p_playlist->status.p_item, NULL );
140 vlc_object_release( p_playlist );
142 xml_ReaderDelete( p_xml, p_xml_reader );
149 /** \brief dummy function for demux callback interface */
150 int xspf_import_Control( demux_t *p_demux, int i_query, va_list args )
156 * \brief parse the root node of a XSPF playlist
157 * \param p_demux demuxer instance
158 * \param p_playlist playlist instance
159 * \param p_item current playlist node
160 * \param p_xml_reader xml reader instance
161 * \param psz_element name of element to parse
163 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
166 char *psz_value=NULL;
167 vlc_bool_t b_version_found = VLC_FALSE;
169 xml_elem_hnd_t *p_handler=NULL;
171 xml_elem_hnd_t pl_elements[] =
172 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
173 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
174 {"annotation", SIMPLE_CONTENT, {NULL} },
175 {"info", SIMPLE_CONTENT, {NULL} },
176 {"location", SIMPLE_CONTENT, {NULL} },
177 {"identifier", SIMPLE_CONTENT, {NULL} },
178 {"image", SIMPLE_CONTENT, {NULL} },
179 {"date", SIMPLE_CONTENT, {NULL} },
180 {"license", SIMPLE_CONTENT, {NULL} },
181 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
182 {"link", SIMPLE_CONTENT, {NULL} },
183 {"meta", SIMPLE_CONTENT, {NULL} },
184 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
185 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
186 {NULL, UNKNOWN_CONTENT, {NULL} }
189 /* read all playlist attributes */
190 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
192 psz_name = xml_ReaderName ( p_xml_reader );
193 psz_value = xml_ReaderValue ( p_xml_reader );
194 if ( !psz_name || !psz_value )
196 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
200 /* attribute: version */
201 if ( !strcmp( psz_name, "version" ) )
203 b_version_found = VLC_TRUE;
204 if ( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
205 msg_Warn( p_demux, "unsupported XSPF version" );
207 /* attribute: xmlns */
208 else if ( !strcmp ( psz_name, "xmlns" ) )
210 /* unknown attribute */
212 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
216 /* attribute version is mandatory !!! */
217 if ( !b_version_found )
218 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
220 /* parse the child elements - we only take care of <trackList> */
221 while ( xml_ReaderRead( p_xml_reader ) == 1 )
223 i_node = xml_ReaderNodeType( p_xml_reader );
226 case XML_READER_NONE:
228 case XML_READER_STARTELEM:
229 /* element start tag */
230 psz_name = xml_ReaderName( p_xml_reader );
231 if ( !psz_name || !*psz_name )
233 msg_Err( p_demux, "invalid xml stream" );
238 for( p_handler = pl_elements;
239 p_handler->name && strcmp( psz_name, p_handler->name );
241 if ( !p_handler->name )
243 msg_Err( p_demux, "unexpected element <%s>", psz_name );
248 /* complex content is parsed in a separate function */
249 if ( p_handler->type == COMPLEX_CONTENT )
251 if ( p_handler->pf_handler.cmplx( p_demux,
268 case XML_READER_TEXT:
269 /* simple element content */
271 psz_value = xml_ReaderValue( p_xml_reader );
274 msg_Err( p_demux, "invalid xml stream" );
280 case XML_READER_ENDELEM:
281 /* element end tag */
282 psz_name = xml_ReaderName( p_xml_reader );
285 msg_Err( p_demux, "invalid xml stream" );
289 /* leave if the current parent node <playlist> is terminated */
290 if ( !strcmp( psz_name, psz_element ) )
295 /* there MUST have been a start tag for that element name */
296 if ( !p_handler || !p_handler->name
297 || strcmp( p_handler->name, psz_name ))
299 msg_Err( p_demux, "there's no open element left for <%s>",
305 if ( p_handler->pf_handler.smpl )
307 p_handler->pf_handler.smpl( p_item, p_handler->name,
315 /* unknown/unexpected xml node */
316 msg_Err( p_demux, "unexpected xml node %i", i_node );
326 * \brief parses the tracklist node which only may contain <track>s
328 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
334 /* now parse the <track>s */
335 while ( xml_ReaderRead( p_xml_reader ) == 1 )
337 i_node = xml_ReaderNodeType( p_xml_reader );
338 if ( i_node == XML_READER_STARTELEM )
340 psz_name = xml_ReaderName( p_xml_reader );
343 msg_Err( p_demux, "unexpected end of xml data" );
347 if ( strcmp( psz_name, "track") )
349 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
356 /* parse the track data in a separate function */
357 if ( parse_track_node( p_demux, p_playlist, p_item, p_xml_reader,
358 "track" ) == VLC_TRUE )
361 else if ( i_node == XML_READER_ENDELEM )
365 /* the <trackList> has to be terminated */
366 if ( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
368 msg_Err( p_demux, "there's a missing </trackList>" );
372 psz_name = xml_ReaderName( p_xml_reader );
373 if ( !psz_name || strcmp( psz_name, "trackList" ) )
375 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
381 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
387 * \brief parse one track element
388 * \param COMPLEX_INTERFACE
390 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
392 playlist_item_t *p_new=NULL;
395 char *psz_value=NULL;
396 xml_elem_hnd_t *p_handler=NULL;
398 xml_elem_hnd_t track_elements[] =
399 { {"location", SIMPLE_CONTENT, {NULL} },
400 {"identifier", SIMPLE_CONTENT, {NULL} },
401 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
402 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
403 {"annotation", SIMPLE_CONTENT, {NULL} },
404 {"info", SIMPLE_CONTENT, {NULL} },
405 {"image", SIMPLE_CONTENT, {NULL} },
406 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
407 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
408 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
409 {"link", SIMPLE_CONTENT, {NULL} },
410 {"meta", SIMPLE_CONTENT, {NULL} },
411 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
412 {NULL, UNKNOWN_CONTENT, {NULL} }
415 while ( xml_ReaderRead( p_xml_reader ) == 1 )
417 i_node = xml_ReaderNodeType( p_xml_reader );
420 case XML_READER_NONE:
423 case XML_READER_STARTELEM:
424 /* element start tag */
425 psz_name = xml_ReaderName( p_xml_reader );
426 if ( !psz_name || !*psz_name )
428 msg_Err( p_demux, "invalid xml stream" );
433 for( p_handler = track_elements;
434 p_handler->name && strcmp( psz_name, p_handler->name );
436 if ( !p_handler->name )
438 msg_Err( p_demux, "unexpected element <%s>", psz_name );
443 /* complex content is parsed in a separate function */
444 if ( p_handler->type == COMPLEX_CONTENT )
449 "at <%s> level no new item has been allocated",
454 if ( p_handler->pf_handler.cmplx( p_demux,
471 case XML_READER_TEXT:
472 /* simple element content */
474 psz_value = xml_ReaderValue( p_xml_reader );
477 msg_Err( p_demux, "invalid xml stream" );
483 case XML_READER_ENDELEM:
484 /* element end tag */
485 psz_name = xml_ReaderName( p_xml_reader );
488 msg_Err( p_demux, "invalid xml stream" );
492 /* leave if the current parent node <track> is terminated */
493 if ( !strcmp( psz_name, psz_element ) )
498 /* there MUST have been a start tag for that element name */
499 if ( !p_handler || !p_handler->name
500 || strcmp( p_handler->name, psz_name ))
502 msg_Err( p_demux, "there's no open element left for <%s>",
508 /* special case: location */
509 if ( !strcmp( p_handler->name, "location" ) )
511 /* there MUST NOT be an item */
515 "a new item has just been created <%s>",
521 if ( insert_new_item( p_playlist, p_item,
522 &p_new, psz_value ) )
535 /* there MUST be an item */
539 "an item hasn't been created yet <%s>",
544 if ( p_handler->pf_handler.smpl )
546 p_handler->pf_handler.smpl( p_new, p_handler->name,
556 /* unknown/unexpected xml node */
557 msg_Err( p_demux, "unexpected xml node %i", i_node );
563 msg_Err( p_demux, "unexpected end of xml data" );
569 * \brief handles the supported <track> sub-elements
571 static vlc_bool_t set_item_info SIMPLE_INTERFACE
573 /* exit if setting is impossible */
574 if ( !psz_name || !psz_value || !p_item )
577 /* re-convert xml special characters inside psz_value */
578 resolve_xml_special_chars ( psz_value );
580 /* handle each info element in a separate "if" clause */
581 if ( !strcmp( psz_name, "title" ) )
583 if ( playlist_ItemSetName ( p_item, (char *)psz_value ) == VLC_SUCCESS )
587 else if ( !strcmp( psz_name, "creator" ) )
589 if ( vlc_input_item_AddInfo( &(p_item->input),
590 _(VLC_META_INFO_CAT), _(VLC_META_ARTIST),
591 "%s", psz_value ) == VLC_SUCCESS )
596 else if ( !strcmp( psz_name, "album" ) )
598 if ( vlc_input_item_AddInfo( &(p_item->input),
599 _(VLC_META_INFO_CAT),
600 _(VLC_META_COLLECTION),
601 "%s", psz_value ) == VLC_SUCCESS )
605 } else if ( !strcmp( psz_name, "trackNum" ) )
607 long i_num = atol( psz_value );
609 && vlc_input_item_AddInfo( &(p_item->input),
610 _(VLC_META_INFO_CAT),
612 "%s", psz_value ) == VLC_SUCCESS )
616 } else if ( !strcmp( psz_name, "duration" ) )
618 long i_num = atol( psz_value );
620 && playlist_ItemSetDuration( p_item, i_num*1000 ) == VLC_SUCCESS )
630 * \brief skips complex element content that we can't manage
632 static vlc_bool_t skip_element COMPLEX_INTERFACE
636 while ( xml_ReaderRead( p_xml_reader ) == 1 )
638 if ( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
640 psz_endname = xml_ReaderName( p_xml_reader );
643 if ( !strcmp( psz_element, psz_endname ) )
656 * \brief creates a new playlist item from the given mrl
658 static vlc_bool_t insert_new_item( playlist_t *p_pl, playlist_item_t *p_cur,
659 playlist_item_t **pp_new, char *psz_location )
662 psz_uri = unescape_URI_duplicate( psz_location );
666 *pp_new = playlist_ItemNew( p_pl, psz_uri, NULL );
674 playlist_NodeAddItem( p_pl, *pp_new, p_cur->pp_parents[0]->i_view,
675 p_cur, PLAYLIST_APPEND, PLAYLIST_END );
677 playlist_CopyParents( p_cur, *pp_new );
679 vlc_input_item_CopyOptions( &p_cur->input, &((*pp_new)->input) );