1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
#region Using
|
28
|
|
using System;
|
29
|
|
using System.Collections.Specialized;
|
30
|
|
using System.IO;
|
31
|
|
using System.Xml;
|
32
|
|
using System.Reflection;
|
33
|
|
|
34
|
|
using log4net;
|
35
|
|
|
36
|
|
using IBatisNet.Common.Exceptions;
|
37
|
|
using IBatisNet.Common.Utilities.TypesResolver;
|
38
|
|
#endregion
|
39
|
|
|
40
|
|
namespace IBatisNet.Common.Utilities
|
41
|
|
{
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
|
|
51
|
|
public class Resources
|
52
|
|
{
|
53
|
|
|
54
|
|
#region Fields
|
55
|
|
private static string _applicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
|
56
|
|
private static string _baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
57
|
|
private static CachedTypeResolver _cachedTypeResolver = null;
|
58
|
|
|
59
|
|
private static readonly ILog _logger = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
|
60
|
|
|
61
|
|
#endregion
|
62
|
|
|
63
|
|
#region Properties
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
|
public static string ApplicationBase
|
68
|
|
{
|
69
|
3
|
get
|
70
|
|
{
|
71
|
3
|
return _applicationBase;
|
72
|
|
}
|
73
|
|
}
|
74
|
|
|
75
|
|
|
76
|
|
|
77
|
|
|
78
|
0
|
public static string BaseDirectory
|
79
|
|
{
|
80
|
|
get
|
81
|
|
{
|
82
|
|
return _baseDirectory;
|
83
|
|
}
|
84
|
|
}
|
85
|
|
|
86
|
|
|
87
|
|
#endregion
|
88
|
|
|
89
|
|
#region Constructor (s) / Destructor
|
90
|
1
|
static Resources()
|
91
|
|
{
|
92
|
1
|
_cachedTypeResolver = new CachedTypeResolver();
|
93
|
|
}
|
94
|
|
#endregion
|
95
|
|
|
96
|
|
#region Methods
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
|
|
102
|
|
|
103
|
|
|
104
|
340
|
public static XmlDocument GetConfigAsXmlDocument(string fileName)
|
105
|
|
{
|
106
|
340
|
XmlDocument config = new XmlDocument();
|
107
|
|
|
108
|
340
|
try
|
109
|
|
{
|
110
|
340
|
XmlTextReader reader = new XmlTextReader(Path.Combine(_baseDirectory, fileName));
|
111
|
340
|
config.Load(reader);
|
112
|
340
|
reader.Close();
|
113
|
|
}
|
114
|
|
catch(Exception e)
|
115
|
|
{
|
116
|
0
|
throw new ConfigurationException(
|
117
|
|
string.Format("Unable to load config file \"{0}\". Cause : ",
|
118
|
|
fileName,
|
119
|
|
e.Message ) ,e);
|
120
|
|
}
|
121
|
|
|
122
|
340
|
return config;
|
123
|
|
}
|
124
|
|
|
125
|
|
|
126
|
|
|
127
|
|
|
128
|
|
|
129
|
|
|
130
|
2040
|
public static XmlDocument GetAsXmlDocument(XmlNode node)
|
131
|
|
{
|
132
|
2040
|
XmlDocument xmlDocument = null;
|
133
|
|
|
134
|
2040
|
if (node.Attributes["resource"] != null)
|
135
|
|
{
|
136
|
2040
|
xmlDocument = Resources.GetResourceAsXmlDocument(node.Attributes["resource"].Value);
|
137
|
|
}
|
138
|
0
|
else if (node.Attributes["url"] != null)
|
139
|
|
{
|
140
|
|
xmlDocument = Resources.GetUrlAsXmlDocument(node.Attributes["url"].Value);
|
141
|
|
}
|
142
|
|
else if (node.Attributes["embedded"] != null)
|
143
|
|
{
|
144
|
|
xmlDocument = Resources.GetEmbeddedResourceAsXmlDocument(node.Attributes["embedded"].Value);
|
145
|
|
}
|
146
|
|
|
147
|
2040
|
return xmlDocument;
|
148
|
|
}
|
149
|
|
|
150
|
|
|
151
|
|
|
152
|
|
|
153
|
|
|
154
|
|
|
155
|
1870
|
public static string GetValueOfNodeResourceUrl(XmlNode node)
|
156
|
|
{
|
157
|
1870
|
string path = null;
|
158
|
|
|
159
|
1870
|
if (node.Attributes["resource"] != null)
|
160
|
|
{
|
161
|
1870
|
path = Path.Combine(_applicationBase, node.Attributes["resource"].Value);
|
162
|
|
}
|
163
|
0
|
else if (node.Attributes["url"] != null)
|
164
|
|
{
|
165
|
|
path = node.Attributes["url"].Value;
|
166
|
|
}
|
167
|
|
|
168
|
1870
|
return path;
|
169
|
|
}
|
170
|
|
|
171
|
|
|
172
|
|
|
173
|
|
|
174
|
|
|
175
|
|
|
176
|
2040
|
public static XmlDocument GetResourceAsXmlDocument(string resource)
|
177
|
|
{
|
178
|
2040
|
string file = string.Empty;
|
179
|
2040
|
XmlDocument config = new XmlDocument();
|
180
|
2040
|
XmlTextReader reader = null;
|
181
|
|
|
182
|
2040
|
try
|
183
|
|
{
|
184
|
2040
|
reader = new XmlTextReader(Path.Combine(_applicationBase, resource));
|
185
|
2040
|
config.Load(reader);
|
186
|
|
}
|
187
|
|
catch(Exception e)
|
188
|
|
{
|
189
|
0
|
throw new ConfigurationException(
|
190
|
|
string.Format("Unable to load file via resource \"{0}\" as resource. Cause : ",
|
191
|
|
resource,
|
192
|
|
e.Message ) ,e);
|
193
|
|
}
|
194
|
|
finally
|
195
|
|
{
|
196
|
2040
|
if (reader != null)
|
197
|
|
{
|
198
|
2040
|
reader.Close();
|
199
|
|
}
|
200
|
|
}
|
201
|
|
|
202
|
2040
|
return config;
|
203
|
|
}
|
204
|
|
|
205
|
|
|
206
|
|
|
207
|
|
|
208
|
|
|
209
|
|
|
210
|
0
|
public static XmlDocument GetUrlAsXmlDocument(string url)
|
211
|
|
{
|
212
|
|
string file = string.Empty;
|
213
|
|
XmlDocument config = new XmlDocument();
|
214
|
|
XmlTextReader reader = null;
|
215
|
|
|
216
|
|
try
|
217
|
|
{
|
218
|
|
reader = new XmlTextReader(url);
|
219
|
|
config.Load(reader);
|
220
|
|
}
|
221
|
|
catch(Exception e)
|
222
|
|
{
|
223
|
|
throw new ConfigurationException(
|
224
|
|
string.Format("Unable to load file via url \"{0}\" as url. Cause : ",
|
225
|
|
url,
|
226
|
|
e.Message ) ,e);
|
227
|
|
}
|
228
|
|
finally
|
229
|
|
{
|
230
|
|
if (reader != null)
|
231
|
|
{
|
232
|
|
reader.Close();
|
233
|
|
}
|
234
|
|
}
|
235
|
|
|
236
|
|
return config;
|
237
|
|
}
|
238
|
|
|
239
|
|
|
240
|
|
|
241
|
|
|
242
|
|
|
243
|
|
|
244
|
0
|
public static XmlDocument GetEmbeddedResourceAsXmlDocument(string fileResource)
|
245
|
|
{
|
246
|
|
XmlDocument config = new XmlDocument();
|
247
|
|
bool isLoad = false;
|
248
|
|
XmlTextReader reader = null;
|
249
|
|
|
250
|
|
FileAssemblyInfo fileInfo = new FileAssemblyInfo (fileResource);
|
251
|
|
if (fileInfo.IsAssemblyQualified)
|
252
|
|
{
|
253
|
|
Assembly assembly = Assembly.LoadWithPartialName (fileInfo.AssemblyName);
|
254
|
|
|
255
|
|
|
256
|
|
|
257
|
|
|
258
|
|
|
259
|
|
Stream resource = assembly.GetManifestResourceStream(fileInfo.ResourceFileName);
|
260
|
|
if (resource != null)
|
261
|
|
{
|
262
|
|
try
|
263
|
|
{
|
264
|
|
reader = new XmlTextReader(resource);
|
265
|
|
config.Load(reader);
|
266
|
|
isLoad = true;
|
267
|
|
}
|
268
|
|
catch(Exception e)
|
269
|
|
{
|
270
|
|
throw new ConfigurationException(
|
271
|
|
string.Format("Unable to load file \"{0}\" in embedded resource. Cause : ",
|
272
|
|
fileResource,
|
273
|
|
e.Message ) ,e);
|
274
|
|
}
|
275
|
|
finally
|
276
|
|
{
|
277
|
|
if (reader != null)
|
278
|
|
{
|
279
|
|
reader.Close();
|
280
|
|
}
|
281
|
|
}
|
282
|
|
}
|
283
|
|
}
|
284
|
|
else
|
285
|
|
{
|
286
|
|
|
287
|
|
Assembly [] assemblies = AppDomain.CurrentDomain.GetAssemblies ();
|
288
|
|
foreach (Assembly assembly in assemblies)
|
289
|
|
{
|
290
|
|
Stream resource = assembly.GetManifestResourceStream(fileInfo.FileName);
|
291
|
|
if (resource != null)
|
292
|
|
{
|
293
|
|
try
|
294
|
|
{
|
295
|
|
reader = new XmlTextReader(resource);
|
296
|
|
config.Load(reader);
|
297
|
|
isLoad = true;
|
298
|
|
}
|
299
|
|
catch(Exception e)
|
300
|
|
{
|
301
|
|
throw new ConfigurationException(
|
302
|
|
string.Format("Unable to load file \"{0}\" in embedded resource. Cause : ",
|
303
|
|
fileResource,
|
304
|
|
e.Message ) ,e);
|
305
|
|
}
|
306
|
|
finally
|
307
|
|
{
|
308
|
|
if (reader != null)
|
309
|
|
{
|
310
|
|
reader.Close();
|
311
|
|
}
|
312
|
|
}
|
313
|
|
break;
|
314
|
|
}
|
315
|
|
}
|
316
|
|
}
|
317
|
|
|
318
|
|
if (isLoad == false)
|
319
|
|
{
|
320
|
|
_logger.Error("Could not load embedded resource from assembly");
|
321
|
|
throw new ConfigurationException(
|
322
|
|
string.Format("Unable to load embedded resource from assembly \"{0}\".",
|
323
|
|
fileInfo.OriginalFileName));
|
324
|
|
}
|
325
|
|
|
326
|
|
return config;
|
327
|
|
}
|
328
|
|
|
329
|
|
|
330
|
|
|
331
|
|
|
332
|
|
|
333
|
|
|
334
|
|
|
335
|
2040
|
public static FileInfo GetFileInfo(string path)
|
336
|
|
{
|
337
|
2040
|
string file = string.Empty;
|
338
|
|
|
339
|
2040
|
if (!File.Exists(path))
|
340
|
|
{
|
341
|
0
|
file = Path.Combine(_applicationBase, path);
|
342
|
|
}
|
343
|
|
else
|
344
|
|
{
|
345
|
2040
|
file = path;
|
346
|
|
}
|
347
|
2040
|
FileInfo fileInfo = null;
|
348
|
2040
|
try
|
349
|
|
{
|
350
|
2040
|
fileInfo = new FileInfo(file);
|
351
|
|
}
|
352
|
|
catch
|
353
|
|
{
|
354
|
0
|
_logger.Error("Could not load file from path : " + path);
|
355
|
0
|
throw new ConfigurationException(
|
356
|
|
string.Format("Unable to load file \"{0}\" from path.", path));
|
357
|
|
}
|
358
|
2040
|
return fileInfo;
|
359
|
|
}
|
360
|
|
|
361
|
|
|
362
|
|
|
363
|
|
|
364
|
|
|
365
|
|
|
366
|
|
|
367
|
|
|
368
|
34850
|
public static string ParsePropertyTokens(string str, NameValueCollection properties)
|
369
|
|
{
|
370
|
34850
|
string OPEN = "${";
|
371
|
34850
|
string CLOSE = "}";
|
372
|
|
|
373
|
34850
|
string newString = str;
|
374
|
34850
|
if (newString != null && properties != null)
|
375
|
|
{
|
376
|
34850
|
int start = newString.IndexOf(OPEN);
|
377
|
34850
|
int end = newString.IndexOf(CLOSE);
|
378
|
|
|
379
|
36210
|
while (start > -1 && end > start)
|
380
|
|
{
|
381
|
1360
|
string prepend = newString.Substring(0, start);
|
382
|
1360
|
string append = newString.Substring(end + CLOSE.Length);
|
383
|
|
|
384
|
1360
|
int index = start + OPEN.Length;
|
385
|
1360
|
string propName = newString.Substring(index, end-index);
|
386
|
1360
|
string propValue = properties.Get(propName);
|
387
|
1360
|
if (propValue == null)
|
388
|
|
{
|
389
|
0
|
newString = prepend + propName + append;
|
390
|
|
}
|
391
|
|
else
|
392
|
|
{
|
393
|
1360
|
newString = prepend + propValue + append;
|
394
|
|
}
|
395
|
1360
|
start = newString.IndexOf(OPEN);
|
396
|
1360
|
end = newString.IndexOf(CLOSE);
|
397
|
|
}
|
398
|
|
}
|
399
|
34850
|
return newString;
|
400
|
|
}
|
401
|
|
|
402
|
|
|
403
|
|
|
404
|
|
|
405
|
|
|
406
|
|
|
407
|
24310
|
public static Type TypeForName(string className)
|
408
|
|
{
|
409
|
24310
|
return _cachedTypeResolver.Resolve(className);
|
410
|
|
}
|
411
|
|
|
412
|
|
#endregion
|
413
|
|
|
414
|
|
#region Inner Class : FileAssemblyInfo
|
415
|
|
|
416
|
|
|
417
|
|
|
418
|
|
|
419
|
|
internal class FileAssemblyInfo
|
420
|
|
{
|
421
|
|
#region Constants
|
422
|
|
|
423
|
|
|
424
|
|
|
425
|
|
|
426
|
|
|
427
|
|
public const string FileAssemblySeparator = ",";
|
428
|
|
#endregion
|
429
|
|
|
430
|
|
#region Fields
|
431
|
|
private string _unresolvedAssemblyName= string.Empty;
|
432
|
|
private string _unresolvedFileName= string.Empty;
|
433
|
|
private string _originalFileName= string.Empty;
|
434
|
|
#endregion
|
435
|
|
|
436
|
|
#region Properties
|
437
|
|
|
438
|
|
|
439
|
|
|
440
|
|
|
441
|
0
|
public string ResourceFileName
|
442
|
|
{
|
443
|
|
get
|
444
|
|
{
|
445
|
|
return AssemblyName+"."+FileName;
|
446
|
|
}
|
447
|
|
}
|
448
|
|
|
449
|
|
|
450
|
|
|
451
|
|
|
452
|
0
|
public string OriginalFileName
|
453
|
|
{
|
454
|
|
get
|
455
|
|
{
|
456
|
|
return _originalFileName;
|
457
|
|
}
|
458
|
|
}
|
459
|
|
|
460
|
|
|
461
|
|
|
462
|
|
|
463
|
0
|
public string FileName
|
464
|
|
{
|
465
|
|
get
|
466
|
|
{
|
467
|
|
return _unresolvedFileName;
|
468
|
|
}
|
469
|
|
}
|
470
|
|
|
471
|
|
|
472
|
|
|
473
|
|
|
474
|
0
|
public string AssemblyName
|
475
|
|
{
|
476
|
|
get
|
477
|
|
{
|
478
|
|
return _unresolvedAssemblyName;
|
479
|
|
}
|
480
|
|
}
|
481
|
|
|
482
|
|
|
483
|
|
|
484
|
|
|
485
|
0
|
public bool IsAssemblyQualified
|
486
|
|
{
|
487
|
|
get
|
488
|
|
{
|
489
|
|
if (AssemblyName == null || AssemblyName.Trim().Length==0)
|
490
|
|
{
|
491
|
|
return false;
|
492
|
|
}
|
493
|
|
else
|
494
|
|
{
|
495
|
|
return true;
|
496
|
|
}
|
497
|
|
}
|
498
|
|
}
|
499
|
|
|
500
|
|
#endregion
|
501
|
|
|
502
|
|
#region Constructor (s) / Destructor
|
503
|
|
|
504
|
|
|
505
|
|
|
506
|
|
|
507
|
|
|
508
|
|
|
509
|
0
|
public FileAssemblyInfo (string unresolvedFileName)
|
510
|
|
{
|
511
|
|
SplitFileAndAssemblyNames (unresolvedFileName);
|
512
|
|
}
|
513
|
|
#endregion
|
514
|
|
|
515
|
|
#region Methods
|
516
|
|
|
517
|
|
|
518
|
|
|
519
|
|
|
520
|
0
|
private void SplitFileAndAssemblyNames (string originalFileName)
|
521
|
|
{
|
522
|
|
_originalFileName = originalFileName;
|
523
|
|
int separatorIndex = originalFileName.IndexOf (
|
524
|
|
FileAssemblyInfo.FileAssemblySeparator);
|
525
|
|
if (separatorIndex < 0)
|
526
|
|
{
|
527
|
|
throw new ConfigurationException(
|
528
|
|
string.Format("Unable to find assembly part to load embedded resource in string \"{0}\". Cause : ",
|
529
|
|
originalFileName));
|
530
|
|
}
|
531
|
|
else
|
532
|
|
{
|
533
|
|
_unresolvedFileName = originalFileName.Substring (
|
534
|
|
0, separatorIndex).Trim ();
|
535
|
|
_unresolvedAssemblyName = originalFileName.Substring (
|
536
|
|
separatorIndex + 1).Trim ();
|
537
|
|
}
|
538
|
|
}
|
539
|
|
#endregion
|
540
|
|
|
541
|
|
}
|
542
|
|
#endregion
|
543
|
|
|
544
|
|
}
|
545
|
|
}
|
546
|
|
|