Merge pull request #2826 from mattleibow/mono.options-update
[mono.git] / mcs / class / System.Web / System.Web.Compilation / AppResourceFilesCollection.cs
1 //
2 // System.Web.Compilation.AppResourceFilesCollection
3 //
4 // Authors:
5 //   Marek Habersack (grendello@gmail.com)
6 //
7 // (C) 2006 Marek Habersack
8 //
9
10 //
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.
29 //
30
31 using System;
32 using System.Collections.Generic;
33 using System.Globalization;
34 using System.IO;
35 using System.Web.Util;
36
37 namespace System.Web.Compilation 
38 {
39         internal enum AppResourceFileKind
40         {
41                 NotResource,
42                 ResX,
43                 Resource,
44                 Binary
45         };
46
47         internal class AppResourcesLengthComparer<T>: IComparer<T>
48         {
49                 int CompareStrings (string a, string b)
50                 {
51                         if (a == null || b == null)
52                                 return 0;
53                         return (int)b.Length - (int)a.Length;
54                 }
55
56                 int IComparer<T>.Compare (T _a, T _b) 
57                 {
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);
75                 }
76         }
77         
78         internal class AppResourceFilesCollection
79         {
80                 List <AppResourceFileInfo> files;
81                 bool isGlobal;
82                 string sourceDir;
83
84                 public string SourceDir {
85                         get { return sourceDir; }
86                 }
87                 
88                 public bool HasFiles {
89                         get {
90                                 if (String.IsNullOrEmpty (sourceDir))
91                                         return false;
92                                 return files.Count > 0;
93                         }
94                 }
95
96                 public List <AppResourceFileInfo> Files {
97                         get { return files; }
98                 }
99                 
100                 public AppResourceFilesCollection (HttpContext context)
101                 {
102                         if (context == null)
103                                 throw new ArgumentNullException ("context");
104                         
105                         this.isGlobal = true;
106                         this.files = new List <AppResourceFileInfo> ();
107
108                         string resourcePath;
109                         resourcePath = Path.Combine (HttpRuntime.AppDomainAppPath, "App_GlobalResources");
110                         if (Directory.Exists (resourcePath))
111                                 sourceDir = resourcePath;
112                 }
113
114                 public AppResourceFilesCollection (string parserDir)
115                 {
116                         if (String.IsNullOrEmpty (parserDir))
117                                 throw new ArgumentException ("parserDir cannot be empty");
118                         this.isGlobal = true;
119                         this.files = new List <AppResourceFileInfo> ();
120
121                         string resourcePath;
122                         resourcePath = Path.Combine (parserDir, "App_LocalResources");
123                         if (Directory.Exists (resourcePath)) {
124                                 sourceDir = resourcePath;
125                                 HttpApplicationFactory.WatchLocationForRestart (sourceDir, "*");
126                         }
127                 }
128                 
129                 public void Collect ()
130                 {
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;
137
138                         string extension;
139                         AppResourceFileInfo arfi;
140                         AppResourceFileKind kind;
141                         
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;
148
149                                 files.Add (arfi);
150                         }
151
152                         if (isGlobal && files.Count == 0)
153                                 return;
154                         AppResourcesLengthComparer<AppResourceFileInfo> lcFiles = new AppResourcesLengthComparer<AppResourceFileInfo> ();
155                         files.Sort (lcFiles);
156                 }
157
158                 bool Acceptable (string extension, out AppResourceFileKind kind)
159                 {
160                         switch (extension.ToLower (Helpers.InvariantCulture))
161                         {
162                                 default:
163                                         kind = AppResourceFileKind.NotResource;
164                                         return false;
165
166                                 case ".resx":
167                                         kind = AppResourceFileKind.ResX;
168                                         return true;
169
170                                 case ".resource":
171                                         kind = AppResourceFileKind.Resource;
172                                         return true;
173                         }
174                 }
175         };
176 };
177