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 )
65 playlist_t *p_playlist = NULL;
66 playlist_item_t *p_current = NULL;
69 int i_ret = VLC_SUCCESS;
72 xml_reader_t *p_xml_reader = NULL;
73 char *psz_name = NULL;
75 /* create new xml parser from stream */
76 p_xml = xml_Create( p_demux );
81 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
86 /* start with parsing the root node */
87 if ( i_ret == VLC_SUCCESS )
88 if ( xml_ReaderRead( p_xml_reader ) != 1 )
90 msg_Err( p_demux, "can't read xml stream" );
93 /* checking root nody type */
94 if ( i_ret == VLC_SUCCESS )
95 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM )
97 msg_Err( p_demux, "invalid root node type: %i", xml_ReaderNodeType( p_xml_reader ) );
100 /* checking root node name */
101 if ( i_ret == VLC_SUCCESS )
102 psz_name = xml_ReaderName( p_xml_reader );
103 if ( !psz_name || strcmp( psz_name, "playlist" ) )
105 msg_Err( p_demux, "invalid root node name: %s", psz_name );
106 i_ret = VLC_EGENERIC;
110 /* get the playlist ... */
111 if ( i_ret == VLC_SUCCESS )
113 p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST, FIND_PARENT );
116 msg_Err( p_demux, "can't find playlist" );
120 /* ... and its current item (to convert it to a node) */
121 if ( i_ret == VLC_SUCCESS )
123 b_play = E_(FindItem)( p_demux, p_playlist, &p_current );
124 playlist_ItemToNode( p_playlist, p_current );
125 p_current->input.i_type = ITEM_TYPE_PLAYLIST;
126 /* parse the playlist node */
127 i_ret = parse_playlist_node( p_demux, p_playlist, p_current,
128 p_xml_reader, "playlist" );
129 /* true/false - success/egeneric mapping */
130 i_ret = ( i_ret==VLC_TRUE ? VLC_SUCCESS : VLC_EGENERIC );
134 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
135 p_playlist->status.i_view,
136 p_playlist->status.p_item, NULL );
141 vlc_object_release( p_playlist );
143 xml_ReaderDelete( p_xml, p_xml_reader );
151 /** \brief dummy function for demux callback interface */
152 int xspf_import_Control( demux_t *p_demux, int i_query, va_list args )
158 * \brief parse the root node of a XSPF playlist
159 * \param p_demux demuxer instance
160 * \param p_playlist playlist instance
161 * \param p_item current playlist node
162 * \param p_xml_reader xml reader instance
163 * \param psz_element name of element to parse
165 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
168 char *psz_value=NULL;
169 vlc_bool_t b_version_found = VLC_FALSE;
171 xml_elem_hnd_t *p_handler=NULL;
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, {NULL} },
177 {"info", SIMPLE_CONTENT, {NULL} },
178 {"location", SIMPLE_CONTENT, {NULL} },
179 {"identifier", SIMPLE_CONTENT, {NULL} },
180 {"image", SIMPLE_CONTENT, {NULL} },
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 = skip_element} },
187 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
188 {NULL, UNKNOWN_CONTENT, {NULL} }
191 /* read all playlist attributes */
192 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
194 psz_name = xml_ReaderName ( p_xml_reader );
195 psz_value = xml_ReaderValue ( p_xml_reader );
196 if ( !psz_name || !psz_value )
198 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
202 /* attribute: version */
203 if ( !strcmp( psz_name, "version" ) )
205 b_version_found = VLC_TRUE;
206 if ( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
207 msg_Warn( p_demux, "unsupported XSPF version" );
209 /* attribute: xmlns */
210 else if ( !strcmp ( psz_name, "xmlns" ) )
212 /* unknown attribute */
214 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
218 /* attribute version is mandatory !!! */
219 if ( !b_version_found )
220 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
222 /* parse the child elements - we only take care of <trackList> */
223 while ( xml_ReaderRead( p_xml_reader ) == 1 )
225 i_node = xml_ReaderNodeType( p_xml_reader );
228 case XML_READER_NONE:
230 case XML_READER_STARTELEM:
231 /* element start tag */
232 psz_name = xml_ReaderName( p_xml_reader );
233 if ( !psz_name || !*psz_name )
235 msg_Err( p_demux, "invalid xml stream" );
240 for( p_handler = pl_elements;
241 p_handler->name && strcmp( psz_name, p_handler->name );
243 if ( !p_handler->name )
245 msg_Err( p_demux, "unexpected element <%s>", psz_name );
250 /* complex content is parsed in a separate function */
251 if ( p_handler->type == COMPLEX_CONTENT )
253 if ( p_handler->pf_handler.cmplx( p_demux,
270 case XML_READER_TEXT:
271 /* simple element content */
273 psz_value = xml_ReaderValue( p_xml_reader );
276 msg_Err( p_demux, "invalid xml stream" );
282 case XML_READER_ENDELEM:
283 /* element end tag */
284 psz_name = xml_ReaderName( p_xml_reader );
287 msg_Err( p_demux, "invalid xml stream" );
291 /* leave if the current parent node <playlist> is terminated */
292 if ( !strcmp( psz_name, psz_element ) )
297 /* there MUST have been a start tag for that element name */
298 if ( !p_handler || !p_handler->name
299 || strcmp( p_handler->name, psz_name ))
301 msg_Err( p_demux, "there's no open element left for <%s>",
307 if ( p_handler->pf_handler.smpl )
309 p_handler->pf_handler.smpl( p_item, p_handler->name,
317 /* unknown/unexpected xml node */
318 msg_Err( p_demux, "unexpected xml node %i", i_node );
328 * \brief parses the tracklist node which only may contain <track>s
330 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
336 /* now parse the <track>s */
337 while ( xml_ReaderRead( p_xml_reader ) == 1 )
339 i_node = xml_ReaderNodeType( p_xml_reader );
340 if ( i_node == XML_READER_STARTELEM )
342 psz_name = xml_ReaderName( p_xml_reader );
345 msg_Err( p_demux, "unexpected end of xml data" );
349 if ( strcmp( psz_name, "track") )
351 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
358 /* parse the track data in a separate function */
359 if ( parse_track_node( p_demux, p_playlist, p_item, p_xml_reader,
360 "track" ) == VLC_TRUE )
363 else if ( i_node == XML_READER_ENDELEM )
367 /* the <trackList> has to be terminated */
368 if ( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
370 msg_Err( p_demux, "there's a missing </trackList>" );
374 psz_name = xml_ReaderName( p_xml_reader );
375 if ( !psz_name || strcmp( psz_name, "trackList" ) )
377 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
383 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
389 * \brief parse one track element
390 * \param COMPLEX_INTERFACE
392 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
394 playlist_item_t *p_new=NULL;
397 char *psz_value=NULL;
398 xml_elem_hnd_t *p_handler=NULL;
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, {NULL} },
406 {"info", SIMPLE_CONTENT, {NULL} },
407 {"image", SIMPLE_CONTENT, {NULL} },
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 = skip_element} },
414 {NULL, UNKNOWN_CONTENT, {NULL} }
417 while ( xml_ReaderRead( p_xml_reader ) == 1 )
419 i_node = xml_ReaderNodeType( p_xml_reader );
422 case XML_READER_NONE:
425 case XML_READER_STARTELEM:
426 /* element start tag */
427 psz_name = xml_ReaderName( p_xml_reader );
428 if ( !psz_name || !*psz_name )
430 msg_Err( p_demux, "invalid xml stream" );
435 for( p_handler = track_elements;
436 p_handler->name && strcmp( psz_name, p_handler->name );
438 if ( !p_handler->name )
440 msg_Err( p_demux, "unexpected element <%s>", psz_name );
445 /* complex content is parsed in a separate function */
446 if ( p_handler->type == COMPLEX_CONTENT )
451 "at <%s> level no new item has been allocated",
456 if ( p_handler->pf_handler.cmplx( p_demux,
473 case XML_READER_TEXT:
474 /* simple element content */
476 psz_value = xml_ReaderValue( p_xml_reader );
479 msg_Err( p_demux, "invalid xml stream" );
485 case XML_READER_ENDELEM:
486 /* element end tag */
487 psz_name = xml_ReaderName( p_xml_reader );
490 msg_Err( p_demux, "invalid xml stream" );
494 /* leave if the current parent node <track> is terminated */
495 if ( !strcmp( psz_name, psz_element ) )
500 /* there MUST have been a start tag for that element name */
501 if ( !p_handler || !p_handler->name
502 || strcmp( p_handler->name, psz_name ))
504 msg_Err( p_demux, "there's no open element left for <%s>",
510 /* special case: location */
511 if ( !strcmp( p_handler->name, "location" ) )
513 /* there MUST NOT be an item */
517 "a new item has just been created <%s>",
523 if ( insert_new_item( p_playlist, p_item,
524 &p_new, psz_value ) )
537 /* there MUST be an item */
541 "an item hasn't been created yet <%s>",
546 if ( p_handler->pf_handler.smpl )
548 p_handler->pf_handler.smpl( p_new, p_handler->name,
558 /* unknown/unexpected xml node */
559 msg_Err( p_demux, "unexpected xml node %i", i_node );
565 msg_Err( p_demux, "unexpected end of xml data" );
571 * \brief handles the supported <track> sub-elements
573 static vlc_bool_t set_item_info SIMPLE_INTERFACE
575 /* exit if setting is impossible */
576 if ( !psz_name || !psz_value || !p_item )
579 /* re-convert xml special characters inside psz_value */
580 resolve_xml_special_chars ( psz_value );
582 /* handle each info element in a separate "if" clause */
583 if ( !strcmp( psz_name, "title" ) )
585 if ( playlist_ItemSetName ( p_item, (char *)psz_value ) == VLC_SUCCESS )
589 else if ( !strcmp( psz_name, "creator" ) )
591 if ( vlc_input_item_AddInfo( &(p_item->input),
592 _(VLC_META_INFO_CAT), _(VLC_META_ARTIST),
593 "%s", psz_value ) == VLC_SUCCESS )
598 else if ( !strcmp( psz_name, "album" ) )
600 if ( vlc_input_item_AddInfo( &(p_item->input),
601 _(VLC_META_INFO_CAT),
602 _(VLC_META_COLLECTION),
603 "%s", psz_value ) == VLC_SUCCESS )
607 } else if ( !strcmp( psz_name, "trackNum" ) )
609 long i_num = atol( psz_value );
611 && vlc_input_item_AddInfo( &(p_item->input),
612 _(VLC_META_INFO_CAT),
614 "%s", psz_value ) == VLC_SUCCESS )
618 } else if ( !strcmp( psz_name, "duration" ) )
620 long i_num = atol( psz_value );
622 && playlist_ItemSetDuration( p_item, i_num*1000 ) == VLC_SUCCESS )
632 * \brief skips complex element content that we can't manage
634 static vlc_bool_t skip_element COMPLEX_INTERFACE
638 while ( xml_ReaderRead( p_xml_reader ) == 1 )
640 if ( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
642 psz_endname = xml_ReaderName( p_xml_reader );
645 if ( !strcmp( psz_element, psz_endname ) )
658 * \brief creates a new playlist item from the given mrl
660 static vlc_bool_t insert_new_item( playlist_t *p_pl, playlist_item_t *p_cur,
661 playlist_item_t **pp_new, char *psz_location )
664 psz_uri = unescape_URI_duplicate( psz_location );
668 *pp_new = playlist_ItemNew( p_pl, psz_uri, NULL );
676 playlist_NodeAddItem( p_pl, *pp_new, p_cur->pp_parents[0]->i_view,
677 p_cur, PLAYLIST_APPEND, PLAYLIST_END );
679 playlist_CopyParents( p_cur, *pp_new );
681 vlc_input_item_CopyOptions( &p_cur->input, &((*pp_new)->input) );