diff options
Diffstat (limited to 'src/api/wix/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs')
-rw-r--r-- | src/api/wix/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/src/api/wix/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs b/src/api/wix/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs new file mode 100644 index 00000000..fcc2b1f6 --- /dev/null +++ b/src/api/wix/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs | |||
@@ -0,0 +1,201 @@ | |||
1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
2 | |||
3 | namespace WixToolset.Data.WindowsInstaller | ||
4 | { | ||
5 | using System.Collections; | ||
6 | using System.Collections.Generic; | ||
7 | using System.Linq; | ||
8 | using System.Xml; | ||
9 | |||
10 | /// <summary> | ||
11 | /// Collection for table definitions indexed by table name. | ||
12 | /// </summary> | ||
13 | public sealed class TableDefinitionCollection : ICollection<TableDefinition> | ||
14 | { | ||
15 | public const string XmlNamespaceUri = "http://wixtoolset.org/schemas/v4/wi/tables"; | ||
16 | |||
17 | private Dictionary<string, TableDefinition> collection; | ||
18 | |||
19 | /// <summary> | ||
20 | /// Instantiate a new TableDefinitionCollection class. | ||
21 | /// </summary> | ||
22 | public TableDefinitionCollection() | ||
23 | { | ||
24 | this.collection = new Dictionary<string, TableDefinition>(); | ||
25 | } | ||
26 | |||
27 | /// <summary> | ||
28 | /// Creates a shallow copy of the provided table definition collection. | ||
29 | /// </summary> | ||
30 | public TableDefinitionCollection(TableDefinitionCollection tableDefinitions) | ||
31 | { | ||
32 | this.collection = new Dictionary<string, TableDefinition>(tableDefinitions.collection); | ||
33 | } | ||
34 | |||
35 | /// <summary> | ||
36 | /// Creates a table definition collection with the given table definitions. | ||
37 | /// </summary> | ||
38 | public TableDefinitionCollection(IEnumerable<TableDefinition> tableDefinitions) | ||
39 | { | ||
40 | this.collection = tableDefinitions.ToDictionary(t => t.Name); | ||
41 | } | ||
42 | |||
43 | /// <summary> | ||
44 | /// Gets the number of items in the collection. | ||
45 | /// </summary> | ||
46 | /// <value>Number of items in collection.</value> | ||
47 | public int Count => this.collection.Count; | ||
48 | |||
49 | /// <summary> | ||
50 | /// Table definition collections are never read-only. | ||
51 | /// </summary> | ||
52 | public bool IsReadOnly => false; | ||
53 | |||
54 | /// <summary> | ||
55 | /// Gets a table definition by name. | ||
56 | /// </summary> | ||
57 | /// <param name="tableName">Name of table to locate.</param> | ||
58 | public TableDefinition this[string tableName] | ||
59 | { | ||
60 | get | ||
61 | { | ||
62 | if (!this.collection.TryGetValue(tableName, out var table)) | ||
63 | { | ||
64 | throw new WixMissingTableDefinitionException(ErrorMessages.MissingTableDefinition(tableName)); | ||
65 | } | ||
66 | |||
67 | return table; | ||
68 | } | ||
69 | } | ||
70 | |||
71 | /// <summary> | ||
72 | /// Tries to get a table definition by name. | ||
73 | /// </summary> | ||
74 | /// <param name="tableName">Name of table to locate.</param> | ||
75 | /// <param name="table">Table definition if found.</param> | ||
76 | /// <returns>True if table definition was found otherwise false.</returns> | ||
77 | public bool TryGet(string tableName, out TableDefinition table) => this.collection.TryGetValue(tableName, out table); | ||
78 | |||
79 | /// <summary> | ||
80 | /// Adds a table definition to the collection. | ||
81 | /// </summary> | ||
82 | /// <param name="tableDefinition">Table definition to add to the collection.</param> | ||
83 | /// <value>Indexes by table definition name.</value> | ||
84 | public void Add(TableDefinition tableDefinition) => this.collection.Add(tableDefinition.Name, tableDefinition); | ||
85 | |||
86 | /// <summary> | ||
87 | /// Removes all table definitions from the collection. | ||
88 | /// </summary> | ||
89 | public void Clear() => this.collection.Clear(); | ||
90 | |||
91 | /// <summary> | ||
92 | /// Checks if the collection contains a table name. | ||
93 | /// </summary> | ||
94 | /// <param name="tableName">The table to check in the collection.</param> | ||
95 | /// <returns>True if collection contains the table.</returns> | ||
96 | public bool Contains(string tableName) => this.collection.ContainsKey(tableName); | ||
97 | |||
98 | /// <summary> | ||
99 | /// Checks if the collection contains a table. | ||
100 | /// </summary> | ||
101 | /// <param name="table">The table to check in the collection.</param> | ||
102 | /// <returns>True if collection contains the table.</returns> | ||
103 | public bool Contains(TableDefinition table) => this.collection.ContainsKey(table.Name); | ||
104 | |||
105 | /// <summary> | ||
106 | /// Copies table definitions to an arry. | ||
107 | /// </summary> | ||
108 | /// <param name="array">Array to copy the table definitions to.</param> | ||
109 | /// <param name="index">Index in the array to start copying at.</param> | ||
110 | public void CopyTo(TableDefinition[] array, int index) => this.collection.Values.CopyTo(array, index); | ||
111 | |||
112 | /// <summary> | ||
113 | /// Removes a table definition from the collection. | ||
114 | /// </summary> | ||
115 | /// <param name="table">Table to remove from the collection.</param> | ||
116 | /// <returns>True if the table definition existed in the collection and was removed.</returns> | ||
117 | public bool Remove(TableDefinition table) => this.collection.Remove(table.Name); | ||
118 | |||
119 | /// <summary> | ||
120 | /// Gets enumerator for the collection. | ||
121 | /// </summary> | ||
122 | /// <returns>Enumerator for the collection.</returns> | ||
123 | public IEnumerator<TableDefinition> GetEnumerator() => this.collection.Values.GetEnumerator(); | ||
124 | |||
125 | /// <summary> | ||
126 | /// Gets the untyped enumerator for the collection. | ||
127 | /// </summary> | ||
128 | /// <returns>Untyped enumerator for the collection.</returns> | ||
129 | IEnumerator IEnumerable.GetEnumerator() => this.collection.Values.GetEnumerator(); | ||
130 | |||
131 | /// <summary> | ||
132 | /// Loads a collection of table definitions from a XmlReader in memory. | ||
133 | /// </summary> | ||
134 | /// <param name="reader">Reader to get data from.</param> | ||
135 | /// <param name="tableDefinitions">Table definitions to use for strongly-typed rows.</param> | ||
136 | /// <returns>The TableDefinitionCollection represented by the xml.</returns> | ||
137 | internal static TableDefinitionCollection Read(XmlReader reader, TableDefinitionCollection tableDefinitions) | ||
138 | { | ||
139 | if ("tableDefinitions" != reader.LocalName) | ||
140 | { | ||
141 | throw new XmlException(); | ||
142 | } | ||
143 | |||
144 | var empty = reader.IsEmptyElement; | ||
145 | var tableDefinitionCollection = new TableDefinitionCollection(); | ||
146 | |||
147 | while (reader.MoveToNextAttribute()) | ||
148 | { | ||
149 | } | ||
150 | |||
151 | // parse the child elements | ||
152 | if (!empty) | ||
153 | { | ||
154 | var done = false; | ||
155 | |||
156 | while (!done && reader.Read()) | ||
157 | { | ||
158 | switch (reader.NodeType) | ||
159 | { | ||
160 | case XmlNodeType.Element: | ||
161 | switch (reader.LocalName) | ||
162 | { | ||
163 | case "tableDefinition": | ||
164 | tableDefinitionCollection.Add(TableDefinition.Read(reader, tableDefinitions)); | ||
165 | break; | ||
166 | default: | ||
167 | throw new XmlException(); | ||
168 | } | ||
169 | break; | ||
170 | case XmlNodeType.EndElement: | ||
171 | done = true; | ||
172 | break; | ||
173 | } | ||
174 | } | ||
175 | |||
176 | if (!done) | ||
177 | { | ||
178 | throw new XmlException(); | ||
179 | } | ||
180 | } | ||
181 | |||
182 | return tableDefinitionCollection; | ||
183 | } | ||
184 | |||
185 | /// <summary> | ||
186 | /// Persists a TableDefinitionCollection in an XML format. | ||
187 | /// </summary> | ||
188 | /// <param name="writer">XmlWriter where the TableDefinitionCollection should persist itself as XML.</param> | ||
189 | internal void Write(XmlWriter writer) | ||
190 | { | ||
191 | writer.WriteStartElement("tableDefinitions", XmlNamespaceUri); | ||
192 | |||
193 | foreach (var tableDefinition in this.collection.Values.OrderBy(t => t.Name)) | ||
194 | { | ||
195 | tableDefinition.Write(writer); | ||
196 | } | ||
197 | |||
198 | writer.WriteEndElement(); | ||
199 | } | ||
200 | } | ||
201 | } | ||