PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Spss/EmbeddedResources.cs

#
C# | 147 lines | 130 code | 10 blank | 7 comment | 51 complexity | 69af9de54d947f579c0da6ecef430e47 MD5 | raw file
Possible License(s): LGPL-2.1
  1. //-----------------------------------------------------------------------
  2. // <copyright file="EmbeddedResources.cs" company="Andrew Arnott">
  3. // Copyright (c) Andrew Arnott. All rights reserved.
  4. // Copyright (c) Brigham Young University
  5. // This file comes from the Nerdbank.Tools.dll assembly.
  6. // </copyright>
  7. //-----------------------------------------------------------------------
  8. namespace Spss {
  9. using System;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Reflection;
  13. using System.Text.RegularExpressions;
  14. using System.Threading;
  15. internal static class EmbeddedResources {
  16. public static string LoadFileFromAssemblyWithNamespace(string filename, string namespacePrefix) {
  17. return LoadFileFromAssemblyWithNamespace(filename, namespacePrefix, Assembly.GetCallingAssembly());
  18. }
  19. public static string ManifestNameFromFileNameAndNamespace(string filename, string namespacePrefix) {
  20. if ((filename == null) || (filename.Length == 0)) {
  21. throw new ArgumentNullException("filename");
  22. }
  23. if (namespacePrefix == null) {
  24. namespacePrefix = string.Empty;
  25. }
  26. if (filename.Substring(0, 1) != "/") {
  27. filename = "/" + filename;
  28. }
  29. string manifestName = filename.Replace('/', '.');
  30. int pathEndsAt = filename.LastIndexOf('/');
  31. manifestName = manifestName.Substring(0, pathEndsAt).Replace(' ', '_') + manifestName.Substring(pathEndsAt);
  32. return (namespacePrefix + manifestName);
  33. }
  34. public static CultureInfo GetCultureFromManifestName(string manifestName, out string newManifestName) {
  35. newManifestName = manifestName;
  36. CultureInfo defaultCulture = null;
  37. Match m = Regex.Match(manifestName, @"\A(?<pre>.+)\.(?<culture>[a-z]{2}(?:-[A-Z]{2})?)(?<post>\.[^\.]+)\z");
  38. if (!m.Success) {
  39. return defaultCulture;
  40. }
  41. try {
  42. CultureInfo culture = CultureInfo.GetCultureInfo(m.Groups["culture"].Value);
  43. newManifestName = m.Groups["pre"].Value + m.Groups["post"].Value;
  44. return culture;
  45. } catch (ArgumentException) {
  46. return defaultCulture;
  47. }
  48. }
  49. public static Stream GetLocalizedManifestResourceStream(string fileName, string namespacePrefix, Assembly baseAssembly) {
  50. string manifestName = ManifestNameFromFileNameAndNamespace(fileName, namespacePrefix);
  51. CultureInfo culture = GetCultureFromManifestName(manifestName, out manifestName);
  52. if (culture == null) {
  53. return GetLocalizedManifestResourceStream(manifestName, baseAssembly);
  54. }
  55. return GetLocalizedManifestResourceStream(manifestName, baseAssembly, culture);
  56. }
  57. public static string LoadFileFromAssemblyWithNamespace(string filename, string namespacePrefix, Assembly assembly) {
  58. string str;
  59. if (filename == null) {
  60. throw new ArgumentNullException("filename");
  61. }
  62. if (assembly == null) {
  63. assembly = Assembly.GetCallingAssembly();
  64. }
  65. StreamReader reader = new StreamReader(GetLocalizedManifestResourceStream(filename, namespacePrefix, assembly));
  66. try {
  67. str = reader.ReadToEnd();
  68. } finally {
  69. reader.Close();
  70. }
  71. return str;
  72. }
  73. public static Stream GetLocalizedManifestResourceStream(string manifestName, Assembly baseAssembly) {
  74. if ((manifestName == null) || (manifestName.Length == 0)) {
  75. throw new ArgumentNullException("manifestName");
  76. }
  77. if (baseAssembly == null) {
  78. throw new ArgumentNullException("baseAssembly");
  79. }
  80. Stream stream = null;
  81. CultureInfo culture = Thread.CurrentThread.CurrentUICulture;
  82. stream = GetLocalizedManifestResourceStreamIfExists(manifestName, baseAssembly, culture);
  83. if (!((stream != null) || culture.IsNeutralCulture)) {
  84. stream = GetLocalizedManifestResourceStreamIfExists(manifestName, baseAssembly, culture.Parent);
  85. }
  86. if (stream == null) {
  87. stream = GetFileStreamFromAssembly(manifestName, baseAssembly);
  88. }
  89. return stream;
  90. }
  91. public static Stream GetLocalizedManifestResourceStreamIfExists(string manifestName, Assembly baseAssembly, CultureInfo culture) {
  92. if ((manifestName == null) || (manifestName.Length == 0)) {
  93. throw new ArgumentNullException("manifestName");
  94. }
  95. if (baseAssembly == null) {
  96. throw new ArgumentNullException("baseAssembly");
  97. }
  98. if (culture == null) {
  99. throw new ArgumentNullException("culture");
  100. }
  101. try {
  102. return baseAssembly.GetSatelliteAssembly(culture).GetManifestResourceStream(manifestName);
  103. } catch (FileNotFoundException) {
  104. return null;
  105. }
  106. }
  107. public static Stream GetFileStreamFromAssembly(string manifestName, Assembly assembly) {
  108. if (manifestName == null) {
  109. throw new ArgumentNullException("manifestName");
  110. }
  111. if (assembly == null) {
  112. throw new ArgumentNullException("assembly");
  113. }
  114. Stream fileStream = assembly.GetManifestResourceStream(manifestName);
  115. if (fileStream == null) {
  116. throw new ArgumentOutOfRangeException("manifestName", manifestName, "The embedded file could not be found in the assembly " + assembly.FullName + ". Check to see that the file's Build Action attribute in the project is set to \"Embedded Resource\".");
  117. }
  118. return fileStream;
  119. }
  120. public static Stream GetLocalizedManifestResourceStream(string manifestName, Assembly baseAssembly, CultureInfo culture) {
  121. if ((manifestName == null) || (manifestName.Length == 0)) {
  122. throw new ArgumentNullException("manifestName");
  123. }
  124. if (baseAssembly == null) {
  125. throw new ArgumentNullException("baseAssembly");
  126. }
  127. if (culture == null) {
  128. throw new ArgumentNullException("culture");
  129. }
  130. Stream stream = baseAssembly.GetSatelliteAssembly(culture).GetManifestResourceStream(manifestName);
  131. if ((stream == null) && manifestName.Contains("_")) {
  132. stream = baseAssembly.GetSatelliteAssembly(culture).GetManifestResourceStream(manifestName.Replace('_', ' '));
  133. }
  134. return stream;
  135. }
  136. }
  137. }