diff options
| author | Patrick Barron <barronpm@gmail.com> | 2023-10-06 13:40:08 -0400 |
|---|---|---|
| committer | Patrick Barron <barronpm@gmail.com> | 2023-10-06 13:40:08 -0400 |
| commit | 1a6ec2c74062e28552089a8b85dc5d45224d4e86 (patch) | |
| tree | 9b5ec2833ff6830c49e527e2f23c670f67fa3ef7 /MediaBrowser.Controller/Extensions/XmlReaderExtensions.cs | |
| parent | bdca4ed322a6eaa1fa1810e3187aa7948a574c60 (diff) | |
Add GetStringArray and GetPersonArray to XmlReaderExtensions
Diffstat (limited to 'MediaBrowser.Controller/Extensions/XmlReaderExtensions.cs')
| -rw-r--r-- | MediaBrowser.Controller/Extensions/XmlReaderExtensions.cs | 38 |
1 files changed, 38 insertions, 0 deletions
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 }; } + + /// <summary> + /// Used to split names of comma or pipe delimited genres and people. + /// </summary> + /// <param name="reader">The <see cref="XmlReader"/>.</param> + /// <returns>IEnumerable{System.String}.</returns> + public static IEnumerable<string> 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(); + } + } + } + + /// <summary> + /// Parses a <see cref="PersonInfo"/> array from the xml node. + /// </summary> + /// <param name="reader">The <see cref="XmlReader"/>.</param> + /// <param name="personKind">The <see cref="PersonKind"/>.</param> + /// <returns>The <see cref="IEnumerable{PersonInfo}"/>.</returns> + public static IEnumerable<PersonInfo> GetPersonArray(this XmlReader reader, PersonKind personKind) + => reader.GetStringArray() + .Select(part => new PersonInfo { Name = part, Type = personKind }); } |
