2 // System.Web.Compilation.AppResourceFilesCollection
5 // Marek Habersack (grendello@gmail.com)
7 // (C) 2006 Marek Habersack
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections.Generic;
33 using System.Globalization;
36 namespace System.Web.Compilation
38 internal enum AppResourceFileKind
46 internal class AppResourcesLengthComparer<T>: IComparer<T>
48 private int CompareStrings (string a, string b)
50 if (a == null || b == null)
52 return (int)b.Length - (int)a.Length;
55 int IComparer<T>.Compare (T _a, T _b)
57 string a = null, b = null;
58 if (_a is string && _b is string) {
61 } else if (_a is List<string> && _b is List<string>) {
62 List<string> tmp = _a as List<string>;
64 tmp = _b as List<string>;
66 } else if (_a is AppResourceFileInfo && _b is AppResourceFileInfo) {
67 AppResourceFileInfo tmp = _a as AppResourceFileInfo;
69 tmp = _b as AppResourceFileInfo;
73 return CompareStrings (a, b);
77 internal class AppResourceFilesCollection
79 List <AppResourceFileInfo> files;
83 public string SourceDir {
84 get { return sourceDir; }
87 public bool HasFiles {
89 if (String.IsNullOrEmpty (sourceDir))
91 return files.Count > 0;
95 public List <AppResourceFileInfo> Files {
99 public AppResourceFilesCollection (HttpContext context, bool isGlobal)
102 throw new ArgumentNullException ("context");
104 //this.context = context;
105 this.isGlobal = isGlobal;
106 this.files = new List <AppResourceFileInfo> ();
110 resourcePath = Path.Combine (HttpRuntime.AppDomainAppPath, "App_GlobalResources");
112 HttpRequest request = context.Request;
113 resourcePath = Path.Combine (
114 Path.GetDirectoryName (request.MapPath (request.CurrentExecutionFilePath)),
115 "App_LocalResources");
117 if (Directory.Exists (resourcePath))
118 sourceDir = resourcePath;
121 public void Collect ()
123 if (String.IsNullOrEmpty (sourceDir))
125 DirectoryInfo di = new DirectoryInfo (sourceDir);
126 FileInfo[] infos = di.GetFiles ();
127 if (infos.Length == 0)
131 AppResourceFileInfo arfi;
132 AppResourceFileKind kind;
134 foreach (FileInfo fi in infos) {
135 extension = fi.Extension;
136 if (Acceptable (extension, out kind))
137 arfi = new AppResourceFileInfo (fi, kind);
144 if (isGlobal && files.Count == 0)
146 AppResourcesLengthComparer<AppResourceFileInfo> lcFiles = new AppResourcesLengthComparer<AppResourceFileInfo> ();
147 files.Sort (lcFiles);
150 bool Acceptable (string extension, out AppResourceFileKind kind)
152 switch (extension.ToLower (CultureInfo.InvariantCulture))
155 kind = AppResourceFileKind.NotResource;
159 kind = AppResourceFileKind.ResX;
163 kind = AppResourceFileKind.Resource;