AppResourceFilesCollection.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // System.Web.Compilation.AppResourceFilesCollection
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2006 Marek Habersack
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Globalization;
  33. using System.IO;
  34. namespace System.Web.Compilation
  35. {
  36. internal enum AppResourceFileKind
  37. {
  38. NotResource,
  39. ResX,
  40. Resource,
  41. Binary
  42. };
  43. internal class AppResourcesLengthComparer<T>: IComparer<T>
  44. {
  45. private int CompareStrings (string a, string b)
  46. {
  47. if (a == null || b == null)
  48. return 0;
  49. return (int)b.Length - (int)a.Length;
  50. }
  51. int IComparer<T>.Compare (T _a, T _b)
  52. {
  53. string a = null, b = null;
  54. if (_a is string && _b is string) {
  55. a = _a as string;
  56. b = _b as string;
  57. } else if (_a is List<string> && _b is List<string>) {
  58. List<string> tmp = _a as List<string>;
  59. a = tmp [0];
  60. tmp = _b as List<string>;
  61. b = tmp [0];
  62. } else if (_a is AppResourceFileInfo && _b is AppResourceFileInfo) {
  63. AppResourceFileInfo tmp = _a as AppResourceFileInfo;
  64. a = tmp.Info.Name;
  65. tmp = _b as AppResourceFileInfo;
  66. b = tmp.Info.Name;
  67. } else
  68. return 0;
  69. return CompareStrings (a, b);
  70. }
  71. }
  72. internal class AppResourceFilesCollection
  73. {
  74. List <AppResourceFileInfo> files;
  75. bool isGlobal;
  76. string sourceDir;
  77. public string SourceDir {
  78. get { return sourceDir; }
  79. }
  80. public bool HasFiles {
  81. get {
  82. if (String.IsNullOrEmpty (sourceDir))
  83. return false;
  84. return files.Count > 0;
  85. }
  86. }
  87. public List <AppResourceFileInfo> Files {
  88. get { return files; }
  89. }
  90. public AppResourceFilesCollection (HttpContext context, bool isGlobal)
  91. {
  92. if (context == null)
  93. throw new ArgumentNullException ("context");
  94. //this.context = context;
  95. this.isGlobal = isGlobal;
  96. this.files = new List <AppResourceFileInfo> ();
  97. string resourcePath;
  98. if (isGlobal)
  99. resourcePath = Path.Combine (HttpRuntime.AppDomainAppPath, "App_GlobalResources");
  100. else {
  101. HttpRequest request = context.Request;
  102. resourcePath = Path.Combine (
  103. Path.GetDirectoryName (request.MapPath (request.CurrentExecutionFilePath)),
  104. "App_LocalResources");
  105. }
  106. if (Directory.Exists (resourcePath))
  107. sourceDir = resourcePath;
  108. }
  109. public void Collect ()
  110. {
  111. if (String.IsNullOrEmpty (sourceDir))
  112. return;
  113. DirectoryInfo di = new DirectoryInfo (sourceDir);
  114. FileInfo[] infos = di.GetFiles ();
  115. if (infos.Length == 0)
  116. return;
  117. string extension;
  118. AppResourceFileInfo arfi;
  119. AppResourceFileKind kind;
  120. foreach (FileInfo fi in infos) {
  121. extension = fi.Extension;
  122. if (Acceptable (extension, out kind))
  123. arfi = new AppResourceFileInfo (fi, kind);
  124. else
  125. continue;
  126. files.Add (arfi);
  127. }
  128. if (isGlobal && files.Count == 0)
  129. return;
  130. AppResourcesLengthComparer<AppResourceFileInfo> lcFiles = new AppResourcesLengthComparer<AppResourceFileInfo> ();
  131. files.Sort (lcFiles);
  132. }
  133. bool Acceptable (string extension, out AppResourceFileKind kind)
  134. {
  135. switch (extension.ToLower (CultureInfo.InvariantCulture))
  136. {
  137. default:
  138. kind = AppResourceFileKind.NotResource;
  139. return false;
  140. case ".resx":
  141. kind = AppResourceFileKind.ResX;
  142. return true;
  143. case ".resource":
  144. kind = AppResourceFileKind.Resource;
  145. return true;
  146. }
  147. }
  148. };
  149. };
  150. #endif