File.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #if NET_2_0
  2. /*
  3. Used to determine Browser Capabilities by the Browsers UserAgent String and related
  4. Browser supplied Headers.
  5. Copyright (C) 2002-Present Owen Brady (Ocean at owenbrady dot net)
  6. and Dean Brettle (dean at brettle dot com)
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is furnished
  12. to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  20. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. namespace System.Web.Configuration.nBrowser
  23. {
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Text;
  27. using System.Web.Configuration.nBrowser;
  28. class File
  29. {
  30. private System.Xml.XmlDocument BrowserFile;
  31. internal System.Web.Configuration.nBrowser.Node[] Nodes;
  32. private System.Collections.Specialized.ListDictionary Lookup;
  33. private System.Collections.Specialized.ListDictionary DefaultLookup;
  34. internal List<Node> RefNodes;
  35. public string FileName
  36. {
  37. get
  38. {
  39. return pFileName;
  40. }
  41. }
  42. private string pFileName = string.Empty;
  43. public File(string file)
  44. {
  45. pFileName = file;
  46. BrowserFile = new System.Xml.XmlDocument();
  47. //I can put this in a try /catch but I want
  48. //this to bubble up.
  49. BrowserFile.Load(file);
  50. this.Load(BrowserFile);
  51. }
  52. /// <summary>
  53. ///
  54. /// </summary>
  55. /// <param name="file"></param>
  56. public File(System.Xml.XmlDocument BrowserFile, string filename)
  57. {
  58. pFileName = filename;
  59. this.Load(BrowserFile);
  60. }
  61. private void Load(System.Xml.XmlDocument BrowserFile)
  62. {
  63. Lookup = new System.Collections.Specialized.ListDictionary();
  64. DefaultLookup = new System.Collections.Specialized.ListDictionary();
  65. RefNodes = new List<Node>();
  66. System.Xml.XmlNode node;
  67. //I know this might allocate more nodes then needed but never less.
  68. Nodes = new Node[BrowserFile.DocumentElement.ChildNodes.Count];
  69. for (int a = 0;a <= BrowserFile.DocumentElement.ChildNodes.Count - 1;a++)
  70. {
  71. node = BrowserFile.DocumentElement.ChildNodes[a];
  72. if (node.NodeType == System.Xml.XmlNodeType.Comment)
  73. {
  74. continue;
  75. }
  76. Nodes[a] = new Node(node);
  77. Nodes[a].FileName = FileName;
  78. if (Nodes[a].NameType != NodeType.DefaultBrowser)
  79. {
  80. //fxcop sugguested this was faster then
  81. //Nodes[a].refID != string.Empty
  82. if (Nodes[a].RefId.Length > 0)
  83. {
  84. RefNodes.Add(Nodes[a]);
  85. }
  86. else if (Lookup.Contains(Nodes[a].Id) == false)
  87. {
  88. Lookup.Add(Nodes[a].Id, a);
  89. }
  90. else
  91. {
  92. throw new nBrowser.Exception("Duplicate ID found \"" + Nodes[a].Id + "\"");
  93. }
  94. }
  95. else
  96. {
  97. //fxcop sugguested this was faster then
  98. //Nodes[a].refID != string.Empty
  99. if (Nodes[a].RefId.Length > 0)
  100. {
  101. RefNodes.Add(Nodes[a]);
  102. }
  103. else if (DefaultLookup.Contains(Nodes[a].Id) == false)
  104. {
  105. DefaultLookup.Add(Nodes[a].Id, a);
  106. }
  107. else
  108. {
  109. throw new nBrowser.Exception("Duplicate ID found \"" + Nodes[a].Id + "\"");
  110. }
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// Returns a Array of strings, which represent the Id Attributes of all the
  116. /// Browser/Gatway Nodes
  117. /// </summary>
  118. public string[] Keys
  119. {
  120. get
  121. {
  122. string[] k = new string[Lookup.Keys.Count];
  123. //12-29-05
  124. //This will copy the Keys In Alphabetical Order
  125. //Lookup.Keys.CopyTo(k,0);
  126. //This Method is ment to copy the Keys in the order
  127. //that they were in the xml file.
  128. int b = 0;
  129. for (int i = 0;i <= Nodes.Length - 1;i++)
  130. {
  131. if (Nodes[i] != null && Nodes[i].NameType != NodeType.DefaultBrowser
  132. && Nodes[i].RefId.Length == 0)
  133. {
  134. k[b] = Nodes[i].Id;
  135. b++;
  136. }
  137. }
  138. return k;
  139. }
  140. }
  141. /// <summary>
  142. /// Returns a Array of strings, which represent the Id Attributes of all the
  143. /// DefaultBrowser Nodes
  144. /// </summary>
  145. public string[] DefaultKeys
  146. {
  147. get
  148. {
  149. string[] k = new string[DefaultLookup.Keys.Count];
  150. //12-29-05
  151. //This will copy the Keys In Alphabetical Order
  152. //DefaultLookup.Keys.CopyTo(k,0);
  153. //This Method is ment to copy the Keys in the order
  154. //that they were in the xml file.
  155. int b = 0;
  156. for (int i = 0;i <= Nodes.Length - 1;i++)
  157. {
  158. if (Nodes[i] != null && Nodes[i].NameType == NodeType.DefaultBrowser)
  159. {
  160. k[b] = Nodes[i].Id;
  161. b++;
  162. }
  163. }
  164. return k;
  165. }
  166. }
  167. /// <summary>
  168. ///
  169. /// </summary>
  170. /// <param name="Key"></param>
  171. /// <returns></returns>
  172. internal Node GetNode(string Key)
  173. {
  174. object o = Lookup[Key];
  175. if (o == null)
  176. return GetDefaultNode (Key);
  177. return Nodes[(int)o];
  178. }
  179. /// <summary>
  180. ///
  181. /// </summary>
  182. /// <param name="Key"></param>
  183. /// <returns></returns>
  184. internal Node GetDefaultNode(string Key)
  185. {
  186. object o = DefaultLookup[Key];
  187. if (o == null)
  188. return null;
  189. return Nodes[(int)o];
  190. }
  191. }
  192. }
  193. #endif