Clover.NET coverage report - Coverage

Coverage timestamp: Friday, May 20, 2005 9:17:00 PM

File Stats: LOC: 546   Methods: 19
NCLOC: 365 Classes: 2
 
Source File Conditionals Statements Methods TOTAL
Utilities\Resources.cs 21.1 % 42.4 % 47.4 % 38.3 %
coverage coverage
1  
2   #region Apache Notice
3   /*****************************************************************************
4   * $Header: $
5   * $Revision: $
6   * $Date: $
7   *
8   * iBATIS.NET Data Mapper
9   * Copyright (C) 2004 - Gilles Bayon
10   *
11   *
12   * Licensed under the Apache License, Version 2.0 (the "License");
13   * you may not use this file except in compliance with the License.
14   * You may obtain a copy of the License at
15   *
16   * http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing, software
19   * distributed under the License is distributed on an "AS IS" BASIS,
20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   * See the License for the specific language governing permissions and
22   * limitations under the License.
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   /// <summary>
43   /// A class to simplify access to resources.
44   ///
45   /// The file can be loaded from the application root directory
46   /// (use the resource attribute)
47   /// or from any valid URL (use the url attribute).
48   /// For example,to load a fixed path file, use:
49   /// &lt;properties url=”file:///c:/config/my.properties” /&gt;
50   /// </summary>
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   /// <summary>
65   /// The name of the directory containing the application
66   /// </summary>
67   public static string ApplicationBase
68   {
69 3 get
70   {
71 3 return _applicationBase;
72   }
73   }
74  
75   /// <summary>
76   /// The name of the directory used to probe the assemblies.
77   /// </summary>
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   /// <summary>
99   /// Get config file from from the base directory that the assembler
100   /// used for probe assemblies
101   /// </summary>
102   /// <param name="fileName"></param>
103   /// <returns></returns>
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   /// <summary>
126   /// Load an XML resource from a location specify by the node.
127   /// </summary>
128   /// <param name="node">An location node</param>
129   /// <returns>Return the Xml document load.</returns>
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   /// <summary>
151   /// Get the path resource of an url or resource location.
152   /// </summary>
153   /// <param name="node">The specification from where to load.</param>
154   /// <returns></returns>
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   /// <summary>
172   /// Load resource from the root directory of the application
173   /// </summary>
174   /// <param name="resource"></param>
175   /// <returns></returns>
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   /// <summary>
206   ///
207   /// </summary>
208   /// <param name="url"></param>
209   /// <returns></returns>
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   /// <summary>
240   ///
241   /// </summary>
242   /// <param name="fileResource"></param>
243   /// <returns></returns>
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   // foreach(string fileName in assembly.GetManifestResourceNames() )
255   // {
256   // Console.WriteLine(fileName);
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   // bare type name... loop thru all loaded assemblies
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   /// <summary>
331   /// Load a file from a given path
332   /// </summary>
333   /// <param name="path">The path</param>
334   /// <returns>return a FileInfo</returns>
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   /// <summary>
363   /// Replace properties by their values in the given string
364   /// </summary>
365   /// <param name="str"></param>
366   /// <param name="properties"></param>
367   /// <returns></returns>
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   /// <summary>
404   /// Find a type by his class name
405   /// </summary>
406   /// <param name="className">The className ot the type to find.</param>
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   /// <summary>
416   /// Holds data about a <see cref="System.Type"/> and it's
417   /// attendant <see cref="System.Reflection.Assembly"/>.
418   /// </summary>
419   internal class FileAssemblyInfo
420   {
421   #region Constants
422   /// <summary>
423   /// The string that separates file name
424   /// from their attendant <see cref="System.Reflection.Assembly"/>
425   /// names in an assembly qualified type name.
426   /// </summary>
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   /// <summary>
439   /// The resource file name .
440   /// </summary>
441 0 public string ResourceFileName
442   {
443   get
444   {
445   return AssemblyName+"."+FileName;
446   }
447   }
448  
449   /// <summary>
450   /// The original name.
451   /// </summary>
452 0 public string OriginalFileName
453   {
454   get
455   {
456   return _originalFileName;
457   }
458   }
459  
460   /// <summary>
461   /// The file name portion.
462   /// </summary>
463 0 public string FileName
464   {
465   get
466   {
467   return _unresolvedFileName;
468   }
469   }
470  
471   /// <summary>
472   /// The (unresolved, possibly partial) name of the attandant assembly.
473   /// </summary>
474 0 public string AssemblyName
475   {
476   get
477   {
478   return _unresolvedAssemblyName;
479   }
480   }
481  
482   /// <summary>
483   /// Is the type name being resolved assembly qualified?
484   /// </summary>
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   /// <summary>
504   /// Creates a new instance of the FileAssemblyInfo class.
505   /// </summary>
506   /// <param name="unresolvedFileName">
507   /// The unresolved name of a <see cref="System.Type"/>.
508   /// </param>
509 0 public FileAssemblyInfo (string unresolvedFileName)
510   {
511   SplitFileAndAssemblyNames (unresolvedFileName);
512   }
513   #endregion
514  
515   #region Methods
516   /// <summary>
517   ///
518   /// </summary>
519   /// <param name="originalFileName"></param>
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