-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFileBasedExternalReferenceResolver.cs
42 lines (33 loc) · 1.45 KB
/
FileBasedExternalReferenceResolver.cs
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
using Firely.Fhir.Validation;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Serialization;
namespace Furore.Fhir.ValidationDemo
{
public class FileBasedExternalReferenceResolver(DirectoryInfo baseDirectory) : IExternalReferenceResolver
{
private FhirXmlPocoDeserializer XmlPocoDeserializer { get; } = new FhirXmlPocoDeserializer();
public DirectoryInfo BaseDirectory { get; private set; } = baseDirectory;
public bool Enabled { get; set; } = true;
public Task<object?> ResolveAsync(string reference)
{
if (!Enabled) return Task.FromResult<object?>(null);
// Now, for our examples we've used the convention that the file can be found in the
// example directory, with the name <id>.<type>.xml, so let's try to get that file.
var identity = new ResourceIdentity(reference);
var filename = $"{identity.Id}.{identity.ResourceType}.xml";
var path = Path.Combine(BaseDirectory.FullName, filename);
if (File.Exists(path))
{
var xml = File.ReadAllText(path);
// Note, this will throw if the file is not really FHIR xml
var poco = XmlPocoDeserializer.DeserializeResource(xml);
return Task.FromResult<object?>(poco);
}
else
{
// Unsuccessful
return Task.FromResult<object?>(null);
}
}
}
}