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