using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Svg { /// /// Provides methods to ensure element ID's are valid and unique. /// public class SvgElementIdManager { private SvgDocument _document; private Dictionary _idValueMap; /// /// Retrieves the with the specified ID. /// /// A containing the ID of the element to find. /// An of one exists with the specified ID; otherwise false. public virtual SvgElement GetElementById(string id) { if (id.StartsWith("#")) id = id.Substring(1); return this._idValueMap[id]; } public virtual SvgElement GetElementById(Uri uri) { return this.GetElementById(uri.ToString()); } /// /// Adds the specified for ID management. /// /// The to be managed. public virtual void Add(SvgElement element) { if (!string.IsNullOrEmpty(element.ID)) { this.EnsureValidId(element.ID); this._idValueMap.Add(element.ID, element); } } /// /// Removed the specified from ID management. /// /// The to be removed from ID management. public virtual void Remove(SvgElement element) { if (!string.IsNullOrEmpty(element.ID)) { this._idValueMap.Remove(element.ID); } } /// /// Ensures that the specified ID is valid within the containing . /// /// A containing the ID to validate. /// /// The ID cannot start with a digit. /// An element with the same ID already exists within the containing . /// public void EnsureValidId(string id) { if (string.IsNullOrEmpty(id)) { return; } if (char.IsDigit(id[0])) { throw new SvgException("ID cannot start with a digit: '" + id + "'."); } if (this._idValueMap.ContainsKey(id)) { throw new SvgException("An element with the same ID already exists: '" + id + "'."); } } /// /// Initialises a new instance of an . /// /// The containing the s to manage. public SvgElementIdManager(SvgDocument document) { this._document = document; this._idValueMap = new Dictionary(); } } }