XspfPlaylistPlugin.cxx 5.13 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "config.h"
21
#include "XspfPlaylistPlugin.hxx"
22 23
#include "../PlaylistPlugin.hxx"
#include "../MemorySongEnumerator.hxx"
24
#include "DetachedSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "input/InputStream.hxx"
26
#include "tag/TagBuilder.hxx"
27
#include "util/Error.hxx"
28
#include "util/StringView.hxx"
29
#include "lib/expat/ExpatParser.hxx"
30
#include "Log.hxx"
31 32 33 34

#include <string.h>

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

	/**
	 * 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.
	 */
57
	TagType tag_type;
58 59

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

64 65
	TagBuilder tag_builder;

66
	XspfParser()
67
		:state(ROOT) {}
68 69
};

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

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

		break;

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

		break;

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

		break;

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

		break;

116
	case XspfParser::LOCATION:
117 118 119 120
		break;
	}
}

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

	switch (parser->state) {
127
	case XspfParser::ROOT:
128 129
		break;

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

		break;

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

		break;

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

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

		break;

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

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

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

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

		break;

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

		break;
	}
}

/*
 * The playlist object
 *
 */

191
static SongEnumerator *
192
xspf_open_stream(InputStreamPtr &&is)
193
{
194
	XspfParser parser;
195

196 197 198 199 200 201
	{
		ExpatParser expat(&parser);
		expat.SetElementHandler(xspf_start_element, xspf_end_element);
		expat.SetCharacterDataHandler(xspf_char_data);

		Error error;
202
		if (!expat.Parse(*is, error)) {
203
			LogError(error);
204
			return nullptr;
205 206 207
		}
	}

208
	parser.songs.reverse();
209
	return new MemorySongEnumerator(std::move(parser.songs));
210 211 212 213
}

static const char *const xspf_suffixes[] = {
	"xspf",
214
	nullptr
215 216 217 218
};

static const char *const xspf_mime_types[] = {
	"application/xspf+xml",
219
	nullptr
220 221 222
};

const struct playlist_plugin xspf_playlist_plugin = {
223
	"xspf",
224

225 226 227 228
	nullptr,
	nullptr,
	nullptr,
	xspf_open_stream,
229

230 231 232
	nullptr,
	xspf_suffixes,
	xspf_mime_types,
233
};