aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Extensions/XmlReaderExtensions.cs
blob: 8e4b7c8594fb483b74ddfda5f8b4da6ef8e884dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Xml;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Entities;

namespace MediaBrowser.Controller.Extensions;

/// <summary>
/// Provides extension methods for <see cref="XmlReader"/> to parse <see cref="BaseItem"/>'s.
/// </summary>
public static class XmlReaderExtensions
{
    /// <summary>
    /// Parses a <see cref="PersonInfo"/> from the xml node.
    /// </summary>
    /// <param name="reader">The <see cref="XmlReader"/>.</param>
    /// <returns>A <see cref="PersonInfo"/>, or <c>null</c> if none is found.</returns>
    public static PersonInfo? GetPersonFromXmlNode(this XmlReader reader)
    {
        ArgumentNullException.ThrowIfNull(reader);

        if (reader.IsEmptyElement)
        {
            reader.Read();
            return null;
        }

        var name = string.Empty;
        var type = PersonKind.Actor;  // If type is not specified assume actor
        var role = string.Empty;
        int? sortOrder = null;
        string? imageUrl = null;

        using var subtree = reader.ReadSubtree();
        subtree.MoveToContent();
        subtree.Read();

        while (subtree is { EOF: false, ReadState: ReadState.Interactive })
        {
            if (subtree.NodeType != XmlNodeType.Element)
            {
                subtree.Read();
                continue;
            }

            switch (subtree.Name)
            {
                case "name":
                case "Name":
                    name = subtree.ReadElementContentAsString();
                    break;
                case "role":
                case "Role":
                    var roleValue = subtree.ReadElementContentAsString();
                    if (!string.IsNullOrWhiteSpace(roleValue))
                    {
                        role = roleValue;
                    }

                    break;
                case "type":
                case "Type":
                    Enum.TryParse(subtree.ReadElementContentAsString(), true, out type);
                    break;
                case "order":
                case "sortorder":
                case "SortOrder":
                    if (int.TryParse(
                        subtree.ReadElementContentAsString(),
                        NumberStyles.Integer,
                        CultureInfo.InvariantCulture,
                        out var intVal))
                    {
                        sortOrder = intVal;
                    }

                    break;
                case "thumb":
                    var thumb = subtree.ReadElementContentAsString();
                    if (!string.IsNullOrWhiteSpace(thumb))
                    {
                        imageUrl = thumb;
                    }

                    break;
                default:
                    subtree.Skip();
                    break;
            }
        }

        if (string.IsNullOrWhiteSpace(name))
        {
            return null;
        }

        return new PersonInfo
        {
            Name = name.Trim(),
            Role = role,
            Type = type,
            SortOrder = sortOrder,
            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 });
}