XspfPlaylistPlugin.cxx 5.04 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "XspfPlaylistPlugin.hxx"
21 22
#include "../PlaylistPlugin.hxx"
#include "../MemorySongEnumerator.hxx"
23
#include "song/DetachedSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "input/InputStream.hxx"
25
#include "tag/Builder.hxx"
26
#include "util/StringView.hxx"
27
#include "lib/expat/ExpatParser.hxx"
28
#include "Log.hxx"
29 30 31 32

#include <string.h>

/**
33
 * This is the state object for our XML parser.
34
 */
35
struct XspfParser {
36 37 38 39
	/**
	 * The list of songs (in reverse order because that's faster
	 * while adding).
	 */
40
	std::forward_list<DetachedSong> songs;
41 42 43 44 45 46 47 48 49 50 51 52 53 54

	/**
	 * The current position in the XML file.
	 */
	enum {
		ROOT, PLAYLIST, TRACKLIST, TRACK,
		LOCATION,
	} state;

	/**
	 * The current tag within the "track" element.  This is only
	 * valid if state==TRACK.  TAG_NUM_OF_ITEM_TYPES means there
	 * is no (known) tag.
	 */
55
	TagType tag_type;
56 57

	/**
58
	 * The current song URI.  It is set by the "location" element.
59
	 */
60
	std::string location;
61

62 63
	TagBuilder tag_builder;

64
	XspfParser()
65
		:state(ROOT) {}
66 67
};

68 69 70
static void XMLCALL
xspf_start_element(void *user_data, const XML_Char *element_name,
		   gcc_unused const XML_Char **atts)
71
{
72
	XspfParser *parser = (XspfParser *)user_data;
73 74

	switch (parser->state) {
75
	case XspfParser::ROOT:
76
		if (strcmp(element_name, "playlist") == 0)
77
			parser->state = XspfParser::PLAYLIST;
78 79 80

		break;

81
	case XspfParser::PLAYLIST:
82
		if (strcmp(element_name, "trackList") == 0)
83
			parser->state = XspfParser::TRACKLIST;
84 85 86

		break;

87
	case XspfParser::TRACKLIST:
88
		if (strcmp(element_name, "track") == 0) {
89
			parser->state = XspfParser::TRACK;
90
			parser->location.clear();
91
			parser->tag_type = TAG_NUM_OF_ITEM_TYPES;
92 93 94 95
		}

		break;

96
	case XspfParser::TRACK:
97
		if (strcmp(element_name, "location") == 0)
98
			parser->state = XspfParser::LOCATION;
99
		else if (strcmp(element_name, "title") == 0)
100
			parser->tag_type = TAG_TITLE;
101 102 103
		else if (strcmp(element_name, "creator") == 0)
			/* TAG_COMPOSER would be more correct
			   according to the XSPF spec */
104
			parser->tag_type = TAG_ARTIST;
105
		else if (strcmp(element_name, "annotation") == 0)
106
			parser->tag_type = TAG_COMMENT;
107
		else if (strcmp(element_name, "album") == 0)
108
			parser->tag_type = TAG_ALBUM;
109
		else if (strcmp(element_name, "trackNum") == 0)
110
			parser->tag_type = TAG_TRACK;
111 112 113

		break;

114
	case XspfParser::LOCATION:
115 116 117 118
		break;
	}
}

119 120
static void XMLCALL
xspf_end_element(void *user_data, const XML_Char *element_name)
121
{
122
	XspfParser *parser = (XspfParser *)user_data;
123 124

	switch (parser->state) {
125
	case XspfParser::ROOT:
126 127
		break;

128
	case XspfParser::PLAYLIST:
129
		if (strcmp(element_name, "playlist") == 0)
130
			parser->state = XspfParser::ROOT;
131 132 133

		break;

134
	case XspfParser::TRACKLIST:
135
		if (strcmp(element_name, "tracklist") == 0)
136
			parser->state = XspfParser::PLAYLIST;
137 138 139

		break;

140
	case XspfParser::TRACK:
141
		if (strcmp(element_name, "track") == 0) {
142 143 144
			if (!parser->location.empty())
				parser->songs.emplace_front(std::move(parser->location),
							    parser->tag_builder.Commit());
145

146
			parser->state = XspfParser::TRACKLIST;
147
		} else
148
			parser->tag_type = TAG_NUM_OF_ITEM_TYPES;
149 150 151

		break;

152 153
	case XspfParser::LOCATION:
		parser->state = XspfParser::TRACK;
154 155 156 157
		break;
	}
}

158 159
static void XMLCALL
xspf_char_data(void *user_data, const XML_Char *s, int len)
160
{
161
	XspfParser *parser = (XspfParser *)user_data;
162 163

	switch (parser->state) {
164 165 166
	case XspfParser::ROOT:
	case XspfParser::PLAYLIST:
	case XspfParser::TRACKLIST:
167 168
		break;

169
	case XspfParser::TRACK:
170
		if (!parser->location.empty() &&
171
		    parser->tag_type != TAG_NUM_OF_ITEM_TYPES)
172 173
			parser->tag_builder.AddItem(parser->tag_type,
						    StringView(s, len));
174 175 176

		break;

177
	case XspfParser::LOCATION:
178
		parser->location.assign(s, len);
179 180 181 182 183 184 185 186 187 188

		break;
	}
}

/*
 * The playlist object
 *
 */

189
static std::unique_ptr<SongEnumerator>
190
xspf_open_stream(InputStreamPtr &&is)
191
{
192
	XspfParser parser;
193

194 195 196 197
	{
		ExpatParser expat(&parser);
		expat.SetElementHandler(xspf_start_element, xspf_end_element);
		expat.SetCharacterDataHandler(xspf_char_data);
198
		expat.Parse(*is);
199 200
	}

201
	parser.songs.reverse();
202
	return std::make_unique<MemorySongEnumerator>(std::move(parser.songs));
203 204 205 206
}

static const char *const xspf_suffixes[] = {
	"xspf",
207
	nullptr
208 209 210 211
};

static const char *const xspf_mime_types[] = {
	"application/xspf+xml",
212
	nullptr
213 214 215
};

const struct playlist_plugin xspf_playlist_plugin = {
216
	"xspf",
217

218 219 220 221
	nullptr,
	nullptr,
	nullptr,
	xspf_open_stream,
222

223 224 225
	nullptr,
	xspf_suffixes,
	xspf_mime_types,
226
};