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)
102 throw new ArgumentNullException ("context");
104 this.isGlobal = true;
105 this.files = new List <AppResourceFileInfo> ();
108 resourcePath = Path.Combine (HttpRuntime.AppDomainAppPath, "App_GlobalResources");
109 if (Directory.Exists (resourcePath))
110 sourceDir = resourcePath;
113 public AppResourceFilesCollection (string parserDir)
115 if (String.IsNullOrEmpty (parserDir))
116 throw new ArgumentException ("parserDir cannot be empty");
117 this.isGlobal = true;
118 this.files = new List <AppResourceFileInfo> ();
121 resourcePath = Path.Combine (parserDir, "App_LocalResources");
122 if (Directory.Exists (resourcePath)) {
123 sourceDir = resourcePath;
124 HttpApplicationFactory.WatchLocationForRestart (sourceDir, "*");
128 public void Collect ()
130 if (String.IsNullOrEmpty (sourceDir))
132 DirectoryInfo di = new DirectoryInfo (sourceDir);
133 FileInfo[] infos = di.GetFiles ();
134 if (infos.Length == 0)
138 AppResourceFileInfo arfi;
139 AppResourceFileKind kind;
141 foreach (FileInfo fi in infos) {
142 extension = fi.Extension;
143 if (Acceptable (extension, out kind))
144 arfi = new AppResourceFileInfo (fi, kind);
151 if (isGlobal && files.Count == 0)
153 AppResourcesLengthComparer<AppResourceFileInfo> lcFiles = new AppResourcesLengthComparer<AppResourceFileInfo> ();
154 files.Sort (lcFiles);
157 bool Acceptable (string extension, out AppResourceFileKind kind)
159 switch (extension.ToLower (CultureInfo.InvariantCulture))
162 kind = AppResourceFileKind.NotResource;
166 kind = AppResourceFileKind.ResX;
170 kind = AppResourceFileKind.Resource;