AppResourcesCompiler.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. //
  2. // System.Web.Compilation.AppResourceFilesCollection
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2006 Marek Habersack
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.CodeDom;
  32. using System.CodeDom.Compiler;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.Globalization;
  36. using System.IO;
  37. using System.Reflection;
  38. using System.Resources;
  39. using System.Web;
  40. using System.Web.Caching;
  41. using System.Web.Configuration;
  42. using System.Web.Util;
  43. namespace System.Web.Compilation
  44. {
  45. internal class AppResourcesCompiler
  46. {
  47. const string cachePrefix = "@@LocalResourcesAssemblies";
  48. bool isGlobal;
  49. HttpContext context;
  50. AppResourceFilesCollection files;
  51. string tempDirectory;
  52. string virtualPath;
  53. string TempDirectory {
  54. get {
  55. if (tempDirectory != null)
  56. return tempDirectory;
  57. return (tempDirectory = AppDomain.CurrentDomain.SetupInformation.DynamicBase);
  58. }
  59. }
  60. public AppResourcesCompiler (HttpContext context)
  61. {
  62. this.context = context;
  63. this.isGlobal = true;
  64. this.files = new AppResourceFilesCollection (context);
  65. }
  66. public AppResourcesCompiler (string virtualPath)
  67. {
  68. this.virtualPath = virtualPath;
  69. this.isGlobal = false;
  70. this.files = new AppResourceFilesCollection (HttpContext.Current.Request.MapPath (virtualPath));
  71. }
  72. public Assembly Compile ()
  73. {
  74. files.Collect ();
  75. if (!files.HasFiles)
  76. return null;
  77. if (isGlobal)
  78. return CompileGlobal ();
  79. else
  80. return CompileLocal ();
  81. }
  82. Assembly CompileGlobal ()
  83. {
  84. string assemblyPath = FileUtils.CreateTemporaryFile (TempDirectory,
  85. "App_GlobalResources",
  86. "dll",
  87. OnCreateRandomFile) as string;
  88. if (assemblyPath == null)
  89. throw new ApplicationException ("Failed to create global resources assembly");
  90. CompilationSection config = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
  91. if (config == null || !CodeDomProvider.IsDefinedLanguage (config.DefaultLanguage))
  92. throw new ApplicationException ("Could not get the default compiler.");
  93. CompilerInfo ci = CodeDomProvider.GetCompilerInfo (config.DefaultLanguage);
  94. if (ci == null || !ci.IsCodeDomProviderTypeValid)
  95. throw new ApplicationException ("Failed to obtain the default compiler information.");
  96. CompilerParameters cp = ci.CreateDefaultCompilerParameters ();
  97. cp.OutputAssembly = assemblyPath;
  98. cp.GenerateExecutable = false;
  99. cp.TreatWarningsAsErrors = true;
  100. cp.IncludeDebugInformation = config.Debug;
  101. List <string>[] fileGroups = GroupGlobalFiles (cp);
  102. if (fileGroups == null || fileGroups.Length == 0)
  103. return null;
  104. CodeCompileUnit unit = new CodeCompileUnit ();
  105. CodeNamespace ns = new CodeNamespace (null);
  106. ns.Imports.Add (new CodeNamespaceImport ("System"));
  107. ns.Imports.Add (new CodeNamespaceImport ("System.Globalization"));
  108. ns.Imports.Add (new CodeNamespaceImport ("System.Reflection"));
  109. ns.Imports.Add (new CodeNamespaceImport ("System.Resources"));
  110. unit.Namespaces.Add (ns);
  111. CodeDomProvider provider;
  112. provider = ci.CreateProvider ();
  113. if (provider == null)
  114. throw new ApplicationException ("Failed to instantiate the default compiler.");
  115. Dictionary <string,bool> assemblies = new Dictionary<string,bool> ();
  116. foreach (List<string> ls in fileGroups)
  117. DomFromResource (ls [0], unit, assemblies, provider);
  118. foreach (KeyValuePair<string,bool> de in assemblies)
  119. unit.ReferencedAssemblies.Add (de.Key);
  120. AssemblyBuilder abuilder = new AssemblyBuilder (provider);
  121. abuilder.AddCodeCompileUnit (unit);
  122. CompilerResults results = abuilder.BuildAssembly (cp);
  123. Assembly ret = null;
  124. if (results.Errors.Count == 0) {
  125. ret = results.CompiledAssembly;
  126. BuildManager.TopLevelAssemblies.Add (ret);
  127. HttpContext.AppGlobalResourcesAssembly = ret;
  128. } else {
  129. if (context.IsCustomErrorEnabled)
  130. throw new ApplicationException ("An error occurred while compiling global resources.");
  131. throw new CompilationException (null, results.Errors, null);
  132. }
  133. HttpRuntime.WritePreservationFile (ret, "App_GlobalResources");
  134. HttpRuntime.EnableAssemblyMapping (true);
  135. return ret;
  136. }
  137. Assembly CompileLocal ()
  138. {
  139. if (String.IsNullOrEmpty (virtualPath))
  140. return null;
  141. Assembly cached = GetCachedLocalResourcesAssembly (virtualPath);
  142. if (cached != null)
  143. return cached;
  144. string prefix;
  145. if (virtualPath == "/")
  146. prefix = "App_LocalResources.root";
  147. else
  148. prefix = "App_LocalResources" + virtualPath.Replace ('/', '.');
  149. string assemblyPath = FileUtils.CreateTemporaryFile (TempDirectory,
  150. prefix,
  151. "dll",
  152. OnCreateRandomFile) as string;
  153. if (assemblyPath == null)
  154. throw new ApplicationException ("Failed to create global resources assembly");
  155. CompilationSection config = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
  156. if (config == null || !CodeDomProvider.IsDefinedLanguage (config.DefaultLanguage))
  157. throw new ApplicationException ("Could not get the default compiler.");
  158. CompilerInfo ci = CodeDomProvider.GetCompilerInfo (config.DefaultLanguage);
  159. if (ci == null || !ci.IsCodeDomProviderTypeValid)
  160. throw new ApplicationException ("Failed to obtain the default compiler information.");
  161. CompilerParameters cp = ci.CreateDefaultCompilerParameters ();
  162. cp.OutputAssembly = assemblyPath;
  163. cp.GenerateExecutable = false;
  164. cp.TreatWarningsAsErrors = true;
  165. cp.IncludeDebugInformation = config.Debug;
  166. List<AppResourceFileInfo> files = this.files.Files;
  167. foreach (AppResourceFileInfo arfi in files)
  168. GetResourceFile (arfi, cp);
  169. CodeDomProvider provider;
  170. provider = ci.CreateProvider ();
  171. if (provider == null)
  172. throw new ApplicationException ("Failed to instantiate the default compiler.");
  173. AssemblyBuilder abuilder = new AssemblyBuilder (provider);
  174. CompilerResults results = abuilder.BuildAssembly (cp);
  175. Assembly ret = null;
  176. if (results.Errors.Count == 0) {
  177. ret = results.CompiledAssembly;
  178. AddAssemblyToCache (virtualPath, ret);
  179. } else {
  180. if (context.IsCustomErrorEnabled)
  181. throw new ApplicationException ("An error occurred while compiling global resources.");
  182. throw new CompilationException (null, results.Errors, null);
  183. }
  184. return ret;
  185. }
  186. internal static Assembly GetCachedLocalResourcesAssembly (string path)
  187. {
  188. Dictionary <string, Assembly> cache;
  189. cache = HttpRuntime.Cache[cachePrefix] as Dictionary <string, Assembly>;
  190. if (cache == null || !cache.ContainsKey (path))
  191. return null;
  192. return cache [path];
  193. }
  194. void AddAssemblyToCache (string path, Assembly asm)
  195. {
  196. Cache runtimeCache = HttpRuntime.Cache;
  197. Dictionary <string, Assembly> cache;
  198. cache = runtimeCache[cachePrefix] as Dictionary <string, Assembly>;
  199. if (cache == null)
  200. cache = new Dictionary <string, Assembly> ();
  201. cache [path] = asm;
  202. runtimeCache.Insert (cachePrefix, cache);
  203. }
  204. uint CountChars (char c, string s)
  205. {
  206. uint ret = 0;
  207. foreach (char ch in s) {
  208. if (ch == c)
  209. ret++;
  210. }
  211. return ret;
  212. }
  213. bool IsFileCultureValid (string fileName)
  214. {
  215. string tmp = Path.GetFileNameWithoutExtension (fileName);
  216. tmp = Path.GetExtension (tmp);
  217. if (tmp != null && tmp.Length > 0) {
  218. tmp = tmp.Substring (1);
  219. try {
  220. CultureInfo.GetCultureInfo (tmp);
  221. return true;
  222. } catch {
  223. return false;
  224. }
  225. }
  226. return false;
  227. }
  228. string GetResourceFile (AppResourceFileInfo arfi, CompilerParameters cp)
  229. {
  230. string resfile;
  231. if (arfi.Kind == AppResourceFileKind.ResX)
  232. resfile = CompileResource (arfi);
  233. else
  234. resfile = arfi.Info.FullName;
  235. if (!String.IsNullOrEmpty (resfile))
  236. cp.EmbeddedResources.Add (resfile);
  237. return resfile;
  238. }
  239. List <string>[] GroupGlobalFiles (CompilerParameters cp)
  240. {
  241. List<AppResourceFileInfo> files = this.files.Files;
  242. List<List<string>> groups = new List<List<string>> ();
  243. AppResourcesLengthComparer<List<string>> lcList = new AppResourcesLengthComparer<List<string>> ();
  244. string tmp, s, basename;
  245. uint basedots, filedots;
  246. AppResourceFileInfo defaultFile;
  247. foreach (AppResourceFileInfo arfi in files) {
  248. if (arfi.Kind != AppResourceFileKind.ResX && arfi.Kind != AppResourceFileKind.Resource)
  249. continue;
  250. s = arfi.Info.FullName;
  251. basename = Path.GetFileNameWithoutExtension (s);
  252. basedots = CountChars ('.', basename);
  253. defaultFile = null;
  254. // If there are any files that start with this baseName, we have a default file
  255. foreach (AppResourceFileInfo fi in files) {
  256. if (fi.Seen)
  257. continue;
  258. string s2 = fi.Info.FullName;
  259. if (s2 == null || s == s2)
  260. continue;
  261. tmp = Path.GetFileNameWithoutExtension (s2);
  262. filedots = CountChars ('.', tmp);
  263. if (filedots == basedots + 1 && tmp.StartsWith (basename)) {
  264. if (IsFileCultureValid (s2)) {
  265. // A valid translated file for this name
  266. defaultFile = arfi;
  267. break;
  268. } else {
  269. // This file shares the base name, but the culture is invalid - we must
  270. // ignore it since the name of the generated strongly typed class for this
  271. // resource will clash with the one generated from the default file with
  272. // the given basename.
  273. fi.Seen = true;
  274. }
  275. }
  276. }
  277. if (defaultFile != null) {
  278. List<string> al = new List<string> ();
  279. al.Add (GetResourceFile (arfi, cp));
  280. arfi.Seen = true;
  281. groups.Add (al);
  282. }
  283. }
  284. groups.Sort (lcList);
  285. string tmp2;
  286. // Now find their translated counterparts
  287. foreach (List<string> al in groups) {
  288. s = al [0];
  289. tmp = Path.GetFileNameWithoutExtension (s);
  290. foreach (AppResourceFileInfo arfi in files) {
  291. if (arfi.Seen)
  292. continue;
  293. s = arfi.Info.FullName;
  294. if (s == null)
  295. continue;
  296. tmp2 = arfi.Info.Name;
  297. if (tmp2.StartsWith (tmp)) {
  298. al.Add (GetResourceFile (arfi, cp));
  299. arfi.Seen = true;
  300. }
  301. }
  302. }
  303. // Anything that's left here might be orphans or lone default files.
  304. // For those files we check the part following the last dot
  305. // before the .resx/.resource extensions and test whether it's a registered
  306. // culture or not. If it is not a culture, then we have a
  307. // default file that doesn't have any translations. Otherwise,
  308. // the file is ignored (it's the same thing MS.NET does)
  309. foreach (AppResourceFileInfo arfi in files) {
  310. if (arfi.Seen)
  311. continue;
  312. if (IsFileCultureValid (arfi.Info.FullName))
  313. continue; // Culture found, we reject the file
  314. // A single default file, create a group
  315. List<string> al = new List<string> ();
  316. al.Add (GetResourceFile (arfi, cp));
  317. groups.Add (al);
  318. }
  319. groups.Sort (lcList);
  320. return groups.ToArray ();
  321. }
  322. // CodeDOM generation
  323. void DomFromResource (string resfile, CodeCompileUnit unit, Dictionary <string,bool> assemblies,
  324. CodeDomProvider provider)
  325. {
  326. if (String.IsNullOrEmpty (resfile))
  327. return;
  328. string fname, nsname, classname;
  329. fname = Path.GetFileNameWithoutExtension (resfile);
  330. nsname = Path.GetFileNameWithoutExtension (fname);
  331. classname = Path.GetExtension (fname);
  332. if (classname == null || classname.Length == 0) {
  333. classname = nsname;
  334. nsname = "Resources";
  335. } else {
  336. if (!nsname.StartsWith ("Resources", StringComparison.InvariantCulture))
  337. nsname = String.Format ("Resources.{0}", nsname);
  338. classname = classname.Substring(1);
  339. }
  340. if (!provider.IsValidIdentifier (nsname) || !provider.IsValidIdentifier (classname))
  341. throw new ApplicationException ("Invalid resource file name.");
  342. ResourceReader res;
  343. try {
  344. res = new ResourceReader (resfile);
  345. } catch (ArgumentException) {
  346. // invalid stream, probably empty - ignore silently and abort
  347. return;
  348. }
  349. CodeNamespace ns = new CodeNamespace (nsname);
  350. CodeTypeDeclaration cls = new CodeTypeDeclaration (classname);
  351. cls.IsClass = true;
  352. cls.TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed;
  353. CodeMemberField cmf = new CodeMemberField (typeof(CultureInfo), "culture");
  354. cmf.InitExpression = new CodePrimitiveExpression (null);
  355. cmf.Attributes = MemberAttributes.Private | MemberAttributes.Final | MemberAttributes.Static;
  356. cls.Members.Add (cmf);
  357. cmf = new CodeMemberField (typeof(ResourceManager), "resourceManager");
  358. cmf.InitExpression = new CodePrimitiveExpression (null);
  359. cmf.Attributes = MemberAttributes.Private | MemberAttributes.Final | MemberAttributes.Static;
  360. cls.Members.Add (cmf);
  361. // Property: ResourceManager
  362. CodeMemberProperty cmp = new CodeMemberProperty ();
  363. cmp.Attributes = MemberAttributes.Public | MemberAttributes.Final | MemberAttributes.Static;
  364. cmp.Name = "ResourceManager";
  365. cmp.HasGet = true;
  366. cmp.Type = new CodeTypeReference (typeof(ResourceManager));
  367. CodePropertyResourceManagerGet (cmp.GetStatements, resfile, classname);
  368. cls.Members.Add (cmp);
  369. // Property: Culture
  370. cmp = new CodeMemberProperty ();
  371. cmp.Attributes = MemberAttributes.Public | MemberAttributes.Final;
  372. cmp.Attributes = MemberAttributes.Public | MemberAttributes.Final | MemberAttributes.Static;
  373. cmp.Name = "Culture";
  374. cmp.HasGet = true;
  375. cmp.HasSet = true;
  376. cmp.Type = new CodeTypeReference (typeof(CultureInfo));
  377. CodePropertyGenericGet (cmp.GetStatements, "culture", classname);
  378. CodePropertyGenericSet (cmp.SetStatements, "culture", classname);
  379. cls.Members.Add (cmp);
  380. // Add the resource properties
  381. Dictionary<string,bool> imports = new Dictionary<string,bool> ();
  382. try {
  383. foreach (DictionaryEntry de in res) {
  384. Type type = de.Value.GetType ();
  385. if (!imports.ContainsKey (type.Namespace))
  386. imports [type.Namespace] = true;
  387. string asname = new AssemblyName (type.Assembly.FullName).Name;
  388. if (!assemblies.ContainsKey (asname))
  389. assemblies [asname] = true;
  390. cmp = new CodeMemberProperty ();
  391. cmp.Attributes = MemberAttributes.Public | MemberAttributes.Final | MemberAttributes.Static;
  392. cmp.Name = SanitizeResourceName ((string)de.Key);
  393. cmp.HasGet = true;
  394. CodePropertyResourceGet (cmp.GetStatements, (string)de.Key, type, classname);
  395. cmp.Type = new CodeTypeReference (type);
  396. cls.Members.Add (cmp);
  397. }
  398. } catch (Exception ex) {
  399. throw new ApplicationException ("Failed to compile global resources.", ex);
  400. }
  401. foreach (KeyValuePair<string,bool> de in imports)
  402. ns.Imports.Add (new CodeNamespaceImport(de.Key));
  403. ns.Types.Add (cls);
  404. unit.Namespaces.Add (ns);
  405. }
  406. string SanitizeResourceName (string name)
  407. {
  408. return name.Replace (' ', '_').Replace ('-', '_').Replace ('.', '_');
  409. }
  410. CodeObjectCreateExpression NewResourceManager (string name, string typename)
  411. {
  412. CodeExpression resname = new CodePrimitiveExpression (name);
  413. CodePropertyReferenceExpression asm = new CodePropertyReferenceExpression (
  414. new CodeTypeOfExpression (new CodeTypeReference (typename)),
  415. "Assembly");
  416. return new CodeObjectCreateExpression ("System.Resources.ResourceManager",
  417. new CodeExpression [] {resname, asm});
  418. }
  419. void CodePropertyResourceManagerGet (CodeStatementCollection csc, string resfile, string typename)
  420. {
  421. string name = Path.GetFileNameWithoutExtension (resfile);
  422. CodeStatement st;
  423. CodeExpression exp;
  424. exp = new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (typename), "resourceManager");
  425. st = new CodeConditionStatement (
  426. new CodeBinaryOperatorExpression (
  427. exp,
  428. CodeBinaryOperatorType.IdentityInequality,
  429. new CodePrimitiveExpression (null)),
  430. new CodeStatement [] { new CodeMethodReturnStatement (exp) });
  431. csc.Add (st);
  432. st = new CodeAssignStatement (exp, NewResourceManager (name, typename));
  433. csc.Add (st);
  434. csc.Add (new CodeMethodReturnStatement (exp));
  435. }
  436. void CodePropertyResourceGet (CodeStatementCollection csc, string resname, Type restype, string typename)
  437. {
  438. CodeStatement st = new CodeVariableDeclarationStatement (
  439. typeof (ResourceManager),
  440. "rm",
  441. new CodePropertyReferenceExpression (
  442. new CodeTypeReferenceExpression (typename), "ResourceManager"));
  443. csc.Add (st);
  444. st = new CodeConditionStatement (
  445. new CodeBinaryOperatorExpression (
  446. new CodeVariableReferenceExpression ("rm"),
  447. CodeBinaryOperatorType.IdentityEquality,
  448. new CodePrimitiveExpression (null)),
  449. new CodeStatement [] { new CodeMethodReturnStatement (new CodePrimitiveExpression (null)) });
  450. csc.Add (st);
  451. bool gotstr = (restype == typeof (string));
  452. CodeExpression exp = new CodeMethodInvokeExpression (
  453. new CodeVariableReferenceExpression ("rm"),
  454. gotstr ? "GetString" : "GetObject",
  455. new CodeExpression [] { new CodePrimitiveExpression (resname),
  456. new CodeFieldReferenceExpression (
  457. new CodeTypeReferenceExpression (typename), "culture") });
  458. st = new CodeVariableDeclarationStatement (
  459. restype,
  460. "obj",
  461. gotstr ? exp : new CodeCastExpression (restype, exp));
  462. csc.Add (st);
  463. csc.Add (new CodeMethodReturnStatement (new CodeVariableReferenceExpression ("obj")));
  464. }
  465. void CodePropertyGenericGet (CodeStatementCollection csc, string field, string typename)
  466. {
  467. csc.Add(new CodeMethodReturnStatement (
  468. new CodeFieldReferenceExpression (
  469. new CodeTypeReferenceExpression (typename), field)));
  470. }
  471. void CodePropertyGenericSet (CodeStatementCollection csc, string field, string typename)
  472. {
  473. csc.Add(new CodeAssignStatement (
  474. new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (typename), field),
  475. new CodeVariableReferenceExpression ("value")));
  476. }
  477. string CompileResource (AppResourceFileInfo arfi)
  478. {
  479. string path = arfi.Info.FullName;
  480. string resource = Path.Combine (TempDirectory,
  481. "Resources." + Path.GetFileNameWithoutExtension (path) + ".resources");
  482. FileStream source = null, destination = null;
  483. IResourceReader reader = null;
  484. ResourceWriter writer = null;
  485. try {
  486. source = new FileStream (path, FileMode.Open, FileAccess.Read);
  487. destination = new FileStream (resource, FileMode.Create, FileAccess.Write);
  488. reader = GetReaderForKind (arfi.Kind, source);
  489. writer = new ResourceWriter (destination);
  490. foreach (DictionaryEntry de in reader) {
  491. object val = de.Value;
  492. if (val is string)
  493. writer.AddResource ((string)de.Key, (string)val);
  494. else
  495. writer.AddResource ((string)de.Key, val);
  496. }
  497. } catch (Exception ex) {
  498. throw new HttpException ("Failed to compile resource file", ex);
  499. } finally {
  500. if (reader != null)
  501. reader.Close ();
  502. else if (source != null)
  503. source.Close ();
  504. if (writer != null)
  505. writer.Close ();
  506. else if (destination != null)
  507. destination.Close ();
  508. }
  509. return resource;
  510. }
  511. IResourceReader GetReaderForKind (AppResourceFileKind kind, Stream stream)
  512. {
  513. switch (kind) {
  514. case AppResourceFileKind.ResX:
  515. return new ResXResourceReader (stream);
  516. case AppResourceFileKind.Resource:
  517. return new ResourceReader (stream);
  518. default:
  519. return null;
  520. }
  521. }
  522. object OnCreateRandomFile (string path)
  523. {
  524. FileStream f = new FileStream (path, FileMode.CreateNew);
  525. f.Close ();
  526. return path;
  527. }
  528. };
  529. };
  530. #endif