-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathResourceScriptSource.cs
More file actions
117 lines (97 loc) · 2.89 KB
/
ResourceScriptSource.cs
File metadata and controls
117 lines (97 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.IO;
using System.Reflection;
using OriginalScriptSource = Jurassic.ScriptSource;
using CoreStrings = JavaScriptEngineSwitcher.Core.Resources.Strings;
namespace JavaScriptEngineSwitcher.Jurassic
{
/// <summary>
/// Represents a embedded JS resource
/// </summary>
internal sealed class ResourceScriptSource : OriginalScriptSource
{
/// <summary>
/// The document name
/// </summary>
private readonly string _documentName;
/// <summary>
/// The case-sensitive resource name
/// </summary>
private readonly string _resourceName;
/// <summary>
/// The assembly, which contains the embedded resource
/// </summary>
private readonly Assembly _assembly;
/// <summary>
/// Constructs an instance of <see cref="ResourceScriptSource"/>
/// </summary>
/// <param name="documentName">The document name</param>
/// <param name="resourceName">The case-sensitive resource name</param>
/// <param name="assembly">The assembly, which contains the embedded resource</param>
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
/// <summary>
/// Gets a document name
/// </summary>
public override string Path
{
get { return _documentName; }
}
/// <summary>
/// Gets a reader that can be used to read the source code from the embedded JS resource
/// </summary>
/// <returns>The reader that can be used to read the source code from the embedded
/// JS resource, positioned at the start of the source code</returns>
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
}
}