using System; using System.IO; using System.Reflection; using OriginalScriptSource = Jurassic.ScriptSource; using CoreStrings = JavaScriptEngineSwitcher.Core.Resources.Strings; namespace JavaScriptEngineSwitcher.Jurassic { /// /// Represents a embedded JS resource /// internal sealed class ResourceScriptSource : OriginalScriptSource { /// /// The document name /// private readonly string _documentName; /// /// The case-sensitive resource name /// private readonly string _resourceName; /// /// The assembly, which contains the embedded resource /// private readonly Assembly _assembly; /// /// Constructs an instance of /// /// The document name /// The case-sensitive resource name /// The assembly, which contains the embedded resource public ResourceScriptSource(string documentName, string resourceName, Assembly assembly) { if (documentName is null) { throw new ArgumentNullException( nameof(documentName), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(documentName)) ); } if (resourceName is null) { throw new ArgumentNullException( nameof(resourceName), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(resourceName)) ); } if (assembly is null) { throw new ArgumentNullException( nameof(assembly), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(assembly)) ); } if (string.IsNullOrWhiteSpace(documentName)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(documentName)), nameof(documentName) ); } if (string.IsNullOrWhiteSpace(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(resourceName)), nameof(resourceName) ); } _documentName = documentName; _resourceName = resourceName; _assembly = assembly; } #region Jurassic.ScriptSource overrides /// /// Gets a document name /// public override string Path { get { return _documentName; } } /// /// Gets a reader that can be used to read the source code from the embedded JS resource /// /// The reader that can be used to read the source code from the embedded /// JS resource, positioned at the start of the source code public override TextReader GetReader() { Stream stream = _assembly.GetManifestResourceStream(_resourceName); if (stream is null) { throw new NullReferenceException( string.Format(CoreStrings.Common_ResourceIsNull, _resourceName)); } return new StreamReader(stream); } #endregion } }