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 );
152 /** \brief dummy function for demux callback interface */
153 int xspf_import_Control( demux_t *p_demux, int i_query, va_list args )
159 * \brief parse the root node of a XSPF playlist
160 * \param p_demux demuxer instance
161 * \param p_playlist playlist instance
162 * \param p_item current playlist node
163 * \param p_xml_reader xml reader instance
164 * \param psz_element name of element to parse
166 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
169 char *psz_value=NULL;
170 vlc_bool_t b_version_found = VLC_FALSE;
172 xml_elem_hnd_t *p_handler=NULL;
174 xml_elem_hnd_t pl_elements[] =
175 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
176 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
177 {"annotation", SIMPLE_CONTENT, {NULL} },
178 {"info", SIMPLE_CONTENT, {NULL} },
179 {"location", SIMPLE_CONTENT, {NULL} },
180 {"identifier", SIMPLE_CONTENT, {NULL} },
181 {"image", SIMPLE_CONTENT, {NULL} },
182 {"date", SIMPLE_CONTENT, {NULL} },
183 {"license", SIMPLE_CONTENT, {NULL} },
184 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
185 {"link", SIMPLE_CONTENT, {NULL} },
186 {"meta", SIMPLE_CONTENT, {NULL} },
187 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
188 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
189 {NULL, UNKNOWN_CONTENT, {NULL} }
192 /* read all playlist attributes */
193 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
195 psz_name = xml_ReaderName ( p_xml_reader );
196 psz_value = xml_ReaderValue ( p_xml_reader );
197 if ( !psz_name || !psz_value )
199 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
203 /* attribute: version */
204 if ( !strcmp( psz_name, "version" ) )
206 b_version_found = VLC_TRUE;
207 if ( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
208 msg_Warn( p_demux, "unsupported XSPF version" );
210 /* attribute: xmlns */
211 else if ( !strcmp ( psz_name, "xmlns" ) )
213 /* unknown attribute */
215 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
219 /* attribute version is mandatory !!! */
220 if ( !b_version_found )
221 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
223 /* parse the child elements - we only take care of <trackList> */
224 while ( xml_ReaderRead( p_xml_reader ) == 1 )
226 i_node = xml_ReaderNodeType( p_xml_reader );
229 case XML_READER_NONE:
231 case XML_READER_STARTELEM:
232 /* element start tag */
233 psz_name = xml_ReaderName( p_xml_reader );
234 if ( !psz_name || !*psz_name )
236 msg_Err( p_demux, "invalid xml stream" );
241 for( p_handler = pl_elements;
242 p_handler->name && strcmp( psz_name, p_handler->name );
244 if ( !p_handler->name )
246 msg_Err( p_demux, "unexpected element <%s>", psz_name );
251 /* complex content is parsed in a separate function */
252 if ( p_handler->type == COMPLEX_CONTENT )
254 if ( p_handler->pf_handler.cmplx( p_demux,
271 case XML_READER_TEXT:
272 /* simple element content */
274 psz_value = xml_ReaderValue( p_xml_reader );
277 msg_Err( p_demux, "invalid xml stream" );
283 case XML_READER_ENDELEM:
284 /* element end tag */
285 psz_name = xml_ReaderName( p_xml_reader );
288 msg_Err( p_demux, "invalid xml stream" );
292 /* leave if the current parent node <playlist> is terminated */
293 if ( !strcmp( psz_name, psz_element ) )
298 /* there MUST have been a start tag for that element name */
299 if ( !p_handler || !p_handler->name
300 || strcmp( p_handler->name, psz_name ))
302 msg_Err( p_demux, "there's no open element left for <%s>",
308 if ( p_handler->pf_handler.smpl )
310 p_handler->pf_handler.smpl( p_item, p_handler->name,
318 /* unknown/unexpected xml node */
319 msg_Err( p_demux, "unexpected xml node %i", i_node );
329 * \brief parses the tracklist node which only may contain <track>s
331 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
337 /* now parse the <track>s */
338 while ( xml_ReaderRead( p_xml_reader ) == 1 )
340 i_node = xml_ReaderNodeType( p_xml_reader );
341 if ( i_node == XML_READER_STARTELEM )
343 psz_name = xml_ReaderName( p_xml_reader );
346 msg_Err( p_demux, "unexpected end of xml data" );
350 if ( strcmp( psz_name, "track") )
352 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
359 /* parse the track data in a separate function */
360 if ( parse_track_node( p_demux, p_playlist, p_item, p_xml_reader,
361 "track" ) == VLC_TRUE )
364 else if ( i_node == XML_READER_ENDELEM )
368 /* the <trackList> has to be terminated */
369 if ( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
371 msg_Err( p_demux, "there's a missing </trackList>" );
375 psz_name = xml_ReaderName( p_xml_reader );
376 if ( !psz_name || strcmp( psz_name, "trackList" ) )
378 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
384 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
390 * \brief parse one track element
391 * \param COMPLEX_INTERFACE
393 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
395 playlist_item_t *p_new=NULL;
398 char *psz_value=NULL;
399 xml_elem_hnd_t *p_handler=NULL;
401 xml_elem_hnd_t track_elements[] =
402 { {"location", SIMPLE_CONTENT, {NULL} },
403 {"identifier", SIMPLE_CONTENT, {NULL} },
404 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
405 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
406 {"annotation", SIMPLE_CONTENT, {NULL} },
407 {"info", SIMPLE_CONTENT, {NULL} },
408 {"image", SIMPLE_CONTENT, {NULL} },
409 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
410 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
411 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
412 {"link", SIMPLE_CONTENT, {NULL} },
413 {"meta", SIMPLE_CONTENT, {NULL} },
414 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
415 {NULL, UNKNOWN_CONTENT, {NULL} }
418 while ( xml_ReaderRead( p_xml_reader ) == 1 )
420 i_node = xml_ReaderNodeType( p_xml_reader );
423 case XML_READER_NONE:
426 case XML_READER_STARTELEM:
427 /* element start tag */
428 psz_name = xml_ReaderName( p_xml_reader );
429 if ( !psz_name || !*psz_name )
431 msg_Err( p_demux, "invalid xml stream" );
436 for( p_handler = track_elements;
437 p_handler->name && strcmp( psz_name, p_handler->name );
439 if ( !p_handler->name )
441 msg_Err( p_demux, "unexpected element <%s>", psz_name );
446 /* complex content is parsed in a separate function */
447 if ( p_handler->type == COMPLEX_CONTENT )
452 "at <%s> level no new item has been allocated",
457 if ( p_handler->pf_handler.cmplx( p_demux,
474 case XML_READER_TEXT:
475 /* simple element content */
477 psz_value = xml_ReaderValue( p_xml_reader );
480 msg_Err( p_demux, "invalid xml stream" );
486 case XML_READER_ENDELEM:
487 /* element end tag */
488 psz_name = xml_ReaderName( p_xml_reader );
491 msg_Err( p_demux, "invalid xml stream" );
495 /* leave if the current parent node <track> is terminated */
496 if ( !strcmp( psz_name, psz_element ) )
501 /* there MUST have been a start tag for that element name */
502 if ( !p_handler || !p_handler->name
503 || strcmp( p_handler->name, psz_name ))
505 msg_Err( p_demux, "there's no open element left for <%s>",
511 /* special case: location */
512 if ( !strcmp( p_handler->name, "location" ) )
514 /* there MUST NOT be an item */
518 "a new item has just been created <%s>",
524 if ( insert_new_item( p_playlist, p_item,
525 &p_new, psz_value ) )
538 /* there MUST be an item */
542 "an item hasn't been created yet <%s>",
547 if ( p_handler->pf_handler.smpl )
549 p_handler->pf_handler.smpl( p_new, p_handler->name,
559 /* unknown/unexpected xml node */
560 msg_Err( p_demux, "unexpected xml node %i", i_node );
566 msg_Err( p_demux, "unexpected end of xml data" );
572 * \brief handles the supported <track> sub-elements
574 static vlc_bool_t set_item_info SIMPLE_INTERFACE
576 /* exit if setting is impossible */
577 if ( !psz_name || !psz_value || !p_item )
580 /* re-convert xml special characters inside psz_value */
581 resolve_xml_special_chars ( psz_value );
583 /* handle each info element in a separate "if" clause */
584 if ( !strcmp( psz_name, "title" ) )
586 if ( playlist_ItemSetName ( p_item, (char *)psz_value ) == VLC_SUCCESS )
590 else if ( !strcmp( psz_name, "creator" ) )
592 if ( vlc_input_item_AddInfo( &(p_item->input),
593 _(VLC_META_INFO_CAT), _(VLC_META_ARTIST),
594 "%s", psz_value ) == VLC_SUCCESS )
599 else if ( !strcmp( psz_name, "album" ) )
601 if ( vlc_input_item_AddInfo( &(p_item->input),
602 _(VLC_META_INFO_CAT),
603 _(VLC_META_COLLECTION),
604 "%s", psz_value ) == VLC_SUCCESS )
608 } else if ( !strcmp( psz_name, "trackNum" ) )
610 long i_num = atol( psz_value );
612 && vlc_input_item_AddInfo( &(p_item->input),
613 _(VLC_META_INFO_CAT),
615 "%s", psz_value ) == VLC_SUCCESS )
619 } else if ( !strcmp( psz_name, "duration" ) )
621 long i_num = atol( psz_value );
623 && playlist_ItemSetDuration( p_item, i_num*1000 ) == VLC_SUCCESS )
633 * \brief skips complex element content that we can't manage
635 static vlc_bool_t skip_element COMPLEX_INTERFACE
639 while ( xml_ReaderRead( p_xml_reader ) == 1 )
641 if ( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
643 psz_endname = xml_ReaderName( p_xml_reader );
646 if ( !strcmp( psz_element, psz_endname ) )
659 * \brief creates a new playlist item from the given mrl
661 static vlc_bool_t insert_new_item( playlist_t *p_pl, playlist_item_t *p_cur,
662 playlist_item_t **pp_new, char *psz_location )
665 psz_uri = unescape_URI_duplicate( psz_location );
669 *pp_new = playlist_ItemNew( p_pl, psz_uri, NULL );
677 playlist_NodeAddItem( p_pl, *pp_new, p_cur->pp_parents[0]->i_view,
678 p_cur, PLAYLIST_APPEND, PLAYLIST_END );
680 playlist_CopyParents( p_cur, *pp_new );
682 vlc_input_item_CopyOptions( &p_cur->input, &((*pp_new)->input) );