using System;
using System.IO;
using System.Text;
using OriginalScriptSource = Jurassic.ScriptSource;
using CoreStrings = JavaScriptEngineSwitcher.Core.Resources.Strings;
namespace JavaScriptEngineSwitcher.Jurassic
{
///
/// Represents a JS file
///
internal sealed class FileScriptSource : OriginalScriptSource
{
///
/// The document name
///
private readonly string _documentName;
///
/// The path to the JS file
///
private readonly string _path;
///
/// The text encoding
///
private readonly Encoding _encoding;
///
/// Constructs an instance of
///
/// The document name
/// The path to the JS file
/// The text encoding
public FileScriptSource(string documentName, string path, Encoding encoding = null)
{
if (documentName is null)
{
throw new ArgumentNullException(
nameof(documentName),
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(documentName))
);
}
if (path is 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
///
/// Gets a document name
///
public override string Path
{
get { return _documentName; }
}
///
/// Gets a reader that can be used to read the source code from JS file
///
/// A reader that can be used to read the source code from JS file,
/// positioned at the start of the source code
public override TextReader GetReader()
{
return new StreamReader(_path, _encoding, true);
}
#endregion
}
}