PageRenderTime 33ms CodeModel.GetById 19ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Spss/EmbeddedResources.cs

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