-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathPropertiesHelper.cs
61 lines (50 loc) · 1.84 KB
/
PropertiesHelper.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
using System;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;
namespace NHibernate.Util {
//Much of this code is taken from Maverick.NET
public class PropertiesHelper {
public static bool GetBoolean(string property, IDictionary properties) {
return bool.Parse(properties[property] as string);
}
public static int GetInt(string property, IDictionary properties, int defaultValue) {
string propValue = properties[property] as string;
return (propValue==null) ? defaultValue : int.Parse(propValue);
}
public static string GetString(string property, IDictionary properties, string defaultValue) {
string propValue = properties[property] as string;
return (propValue==null) ? defaultValue : propValue;
}
public const string TagParam = "param";
public const string AttrValue = "value";
public const string AttrName = "name";
/// <summary>
/// Extracts a set of param child nodes from the specified node
/// <param name="theName" value="theValue"/>
/// </summary>
/// <param name="node">Parent element.</param>
/// <returns>null if no parameters are found</returns>
public static IDictionary GetParams(XmlElement node) {
IDictionary result = new Hashtable();
foreach( XmlElement paramNode in node.GetElementsByTagName(TagParam) ) {
string name = GetAttribute(paramNode, AttrName);
string val = GetAttribute(paramNode, AttrValue);
if (val == null)
if (paramNode.HasChildNodes) {
val = paramNode.InnerText; //TODO: allow for multiple values?
} else {
val = paramNode.InnerText;
}
result.Add(name, val);
}
return result;
}
private static string GetAttribute(XmlElement node, string attr) {
string result = node.GetAttribute(attr);
if (result!=null && result.Trim().Length == 0)
result = null;
return result;
}
}
}