* SiteMapNode.cs (GetExplicitResourceString): implement.
[mono-project.git] / mcs / class / System.Web / System.Web.Compilation / AppResourceFilesCollection.cs
blob9cf705fd6aecf9118e3c3b681819679cbe283763
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.
30 #if NET_2_0
31 using System;
32 using System.Collections.Generic;
33 using System.Globalization;
34 using System.IO;
36 namespace System.Web.Compilation
38 internal enum AppResourceFileKind
40 NotResource,
41 ResX,
42 Resource,
43 Binary
46 internal class AppResourcesLengthComparer<T>: IComparer<T>
48 private int CompareStrings (string a, string b)
50 if (a == null || b == null)
51 return 0;
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) {
59 a = _a as string;
60 b = _b as string;
61 } else if (_a is List<string> && _b is List<string>) {
62 List<string> tmp = _a as List<string>;
63 a = tmp [0];
64 tmp = _b as List<string>;
65 b = tmp [0];
66 } else if (_a is AppResourceFileInfo && _b is AppResourceFileInfo) {
67 AppResourceFileInfo tmp = _a as AppResourceFileInfo;
68 a = tmp.Info.Name;
69 tmp = _b as AppResourceFileInfo;
70 b = tmp.Info.Name;
71 } else
72 return 0;
73 return CompareStrings (a, b);
77 internal class AppResourceFilesCollection
79 List <AppResourceFileInfo> files;
80 bool isGlobal;
81 string sourceDir;
83 public string SourceDir {
84 get { return sourceDir; }
87 public bool HasFiles {
88 get {
89 if (String.IsNullOrEmpty (sourceDir))
90 return false;
91 return files.Count > 0;
95 public List <AppResourceFileInfo> Files {
96 get { return files; }
99 public AppResourceFilesCollection (HttpContext context, bool isGlobal)
101 if (context == null)
102 throw new ArgumentNullException ("context");
104 //this.context = context;
105 this.isGlobal = isGlobal;
106 this.files = new List <AppResourceFileInfo> ();
108 string resourcePath;
109 if (isGlobal)
110 resourcePath = Path.Combine (HttpRuntime.AppDomainAppPath, "App_GlobalResources");
111 else {
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))
124 return;
125 DirectoryInfo di = new DirectoryInfo (sourceDir);
126 FileInfo[] infos = di.GetFiles ();
127 if (infos.Length == 0)
128 return;
130 string extension;
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);
138 else
139 continue;
141 files.Add (arfi);
144 if (isGlobal && files.Count == 0)
145 return;
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))
154 default:
155 kind = AppResourceFileKind.NotResource;
156 return false;
158 case ".resx":
159 kind = AppResourceFileKind.ResX;
160 return true;
162 case ".resource":
163 kind = AppResourceFileKind.Resource;
164 return true;
169 #endif