-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAnnotatingElementReader.cs
More file actions
299 lines (242 loc) · 14.1 KB
/
AnnotatingElementReader.cs
File metadata and controls
299 lines (242 loc) · 14.1 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// -------------------------------------------------------------------------------------------------
// <copyright file="AnnotatingElementReader.cs" company="Starion Group S.A.">
//
// Copyright 2022-2026 Starion Group S.A.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
// ------------------------------------------------------------------------------------------------
namespace SysML2.NET.Serializer.Xmi.Readers
{
using System;
using System.Collections.Generic;
using System.Xml;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using SysML2.NET.Common;
using SysML2.NET.Core.POCO.Root.Elements;
using SysML2.NET.Core.POCO.Root.Namespaces;
using SysML2.NET.Core.POCO.Root.Annotations;
/// <summary>
/// The purpose of the <see cref="{this.Name}}Reader" /> is to read an instance of <see cref="I{this.Name}}" />
/// from the XMI document
/// </summary>
public class AnnotatingElementReader : XmiDataReader<IAnnotatingElement>
{
/// <summary>
/// The instantiated logger from the injected <see cref="ILoggerFactory" />
/// </summary>
private readonly ILogger<AnnotatingElementReader> logger;
/// <summary>
/// Initializes a new instance of the <see cref="AnnotatingElementReader" />
/// </summary>
/// <param name="cache">
/// The injected <see cref="IXmiDataCache" /> used cache to store and resolve <see cref="IData" />
/// </param>
/// <param name="xmiDataReaderFacade">
/// The injected <see cref="IXmiDataReaderFacade" /> that provides
/// <see cref="XmiDataReader{TData}" /> resolve
/// </param>
/// <param name="externalReferenceService">The injected <see cref="IExternalReferenceService"/> used to register and process external references</param>
/// <param name="loggerFactory">The injected <see cref="ILoggerFactory" /> used to set up logging</param>
public AnnotatingElementReader(IXmiDataCache cache, IXmiDataReaderFacade xmiDataReaderFacade, IExternalReferenceService externalReferenceService, ILoggerFactory loggerFactory) : base(cache, xmiDataReaderFacade, externalReferenceService, loggerFactory)
{
this.logger = loggerFactory == null ? NullLogger<AnnotatingElementReader>.Instance : loggerFactory.CreateLogger<AnnotatingElementReader>();
}
/// <summary>
/// Reads the <see cref="IAnnotatingElement" /> object from its XML representation
/// </summary>
/// <param name="xmiReader">An instance of <see cref="XmlReader" /></param>
/// <param name="currentLocation">The <see cref="Uri" /> that keep tracks of the current location</param>
/// <returns>The read <see cref="IAnnotatingElement" /></returns>
public override IAnnotatingElement Read(XmlReader xmiReader, Uri currentLocation)
{
if (xmiReader == null)
{
throw new ArgumentNullException(nameof(xmiReader));
}
var xmlLineInfo = xmiReader as IXmlLineInfo;
IAnnotatingElement poco = new SysML2.NET.Core.POCO.Root.Annotations.AnnotatingElement();
if (xmiReader.MoveToContent() == XmlNodeType.Element)
{
this.logger.LogTrace("reading AnnotatingElement at line:position {LineNumber}:{LinePosition}", xmlLineInfo?.LineNumber, xmlLineInfo?.LinePosition);
var xsiType = xmiReader.GetAttribute("xsi:type");
if (!string.IsNullOrEmpty(xsiType) && xsiType != "sysml:AnnotatingElement")
{
throw new InvalidOperationException($"The xsi:type {xsiType} is not supported by the AnnotatingElementReader");
}
var xmiId = xmiReader.GetAttribute("xmi:id");
if (!Guid.TryParse(xmiId, out var guid))
{
throw new InvalidOperationException($"The xmi:id {xmiId} could not be parsed");
}
poco.Id = guid;
if (!this.Cache.TryAdd(poco) && this.logger.IsEnabled(LogLevel.Critical))
{
this.logger.LogCritical("Failed to add element type [{Poco}] with id [{Id}] as it was already in the Cache. The XMI document seems to have duplicate xmi:id values", "AnnotatingElement", poco.Id);
}
var aliasIdsXmlAttribute = xmiReader.GetAttribute("aliasIds");
if (!string.IsNullOrWhiteSpace(aliasIdsXmlAttribute))
{
foreach (var aliasIdsXmlAttributeValue in aliasIdsXmlAttribute.Split(SplitMultiReference, StringSplitOptions.RemoveEmptyEntries))
{
poco.AliasIds.Add(aliasIdsXmlAttributeValue);
}
}
var declaredNameXmlAttribute = xmiReader.GetAttribute("declaredName");
if (!string.IsNullOrWhiteSpace(declaredNameXmlAttribute))
{
poco.DeclaredName = declaredNameXmlAttribute;
}
var declaredShortNameXmlAttribute = xmiReader.GetAttribute("declaredShortName");
if (!string.IsNullOrWhiteSpace(declaredShortNameXmlAttribute))
{
poco.DeclaredShortName = declaredShortNameXmlAttribute;
}
var elementIdXmlAttribute = xmiReader.GetAttribute("elementId");
if (!string.IsNullOrWhiteSpace(elementIdXmlAttribute))
{
poco.ElementId = elementIdXmlAttribute;
}
var isImpliedIncludedXmlAttribute = xmiReader.GetAttribute("isImpliedIncluded");
if (!string.IsNullOrWhiteSpace(isImpliedIncludedXmlAttribute))
{
if (bool.TryParse(isImpliedIncludedXmlAttribute, out var isImpliedIncludedXmlAttributeAsBool))
{
poco.IsImpliedIncluded = isImpliedIncludedXmlAttributeAsBool;
}
}
var ownedRelationshipXmlAttribute = xmiReader.GetAttribute("ownedRelationship");
if (!string.IsNullOrWhiteSpace(ownedRelationshipXmlAttribute))
{
var ownedRelationshipXmlAttributeReferences = new List<Guid>();
foreach (var ownedRelationshipXmlAttributeValue in ownedRelationshipXmlAttribute.Split(SplitMultiReference, StringSplitOptions.RemoveEmptyEntries))
{
if (Guid.TryParse(ownedRelationshipXmlAttributeValue, out var ownedRelationshipXmlAttributeReference))
{
ownedRelationshipXmlAttributeReferences.Add(ownedRelationshipXmlAttributeReference);
}
}
if (ownedRelationshipXmlAttributeReferences.Count != 0)
{
this.Cache.AddMultipleValueReferencePropertyIdentifiers(poco.Id, "ownedRelationship", ownedRelationshipXmlAttributeReferences);
}
}
var owningRelationshipXmlAttribute = xmiReader.GetAttribute("owningRelationship");
if (!string.IsNullOrWhiteSpace(owningRelationshipXmlAttribute))
{
if (Guid.TryParse(owningRelationshipXmlAttribute, out var owningRelationshipXmlAttributeReference))
{
this.Cache.AddSingleValueReferencePropertyIdentifier(poco.Id, "owningRelationship", owningRelationshipXmlAttributeReference);
}
}
while (xmiReader.Read())
{
if (xmiReader.NodeType == XmlNodeType.Element)
{
switch (xmiReader.LocalName)
{
case "aliasIds":
{
var aliasIdsValue = xmiReader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(aliasIdsValue))
{
poco.AliasIds.Add(aliasIdsValue);
}
break;
}
case "declaredName":
{
var declaredNameValue = xmiReader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(declaredNameValue))
{
poco.DeclaredName = declaredNameValue;
}
break;
}
case "declaredShortName":
{
var declaredShortNameValue = xmiReader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(declaredShortNameValue))
{
poco.DeclaredShortName = declaredShortNameValue;
}
break;
}
case "elementId":
{
var elementIdValue = xmiReader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(elementIdValue))
{
poco.ElementId = elementIdValue;
}
break;
}
case "isImpliedIncluded":
{
var isImpliedIncludedValue = xmiReader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(isImpliedIncludedValue))
{
poco.IsImpliedIncluded = bool.Parse(isImpliedIncludedValue);
}
break;
}
case "ownedRelationship":
{
var hrefAttribute = xmiReader.GetAttribute("href");
if (!string.IsNullOrWhiteSpace(hrefAttribute))
{
var hrefSplit = hrefAttribute.Split('#');
this.ExternalReferenceService.AddExternalReferenceToProcess(currentLocation, hrefSplit[0]);
var ownedRelationshipId = Guid.Parse(hrefSplit[1]);
this.Cache.AddMultipleValueReferencePropertyIdentifiers(poco.Id, "ownedRelationship", ownedRelationshipId);
}
else
{
var ownedRelationshipValue = (IRelationship)this.XmiDataReaderFacade.QueryXmiData(xmiReader, this.Cache, currentLocation, this.ExternalReferenceService, this.LoggerFactory);
poco.OwnedRelationship.Add(ownedRelationshipValue);
}
break;
}
case "owningRelationship":
{
var hrefAttribute = xmiReader.GetAttribute("href");
if (!string.IsNullOrWhiteSpace(hrefAttribute))
{
var hrefSplit = hrefAttribute.Split('#');
this.ExternalReferenceService.AddExternalReferenceToProcess(currentLocation, hrefSplit[0]);
var owningRelationshipId = Guid.Parse(hrefSplit[1]);
this.Cache.AddSingleValueReferencePropertyIdentifier(poco.Id, "owningRelationship", owningRelationshipId);
}
else
{
var owningRelationshipValue = (IRelationship)this.XmiDataReaderFacade.QueryXmiData(xmiReader, this.Cache, currentLocation, this.ExternalReferenceService, this.LoggerFactory);
poco.OwningRelationship = owningRelationshipValue;
}
break;
}
}
}
}
}
return poco;
}
}
}
// ------------------------------------------------------------------------------------------------
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
// ------------------------------------------------------------------------------------------------