* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Web / Test / mainsoft / MainsoftWebTest / HtmlAgilityPack / Header.cs
1 // HtmlAgilityPack V1.0 - Simon Mourier <simonm@microsoft.com>
2 using System;
3 using System.Collections;
4
5 namespace HtmlAgilityPack
6 {
7         internal class NameValuePair
8         {
9                 internal readonly string Name;
10                 internal string Value;
11
12                 internal NameValuePair()
13                 {
14                 }
15
16                 internal NameValuePair(string name):
17                         this()
18                 {
19                         Name = name;
20                 }
21
22                 internal NameValuePair(string name, string value):
23                         this(name)
24                 {
25                         Value = value;
26                 }
27         }
28
29         internal class NameValuePairList
30         {
31                 internal readonly string Text;
32                 private ArrayList _allPairs;
33                 private Hashtable _pairsWithName;
34
35                 internal NameValuePairList():
36                         this(null)
37                 {
38                 }
39
40                 internal NameValuePairList(string text)
41                 {
42                         Text = text;
43                         _allPairs = new ArrayList();
44                         _pairsWithName = new Hashtable();
45
46                         Parse(text);
47                 }
48
49                 internal string GetNameValuePairValue(string name)
50                 {
51                         if (name==null)
52                                 throw new ArgumentNullException();
53                         ArrayList al = GetNameValuePairs(name);
54                         if (al==null)
55                                 return null;
56
57                         // return first item
58                         NameValuePair nvp = al[0] as NameValuePair;
59                         return nvp.Value;
60                 }
61
62                 internal ArrayList GetNameValuePairs(string name)
63                 {
64                         if (name==null)
65                                 return _allPairs;
66                         return _pairsWithName[name] as ArrayList;
67                 }
68
69                 private void Parse(string text)
70                 {
71                         _allPairs.Clear();
72                         _pairsWithName.Clear();
73                         if (text==null)
74                                 return;
75
76                         string[] p = text.Split(';');
77                         if (p==null)
78                                 return;
79                         foreach(string pv in p)
80                         {
81                                 if (pv.Length==0)
82                                         continue;
83                                 string[] onep = pv.Split(new char[]{'='}, 2);
84                                 if (onep==null)
85                                         continue;
86                                 NameValuePair nvp = new NameValuePair(onep[0].Trim().ToLower());
87                                 if (onep.Length<2)
88                                         nvp.Value = "";
89                                 else
90                                         nvp.Value = onep[1];
91
92                                 _allPairs.Add(nvp);
93
94                                 // index by name
95                                 ArrayList al = _pairsWithName[nvp.Name] as ArrayList;
96                                 if (al==null)
97                                 {
98                                         al = new ArrayList();
99                                         _pairsWithName[nvp.Name] = al;
100                                 }
101                                 al.Add(nvp);
102                         }
103                 }
104
105                 internal static string GetNameValuePairsValue(string text, string name)
106                 {
107                         NameValuePairList l = new NameValuePairList(text);
108                         return l.GetNameValuePairValue(name);
109                 }
110         }
111 }