forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileScriptSource.cs
More file actions
101 lines (84 loc) · 2.31 KB
/
FileScriptSource.cs
File metadata and controls
101 lines (84 loc) · 2.31 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
using System;
using System.IO;
using System.Text;
using OriginalScriptSource = Jurassic.ScriptSource;
using CoreStrings = JavaScriptEngineSwitcher.Core.Resources.Strings;
namespace JavaScriptEngineSwitcher.Jurassic
{
/// <summary>
/// Represents a JS file
/// </summary>
internal sealed class FileScriptSource : OriginalScriptSource
{
/// <summary>
/// The document name
/// </summary>
private readonly string _documentName;
/// <summary>
/// The path to the JS file
/// </summary>
private readonly string _path;
/// <summary>
/// The text encoding
/// </summary>
private readonly Encoding _encoding;
/// <summary>
/// Constructs an instance of <see cref="FileScriptSource"/>
/// </summary>
/// <param name="documentName">The document name</param>
/// <param name="path">The path to the JS file</param>
/// <param name="encoding">The text encoding</param>
public FileScriptSource(string documentName, string path, Encoding encoding = null)
{
if (documentName == null)
{
throw new ArgumentNullException(
nameof(documentName),
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(documentName))
);
}
if (path == null)
{
throw new ArgumentNullException(
nameof(path),
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(path))
);
}
if (string.IsNullOrWhiteSpace(documentName))
{
throw new ArgumentException(
string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(documentName)),
nameof(documentName)
);
}
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException(
string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(path)),
nameof(path)
);
}
_documentName = documentName;
_path = path;
_encoding = encoding ?? Encoding.UTF8;
}
#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 JS file
/// </summary>
/// <returns>A reader that can be used to read the source code from JS file,
/// positioned at the start of the source code</returns>
public override TextReader GetReader()
{
return new StreamReader(_path, _encoding, true);
}
#endregion
}
}