-
Notifications
You must be signed in to change notification settings - Fork 339
/
MaskingNode.cs
170 lines (134 loc) · 6.36 KB
/
MaskingNode.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
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
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (c) 2018, Firely (info@fire.ly) and contributors
* See the file CONTRIBUTORS for details.
*
* This file is licensed under the BSD 3-Clause license
* available at https://github.com/FirelyTeam/firely-net-sdk/blob/master/LICENSE
*/
using Hl7.Fhir.Specification;
using Hl7.Fhir.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Hl7.Fhir.ElementModel
{
public class MaskingNode : ITypedElement, IAnnotated, IExceptionSource
{
/// <summary>
/// Set to true when a complex type property is mandatory so all its children need to be included
/// </summary>
private bool _includeAll { get; set; }
public static MaskingNode ForSummary(ITypedElement node) =>
new MaskingNode(node, new MaskingNodeSettings
{
IncludeInSummary = true,
PreserveBundle = MaskingNodeSettings.PreserveBundleMode.Root
});
public static MaskingNode ForText(ITypedElement node) =>
new MaskingNode(node, new MaskingNodeSettings
{
IncludeElements = new[] { "text", "id", "meta" },
IncludeMandatory = true, //IncludeIsModifier = true,
PreserveBundle = MaskingNodeSettings.PreserveBundleMode.All
});
public static MaskingNode ForElements(ITypedElement node, string[] _elements) =>
ForElements(node, _elements, false);
public static MaskingNode ForElements(ITypedElement node, string[] _elements, bool includeMandatory) =>
new MaskingNode(node, new MaskingNodeSettings
{
IncludeElements = _elements ?? new string[] { },
IncludeMandatory = includeMandatory,
PreserveBundle = MaskingNodeSettings.PreserveBundleMode.All
});
public static MaskingNode ForData(ITypedElement node) =>
new MaskingNode(node, new MaskingNodeSettings
{
IncludeAll = true,
ExcludeNarrative = true
});
public static MaskingNode ForCount(ITypedElement node) =>
new MaskingNode(node, new MaskingNodeSettings
{
IncludeMandatory = true,
IncludeElements = new[] { "id", "total", "link" },
});
public MaskingNode(ITypedElement source, MaskingNodeSettings settings = null)
{
if (source == null) throw Error.ArgumentNull(nameof(source));
if (source.Annotation<ScopedNode>() == null)
throw Error.Argument("MaskingNode can only be used on a navigator chain that contains a ScopedNode", nameof(source));
Source = source;
_settings = settings?.Clone() ?? new MaskingNodeSettings();
if (Source is IExceptionSource ies && ies.ExceptionHandler == null)
ies.ExceptionHandler = (o, a) => ExceptionHandler.NotifyOrThrow(o, a);
}
private MaskingNode(MaskingNode parent, ITypedElement source, bool includeAll)
{
Source = source;
_settings = parent._settings;
_includeAll = includeAll;
ExceptionHandler = parent.ExceptionHandler;
}
private ScopedNode getScope(ITypedElement node) =>
node.Annotation<ScopedNode>();
private readonly MaskingNodeSettings _settings;
public ITypedElement Source { get; private set; }
public ExceptionNotificationHandler ExceptionHandler { get; set; }
public string Name => Source.Name;
public string InstanceType => Source.InstanceType;
public object Value => Source.Value;
public string Location => Source.Location;
public IElementDefinitionSummary Definition => Source.Definition;
private (bool included, bool mandatory) included(ITypedElement node)
{
var scope = getScope(node);
// Trivially, we will include the root
if (!scope.Location.Contains(".")) return (true, false);
bool atRootBundle() => atBundle() && scope.ParentResource == null;
bool atBundle() => scope.NearestResourceType == "Bundle";
switch (_settings.PreserveBundle)
{
case MaskingNodeSettings.PreserveBundleMode.All when atBundle():
case MaskingNodeSettings.PreserveBundleMode.Root when atRootBundle():
return (true, false);
// fall through...
}
var included = _settings.IncludeAll || _includeAll;
bool mandatory = false; // included because it's required & includeMandatory is on
var ed = scope.Definition;
if (ed != null)
{
mandatory = _settings.IncludeMandatory && ed.IsRequired;
included |= mandatory;
included |= _settings.IncludeInSummary && (ed.InSummary || ed.IsModifier);
// Also include Element.id in the summary. Not a nice way to determine this, but for now the only way.
included |= _settings.IncludeInSummary && ed.ElementName == "id" && scope.InstanceType == "string";
}
var loc = scope.LocalLocation;
var nearest = scope.NearestResourceType;
included |= _settings.IncludeElements?.Any(matches) ?? false;
if (_settings.ExcludeElements?.Any(matches) == true)
return (false, false);
bool matches(string filter)
{
var f = nearest + "." + filter;
return loc == f || loc.StartsWith(f + ".") || loc.StartsWith(f + "["); // include matches + children
}
if (_settings.ExcludeMarkdown && scope.InstanceType == "markdown") return (false, false);
if (_settings.ExcludeNarrative & scope.InstanceType == "Narrative") return (false, false);
return (included, mandatory);
}
public IEnumerable<object> Annotations(Type type)
{
if (type == typeof(MaskingNode))
return new[] { this };
else
return Source.Annotations(type);
}
public IEnumerable<ITypedElement> Children(string name = null) =>
from c in Source.Children(name)
let inc = included(c)
where inc.included
select new MaskingNode(this, c, inc.mandatory);
}
}