Skip to content

Commit

Permalink
[WIP] SupportedProfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanael smiechowski committed Jan 9, 2025
1 parent 29c817f commit c811eb0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
57 changes: 56 additions & 1 deletion uml4net.xmi/Readers/Packages/ModelReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ namespace uml4net.xmi.Readers.Packages
using Microsoft.Extensions.Logging;
using POCO.CommonStructure;
using POCO;
using POCO.Classification;
using POCO.Values;
using uml4net.POCO.Packages;
using Readers;
using Settings;
using System.IO.Packaging;
using System;
using System.Collections.Generic;
using uml4net.POCO.StructuredClassifiers;
using uml4net.POCO.SimpleClassifiers;

Expand All @@ -39,6 +43,11 @@ namespace uml4net.xmi.Readers.Packages
/// </summary>
public class ModelReader : XmiElementReader<IModel>, IXmiElementReader<IModel>
{
/// <summary>
/// Gets INJECTED <see cref="IXmiReaderSettings"/>
/// </summary>
private readonly IXmiReaderSettings settings;

/// <summary>
/// Gets the INJECTED <see cref="IXmiElementReader{T}"/> of <see cref="IAssociation"/>
/// </summary>
Expand Down Expand Up @@ -88,9 +97,11 @@ public class ModelReader : XmiElementReader<IModel>, IXmiElementReader<IModel>
/// <param name="logger">
/// The (injected) <see cref="ILogger{T}"/> used to setup logging
/// </param>
public ModelReader(IXmiReaderCache cache, ILogger<ModelReader> logger)
/// <param name="settings">The <see cref="IXmiReaderSettings"/></param>
public ModelReader(IXmiReaderCache cache, ILogger<ModelReader> logger, IXmiReaderSettings settings)
: base(cache, logger)
{
this.settings = settings;
}

/// <summary>
Expand Down Expand Up @@ -145,6 +156,12 @@ public override IModel Read(XmlReader xmlReader)
case "packagedElement":
this.ReadPackagedElements(model, xmlReader);
break;
case var _ when this.settings.SupportedProfiles.Contains(xmlReader.Prefix):
using (var customXmlReader = xmlReader.ReadSubtree())
{
this.ReadCustomElement(customXmlReader, xmlReader.LocalName, xmlReader.NamespaceURI);
}
break;
default:
var defaultLineInfo = xmlReader as IXmlLineInfo;
throw new NotImplementedException($"ModelReader: {xmlReader.LocalName} at line:position {defaultLineInfo.LineNumber}:{defaultLineInfo.LinePosition}");
Expand All @@ -156,6 +173,44 @@ public override IModel Read(XmlReader xmlReader)
return model;
}

private void ReadCustomElement(XmlReader customXmlReader, string stereotypeName, string xmlReaderNamespaceUri)
{
if (!(customXmlReader.Read() && customXmlReader.MoveToFirstAttribute()))
{
this.Logger.LogWarning("Stereotype '{stereotypeName}' is unreadable (no attributes found).", stereotypeName);
return;
}

do
{
if (!customXmlReader.Name.StartsWith("base_"))
{
continue;
}

if (!string.IsNullOrEmpty(customXmlReader.Value)
&& this.Cache.TryResolveContext(customXmlReader.Value, out var resource)
&& this.Cache.TryGetValue(resource.Context, resource.ResourceId, out var reference))
{
if (reference.GetType().Name == customXmlReader.Name.Substring(5))
{
IStereotype stereotype = new Stereotype();
stereotype.OwnedAttribute.Add(new Property() { Name = "Name", DefaultValue = new LiteralString() { Value = stereotypeName } });
stereotype.Name = stereotypeName;

reference.Extensions.Add(stereotype);
}
}
else
{
throw new InvalidOperationException($"The attribute '{customXmlReader.Name}' has no value.");
}

break;
}
while (customXmlReader.MoveToNextAttribute());
}

/// <summary>
/// Reads the packaged elements
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions uml4net.xmi/Settings/DefaultSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ public class DefaultSettings : IXmiReaderSettings
/// Gets or sets the base directory path used as the local root for resolving referenced XMI files.
/// </summary>
public string LocalReferenceBasePath { get; set; } = "";

/// <summary>
/// Gets or sets the collection of supported profiles
/// </summary>
public List<string> SupportedProfiles { get; set; } = [];
}
}
5 changes: 5 additions & 0 deletions uml4net.xmi/Settings/IXmiReaderSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ public interface IXmiReaderSettings
/// Gets or sets the base directory path used as the local root for resolving referenced XMI files.
/// </summary>
string LocalReferenceBasePath { get; set; }

/// <summary>
/// Gets or sets the collection of supported profiles
/// </summary>
List<string> SupportedProfiles { get; set; }
}
}

0 comments on commit c811eb0

Please sign in to comment.