/
ValueGetter.cs
252 lines (211 loc) · 6.75 KB
/
ValueGetter.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Xml;
namespace Nustache.Core
{
public abstract class ValueGetter
{
public static readonly object NoValue = new object();
#region Static helper methods
public static object GetValue(object target, string name)
{
return ValueGetterFactories.Factories.GetValueGetter(target, name).GetValue();
}
#endregion
#region Abstract methods
public abstract object GetValue();
public void Accept(ValueGetterVisitor visitor)
{
try
{
var method = visitor.GetType().GetMethod("Visit", new Type[] { this.GetType() });
if (method != null)
{
method.Invoke(visitor, new object[] { this });
}
else
{
visitor.NoMatch(this);
}
}
catch (System.Reflection.TargetInvocationException ex)
{
throw ex.InnerException;
}
}
#endregion
}
internal class XmlNodeValueGetter : ValueGetter
{
private readonly XmlNode _target;
private readonly string _name;
private string _textValueLocated;
private XmlNodeList _childNodeList;
internal XmlNodeValueGetter(XmlNode target, string name)
{
_target = target;
_name = name;
}
private bool HasChildNodeList()
{
_childNodeList = _target.SelectNodes(_name);
return _childNodeList != null && _childNodeList.Count > 0;
}
private bool TryGetSingleTextNodeValue()
{
if (_childNodeList.Count != 1)
return false;
return TryGetNodeValueAsText(_childNodeList[0]);
}
private bool TryGetNodeValueAsText(XmlNode node)
{
if (node.ChildNodes.Count == 1
&& node.ChildNodes[0].NodeType == XmlNodeType.Text)
{
_textValueLocated = node.ChildNodes[0].Value;
return true;
}
return false;
}
public override object GetValue()
{
if (_name[0] == '@' && TryGetStringByAttributeName(_name.Substring(1)))
return _textValueLocated;
if (HasChildNodeList())
{
if (TryGetSingleTextNodeValue())
return _textValueLocated;
return _childNodeList;
}
if (TryGetStringByAttributeName(_name))
{
return _textValueLocated;
}
return NoValue;
}
private bool TryGetStringByAttributeName(string attributeName)
{
if (_target.Attributes != null)
{
XmlNode attribute = _target.Attributes.GetNamedItem(attributeName);
if (attribute != null)
{
_textValueLocated = attribute.Value;
return true;
}
}
return false;
}
}
internal class PropertyDescriptorValueGetter : ValueGetter
{
private readonly object _target;
private readonly PropertyDescriptor _propertyDescriptor;
internal PropertyDescriptorValueGetter(object target, PropertyDescriptor propertyDescriptor)
{
_target = target;
_propertyDescriptor = propertyDescriptor;
}
public override object GetValue()
{
return _propertyDescriptor.GetValue(_target);
}
}
public class MethodInfoValueGetter : ValueGetter
{
private readonly object _target;
public readonly MethodInfo MethodInfo;
internal MethodInfoValueGetter(object target, MethodInfo methodInfo)
{
_target = target;
MethodInfo = methodInfo;
}
public override object GetValue()
{
return MethodInfo.Invoke(_target, null);
}
}
public class PropertyInfoValueGetter : ValueGetter
{
private readonly object _target;
public readonly PropertyInfo PropertyInfo;
internal PropertyInfoValueGetter(object target, PropertyInfo propertyInfo)
{
_target = target;
PropertyInfo = propertyInfo;
}
public override object GetValue()
{
return PropertyInfo.GetValue(_target, null);
}
}
public class FieldInfoValueGetter : ValueGetter
{
private readonly object _target;
public readonly FieldInfo FieldInfo;
internal FieldInfoValueGetter(object target, FieldInfo fieldInfo)
{
_target = target;
FieldInfo = fieldInfo;
}
public override object GetValue()
{
return FieldInfo.GetValue(_target);
}
}
internal class DictionaryValueGetter : ValueGetter
{
private readonly IDictionary _target;
private readonly string _key;
internal DictionaryValueGetter(IDictionary target, string key)
{
_target = target;
_key = key;
}
public override object GetValue()
{
return _target[_key];
}
}
public class GenericDictionaryValueGetter : ValueGetter
{
private readonly object _target;
public readonly string Key;
public readonly MethodInfo GetMethod;
public readonly Type DictionaryType;
internal GenericDictionaryValueGetter(object target, string key, Type dictionaryType)
{
_target = target;
Key = key;
GetMethod = dictionaryType.GetMethod("get_Item");
DictionaryType = dictionaryType;
}
public override object GetValue()
{
return GetMethod.Invoke(_target, new object[] { Key });
}
}
internal class ListValueByIndexGetter : ValueGetter
{
private readonly IList _target;
private readonly int _index;
public ListValueByIndexGetter(IList target, int index)
{
_target = target;
_index = index;
}
public override object GetValue()
{
return _target[_index];
}
}
internal class NoValueGetter : ValueGetter
{
public override object GetValue()
{
return NoValue;
}
}
}