forked from garuma/MobileHtmlAgilityPack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNameValuePairList.cs
103 lines (82 loc) · 2.25 KB
/
NameValuePairList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// HtmlAgilityPack V1.0 - Simon Mourier <simon underscore mourier at hotmail dot com>
using System;
using System.Collections;
using System.Collections.Generic;
namespace HtmlAgilityPack
{
internal class NameValuePairList
{
#region Fields
internal readonly string Text;
private List<KeyValuePair<string, string>> _allPairs;
private Dictionary<string,List<KeyValuePair<string,string>>> _pairsWithName;
#endregion
#region Constructors
internal NameValuePairList() :
this(null)
{
}
internal NameValuePairList(string text)
{
Text = text;
_allPairs = new List<KeyValuePair<string, string>>();
_pairsWithName = new Dictionary<string, List<KeyValuePair<string, string>>>();
Parse(text);
}
#endregion
#region Internal Methods
internal static string GetNameValuePairsValue(string text, string name)
{
NameValuePairList l = new NameValuePairList(text);
return l.GetNameValuePairValue(name);
}
internal List<KeyValuePair<string,string>> GetNameValuePairs(string name)
{
if (name == null)
return _allPairs;
return _pairsWithName.ContainsKey(name) ? _pairsWithName[name] : new List<KeyValuePair<string,string>>();
}
internal string GetNameValuePairValue(string name)
{
if (name == null)
throw new ArgumentNullException();
List<KeyValuePair<string,string>> al = GetNameValuePairs(name);
if (al.Count==0)
return string.Empty;
// return first item
return al[0].Value.Trim();
}
#endregion
#region Private Methods
private void Parse(string text)
{
_allPairs.Clear();
_pairsWithName.Clear();
if (text == null)
return;
string[] p = text.Split(';');
foreach (string pv in p)
{
if (pv.Length == 0)
continue;
string[] onep = pv.Split(new[] {'='}, 2);
if (onep.Length==0)
continue;
KeyValuePair<string, string> nvp = new KeyValuePair<string, string>(onep[0].Trim().ToLower(),
onep.Length < 2 ? "" : onep[1]);
_allPairs.Add(nvp);
// index by name
List<KeyValuePair<string, string>> al;
if (!_pairsWithName.ContainsKey(nvp.Key))
{
al = new List<KeyValuePair<string, string>>();
_pairsWithName[nvp.Key] = al;
}
else
al = _pairsWithName[nvp.Key];
al.Add(nvp);
}
}
#endregion
}
}