From 1a6ec2c74062e28552089a8b85dc5d45224d4e86 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Fri, 6 Oct 2023 13:40:08 -0400 Subject: Add GetStringArray and GetPersonArray to XmlReaderExtensions --- .../Extensions/XmlReaderExtensions.cs | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'MediaBrowser.Controller/Extensions/XmlReaderExtensions.cs') diff --git a/MediaBrowser.Controller/Extensions/XmlReaderExtensions.cs b/MediaBrowser.Controller/Extensions/XmlReaderExtensions.cs index cd7db91dd..8e4b7c859 100644 --- a/MediaBrowser.Controller/Extensions/XmlReaderExtensions.cs +++ b/MediaBrowser.Controller/Extensions/XmlReaderExtensions.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Globalization; +using System.Linq; using System.Xml; using Jellyfin.Data.Enums; using MediaBrowser.Controller.Entities; @@ -104,4 +106,40 @@ public static class XmlReaderExtensions ImageUrl = imageUrl }; } + + /// + /// Used to split names of comma or pipe delimited genres and people. + /// + /// The . + /// IEnumerable{System.String}. + public static IEnumerable GetStringArray(this XmlReader reader) + { + ArgumentNullException.ThrowIfNull(reader); + var value = reader.ReadElementContentAsString(); + + // Only split by comma if there is no pipe in the string + // We have to be careful to not split names like Matthew, Jr. + var separator = !value.Contains('|', StringComparison.Ordinal) + && !value.Contains(';', StringComparison.Ordinal) + ? new[] { ',' } + : new[] { '|', ';' }; + + foreach (var part in value.Trim().Trim(separator).Split(separator)) + { + if (!string.IsNullOrWhiteSpace(part)) + { + yield return part.Trim(); + } + } + } + + /// + /// Parses a array from the xml node. + /// + /// The . + /// The . + /// The . + public static IEnumerable GetPersonArray(this XmlReader reader, PersonKind personKind) + => reader.GetStringArray() + .Select(part => new PersonInfo { Name = part, Type = personKind }); } -- cgit v1.2.3