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"
34 playlist_item_t *p_item_in_category;
39 * \brief XSPF submodule initialization function
41 int E_(xspf_import_Activate)( vlc_object_t *p_this )
43 demux_t *p_demux = (demux_t *)p_this;
46 psz_ext = strrchr ( p_demux->psz_path, '.' );
48 if( ( psz_ext && !strcasecmp( psz_ext, ".xspf") ) ||
49 ( p_demux->psz_demux && !strcmp(p_demux->psz_demux, "xspf-open") ) )
57 msg_Dbg( p_demux, "using xspf playlist import");
59 p_demux->p_sys = (demux_sys_t*)malloc(sizeof(demux_sys_t ) );
61 p_demux->pf_control = xspf_import_Control;
62 p_demux->pf_demux = xspf_import_Demux;
68 * \brief demuxer function for XSPF parsing
70 int xspf_import_Demux( demux_t *p_demux )
72 int i_ret = VLC_SUCCESS;
74 xml_reader_t *p_xml_reader = NULL;
75 char *psz_name = NULL;
77 p_demux->p_sys->p_item_in_category = p_item_in_category;
78 p_demux->p_sys->i_parent_id = i_parent_id;
80 /* create new xml parser from stream */
81 p_xml = xml_Create( p_demux );
86 p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
91 /* start with parsing the root node */
92 if ( i_ret == VLC_SUCCESS )
93 if ( xml_ReaderRead( p_xml_reader ) != 1 )
95 msg_Err( p_demux, "can't read xml stream" );
98 /* checking root node type */
99 if ( i_ret == VLC_SUCCESS )
100 if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM )
102 msg_Err( p_demux, "invalid root node type: %i",
103 xml_ReaderNodeType( p_xml_reader ) );
104 i_ret = VLC_EGENERIC;
106 /* checking root node name */
107 if ( i_ret == VLC_SUCCESS )
108 psz_name = xml_ReaderName( p_xml_reader );
109 if ( !psz_name || strcmp( psz_name, "playlist" ) )
111 msg_Err( p_demux, "invalid root node name: %s", psz_name );
112 i_ret = VLC_EGENERIC;
116 i_ret = parse_playlist_node( p_demux, p_playlist, p_current, NULL,
117 p_xml_reader, "playlist" );
118 HANDLE_PLAY_AND_RELEASE;
120 xml_ReaderDelete( p_xml, p_xml_reader );
128 /** \brief dummy function for demux callback interface */
129 int xspf_import_Control( demux_t *p_demux, int i_query, va_list args )
135 * \brief parse the root node of a XSPF playlist
136 * \param p_demux demuxer instance
137 * \param p_playlist playlist instance
138 * \param p_item current playlist node
139 * \param p_xml_reader xml reader instance
140 * \param psz_element name of element to parse
142 static vlc_bool_t parse_playlist_node COMPLEX_INTERFACE
145 char *psz_value=NULL;
146 vlc_bool_t b_version_found = VLC_FALSE;
148 xml_elem_hnd_t *p_handler=NULL;
150 xml_elem_hnd_t pl_elements[] =
151 { {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
152 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
153 {"annotation", SIMPLE_CONTENT, {NULL} },
154 {"info", SIMPLE_CONTENT, {NULL} },
155 {"location", SIMPLE_CONTENT, {NULL} },
156 {"identifier", SIMPLE_CONTENT, {NULL} },
157 {"image", SIMPLE_CONTENT, {NULL} },
158 {"date", SIMPLE_CONTENT, {NULL} },
159 {"license", SIMPLE_CONTENT, {NULL} },
160 {"attribution", COMPLEX_CONTENT, {.cmplx = skip_element} },
161 {"link", SIMPLE_CONTENT, {NULL} },
162 {"meta", SIMPLE_CONTENT, {NULL} },
163 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
164 {"trackList", COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
165 {NULL, UNKNOWN_CONTENT, {NULL} }
168 /* read all playlist attributes */
169 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
171 psz_name = xml_ReaderName ( p_xml_reader );
172 psz_value = xml_ReaderValue ( p_xml_reader );
173 if ( !psz_name || !psz_value )
175 msg_Err( p_demux, "invalid xml stream @ <playlist>" );
179 /* attribute: version */
180 if ( !strcmp( psz_name, "version" ) )
182 b_version_found = VLC_TRUE;
183 if ( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
184 msg_Warn( p_demux, "unsupported XSPF version" );
186 /* attribute: xmlns */
187 else if ( !strcmp ( psz_name, "xmlns" ) )
189 /* unknown attribute */
191 msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
195 /* attribute version is mandatory !!! */
196 if ( !b_version_found )
197 msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
199 /* parse the child elements - we only take care of <trackList> */
200 while ( xml_ReaderRead( p_xml_reader ) == 1 )
202 i_node = xml_ReaderNodeType( p_xml_reader );
205 case XML_READER_NONE:
207 case XML_READER_STARTELEM:
208 /* element start tag */
209 psz_name = xml_ReaderName( p_xml_reader );
210 if ( !psz_name || !*psz_name )
212 msg_Err( p_demux, "invalid xml stream" );
217 for( p_handler = pl_elements;
218 p_handler->name && strcmp( psz_name, p_handler->name );
220 if ( !p_handler->name )
222 msg_Err( p_demux, "unexpected element <%s>", psz_name );
227 /* complex content is parsed in a separate function */
228 if ( p_handler->type == COMPLEX_CONTENT )
230 if ( p_handler->pf_handler.cmplx( p_demux,
247 case XML_READER_TEXT:
248 /* simple element content */
250 psz_value = xml_ReaderValue( p_xml_reader );
253 msg_Err( p_demux, "invalid xml stream" );
259 case XML_READER_ENDELEM:
260 /* element end tag */
261 psz_name = xml_ReaderName( p_xml_reader );
264 msg_Err( p_demux, "invalid xml stream" );
268 /* leave if the current parent node <playlist> is terminated */
269 if ( !strcmp( psz_name, psz_element ) )
274 /* there MUST have been a start tag for that element name */
275 if ( !p_handler || !p_handler->name
276 || strcmp( p_handler->name, psz_name ))
278 msg_Err( p_demux, "there's no open element left for <%s>",
284 if ( p_handler->pf_handler.smpl )
286 p_handler->pf_handler.smpl( p_item, NULL, p_handler->name,
294 /* unknown/unexpected xml node */
295 msg_Err( p_demux, "unexpected xml node %i", i_node );
305 * \brief parses the tracklist node which only may contain <track>s
307 static vlc_bool_t parse_tracklist_node COMPLEX_INTERFACE
313 /* now parse the <track>s */
314 while ( xml_ReaderRead( p_xml_reader ) == 1 )
316 i_node = xml_ReaderNodeType( p_xml_reader );
317 if ( i_node == XML_READER_STARTELEM )
319 psz_name = xml_ReaderName( p_xml_reader );
322 msg_Err( p_demux, "unexpected end of xml data" );
326 if ( strcmp( psz_name, "track") )
328 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
335 /* parse the track data in a separate function */
336 if ( parse_track_node( p_demux, p_playlist, p_item, NULL,
337 p_xml_reader,"track" ) == VLC_TRUE )
340 else if ( i_node == XML_READER_ENDELEM )
344 /* the <trackList> has to be terminated */
345 if ( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
347 msg_Err( p_demux, "there's a missing </trackList>" );
351 psz_name = xml_ReaderName( p_xml_reader );
352 if ( !psz_name || strcmp( psz_name, "trackList" ) )
354 msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
360 msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
366 * \brief parse one track element
367 * \param COMPLEX_INTERFACE
369 static vlc_bool_t parse_track_node COMPLEX_INTERFACE
371 input_item_t *p_new_input = NULL;
374 char *psz_value=NULL;
375 xml_elem_hnd_t *p_handler=NULL;
377 xml_elem_hnd_t track_elements[] =
378 { {"location", SIMPLE_CONTENT, {NULL} },
379 {"identifier", SIMPLE_CONTENT, {NULL} },
380 {"title", SIMPLE_CONTENT, {.smpl = set_item_info} },
381 {"creator", SIMPLE_CONTENT, {.smpl = set_item_info} },
382 {"annotation", SIMPLE_CONTENT, {NULL} },
383 {"info", SIMPLE_CONTENT, {NULL} },
384 {"image", SIMPLE_CONTENT, {NULL} },
385 {"album", SIMPLE_CONTENT, {.smpl = set_item_info} },
386 {"trackNum", SIMPLE_CONTENT, {.smpl = set_item_info} },
387 {"duration", SIMPLE_CONTENT, {.smpl = set_item_info} },
388 {"link", SIMPLE_CONTENT, {NULL} },
389 {"meta", SIMPLE_CONTENT, {NULL} },
390 {"extension", COMPLEX_CONTENT, {.cmplx = skip_element} },
391 {NULL, UNKNOWN_CONTENT, {NULL} }
394 while ( xml_ReaderRead( p_xml_reader ) == 1 )
396 i_node = xml_ReaderNodeType( p_xml_reader );
399 case XML_READER_NONE:
402 case XML_READER_STARTELEM:
403 /* element start tag */
404 psz_name = xml_ReaderName( p_xml_reader );
405 if ( !psz_name || !*psz_name )
407 msg_Err( p_demux, "invalid xml stream" );
412 for( p_handler = track_elements;
413 p_handler->name && strcmp( psz_name, p_handler->name );
415 if ( !p_handler->name )
417 msg_Err( p_demux, "unexpected element <%s>", psz_name );
422 /* complex content is parsed in a separate function */
423 if ( p_handler->type == COMPLEX_CONTENT )
428 "at <%s> level no new item has been allocated",
433 if ( p_handler->pf_handler.cmplx( p_demux,
450 case XML_READER_TEXT:
451 /* simple element content */
453 psz_value = xml_ReaderValue( p_xml_reader );
456 msg_Err( p_demux, "invalid xml stream" );
462 case XML_READER_ENDELEM:
463 /* element end tag */
464 psz_name = xml_ReaderName( p_xml_reader );
467 msg_Err( p_demux, "invalid xml stream" );
471 /* leave if the current parent node <track> is terminated */
472 if ( !strcmp( psz_name, psz_element ) )
476 playlist_AddWhereverNeeded( p_playlist, p_new_input,
477 p_item, p_demux->p_sys->p_item_in_category,
478 (p_demux->p_sys->i_parent_id >0 ) ? VLC_TRUE:
479 VLC_FALSE, PLAYLIST_APPEND );
482 /* there MUST have been a start tag for that element name */
483 if ( !p_handler || !p_handler->name
484 || strcmp( p_handler->name, psz_name ))
486 msg_Err( p_demux, "there's no open element left for <%s>",
492 /* special case: location */
493 if ( !strcmp( p_handler->name, "location" ) )
496 /* there MUST NOT be an item */
499 msg_Err( p_demux, "item <%s> already created",
504 psz_uri = unescape_URI_duplicate( psz_value );
508 p_new_input = input_ItemNewExt( p_playlist, psz_uri,
510 p_new_input->p_meta = vlc_meta_New();
512 vlc_input_item_CopyOptions( p_item->p_input, p_new_input );
525 /* there MUST be an item */
528 msg_Err( p_demux, "item not yet created at <%s>",
533 if ( p_handler->pf_handler.smpl )
535 p_handler->pf_handler.smpl( NULL, p_input,
546 /* unknown/unexpected xml node */
547 msg_Err( p_demux, "unexpected xml node %i", i_node );
553 msg_Err( p_demux, "unexpected end of xml data" );
559 * \brief handles the supported <track> sub-elements
561 static vlc_bool_t set_item_info SIMPLE_INTERFACE
563 /* exit if setting is impossible */
564 if ( !psz_name || !psz_value || !p_input )
567 /* re-convert xml special characters inside psz_value */
568 resolve_xml_special_chars ( psz_value );
570 /* handle each info element in a separate "if" clause */
571 if ( !strcmp( psz_name, "title" ) )
573 p_input->psz_name = strdup( (char*)psz_value );
575 else if ( !strcmp( psz_name, "creator" ) )
577 vlc_meta_SetArtist( p_input->p_meta, psz_value );
579 else if ( !strcmp( psz_name, "album" ) )
581 vlc_meta_SetAlbum( p_input->p_meta, psz_value );
584 else if ( !strcmp( psz_name, "trackNum" ) )
586 vlc_meta_SetTracknum( p_input->p_meta, psz_value );
588 else if ( !strcmp( psz_name, "duration" ) )
590 long i_num = atol( psz_value );
591 p_input->i_duration = i_num*1000;
597 * \brief skips complex element content that we can't manage
599 static vlc_bool_t skip_element COMPLEX_INTERFACE
603 while ( xml_ReaderRead( p_xml_reader ) == 1 )
605 if ( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
607 psz_endname = xml_ReaderName( p_xml_reader );
610 if ( !strcmp( psz_element, psz_endname ) )