added xspf parser
moved files to better namespaces added example playlistfiles codereformat
This commit is contained in:
45
Tv7Playlist.Core/Parsers/Xspf/XspfParser.cs
Normal file
45
Tv7Playlist.Core/Parsers/Xspf/XspfParser.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
using System.Xml.Schema;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace Tv7Playlist.Core.Parsers.Xspf
|
||||
{
|
||||
public class XspfParser : IPlaylistParser
|
||||
{
|
||||
public Task<IReadOnlyCollection<ParsedTrack>> ParseFromStream(Stream stream)
|
||||
{
|
||||
var deserializedList = DeserializePlaylist(stream);
|
||||
|
||||
var tracks = deserializedList.TrackList.Select(t => new ParsedTrack(t.Extension.Id, t.Title, t.Location))
|
||||
.ToList();
|
||||
|
||||
return Task.FromResult((IReadOnlyCollection<ParsedTrack>) tracks);
|
||||
}
|
||||
|
||||
private static Playlist DeserializePlaylist(Stream stream)
|
||||
{
|
||||
var serializer = new XmlSerializer(typeof(Playlist));
|
||||
var xmlReaderSettings = GetXmlReaderSettings();
|
||||
|
||||
using (var reader = XmlReader.Create(stream, xmlReaderSettings))
|
||||
{
|
||||
var deserializedList = (Playlist) serializer.Deserialize(reader);
|
||||
return deserializedList;
|
||||
}
|
||||
}
|
||||
|
||||
private static XmlReaderSettings GetXmlReaderSettings()
|
||||
{
|
||||
var xmlReaderSettings = new XmlReaderSettings
|
||||
{
|
||||
IgnoreProcessingInstructions = true,
|
||||
ValidationFlags = XmlSchemaValidationFlags.None
|
||||
};
|
||||
return xmlReaderSettings;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user