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