DefaultResourceProvider.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // DefaultResourceProvider.cs
  2. //
  3. // Authors:
  4. // Marek Habersack ([email protected])
  5. //
  6. // (C) 2009 Novell, Inc (http://novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Globalization;
  30. using System.IO;
  31. using System.Resources;
  32. using System.Reflection;
  33. using System.Web;
  34. namespace System.Web.Compilation
  35. {
  36. sealed class DefaultResourceProvider : IResourceProvider
  37. {
  38. sealed class ResourceManagerCacheKey
  39. {
  40. readonly string _name;
  41. readonly Assembly _asm;
  42. public ResourceManagerCacheKey (string name, Assembly asm)
  43. {
  44. _name = name;
  45. _asm = asm;
  46. }
  47. public override bool Equals (object obj)
  48. {
  49. if (!(obj is ResourceManagerCacheKey))
  50. return false;
  51. ResourceManagerCacheKey key = (ResourceManagerCacheKey) obj;
  52. return key._asm == _asm && _name.Equals (key._name, StringComparison.Ordinal);
  53. }
  54. public override int GetHashCode ()
  55. {
  56. return _name.GetHashCode () + _asm.GetHashCode ();
  57. }
  58. }
  59. [ThreadStatic]
  60. static Dictionary <ResourceManagerCacheKey, ResourceManager> resourceManagerCache;
  61. string resource;
  62. bool isGlobal;
  63. public IResourceReader ResourceReader {
  64. get {
  65. Assembly asm;
  66. string path;
  67. if (isGlobal) {
  68. asm = HttpContext.AppGlobalResourcesAssembly;
  69. path = resource;
  70. } else {
  71. asm = GetLocalResourcesAssembly ();
  72. path = Path.GetFileName (resource);
  73. if (String.IsNullOrEmpty (path))
  74. return null;
  75. path += ".resources";
  76. }
  77. if (asm == null)
  78. return null;
  79. Stream ms = asm.GetManifestResourceStream (path);
  80. if (ms == null)
  81. return null;
  82. return new ResourceReader (ms);
  83. }
  84. }
  85. public DefaultResourceProvider (string resource, bool isGlobal)
  86. {
  87. if (String.IsNullOrEmpty (resource))
  88. throw new ArgumentNullException ("resource");
  89. this.resource = resource;
  90. this.isGlobal = isGlobal;
  91. }
  92. public object GetObject (string resourceKey, CultureInfo culture)
  93. {
  94. if (String.IsNullOrEmpty (resourceKey))
  95. return null;
  96. ResourceManager rm = GetResourceManager ();
  97. if (rm == null)
  98. return null;
  99. return rm.GetObject (resourceKey, culture);
  100. }
  101. Assembly GetLocalResourcesAssembly ()
  102. {
  103. string path;
  104. Assembly asm;
  105. path = VirtualPathUtility.GetDirectory (resource);
  106. asm = AppResourcesCompiler.GetCachedLocalResourcesAssembly (path);
  107. if (asm == null) {
  108. AppResourcesCompiler ac = new AppResourcesCompiler (path);
  109. asm = ac.Compile ();
  110. if (asm == null)
  111. throw new MissingManifestResourceException ("A resource object was not found at the specified virtualPath.");
  112. }
  113. return asm;
  114. }
  115. ResourceManager GetResourceManager ()
  116. {
  117. string path;
  118. Assembly asm;
  119. if (isGlobal) {
  120. asm = HttpContext.AppGlobalResourcesAssembly;
  121. path = resource;
  122. } else {
  123. asm = GetLocalResourcesAssembly ();
  124. path = Path.GetFileName (resource);
  125. if (String.IsNullOrEmpty (path))
  126. return null;
  127. }
  128. if (asm == null)
  129. return null;
  130. ResourceManager rm;
  131. try {
  132. if (resourceManagerCache == null)
  133. resourceManagerCache = new Dictionary <ResourceManagerCacheKey, ResourceManager> ();
  134. ResourceManagerCacheKey key = new ResourceManagerCacheKey (path, asm);
  135. if (!resourceManagerCache.TryGetValue (key, out rm)) {
  136. rm = new ResourceManager (path, asm);
  137. rm.IgnoreCase = true;
  138. resourceManagerCache.Add (key, rm);
  139. }
  140. return rm;
  141. } catch (MissingManifestResourceException) {
  142. throw;
  143. } catch (Exception ex) {
  144. throw new HttpException ("Failed to retrieve the specified global resource object.", ex);
  145. }
  146. }
  147. }
  148. }