Build.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. internal class Build : System.Web.Configuration.CapabilitiesBuild
  28. {
  29. //This keeps a list of filenames and FileNodes linked
  30. private System.Collections.Generic.Dictionary<string, System.Web.Configuration.nBrowser.File> Browserfiles;
  31. //Just links FileNodes
  32. private System.Collections.Generic.List<System.Web.Configuration.nBrowser.File> nbrowserfiles;
  33. //
  34. private System.Collections.Generic.Dictionary<string, string> DefaultKeys;
  35. private System.Collections.Generic.Dictionary<string, string> BrowserKeys;
  36. //
  37. private object browserSyncRoot = new object();
  38. private System.Web.Configuration.nBrowser.Node browser;
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. public Build()
  43. : base()
  44. {
  45. Browserfiles = new System.Collections.Generic.Dictionary<string, System.Web.Configuration.nBrowser.File>();
  46. nbrowserfiles = new System.Collections.Generic.List<System.Web.Configuration.nBrowser.File>();
  47. DefaultKeys = new System.Collections.Generic.Dictionary<string, string>();
  48. BrowserKeys = new System.Collections.Generic.Dictionary<string, string>();
  49. }
  50. /// <summary>
  51. /// Reads an entire directory and process's all of the browser files in that
  52. /// directory.
  53. /// </summary>
  54. /// <param name="path"></param>
  55. public void AddBrowserDirectory(string path)
  56. {
  57. //I allow this function to be a little messy
  58. //just in case they pass in a path that really a file
  59. //name
  60. if (System.IO.Directory.Exists(path) == true)
  61. {
  62. System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
  63. System.IO.FileInfo[] file = dir.GetFiles("*.browser");
  64. //we are done with it so let the GC have it early as possible
  65. dir = null;
  66. for (int a = 0;a <= file.Length - 1;a++)
  67. {
  68. AddBrowserFile(file[a].FullName);
  69. }
  70. }
  71. else if (System.IO.File.Exists(path) == true)
  72. {
  73. AddBrowserFile(path);
  74. }
  75. }
  76. /// <summary>
  77. /// Reads a browser file and builds the Nodes which that file contains.
  78. /// </summary>
  79. /// <param name="filename"></param>
  80. public void AddBrowserFile(string fileName)
  81. {
  82. if (Browserfiles.ContainsKey(fileName) == false)
  83. {
  84. nBrowser.File b = new nBrowser.File(fileName);
  85. this.AddBrowserFile(b);
  86. }
  87. }
  88. private void AddBrowserFile(nBrowser.File file)
  89. {
  90. if (Browserfiles.ContainsKey(file.FileName) == false)
  91. {
  92. Browserfiles.Add(file.FileName, file);
  93. nbrowserfiles.Add(file);
  94. string[] keys = file.Keys;
  95. for (int i = 0;i <= keys.Length - 1;i++)
  96. {
  97. if (BrowserKeys.ContainsKey(keys[i]) == false)
  98. {
  99. BrowserKeys.Add(keys[i], file.FileName);
  100. }
  101. else
  102. {
  103. throw new nBrowser.Exception("Duplicate Key \"" + keys[i] + "\" found in " + file.FileName + " and in file " + BrowserKeys[keys[i]]);
  104. }
  105. }
  106. keys = file.DefaultKeys;
  107. for (int i = 0;i <= keys.Length - 1;i++)
  108. {
  109. if (DefaultKeys.ContainsKey(keys[i]) == false)
  110. {
  111. DefaultKeys.Add(keys[i], file.FileName);
  112. }
  113. else
  114. {
  115. throw new nBrowser.Exception("Duplicate Key \"" + keys[i] + "\" found in " + file.FileName + " and in file " + DefaultKeys[keys[i]]);
  116. }
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// Reads a browser file and builds the Nodes which that file contains.
  122. /// </summary>
  123. /// <param name="file"></param>
  124. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1059")]
  125. public void AddBrowserFile(System.Xml.XmlDocument browser, string fileName)
  126. {
  127. if (Browserfiles.ContainsKey(fileName) == false)
  128. {
  129. nBrowser.File file = new nBrowser.File(browser, fileName);
  130. this.AddBrowserFile(file);
  131. }
  132. }
  133. /// <summary>
  134. /// Returns the root Node of the Browser Tree.
  135. /// </summary>
  136. /// <returns></returns>
  137. public Node Browser()
  138. {
  139. if (browser == null)
  140. {
  141. lock (browserSyncRoot)
  142. {
  143. if (browser == null)
  144. {
  145. browser = InitializeTree();
  146. }
  147. }
  148. }
  149. return browser;
  150. }
  151. private Node InitializeTree()
  152. {
  153. Node root = new Node();
  154. //Custom Sorted List, to allow where Multple files in Diff directorys might have the same
  155. //filename. So still to some degree first come first serve but might be close enough
  156. //to how microsoft System to match much more closely.
  157. System.Collections.Generic.SortedList<string, System.Collections.Generic.List<nBrowser.File>> list;
  158. list = new System.Collections.Generic.SortedList<string, System.Collections.Generic.List<nBrowser.File>>();
  159. for (int i = 0;i <= Browserfiles.Count - 1;i++)
  160. {
  161. if (list.ContainsKey(nbrowserfiles[i].FileName) == false)
  162. {
  163. System.Collections.Generic.List<nBrowser.File> l;
  164. l = new System.Collections.Generic.List<nBrowser.File>();
  165. list.Add(nbrowserfiles[i].FileName, l);
  166. }
  167. list[nbrowserfiles[i].FileName].Add(nbrowserfiles[i]);
  168. }
  169. nBrowser.File[] files = new nBrowser.File[Browserfiles.Count];
  170. int count = 0;
  171. for (int i = 0;i <= list.Count - 1;i++)
  172. {
  173. System.Collections.Generic.List<nBrowser.File> l = list[list.Keys[i]];
  174. for (int b = 0;b <= l.Count - 1;b++)
  175. {
  176. files[count] = l[b];
  177. count++;
  178. }
  179. }
  180. #region Connect Nodes
  181. for (int i = 0;i <= Browserfiles.Count - 1;i++)
  182. {
  183. for (int a = 0;a <= files[i].Keys.Length - 1;a++)
  184. {
  185. Node child = files[i].GetNode(files[i].Keys[a]);
  186. Node parent = null;
  187. if (child.ParentId.Length > 0)
  188. {
  189. parent = this.GetNode(child.ParentId);
  190. if (parent == null)
  191. throw new nBrowser.Exception(String.Format("Parent not found with id = {0}", child.ParentId));
  192. }
  193. if (parent == null)
  194. parent = root;
  195. parent.AddChild(child);
  196. }
  197. }
  198. #endregion
  199. #region Inject DefaultBrowser Nodes
  200. for (int i = 0;i <= Browserfiles.Count - 1;i++)
  201. {
  202. for (int a = 0;a <= files[i].DefaultKeys.Length - 1;a++)
  203. {
  204. Node defaultNode = files[i].GetDefaultNode(files[i].DefaultKeys[a]);
  205. Node node = this.GetNode(defaultNode.Id);
  206. if (node == defaultNode)
  207. {
  208. // there is no regular node so the defaultNode is already at
  209. // the correct spot in the tree.
  210. continue;
  211. }
  212. Node parentNode = this.GetNode(node.ParentId);
  213. if (parentNode == null)
  214. parentNode = root;
  215. // insert the default node between the regular node and it's parent.
  216. parentNode.RemoveChild(node);
  217. defaultNode.AddChild(node);
  218. parentNode.AddChild(defaultNode);
  219. }
  220. }
  221. #endregion
  222. #region Merge Ref Nodes
  223. for (int i = 0;i <= Browserfiles.Count - 1;i++)
  224. {
  225. foreach (Node refNode in files[i].RefNodes) {
  226. GetNode(refNode.RefId).MergeFrom(refNode);
  227. }
  228. }
  229. #endregion
  230. return root;
  231. }
  232. /// <summary>
  233. /// returns a Node Matching the Key supplied, for either a
  234. /// DefaultBrowser, or Gatway/Browser node.
  235. /// </summary>
  236. /// <param name="Key"></param>
  237. /// <returns></returns>
  238. private Node GetNode(string Key)
  239. {
  240. if (Key == null || Key.Length == 0)
  241. return null;
  242. string filename;
  243. //Must find what file node that this key is located in
  244. //so we look it up in the string dictionary's
  245. if (!BrowserKeys.TryGetValue(Key, out filename)
  246. && !DefaultKeys.TryGetValue(Key, out filename))
  247. return null;
  248. //fxcop sugguested this was faster then
  249. //filename!= string.Empty
  250. if (filename != null && filename.Length > 0)
  251. {
  252. //now that we have a name we look it up in the hasttable containing
  253. //the actual node.
  254. nBrowser.File b = Browserfiles[filename];
  255. Node n = b.GetNode(Key);
  256. return n;
  257. }
  258. return null;
  259. }
  260. /// <summary>
  261. /// Returns an Array of Nodes that have been built. To be used for Design/Editors of Browser
  262. /// files.
  263. /// </summary>
  264. /// <returns></returns>
  265. public Node[] Nodes()
  266. {
  267. Node[] browsers;
  268. nBrowser.File[] files = new nBrowser.File[Browserfiles.Count];
  269. Browserfiles.Values.CopyTo(files, 0);
  270. int count = 0;
  271. for (int i = 0;i <= files.Length - 1;i++)
  272. {
  273. count += files[i].Nodes.Length;
  274. }
  275. browsers = new Node[count];
  276. count = 0;
  277. for (int i = 0;i <= files.Length - 1;i++)
  278. {
  279. for (int a = 0;a <= files[i].Nodes.Length - 1;a++)
  280. {
  281. browsers[count] = files[i].Nodes[a];
  282. count++;
  283. }
  284. }
  285. return browsers;
  286. }
  287. /// <summary>
  288. ///
  289. /// </summary>
  290. /// <param name="header"></param>
  291. /// <param name="initialCapabilities"></param>
  292. /// <returns></returns>
  293. public override System.Web.Configuration.CapabilitiesResult Process(System.Collections.Specialized.NameValueCollection header, System.Collections.IDictionary initialCapabilities)
  294. {
  295. if (initialCapabilities == null)
  296. initialCapabilities = new System.Collections.Generic.Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  297. System.Web.Configuration.nBrowser.Result r = new System.Web.Configuration.nBrowser.Result(initialCapabilities);
  298. #if trace
  299. System.Diagnostics.Trace.WriteLine(string.Join("+", new string[50]));
  300. for (int i=0;i <= header.Count -1;i++)
  301. {
  302. System.Diagnostics.Trace.WriteLine(string.Format("{0}{1}",header.GetKey(i).PadRight(25),header[i]));
  303. }
  304. System.Diagnostics.Trace.WriteLine(string.Join("+", new string[50]));
  305. #endif
  306. Browser().Process(header, r, new System.Collections.Generic.List<System.Text.RegularExpressions.Match>());
  307. return r;
  308. }
  309. /// <summary>
  310. ///
  311. /// </summary>
  312. /// <param name="list"></param>
  313. /// <returns></returns>
  314. protected override System.Collections.ObjectModel.Collection<string> HeaderNames(System.Collections.ObjectModel.Collection<string> list)
  315. {
  316. return this.Browser().HeaderNames(list);
  317. }
  318. }
  319. }
  320. #endif