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
;
35 using System
.Web
.Util
;
37 namespace System
.Web
.Compilation
39 internal enum AppResourceFileKind
47 internal class AppResourcesLengthComparer
<T
>: IComparer
<T
>
49 int CompareStrings (string a
, string b
)
51 if (a
== null || b
== null)
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) {
62 } else if (_a
is List
<string> && _b
is List
<string>) {
63 List
<string> tmp
= _a
as List
<string>;
65 tmp
= _b
as List
<string>;
67 } else if (_a
is AppResourceFileInfo
&& _b
is AppResourceFileInfo
) {
68 AppResourceFileInfo tmp
= _a
as AppResourceFileInfo
;
70 tmp
= _b
as AppResourceFileInfo
;
74 return CompareStrings (a
, b
);
78 internal class AppResourceFilesCollection
80 List
<AppResourceFileInfo
> files
;
84 public string SourceDir
{
85 get { return sourceDir; }
88 public bool HasFiles
{
90 if (String
.IsNullOrEmpty (sourceDir
))
92 return files
.Count
> 0;
96 public List
<AppResourceFileInfo
> Files
{
100 public AppResourceFilesCollection (HttpContext context
)
103 throw new ArgumentNullException ("context");
105 this.isGlobal
= true;
106 this.files
= new List
<AppResourceFileInfo
> ();
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
> ();
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
))
133 DirectoryInfo di
= new DirectoryInfo (sourceDir
);
134 FileInfo
[] infos
= di
.GetFiles ();
135 if (infos
.Length
== 0)
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
);
152 if (isGlobal
&& files
.Count
== 0)
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
))
163 kind
= AppResourceFileKind
.NotResource
;
167 kind
= AppResourceFileKind
.ResX
;
171 kind
= AppResourceFileKind
.Resource
;