| 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 |
namespace I18N.Common |
| 26 |
{ |
| 27 |
|
| 28 |
using System; |
| 29 |
using System.IO; |
| 30 |
using System.Text; |
| 31 |
using System.Globalization; |
| 32 |
using System.Collections; |
| 33 |
using System.Reflection; |
| 34 |
using System.Security; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class Manager |
| 42 |
{ |
| 43 |
|
| 44 |
private static Manager manager; |
| 45 |
|
| 46 |
|
| 47 |
private Hashtable handlers; |
| 48 |
private Hashtable active; |
| 49 |
private Hashtable assemblies; |
| 50 |
static readonly object lockobj = new object (); |
| 51 |
|
| 52 |
|
| 53 |
private Manager() |
| 54 |
{ |
| 55 |
handlers = new Hashtable (CaseInsensitiveHashCodeProvider.Default, |
| 56 |
CaseInsensitiveComparer.Default); |
| 57 |
active = new Hashtable(16); |
| 58 |
assemblies = new Hashtable(8); |
| 59 |
LoadClassList(); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
public static Manager PrimaryManager |
| 64 |
{ |
| 65 |
get |
| 66 |
{ |
| 67 |
lock(lockobj) |
| 68 |
{ |
| 69 |
if(manager == null) |
| 70 |
{ |
| 71 |
manager = new Manager(); |
| 72 |
} |
| 73 |
return manager; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private static String Normalize(String name) |
| 81 |
{ |
| 82 |
#if ECMA_COMPAT |
| 83 |
return (name.ToLower()).Replace('-', '_'); |
| 84 |
#else |
| 85 |
return (name.ToLower(CultureInfo.InvariantCulture)) |
| 86 |
.Replace('-', '_'); |
| 87 |
#endif |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public Encoding GetEncoding(int codePage) |
| 93 |
{ |
| 94 |
return (Instantiate("CP" + codePage.ToString()) as Encoding); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public Encoding GetEncoding(String name) |
| 100 |
{ |
| 101 |
|
| 102 |
if(name == null) |
| 103 |
{ |
| 104 |
return null; |
| 105 |
} |
| 106 |
|
| 107 |
string orgName = name; |
| 108 |
|
| 109 |
|
| 110 |
name = Normalize(name); |
| 111 |
|
| 112 |
|
| 113 |
Encoding e = Instantiate ("ENC" + name) as Encoding; |
| 114 |
if (e == null) |
| 115 |
e = Instantiate (name) as Encoding; |
| 116 |
|
| 117 |
if (e == null) { |
| 118 |
|
| 119 |
string alias = Handlers.GetAlias (name); |
| 120 |
if (alias != null) { |
| 121 |
e = Instantiate ("ENC" + alias) as Encoding; |
| 122 |
if (e == null) |
| 123 |
e = Instantiate (alias) as Encoding; |
| 124 |
} |
| 125 |
} |
| 126 |
if (e == null) |
| 127 |
return null; |
| 128 |
|
| 129 |
|
| 130 |
if (orgName.IndexOf ('_') > 0 && e.WebName.IndexOf ('-') > 0) |
| 131 |
return null; |
| 132 |
if (orgName.IndexOf ('-') > 0 && e.WebName.IndexOf ('_') > 0) |
| 133 |
return null; |
| 134 |
return e; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
private const String hex = "0123456789abcdef"; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public CultureInfo GetCulture(int culture, bool useUserOverride) |
| 143 |
{ |
| 144 |
|
| 145 |
StringBuilder builder = new StringBuilder(); |
| 146 |
builder.Append(hex[(culture >> 12) & 0x0F]); |
| 147 |
builder.Append(hex[(culture >> 8) & 0x0F]); |
| 148 |
builder.Append(hex[(culture >> 4) & 0x0F]); |
| 149 |
builder.Append(hex[culture & 0x0F]); |
| 150 |
String name = builder.ToString(); |
| 151 |
|
| 152 |
|
| 153 |
if(useUserOverride) |
| 154 |
{ |
| 155 |
Object obj = Instantiate("CIDO" + name); |
| 156 |
if(obj != null) |
| 157 |
{ |
| 158 |
return (obj as CultureInfo); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
return (Instantiate("CID" + name) as CultureInfo); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public CultureInfo GetCulture(String name, bool useUserOverride) |
| 169 |
{ |
| 170 |
|
| 171 |
if(name == null) |
| 172 |
{ |
| 173 |
return null; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
name = Normalize(name); |
| 178 |
|
| 179 |
|
| 180 |
if(useUserOverride) |
| 181 |
{ |
| 182 |
Object obj = Instantiate("CNO" + name.ToString()); |
| 183 |
if(obj != null) |
| 184 |
{ |
| 185 |
return (obj as CultureInfo); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
return (Instantiate("CN" + name.ToString()) as CultureInfo); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
internal Object Instantiate(String name) |
| 196 |
{ |
| 197 |
Object handler; |
| 198 |
String region; |
| 199 |
Assembly assembly; |
| 200 |
Type type; |
| 201 |
|
| 202 |
lock(this) |
| 203 |
{ |
| 204 |
|
| 205 |
handler = active[name]; |
| 206 |
if(handler != null) |
| 207 |
{ |
| 208 |
return handler; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
region = (String)(handlers[name]); |
| 213 |
if(region == null) |
| 214 |
{ |
| 215 |
|
| 216 |
return null; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
assembly = (Assembly)(assemblies[region]); |
| 221 |
if(assembly == null) |
| 222 |
{ |
| 223 |
try |
| 224 |
{ |
| 225 |
|
| 226 |
AssemblyName myName = typeof(Manager).Assembly.GetName(); |
| 227 |
myName.Name = region; |
| 228 |
assembly = Assembly.Load(myName); |
| 229 |
} |
| 230 |
catch(SystemException) |
| 231 |
{ |
| 232 |
assembly = null; |
| 233 |
} |
| 234 |
if(assembly == null) |
| 235 |
{ |
| 236 |
return null; |
| 237 |
} |
| 238 |
assemblies[region] = assembly; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
type = assembly.GetType(region + "." + name, false, true); |
| 243 |
if(type == null) |
| 244 |
{ |
| 245 |
return null; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
try |
| 251 |
{ |
| 252 |
handler = Activator.CreateInstance (type); |
| 253 |
} |
| 254 |
catch(MissingMethodException) |
| 255 |
{ |
| 256 |
|
| 257 |
return null; |
| 258 |
} |
| 259 |
catch(SecurityException) |
| 260 |
{ |
| 261 |
|
| 262 |
return null; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
active.Add(name, handler); |
| 267 |
|
| 268 |
|
| 269 |
return handler; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
private void LoadClassList() |
| 275 |
{ |
| 276 |
FileStream stream; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
try |
| 288 |
{ |
| 289 |
stream = Assembly.GetExecutingAssembly() |
| 290 |
.GetFile("I18N-handlers.def"); |
| 291 |
if(stream == null) |
| 292 |
{ |
| 293 |
LoadInternalClasses(); |
| 294 |
return; |
| 295 |
} |
| 296 |
} |
| 297 |
catch(FileLoadException) |
| 298 |
{ |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
LoadInternalClasses(); |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
StreamReader reader = new StreamReader(stream); |
| 309 |
String line; |
| 310 |
int posn; |
| 311 |
while((line = reader.ReadLine()) != null) |
| 312 |
{ |
| 313 |
|
| 314 |
if(line.Length == 0 || line[0] == '#') |
| 315 |
{ |
| 316 |
continue; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
posn = line.LastIndexOf('.'); |
| 322 |
if(posn != -1) |
| 323 |
{ |
| 324 |
|
| 325 |
|
| 326 |
String name = line.Substring(posn + 1); |
| 327 |
if(!handlers.Contains(name)) |
| 328 |
{ |
| 329 |
handlers.Add(name, line.Substring(0, posn)); |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
reader.Close(); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
private void LoadInternalClasses() |
| 338 |
{ |
| 339 |
int posn; |
| 340 |
foreach(String line in Handlers.List) |
| 341 |
{ |
| 342 |
|
| 343 |
|
| 344 |
posn = line.LastIndexOf('.'); |
| 345 |
if(posn != -1) |
| 346 |
{ |
| 347 |
|
| 348 |
|
| 349 |
String name = line.Substring(posn + 1); |
| 350 |
if(!handlers.Contains(name)) |
| 351 |
{ |
| 352 |
handlers.Add(name, line.Substring(0, posn)); |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
}; |
| 359 |
|
| 360 |
}; |