-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathDocumentLoader.cs
More file actions
159 lines (138 loc) · 6.88 KB
/
DocumentLoader.cs
File metadata and controls
159 lines (138 loc) · 6.88 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using Microsoft.ClearScript.Util;
using System;
using System.IO;
using System.Threading.Tasks;
namespace Microsoft.ClearScript
{
/// <summary>
/// Represents a document loader.
/// </summary>
public abstract class DocumentLoader
{
// ReSharper disable EmptyConstructor
/// <summary>
/// Initializes a new <c><see cref="DocumentLoader"/></c> instance.
/// </summary>
protected DocumentLoader()
{
// the help file builder (SHFB) insists on an empty constructor here
}
// ReSharper restore EmptyConstructor
/// <summary>
/// Gets the default document loader.
/// </summary>
public static DocumentLoader Default => DefaultImpl.Instance;
/// <summary>
/// Gets or sets the maximum size of the document loader's cache.
/// </summary>
/// <remarks>
/// This property specifies the maximum number of documents to be cached by the document
/// loader. For the default document loader, its initial value is 1024.
/// </remarks>
/// <c><seealso cref="Default"/></c>
public virtual uint MaxCacheSize
{
get => 0;
set => throw new NotSupportedException("The document loader does not support caching");
}
/// <summary>
/// Loads a document.
/// </summary>
/// <param name="settings">Document access settings for the operation.</param>
/// <param name="sourceInfo">An optional structure containing meta-information for the requesting document.</param>
/// <param name="specifier">A string specifying the document to be loaded.</param>
/// <param name="category">An optional category for the requested document.</param>
/// <param name="contextCallback">An optional context callback for the requested document.</param>
/// <returns>A <c><see cref="Document"/></c> instance that represents the loaded document.</returns>
/// <remarks>
/// A loaded document must have an absolute <see cref="DocumentInfo.Uri">URI</see>. Once a
/// load operation has completed successfully, subsequent requests that resolve to the same
/// URI are expected to return the same <c><see cref="Document"/></c> reference, although loaders
/// are not required to manage document caches of unlimited size.
/// </remarks>
public virtual Document LoadDocument(DocumentSettings settings, DocumentInfo? sourceInfo, string specifier, DocumentCategory category, DocumentContextCallback contextCallback)
{
MiscHelpers.VerifyNonBlankArgument(specifier, nameof(specifier), "Invalid document specifier");
try
{
return LoadDocumentAsync(settings, sourceInfo, specifier, category, contextCallback).Result;
}
catch (AggregateException exception)
{
exception = exception.Flatten();
if (exception.InnerExceptions.Count == 1)
{
throw new FileLoadException(null, specifier, exception.InnerExceptions[0]);
}
throw new FileLoadException(null, specifier, exception);
}
}
/// <summary>
/// Loads a document asynchronously.
/// </summary>
/// <param name="settings">Document access settings for the operation.</param>
/// <param name="sourceInfo">An optional structure containing meta-information for the requesting document.</param>
/// <param name="specifier">A string specifying the document to be loaded.</param>
/// <param name="category">An optional category for the requested document.</param>
/// <param name="contextCallback">An optional context callback for the requested document.</param>
/// <returns>A task that represents the asynchronous operation. Upon completion, the task's result is a <c><see cref="Document"/></c> instance that represents the loaded document.</returns>
/// <remarks>
/// A loaded document must have an absolute <see cref="DocumentInfo.Uri">URI</see>. Once a
/// load operation has completed successfully, subsequent requests that resolve to the same
/// URI are expected to return the same <c><see cref="Document"/></c> reference, although loaders
/// are not required to manage document caches of unlimited size.
/// </remarks>
public abstract Task<Document> LoadDocumentAsync(DocumentSettings settings, DocumentInfo? sourceInfo, string specifier, DocumentCategory category, DocumentContextCallback contextCallback);
/// <summary>
/// Searches for a cached document by <see cref="DocumentInfo.Uri">URI</see>.
/// </summary>
/// <param name="uri">The document URI for which to search.</param>
/// <returns>The cached document if it was found, <c>null</c> otherwise.</returns>
public virtual Document GetCachedDocument(Uri uri)
{
return null;
}
/// <summary>
/// Stores a document in the cache.
/// </summary>
/// <param name="document">The document to store in the cache.</param>
/// <param name="replace"><c>True</c> to replace any existing document with the same URI, <c>false</c> otherwise.</param>
/// <returns>The cached document, which may be different from <paramref name="document"/> if <paramref name="replace"/> is <c>false</c>.</returns>
/// <remarks>
/// A cached document must have an absolute <see cref="DocumentInfo.Uri">URI</see>.
/// </remarks>
public virtual Document CacheDocument(Document document, bool replace)
{
throw new NotSupportedException("The document loader does not support caching");
}
/// <summary>
/// Discards all cached documents.
/// </summary>
public virtual void DiscardCachedDocuments()
{
}
#region Nested type: IStatistics
internal interface IStatistics
{
long FileCheckCount { get; }
long WebCheckCount { get; }
void ResetCheckCounts();
}
#endregion
#region Nested type: DefaultImpl
// IMPORTANT: Before its implementation was factored out and made public, some hosts used
// reflection to instantiate this class in order to maintain multiple document caches. It
// should therefore be treated and retained as part of the public API, as well as a
// placeholder for any future overrides of the default functionality.
private sealed class DefaultImpl : DefaultDocumentLoader
{
public static readonly DefaultImpl Instance = new();
private DefaultImpl()
{
}
}
#endregion
}
}