aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs')
-rw-r--r--src/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs238
1 files changed, 238 insertions, 0 deletions
diff --git a/src/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs b/src/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs
new file mode 100644
index 00000000..0954e9de
--- /dev/null
+++ b/src/WixToolset.Data/WindowsInstaller/TableDefinitionCollection.cs
@@ -0,0 +1,238 @@
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
3namespace 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 /// Gets the number of items in the collection.
37 /// </summary>
38 /// <value>Number of items in collection.</value>
39 public int Count
40 {
41 get { return this.collection.Count; }
42 }
43
44 /// <summary>
45 /// Table definition collections are never read-only.
46 /// </summary>
47 public bool IsReadOnly
48 {
49 get { return false; }
50 }
51
52 /// <summary>
53 /// Gets a table definition by name.
54 /// </summary>
55 /// <param name="tableName">Name of table to locate.</param>
56 public TableDefinition this[string tableName]
57 {
58 get
59 {
60 if (!this.collection.TryGetValue(tableName, out var table))
61 {
62 throw new WixMissingTableDefinitionException(WixDataErrors.MissingTableDefinition(tableName));
63 }
64
65 return table;
66 }
67 }
68
69 /// <summary>
70 /// Tries to get a table definition by name.
71 /// </summary>
72 /// <param name="tableName">Name of table to locate.</param>
73 /// <param name="table">Table definition if found.</param>
74 /// <returns>True if table definition was found otherwise false.</returns>
75 public bool TryGet(string tableName, out TableDefinition table)
76 {
77 return this.collection.TryGetValue(tableName, out table);
78 }
79
80 /// <summary>
81 /// Load a table definition collection from an XmlReader.
82 /// </summary>
83 /// <param name="reader">Reader to get data from.</param>
84 /// <param name="suppressSchema">Suppress xml schema validation while loading.</param>
85 /// <returns>The TableDefinitionCollection represented by the xml.</returns>
86 public static TableDefinitionCollection Load(XmlReader reader)
87 {
88 reader.MoveToContent();
89
90 return Read(reader);
91 }
92
93 /// <summary>
94 /// Adds a table definition to the collection.
95 /// </summary>
96 /// <param name="tableDefinition">Table definition to add to the collection.</param>
97 /// <value>Indexes by table definition name.</value>
98 public void Add(TableDefinition tableDefinition)
99 {
100 this.collection.Add(tableDefinition.Name, tableDefinition);
101 }
102
103 /// <summary>
104 /// Removes all table definitions from the collection.
105 /// </summary>
106 public void Clear()
107 {
108 this.collection.Clear();
109 }
110
111 /// <summary>
112 /// Checks if the collection contains a table name.
113 /// </summary>
114 /// <param name="tableName">The table to check in the collection.</param>
115 /// <returns>True if collection contains the table.</returns>
116 public bool Contains(string tableName)
117 {
118 return this.collection.ContainsKey(tableName);
119 }
120
121 /// <summary>
122 /// Checks if the collection contains a table.
123 /// </summary>
124 /// <param name="table">The table to check in the collection.</param>
125 /// <returns>True if collection contains the table.</returns>
126 public bool Contains(TableDefinition table)
127 {
128 return this.collection.ContainsKey(table.Name);
129 }
130
131 /// <summary>
132 /// Copies table definitions to an arry.
133 /// </summary>
134 /// <param name="array">Array to copy the table definitions to.</param>
135 /// <param name="index">Index in the array to start copying at.</param>
136 public void CopyTo(TableDefinition[] array, int index)
137 {
138 this.collection.Values.CopyTo(array, index);
139 }
140
141 /// <summary>
142 /// Removes a table definition from the collection.
143 /// </summary>
144 /// <param name="table">Table to remove from the collection.</param>
145 /// <returns>True if the table definition existed in the collection and was removed.</returns>
146 public bool Remove(TableDefinition table)
147 {
148 return this.collection.Remove(table.Name);
149 }
150
151 /// <summary>
152 /// Gets enumerator for the collection.
153 /// </summary>
154 /// <returns>Enumerator for the collection.</returns>
155 public IEnumerator<TableDefinition> GetEnumerator()
156 {
157 return this.collection.Values.GetEnumerator();
158 }
159
160 /// <summary>
161 /// Gets the untyped enumerator for the collection.
162 /// </summary>
163 /// <returns>Untyped enumerator for the collection.</returns>
164 IEnumerator IEnumerable.GetEnumerator()
165 {
166 return this.collection.Values.GetEnumerator();
167 }
168
169 /// <summary>
170 /// Loads a collection of table definitions from a XmlReader in memory.
171 /// </summary>
172 /// <param name="reader">Reader to get data from.</param>
173 /// <returns>The TableDefinitionCollection represented by the xml.</returns>
174 internal static TableDefinitionCollection Read(XmlReader reader)
175 {
176 if ("tableDefinitions" != reader.LocalName)
177 {
178 throw new XmlException();
179 }
180
181 bool empty = reader.IsEmptyElement;
182 TableDefinitionCollection tableDefinitionCollection = new TableDefinitionCollection();
183
184 while (reader.MoveToNextAttribute())
185 {
186 }
187
188 // parse the child elements
189 if (!empty)
190 {
191 bool done = false;
192
193 while (!done && reader.Read())
194 {
195 switch (reader.NodeType)
196 {
197 case XmlNodeType.Element:
198 switch (reader.LocalName)
199 {
200 case "tableDefinition":
201 tableDefinitionCollection.Add(TableDefinition.Read(reader));
202 break;
203 default:
204 throw new XmlException();
205 }
206 break;
207 case XmlNodeType.EndElement:
208 done = true;
209 break;
210 }
211 }
212
213 if (!done)
214 {
215 throw new XmlException();
216 }
217 }
218
219 return tableDefinitionCollection;
220 }
221
222 /// <summary>
223 /// Persists a TableDefinitionCollection in an XML format.
224 /// </summary>
225 /// <param name="writer">XmlWriter where the TableDefinitionCollection should persist itself as XML.</param>
226 internal void Write(XmlWriter writer)
227 {
228 writer.WriteStartElement("tableDefinitions", XmlNamespaceUri);
229
230 foreach (TableDefinition tableDefinition in this.collection.Values.OrderBy(t => t.Name))
231 {
232 tableDefinition.Write(writer);
233 }
234
235 writer.WriteEndElement();
236 }
237 }
238}