#region Copyright // // Nini Configuration Project. // Copyright (C) 2006 Brent R. Matzelle. All rights reserved. // // This software is published under the terms of the MIT X11 license, a copy of // which has been included with this distribution in the LICENSE.txt file. // #endregion using System; using System.Collections; namespace ExtensionLoader.Config { /// public class AliasText { #region Private variables Hashtable intAlias = null; Hashtable booleanAlias = null; #endregion #region Constructors /// public AliasText () { intAlias = InsensitiveHashtable (); booleanAlias = InsensitiveHashtable (); DefaultAliasLoad (); } #endregion #region Public methods /// public void AddAlias (string key, string alias, int value) { if (intAlias.Contains (key)) { Hashtable keys = (Hashtable)intAlias[key]; keys[alias] = value; } else { Hashtable keys = InsensitiveHashtable (); keys[alias] = value; intAlias.Add (key, keys); } } /// public void AddAlias (string alias, bool value) { booleanAlias[alias] = value; } #if (NET_COMPACT_1_0) #else /// public void AddAlias (string key, Enum enumAlias) { SetAliasTypes (key, enumAlias); } #endif /// public bool ContainsBoolean (string key) { return booleanAlias.Contains (key); } /// public bool ContainsInt (string key, string alias) { bool result = false; if (intAlias.Contains (key)) { Hashtable keys = (Hashtable)intAlias[key]; result = (keys.Contains (alias)); } return result; } /// public bool GetBoolean (string key) { if (!booleanAlias.Contains (key)) { throw new ArgumentException ("Alias does not exist for text"); } return (bool)booleanAlias[key]; } /// public int GetInt (string key, string alias) { if (!intAlias.Contains (key)) { throw new ArgumentException ("Alias does not exist for key"); } Hashtable keys = (Hashtable)intAlias[key]; if (!keys.Contains (alias)) { throw new ArgumentException ("Config value does not match a " + "supplied alias"); } return (int)keys[alias]; } #endregion #region Private methods /// /// Loads the default alias values. /// private void DefaultAliasLoad () { AddAlias("true", true); AddAlias("false", false); } #if (NET_COMPACT_1_0) #else /// /// Extracts and sets the alias types from an enumeration. /// private void SetAliasTypes (string key, Enum enumAlias) { string[] names = Enum.GetNames (enumAlias.GetType ()); int[] values = (int[])Enum.GetValues (enumAlias.GetType ()); for (int i = 0; i < names.Length; i++) { AddAlias (key, names[i], values[i]); } } #endif /// /// Returns a case insensitive hashtable. /// private Hashtable InsensitiveHashtable () { return new Hashtable (StringComparer.OrdinalIgnoreCase); } #endregion } }