2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / System.Web / System.Web.Compilation / AppResourceFilesCollection.cs
blob3c4063554df0a2d5a2bb0446dc3c9cfd0ee86e5f
1 //
2 // System.Web.Compilation.AppResourceFilesCollection
3 //
4 // Authors:
5 // Marek Habersack (grendello@gmail.com)
6 //
7 // (C) 2006 Marek Habersack
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
31 using System;
32 using System.Collections.Generic;
33 using System.Globalization;
34 using System.IO;
35 using System.Web.Util;
37 namespace System.Web.Compilation
39 internal enum AppResourceFileKind
41 NotResource,
42 ResX,
43 Resource,
44 Binary
47 internal class AppResourcesLengthComparer<T>: IComparer<T>
49 int CompareStrings (string a, string b)
51 if (a == null || b == null)
52 return 0;
53 return (int)b.Length - (int)a.Length;
56 int IComparer<T>.Compare (T _a, T _b)
58 string a = null, b = null;
59 if (_a is string && _b is string) {
60 a = _a as string;
61 b = _b as string;
62 } else if (_a is List<string> && _b is List<string>) {
63 List<string> tmp = _a as List<string>;
64 a = tmp [0];
65 tmp = _b as List<string>;
66 b = tmp [0];
67 } else if (_a is AppResourceFileInfo && _b is AppResourceFileInfo) {
68 AppResourceFileInfo tmp = _a as AppResourceFileInfo;
69 a = tmp.Info.Name;
70 tmp = _b as AppResourceFileInfo;
71 b = tmp.Info.Name;
72 } else
73 return 0;
74 return CompareStrings (a, b);
78 internal class AppResourceFilesCollection
80 List <AppResourceFileInfo> files;
81 bool isGlobal;
82 string sourceDir;
84 public string SourceDir {
85 get { return sourceDir; }
88 public bool HasFiles {
89 get {
90 if (String.IsNullOrEmpty (sourceDir))
91 return false;
92 return files.Count > 0;
96 public List <AppResourceFileInfo> Files {
97 get { return files; }
100 public AppResourceFilesCollection (HttpContext context)
102 if (context == null)
103 throw new ArgumentNullException ("context");
105 this.isGlobal = true;
106 this.files = new List <AppResourceFileInfo> ();
108 string resourcePath;
109 resourcePath = Path.Combine (HttpRuntime.AppDomainAppPath, "App_GlobalResources");
110 if (Directory.Exists (resourcePath))
111 sourceDir = resourcePath;
114 public AppResourceFilesCollection (string parserDir)
116 if (String.IsNullOrEmpty (parserDir))
117 throw new ArgumentException ("parserDir cannot be empty");
118 this.isGlobal = true;
119 this.files = new List <AppResourceFileInfo> ();
121 string resourcePath;
122 resourcePath = Path.Combine (parserDir, "App_LocalResources");
123 if (Directory.Exists (resourcePath)) {
124 sourceDir = resourcePath;
125 HttpApplicationFactory.WatchLocationForRestart (sourceDir, "*");
129 public void Collect ()
131 if (String.IsNullOrEmpty (sourceDir))
132 return;
133 DirectoryInfo di = new DirectoryInfo (sourceDir);
134 FileInfo[] infos = di.GetFiles ();
135 if (infos.Length == 0)
136 return;
138 string extension;
139 AppResourceFileInfo arfi;
140 AppResourceFileKind kind;
142 foreach (FileInfo fi in infos) {
143 extension = fi.Extension;
144 if (Acceptable (extension, out kind))
145 arfi = new AppResourceFileInfo (fi, kind);
146 else
147 continue;
149 files.Add (arfi);
152 if (isGlobal && files.Count == 0)
153 return;
154 AppResourcesLengthComparer<AppResourceFileInfo> lcFiles = new AppResourcesLengthComparer<AppResourceFileInfo> ();
155 files.Sort (lcFiles);
158 bool Acceptable (string extension, out AppResourceFileKind kind)
160 switch (extension.ToLower (Helpers.InvariantCulture))
162 default:
163 kind = AppResourceFileKind.NotResource;
164 return false;
166 case ".resx":
167 kind = AppResourceFileKind.ResX;
168 return true;
170 case ".resource":
171 kind = AppResourceFileKind.Resource;
172 return true;