-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathPropertiesHelper.cs
184 lines (167 loc) · 4.83 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
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
using System;
using System.Collections;
using System.Xml;
namespace NHibernate.Util
{
//Much of this code is taken from Maverick.NET
/// <summary></summary>
public sealed class PropertiesHelper
{
private PropertiesHelper()
{
// should not be created
}
/// <summary>
///
/// </summary>
/// <param name="property"></param>
/// <param name="properties"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool GetBoolean( string property, IDictionary properties, bool defaultValue )
{
return properties[ property ] == null ?
defaultValue :
bool.Parse( properties[ property ] as string );
}
/// <summary>
///
/// </summary>
/// <param name="property"></param>
/// <param name="properties"></param>
/// <returns></returns>
public static bool GetBoolean( string property, IDictionary properties )
{
return properties[ property ] == null ?
false :
bool.Parse( properties[ property ] as string );
}
/// <summary>
///
/// </summary>
/// <param name="property"></param>
/// <param name="properties"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int GetInt32( string property, IDictionary properties, int defaultValue )
{
string propValue = properties[ property ] as string;
return ( propValue == null ) ? defaultValue : int.Parse( propValue );
}
/// <summary>
///
/// </summary>
/// <param name="property"></param>
/// <param name="properties"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static long GetInt64(string property, IDictionary properties, long defaultValue)
{
string propValue = properties[property] as string;
return (propValue == null) ? defaultValue : long.Parse(propValue);
}
/// <summary>
///
/// </summary>
/// <param name="property"></param>
/// <param name="properties"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static string GetString( string property, IDictionary properties, string defaultValue )
{
string propValue = properties[ property ] as string;
return ( propValue == null ) ? defaultValue : propValue;
}
/// <summary>
///
/// </summary>
/// <param name="property"></param>
/// <param name="delim"></param>
/// <param name="properties"></param>
/// <returns></returns>
public static IDictionary ToDictionary( string property, string delim, IDictionary properties )
{
IDictionary map = new Hashtable();
string propValue = ( string ) properties[ property ];
if( propValue != null )
{
StringTokenizer tokens = new StringTokenizer( propValue, delim, false );
IEnumerator en = tokens.GetEnumerator();
while( en.MoveNext() )
{
string key = ( string ) en.Current;
string value = en.MoveNext() ? ( string ) en.Current : String.Empty;
map[ key ] = value;
}
}
return map;
}
/// <summary>
///
/// </summary>
/// <param name="property"></param>
/// <param name="delim"></param>
/// <param name="properties"></param>
/// <returns></returns>
public static string[ ] ToStringArray( string property, string delim, IDictionary properties )
{
return ToStringArray( ( string ) properties[ property ], delim );
}
/// <summary>
///
/// </summary>
/// <param name="propValue"></param>
/// <param name="delim"></param>
/// <returns></returns>
public static string[ ] ToStringArray( string propValue, string delim )
{
if( propValue != null )
{
return StringHelper.Split( delim, propValue );
}
else
{
return new string[0];
}
}
/// <summary></summary>
public static readonly string TagParam = "param";
/// <summary></summary>
public static readonly string AttrValue = "value";
/// <summary></summary>
public static readonly 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;
}
}
}