-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex-body.ascx.cs
282 lines (253 loc) · 9.21 KB
/
index-body.ascx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Linq;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Index_html : System.Web.UI.UserControl
{
private string _supportedLocales;
protected string SupportedLocales
{
get
{
if (null == _supportedLocales)
_supportedLocales = Serialize(
Enumerate(@"localization\locales\crm", (file) => true)
.Select(item => item.Directory.Name).Distinct());
return _supportedLocales;
}
}
private string _configuration;
protected string Configuration
{
get
{
if (null == _configuration)
_configuration = Serialize(
Enumerate("configuration", (file) => file.Name == "production.js")
.Select(item => item.Path.Substring(0, item.Path.Length - 3)));
return _configuration;
}
}
private string _legacyLocalization;
protected string LegacyLocalization
{
get
{
if (null == _legacyLocalization)
_legacyLocalization = Serialize(
EnumerateLocalizations("localization")
.Select(item => item.Path.Substring(0, item.Path.Length - 3)));
return _legacyLocalization;
}
}
private string _legacyLocalizationFallback;
protected string LegacyLocalizationFallback
{
get
{
if (null == _legacyLocalizationFallback)
_legacyLocalizationFallback = Serialize(
EnumerateLocalizations(string.Empty, "localization", "en")
.Select(item => item.Path.Substring(0, item.Path.Length - 3)));
return _legacyLocalizationFallback;
}
}
private string _localeFiles;
protected string LocaleFiles
{
get
{
if (null == _localeFiles)
_localeFiles = Serialize(
Enumerate(@"localization", (file) => file.Extension == ".l20n" && file.Name.IndexOf("regional") == -1)
.Select(item => item.Path));
return _localeFiles;
}
}
private string _regionalFiles;
protected string RegionalFiles
{
get
{
if (null == _regionalFiles)
_regionalFiles = Serialize(
Enumerate(@"localization", (file) => file.Extension == ".l20n" && file.Name.IndexOf("regional") != -1)
.Select(item => item.Path));
return _regionalFiles;
}
}
private string _filesToCache;
protected string FilesToCache
{
get
{
if (_filesToCache == null) {
IEnumerable<FileItem> [] listings = {
Enumerate(@"content"),
Enumerate(@"localization"),
Enumerate(@"help"),
Enumerate(@"configuration"),
Enumerate(@".", (file) => file.Name == "index.aspx")
};
_filesToCache = Serialize(listings.SelectMany(listing => listing).Select(item => item.Path));
}
return _filesToCache;
}
}
protected string Languages
{
get
{
if (CustomListener.languages.Count == 0)
{
var folders = Enumerate(@"localization\locales\argos", (file) => true)
.Select(item => item.Directory.FullName).Distinct();
foreach (var folder in folders)
{
string[] files = Directory.GetFiles(folder);
foreach (var file in files)
{
if (file.EndsWith("regional.l20n"))
{
var content = File.ReadAllText(file);
Parser(content);
}
}
}
}
return Serialize(CustomListener.languages);
}
}
protected class FileItem
{
public string Path { get; set; }
public FileInfo File { get; set; }
public DirectoryInfo Directory { get; set; }
}
protected string Serialize(object item)
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(item);
}
protected string ToRelativeUrlPath(DirectoryInfo rootDirectory, FileInfo file)
{
var rootPath = rootDirectory.FullName;
var filePath = file.FullName;
if (filePath.StartsWith(rootPath))
{
var relativePath = filePath.Substring(rootPath.Length + 1);
return relativePath.Replace('\\', '/');
}
throw new ApplicationException("Invalid root path specified.");
}
protected IEnumerable<FileItem> Enumerate(string path, Predicate<FileInfo> predicate)
{
var rootDirectory = new DirectoryInfo(Path.GetDirectoryName(Request.PhysicalPath));
var includeDirectory = new DirectoryInfo(Path.Combine(rootDirectory.FullName, path));
if (includeDirectory.Exists)
{
var files = includeDirectory.GetFiles("*", SearchOption.AllDirectories).AsEnumerable();
if (predicate != null) files = files.Where(file => predicate(file));
foreach (var file in files)
yield return new FileItem
{
Path = ToRelativeUrlPath(rootDirectory, file),
File = file,
Directory = file.Directory
};
}
}
protected IEnumerable<FileItem> Enumerate(string path)
{
return Enumerate(path, (file) => true);
}
protected IEnumerable<FileItem> Enumerate(string path, Regex include)
{
return Enumerate(path, (file) => include.IsMatch(file.Name));
}
protected IEnumerable<FileItem> EnumerateLocalizations(string path)
{
return EnumerateLocalizations(String.Empty, path, null);
}
protected IEnumerable<FileItem> EnumerateLocalizations(string root, string path, string culture)
{
var currentCulture = System.Globalization.CultureInfo.CurrentCulture;
var rootDirectory = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(Request.PhysicalPath), root));
var includeDirectory = new DirectoryInfo(Path.Combine(rootDirectory.FullName, path));
if (includeDirectory.Exists)
{
var parentFileName = String.Format(@"{0}.js", culture ?? currentCulture.Parent.Name);
var parentFile = new FileInfo(Path.Combine(includeDirectory.FullName, parentFileName));
var targetFileName = String.Format(@"{0}.js", culture ?? currentCulture.Name);
var targetFile = new FileInfo(Path.Combine(includeDirectory.FullName, targetFileName));
if (targetFile.Exists)
yield return new FileItem
{
Path = ToRelativeUrlPath(rootDirectory, targetFile),
File = targetFile
};
else if (parentFile.Exists)
yield return new FileItem
{
Path = ToRelativeUrlPath(rootDirectory, parentFile),
File = targetFile
};
foreach (var moduleDirectory in includeDirectory.GetDirectories())
{
parentFile = new FileInfo(Path.Combine(moduleDirectory.FullName, parentFileName));
targetFile = new FileInfo(Path.Combine(moduleDirectory.FullName, targetFileName));
if (targetFile.Exists)
yield return new FileItem
{
Path = ToRelativeUrlPath(rootDirectory, targetFile),
File = targetFile
};
else if (parentFile.Exists)
yield return new FileItem
{
Path = ToRelativeUrlPath(rootDirectory, parentFile),
File = targetFile
};
}
}
}
private CultureInfo _culture;
protected CultureInfo CurrentCulture
{
get
{
if (null == _culture)
_culture = System.Globalization.CultureInfo.CurrentCulture;
return _culture;
}
}
private RegionInfo _region;
protected RegionInfo CurrentRegion
{
get
{
if (null == _region)
_region = new RegionInfo(CurrentCulture.LCID);
return _region;
}
}
protected void Parser(string input)
{
ICharStream stream = CharStreams.fromstring(input);
ITokenSource lexer = new L20nLexer(stream);
ITokenStream tokens = new CommonTokenStream(lexer);
L20nParser parser = new L20nParser(tokens);
parser.BuildParseTree = true;
IParseTree tree = parser.document();
CustomListener listener = new CustomListener();
ParseTreeWalker.Default.Walk(listener, tree);
}
}