// 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.
namespace WixToolset.Data
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
///
/// Collection for table definitions indexed by table name.
///
public sealed class TableDefinitionCollection : ICollection
{
public const string XmlNamespaceUri = "http://wixtoolset.org/schemas/v4/wi/tables";
private Dictionary collection;
///
/// Instantiate a new TableDefinitionCollection class.
///
public TableDefinitionCollection()
{
this.collection = new Dictionary();
}
///
/// Creates a shallow copy of the provided table definition collection.
///
public TableDefinitionCollection(TableDefinitionCollection tableDefinitions)
{
this.collection = new Dictionary(tableDefinitions.collection);
}
///
/// Gets the number of items in the collection.
///
/// Number of items in collection.
public int Count
{
get { return this.collection.Count; }
}
///
/// Table definition collections are never read-only.
///
public bool IsReadOnly
{
get { return false; }
}
///
/// Gets a table definition by name.
///
/// Name of table to locate.
public TableDefinition this[string tableName]
{
get
{
TableDefinition table;
if (!this.collection.TryGetValue(tableName, out table))
{
throw new WixMissingTableDefinitionException(WixDataErrors.MissingTableDefinition(tableName));
}
return table;
}
}
///
/// Tries to get a table definition by name.
///
/// Name of table to locate.
/// Table definition if found.
/// True if table definition was found otherwise false.
public bool TryGet(string tableName, out TableDefinition table)
{
return this.collection.TryGetValue(tableName, out table);
}
///
/// Load a table definition collection from an XmlReader.
///
/// Reader to get data from.
/// Suppress xml schema validation while loading.
/// The TableDefinitionCollection represented by the xml.
public static TableDefinitionCollection Load(XmlReader reader)
{
reader.MoveToContent();
return Read(reader);
}
///
/// Adds a table definition to the collection.
///
/// Table definition to add to the collection.
/// Indexes by table definition name.
public void Add(TableDefinition tableDefinition)
{
this.collection.Add(tableDefinition.Name, tableDefinition);
}
///
/// Removes all table definitions from the collection.
///
public void Clear()
{
this.collection.Clear();
}
///
/// Checks if the collection contains a table name.
///
/// The table to check in the collection.
/// True if collection contains the table.
public bool Contains(string tableName)
{
return this.collection.ContainsKey(tableName);
}
///
/// Checks if the collection contains a table.
///
/// The table to check in the collection.
/// True if collection contains the table.
public bool Contains(TableDefinition table)
{
return this.collection.ContainsKey(table.Name);
}
///
/// Copies table definitions to an arry.
///
/// Array to copy the table definitions to.
/// Index in the array to start copying at.
public void CopyTo(TableDefinition[] array, int index)
{
this.collection.Values.CopyTo(array, index);
}
///
/// Removes a table definition from the collection.
///
/// Table to remove from the collection.
/// True if the table definition existed in the collection and was removed.
public bool Remove(TableDefinition table)
{
return this.collection.Remove(table.Name);
}
///
/// Gets enumerator for the collection.
///
/// Enumerator for the collection.
public IEnumerator GetEnumerator()
{
return this.collection.Values.GetEnumerator();
}
///
/// Gets the untyped enumerator for the collection.
///
/// Untyped enumerator for the collection.
IEnumerator IEnumerable.GetEnumerator()
{
return this.collection.Values.GetEnumerator();
}
///
/// Loads a collection of table definitions from a XmlReader in memory.
///
/// Reader to get data from.
/// The TableDefinitionCollection represented by the xml.
internal static TableDefinitionCollection Read(XmlReader reader)
{
if ("tableDefinitions" != reader.LocalName)
{
throw new XmlException();
}
bool empty = reader.IsEmptyElement;
TableDefinitionCollection tableDefinitionCollection = new TableDefinitionCollection();
while (reader.MoveToNextAttribute())
{
}
// parse the child elements
if (!empty)
{
bool done = false;
while (!done && reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
switch (reader.LocalName)
{
case "tableDefinition":
tableDefinitionCollection.Add(TableDefinition.Read(reader));
break;
default:
throw new XmlException();
}
break;
case XmlNodeType.EndElement:
done = true;
break;
}
}
if (!done)
{
throw new XmlException();
}
}
return tableDefinitionCollection;
}
///
/// Persists a TableDefinitionCollection in an XML format.
///
/// XmlWriter where the TableDefinitionCollection should persist itself as XML.
internal void Write(XmlWriter writer)
{
writer.WriteStartElement("tableDefinitions", XmlNamespaceUri);
foreach (TableDefinition tableDefinition in this.collection.Values.OrderBy(t => t.Name))
{
tableDefinition.Write(writer);
}
writer.WriteEndElement();
}
}
}