blob: 78320f1feeb79f2024aa8391ad746f28760e648c (
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
146
147
148
149
150
151
152
153
154
155
|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PList.cs" company="HandBrake Project (http://handbrake.fr)">
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
// A Helper class to parse plist files.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrake.ApplicationServices.Utilities
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
/// <summary>
/// A Helper class to parse plist files.
/// </summary>
public class PList : Dictionary<string, dynamic>
{
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="PList"/> class.
/// </summary>
public PList()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PList"/> class.
/// </summary>
/// <param name="file">
/// The file.
/// </param>
public PList(string file)
{
this.Load(file);
}
#endregion
#region Public Methods
/// <summary>
/// Load a plist file.
/// </summary>
/// <param name="file">
/// The file name / path
/// </param>
/// <returns>
/// True if successful, false otherwise.
/// </returns>
public bool Load(string file)
{
this.Clear();
XDocument doc = XDocument.Load(file);
XElement plist = doc.Element("plist");
if (plist != null)
{
XElement array = plist.Element("array");
if (array != null)
{
XElement dict = array.Element("dict");
if (dict != null)
{
IEnumerable<XElement> dictElements = dict.Elements();
this.Parse(this, dictElements);
return true;
}
}
}
return false;
}
#endregion
#region Methods
/// <summary>
/// Parse a list of elements
/// </summary>
/// <param name="dict">
/// The dict.
/// </param>
/// <param name="elements">
/// The elements.
/// </param>
private void Parse(PList dict, IEnumerable<XElement> elements)
{
for (int i = 0; i < elements.Count(); i += 2)
{
XElement key = elements.ElementAt(i);
XElement val = elements.ElementAt(i + 1);
dict[key.Value] = this.ParseValue(val);
}
}
/// <summary>
/// The parse array.
/// </summary>
/// <param name="elements">
/// The elements.
/// </param>
/// <returns>
/// The <see cref="List"/>.
/// </returns>
private List<dynamic> ParseArray(IEnumerable<XElement> elements)
{
return elements.Select(e => this.ParseValue(e)).ToList();
}
/// <summary>
/// The parse value.
/// </summary>
/// <param name="val">
/// The XElement.
/// </param>
/// <returns>
/// The parsed value object.
/// </returns>
private dynamic ParseValue(XElement val)
{
switch (val.Name.ToString())
{
case "string":
return val.Value;
case "integer":
return int.Parse(val.Value);
case "real":
return float.Parse(val.Value);
case "true":
return true;
case "false":
return false;
case "dict":
var plist = new PList();
this.Parse(plist, val.Elements());
return plist;
case "array":
List<dynamic> list = this.ParseArray(val.Elements());
return list;
default:
throw new ArgumentException("This plist file is not supported.");
}
}
#endregion
}
}
|