PageRenderTime 66ms CodeModel.GetById 18ms RepoModel.GetById 13ms app.codeStats 1ms

/source/library/Interlace/PropertyLists/PropertyDictionary.cs

https://bitbucket.org/VahidN/interlace
C# | 350 lines | 258 code | 67 blank | 25 comment | 23 complexity | 955c010a142eda68993464f3c3d3c7f3 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Text;
  33. #endregion
  34. namespace Interlace.PropertyLists
  35. {
  36. [Serializable]
  37. public class PropertyDictionary : IEnumerable
  38. {
  39. Dictionary<object, object> _dictionary;
  40. internal PropertyDictionary()
  41. {
  42. _dictionary = new Dictionary<object, object>();
  43. }
  44. public static PropertyDictionary FromFileInExecutableDirectory(string relativePath)
  45. {
  46. string path = Path.Combine(
  47. Path.GetDirectoryName(Assembly.GetCallingAssembly().Location), relativePath);
  48. return FromFile(path);
  49. }
  50. public static PropertyDictionary FromStream(Stream stream, string nameForExceptions)
  51. {
  52. if (stream.Length > 0)
  53. {
  54. using (TextReader reader = new StreamReader(stream))
  55. {
  56. return FromReader(reader, nameForExceptions);
  57. }
  58. }
  59. else
  60. {
  61. return PropertyDictionary.EmptyDictionary();
  62. }
  63. }
  64. public static PropertyDictionary FromReader(TextReader reader, string nameForExceptions)
  65. {
  66. Lexer lexer = new Lexer(reader, nameForExceptions);
  67. return Parser.Parse(lexer) as PropertyDictionary;
  68. }
  69. public static PropertyDictionary FromStream(Stream stream)
  70. {
  71. return PropertyDictionary.FromStream(stream, "unnamed stream");
  72. }
  73. public static PropertyDictionary FromReader(TextReader reader)
  74. {
  75. return PropertyDictionary.FromReader(reader, "unnamed stream");
  76. }
  77. public static PropertyDictionary FromFile(string path)
  78. {
  79. if (!File.Exists(path)) return null;
  80. using (StreamReader reader = new StreamReader(path))
  81. {
  82. return PropertyDictionary.FromReader(reader, path);
  83. }
  84. }
  85. public static PropertyDictionary FromString(string text)
  86. {
  87. if (string.IsNullOrEmpty(text)) return PropertyDictionary.EmptyDictionary();
  88. using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(text)))
  89. {
  90. using (StreamReader reader = new StreamReader(stream))
  91. {
  92. return PropertyDictionary.FromReader(reader, "text string");
  93. }
  94. }
  95. }
  96. public static PropertyDictionary EmptyDictionary()
  97. {
  98. return new PropertyDictionary();
  99. }
  100. public void PersistToFileInExecutableDirectory(string relativePath)
  101. {
  102. string path = Path.Combine(
  103. Path.GetDirectoryName(Assembly.GetCallingAssembly().Location), relativePath);
  104. PersistToFile(path);
  105. }
  106. public void PersistToFile(string path)
  107. {
  108. FileStream file = File.Open(path, FileMode.Create);
  109. byte[] bytes = Encoding.ASCII.GetBytes(GetPersistString());
  110. file.Write(bytes, 0, bytes.Length);
  111. file.Close();
  112. }
  113. public void PersistToStream(Stream stream)
  114. {
  115. byte[] bytes = Encoding.ASCII.GetBytes(GetPersistString());
  116. stream.Write(bytes, 0, bytes.Length);
  117. }
  118. string GetPersistString()
  119. {
  120. Writer writer = new Writer();
  121. writer.WriteDictionary(this);
  122. return writer.ToString();
  123. }
  124. public byte[] PersistToByteArray()
  125. {
  126. return Encoding.ASCII.GetBytes(GetPersistString());
  127. }
  128. public string PersistToString()
  129. {
  130. return GetPersistString();
  131. }
  132. private string AppendIndent(string value, int indentLevel)
  133. {
  134. for (int i = 0; i < indentLevel; ++i)
  135. value += "\t";
  136. return value;
  137. }
  138. public bool HasValueFor(params object[] keys)
  139. {
  140. foreach (object key in keys)
  141. {
  142. if (!_dictionary.ContainsKey(key)) return false;
  143. }
  144. return true;
  145. }
  146. public bool HasStringFor(params object[] keys)
  147. {
  148. foreach (object key in keys)
  149. {
  150. if (!_dictionary.ContainsKey(key) || !(_dictionary[key] is string)) return false;
  151. }
  152. return true;
  153. }
  154. public bool HasIntegerFor(params object[] keys)
  155. {
  156. foreach (object key in keys)
  157. {
  158. if (!_dictionary.ContainsKey(key) || !(_dictionary[key] is int)) return false;
  159. }
  160. return true;
  161. }
  162. public bool HasDoubleFor(params object[] keys)
  163. {
  164. foreach (object key in keys)
  165. {
  166. if (!_dictionary.ContainsKey(key) || !(_dictionary[key] is double || _dictionary[key] is int)) return false;
  167. }
  168. return true;
  169. }
  170. public bool HasDictionaryFor(params object[] keys)
  171. {
  172. foreach (object key in keys)
  173. {
  174. if (!_dictionary.ContainsKey(key) || !(_dictionary[key] is PropertyDictionary)) return false;
  175. }
  176. return true;
  177. }
  178. public bool HasArrayFor(params object[] keys)
  179. {
  180. foreach (object key in keys)
  181. {
  182. if (!_dictionary.ContainsKey(key) || !(_dictionary[key] is PropertyArray)) return false;
  183. }
  184. return true;
  185. }
  186. public object ValueFor(object key)
  187. {
  188. if (_dictionary.ContainsKey(key))
  189. {
  190. return _dictionary[key];
  191. }
  192. else
  193. {
  194. return null;
  195. }
  196. }
  197. public object ValueFor(object key, object defaultValue)
  198. {
  199. if (_dictionary.ContainsKey(key))
  200. {
  201. return _dictionary[key];
  202. }
  203. else
  204. {
  205. return defaultValue;
  206. }
  207. }
  208. public string StringFor(object key)
  209. {
  210. return ValueFor(key) as string;
  211. }
  212. public string StringFor(object key, string defaultValue)
  213. {
  214. string value = ValueFor(key) as string;
  215. return value != null ? value : defaultValue;
  216. }
  217. public int? IntegerFor(object key)
  218. {
  219. return ValueFor(key) as int?;
  220. }
  221. public double? DoubleFor(object key)
  222. {
  223. object value = ValueFor(key);
  224. if (value is int) return (double)(int)value;
  225. return value as double?;
  226. }
  227. public int IntegerFor(object key, int defaultValue)
  228. {
  229. int? value = ValueFor(key) as int?;
  230. return value != null ? (int)value : defaultValue;
  231. }
  232. public bool? BooleanFor(object key)
  233. {
  234. return ValueFor(key) as bool?;
  235. }
  236. public bool BooleanFor(object key, bool defaultValue)
  237. {
  238. bool? value = ValueFor(key) as bool?;
  239. return value != null ? (bool)value : defaultValue;
  240. }
  241. public byte[] DataFor(object key)
  242. {
  243. return ValueFor(key) as byte[];
  244. }
  245. public byte[] DataFor(object key, byte[] defaultValue)
  246. {
  247. byte[] value = ValueFor(key) as byte[];
  248. return value != null ? (byte[])value : defaultValue;
  249. }
  250. public PropertyArray ArrayFor(object key)
  251. {
  252. return ValueFor(key) as PropertyArray;
  253. }
  254. public PropertyDictionary DictionaryFor(object key)
  255. {
  256. return ValueFor(key) as PropertyDictionary;
  257. }
  258. public void SetValueFor(object key, object value)
  259. {
  260. _dictionary[key] = value;
  261. }
  262. public object[] Keys
  263. {
  264. get
  265. {
  266. object[] array = new object[_dictionary.Count];
  267. _dictionary.Keys.CopyTo(array, 0);
  268. return array;
  269. }
  270. }
  271. public object this[object key]
  272. {
  273. get { return _dictionary[key]; }
  274. set { _dictionary[key] = value; }
  275. }
  276. #region IEnumerable Members
  277. public IEnumerator GetEnumerator()
  278. {
  279. return _dictionary.GetEnumerator();
  280. }
  281. #endregion
  282. }
  283. }