Configuration.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #region Using directives
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Xml;
  6. using System.Xml.XPath;
  7. using System.IO;
  8. #endregion
  9. namespace Eq2VpkTool
  10. {
  11. public class Configuration
  12. {
  13. #region Constructors
  14. static Configuration()
  15. {
  16. instance = new Configuration();
  17. }
  18. protected Configuration()
  19. {
  20. }
  21. #endregion
  22. public static Configuration Instance
  23. {
  24. get { return instance; }
  25. }
  26. public void Load(Stream stream)
  27. {
  28. reader = new XPathDocument(stream);
  29. navigator = reader.CreateNavigator();
  30. loaded = true;
  31. }
  32. public string GetValue(string key)
  33. {
  34. #region Preconditions
  35. Debug.Assert(reader != null && navigator != null, "No XML file loaded, call Load() first.");
  36. #endregion
  37. XPathNavigator nodeNavigator;
  38. // Select the first node that matches the query.
  39. // If the query is invalid, XPathNavigator.SelectSingleNode() throws an exception and we return null.
  40. try { nodeNavigator = navigator.SelectSingleNode(key); }
  41. catch (Exception) { return null; }
  42. // If the query is valid but it yields no results, return null.
  43. if (nodeNavigator == null) return null;
  44. // Otherwise, return the first node found.
  45. return nodeNavigator.Value;
  46. }
  47. /*public T? GetValue<T>(string key)
  48. {
  49. #region Preconditions
  50. Debug.Assert(typeof(T).IsValueType, "Type " + typeof(T).Name + " is not a value type. Use of GetValue<T>() requires a value type. Use GetValue() instead.");
  51. Debug.Assert(reader != null && navigator != null, "No XML file loaded, call Load() first.");
  52. #endregion
  53. XPathNavigator nodeNavigator;
  54. // Select the first node that matches the query.
  55. // If the query is invalid, XPathNavigator.SelectSingleNode() throws an exception and we return null.
  56. try { nodeNavigator = navigator.SelectSingleNode(key); }
  57. catch (Exception) { return null; }
  58. // If the query is valid but yields no results, return null.
  59. if (nodeNavigator == null) return null;
  60. T? value;
  61. // Read the first result as a value of the required type.
  62. // If the format or the cast is invalid we return null.
  63. try { value = (T)nodeNavigator.ValueAs(typeof(T)); }
  64. catch (Exception) { return null; }
  65. // Otherwise, we return the converted value.
  66. return value;
  67. }*/
  68. public IEnumerator<string> GetValues(string key)
  69. {
  70. #region Preconditions
  71. Debug.Assert(reader != null && navigator != null, "No XML file loaded");
  72. #endregion
  73. XPathNodeIterator nodes;
  74. // Select all the nodes that match the query.
  75. // If the query is invalid XPathNavigator.Select() throws an exception and we return an empty enumerator.
  76. try { nodes = navigator.Select(key); }
  77. catch (Exception) { yield break; }
  78. // Yield values until we consume all of them.
  79. foreach (XPathNavigator node in nodes) yield return node.Value;
  80. }
  81. /*public IEnumerator<T> GetValues<T>(string key)
  82. {
  83. // First check whether the type parameter T is actually class String.
  84. // If it is, redirect this method call to GetValues().
  85. // Note: This workaround makes it possible to use GetValues() and GetValues<string>() interchangeably.
  86. if (typeof(T) == typeof(string))
  87. {
  88. // This cast is safe because we've made sure T is String.
  89. IEnumerator<T> iterator = GetValues(key) as IEnumerator<T>;
  90. // We can't just return the iterator, we have to yield all the elements.
  91. while (iterator.MoveNext()) yield return iterator.Current;
  92. }
  93. else
  94. {
  95. #region Preconditions
  96. Debug.Assert(typeof(T).IsValueType, "Type " + typeof(T).Name + " is not a value type. Use of GetValue<T>() requires a value type. Use GetValue() instead.");
  97. Debug.Assert(reader != null && navigator != null, "No XML file loaded, call Load() first.");
  98. #endregion
  99. XPathNodeIterator nodes;
  100. // Select all the nodes that match the query.
  101. // If the query is invalid XPathNavigator.Select throws an exception and we return null.
  102. try { nodes = navigator.Select(key); }
  103. catch (Exception) { yield break; }
  104. // Yield values until we consume all of them.
  105. foreach (XPathNavigator node in nodes)
  106. {
  107. T? value = null;
  108. // If the format or the cast to type T is invalid we don't yield this value.
  109. // Note: If none of the values have valid conversions to T, an empty enumerator is returned.
  110. try { value = (T)node.ValueAs(typeof(T)); }
  111. catch (Exception) {}
  112. if (value.HasValue) yield return value.Value;
  113. }
  114. }
  115. }*/
  116. public bool IsLoaded
  117. {
  118. get { return loaded; }
  119. }
  120. private bool loaded = false;
  121. private XPathDocument reader;
  122. private XPathNavigator navigator;
  123. private static Configuration instance;
  124. }
  125. }
  126. /* EOF */