FileBasedResourceGroveler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. /*============================================================
  5. **
  6. **
  7. ** Purpose: Searches for resources on disk, used for file-
  8. ** based resource lookup.
  9. **
  10. **
  11. ===========================================================*/
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.Globalization;
  15. using System.IO;
  16. using System.Threading;
  17. using Internal.IO;
  18. namespace System.Resources
  19. {
  20. internal class FileBasedResourceGroveler : IResourceGroveler
  21. {
  22. private ResourceManager.ResourceManagerMediator _mediator;
  23. public FileBasedResourceGroveler(ResourceManager.ResourceManagerMediator mediator)
  24. {
  25. Debug.Assert(mediator != null, "mediator shouldn't be null; check caller");
  26. _mediator = mediator;
  27. }
  28. // Consider modifying IResourceGroveler interface (hence this method signature) when we figure out
  29. // serialization compat story for moving ResourceManager members to either file-based or
  30. // manifest-based classes. Want to continue tightening the design to get rid of unused params.
  31. public ResourceSet GrovelForResourceSet(CultureInfo culture, Dictionary<string, ResourceSet> localResourceSets, bool tryParents, bool createIfNotExists)
  32. {
  33. Debug.Assert(culture != null, "culture shouldn't be null; check caller");
  34. string fileName = null;
  35. ResourceSet rs = null;
  36. // Don't use Assembly manifest, but grovel on disk for a file.
  37. // Create new ResourceSet, if a file exists on disk for it.
  38. string tempFileName = _mediator.GetResourceFileName(culture);
  39. fileName = FindResourceFile(culture, tempFileName);
  40. if (fileName == null)
  41. {
  42. if (tryParents)
  43. {
  44. // If we've hit top of the Culture tree, return.
  45. if (culture.HasInvariantCultureName)
  46. {
  47. // We really don't think this should happen - we always
  48. // expect the neutral locale's resources to be present.
  49. throw new MissingManifestResourceException(SR.MissingManifestResource_NoNeutralDisk + Environment.NewLine + "baseName: " + _mediator.BaseNameField + " locationInfo: " + (_mediator.LocationInfo == null ? "<null>" : _mediator.LocationInfo.FullName) + " fileName: " + _mediator.GetResourceFileName(culture));
  50. }
  51. }
  52. }
  53. else
  54. {
  55. rs = CreateResourceSet(fileName);
  56. }
  57. return rs;
  58. }
  59. // Given a CultureInfo, it generates the path &; file name for
  60. // the .resources file for that CultureInfo. This method will grovel
  61. // the disk looking for the correct file name & path. Uses CultureInfo's
  62. // Name property. If the module directory was set in the ResourceManager
  63. // constructor, we'll look there first. If it couldn't be found in the module
  64. // diretory or the module dir wasn't provided, look in the current
  65. // directory.
  66. private string FindResourceFile(CultureInfo culture, string fileName)
  67. {
  68. Debug.Assert(culture != null, "culture shouldn't be null; check caller");
  69. Debug.Assert(fileName != null, "fileName shouldn't be null; check caller");
  70. // If we have a moduleDir, check there first. Get module fully
  71. // qualified name, append path to that.
  72. if (_mediator.ModuleDir != null)
  73. {
  74. string path = Path.Combine(_mediator.ModuleDir, fileName);
  75. if (File.Exists(path))
  76. {
  77. return path;
  78. }
  79. }
  80. // look in .
  81. if (File.Exists(fileName))
  82. return fileName;
  83. return null; // give up.
  84. }
  85. // Constructs a new ResourceSet for a given file name.
  86. private ResourceSet CreateResourceSet(string file)
  87. {
  88. Debug.Assert(file != null, "file shouldn't be null; check caller");
  89. if (_mediator.UserResourceSet == null)
  90. {
  91. return new RuntimeResourceSet(file);
  92. }
  93. else
  94. {
  95. object[] args = new object[1];
  96. args[0] = file;
  97. try
  98. {
  99. return (ResourceSet)Activator.CreateInstance(_mediator.UserResourceSet, args);
  100. }
  101. catch (MissingMethodException e)
  102. {
  103. throw new InvalidOperationException(SR.Format(SR.InvalidOperation_ResMgrBadResSet_Type, _mediator.UserResourceSet.AssemblyQualifiedName), e);
  104. }
  105. }
  106. }
  107. }
  108. }