using System; using System.Collections.Generic; namespace Substrate.Nbt { /// /// A concrete representing a . /// public sealed class SchemaNodeCompound : SchemaNode, ICollection { private List _subnodes; #region ICollection Members /// /// Adds a as a child of this node. /// /// The to add. public void Add (SchemaNode item) { _subnodes.Add(item); } /// /// Removes all objects from the node. /// public void Clear () { _subnodes.Clear(); } /// /// Checks if a is a child of this node. /// /// The to check for existance. /// Status indicating if the exists as a child of this node. public bool Contains (SchemaNode item) { return _subnodes.Contains(item); } /// /// Copies all child objects of this node to a compatible one-dimensional array, starting at the specified index of the target array. /// /// The one-dimensional that is the destination of the subnodes copied. The Array must have zero-based indexing. /// The zero-based index in at which copying begins. public void CopyTo (SchemaNode[] array, int arrayIndex) { _subnodes.CopyTo(array, arrayIndex); } /// /// Gets the number of child objects in this node. /// public int Count { get { return _subnodes.Count; } } /// /// Gets a value indicating whether the node is readonly. /// public bool IsReadOnly { get { return false; } } /// /// Removes the first occurance of a from this node. /// /// The to remove. /// Status indicating whether a was removed. public bool Remove (SchemaNode item) { return _subnodes.Remove(item); } #endregion #region IEnumerable Members /// /// Iterates through all of the objects in this . /// /// An enumerator for this node. public IEnumerator GetEnumerator () { return _subnodes.GetEnumerator(); } #endregion #region IEnumerable Members /// /// Iterates through all of the objects in this . /// /// An enumerator for this node. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () { return _subnodes.GetEnumerator(); } #endregion /// /// Constructs a new representing a root . /// public SchemaNodeCompound () : base("") { _subnodes = new List(); } /// /// Constructs a new with additional options. /// /// One or more option flags modifying the processing of this node. public SchemaNodeCompound (SchemaOptions options) : base("", options) { _subnodes = new List(); } /// /// Constructs a new representing a named . /// /// The name of the corresponding . public SchemaNodeCompound (string name) : base(name) { _subnodes = new List(); } /// /// Constructs a new with additional options. /// /// The name of the corresponding . /// One or more option flags modifying the processing of this node. public SchemaNodeCompound (string name, SchemaOptions options) : base(name, options) { _subnodes = new List(); } /// /// Constructs a new representing a named matching the given schema. /// /// The name of the corresponding . /// A representing a schema to verify against the corresponding . public SchemaNodeCompound (string name, SchemaNode subschema) : base(name) { _subnodes = new List(); SchemaNodeCompound schema = subschema as SchemaNodeCompound; if (schema == null) { return; } foreach (SchemaNode node in schema._subnodes) { _subnodes.Add(node); } } /// /// Constructs a new with additional options. /// /// The name of the corresponding . /// A representing a schema to verify against the corresponding . /// One or more option flags modifying the processing of this node. public SchemaNodeCompound (string name, SchemaNode subschema, SchemaOptions options) : base(name, options) { _subnodes = new List(); SchemaNodeCompound schema = subschema as SchemaNodeCompound; if (schema == null) { return; } foreach (SchemaNode node in schema._subnodes) { _subnodes.Add(node); } } /// /// Copies all the elements of this into . /// /// The destination to copy elements into. /// A reference to . public SchemaNodeCompound MergeInto (SchemaNodeCompound tree) { foreach (SchemaNode node in _subnodes) { SchemaNode f = tree._subnodes.Find(n => n.Name == node.Name); if (f != null) { continue; } tree.Add(node); } return tree; } /// /// Constructs a default satisfying the constraints of this node. /// /// A with a sensible default value. A default child is created for every contained in this . public override TagNode BuildDefaultTree () { TagNodeCompound list = new TagNodeCompound(); foreach (SchemaNode node in _subnodes) { list[node.Name] = node.BuildDefaultTree(); } return list; } } }