|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="XmiDataWriterFacade.cs" company="Starion Group S.A."> |
| 3 | +// |
| 4 | +// Copyright 2022-2026 Starion Group S.A. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// </copyright> |
| 19 | +// ------------------------------------------------------------------------------------------------ |
| 20 | +
|
| 21 | +// ------------------------------------------------------------------------------------------------ |
| 22 | +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- |
| 23 | +// ------------------------------------------------------------------------------------------------ |
| 24 | +
|
| 25 | +namespace SysML2.NET.Serializer.Xmi.Writers |
| 26 | +{ |
| 27 | + using System; |
| 28 | + using System.Collections.Generic; |
| 29 | + using System.Xml; |
| 30 | +
|
| 31 | + using SysML2.NET.Common; |
| 32 | + using SysML2.NET.Core.POCO.Root.Elements; |
| 33 | +
|
| 34 | + /// <summary> |
| 35 | + /// The purpose of the <see cref="XmiDataWriterFacade"/> is to dispatch writing of <see cref="IData"/> instances |
| 36 | + /// to the appropriate per-type static writer class |
| 37 | + /// </summary> |
| 38 | + public class XmiDataWriterFacade : IXmiDataWriterFacade |
| 39 | + { |
| 40 | + /// <summary> |
| 41 | + /// A dictionary that contains actions that write <see cref="IData"/> based on a key that represents the POCO type name |
| 42 | + /// </summary> |
| 43 | + private readonly Dictionary<string, Action<XmlWriter, IData, string, bool, IXmiDataWriterFacade, IXmiElementOriginMap, Uri>> writerCache; |
| 44 | +
|
| 45 | + /// <summary> |
| 46 | + /// Initializes a new instance of the <see cref="XmiDataWriterFacade"/> |
| 47 | + /// </summary> |
| 48 | + public XmiDataWriterFacade() |
| 49 | + { |
| 50 | + this.writerCache = new Dictionary<string, Action<XmlWriter, IData, string, bool, IXmiDataWriterFacade, IXmiElementOriginMap, Uri>> |
| 51 | + { |
| 52 | + {{ #each this as | class | }} |
| 53 | + ["{{ class.Name }}"] = (xmlWriter, data, elementName, includeDerived, facade, originMap, uri) => |
| 54 | + {{ class.Name }}Writer.Write(xmlWriter, (Core.POCO.{{ #NamedElement.WriteFullyQualifiedNameSpace class }}.I{{ class.Name }})data, elementName, includeDerived, facade, originMap, uri), |
| 55 | + {{/each}} |
| 56 | + }; |
| 57 | + } |
| 58 | +
|
| 59 | + /// <summary> |
| 60 | + /// Writes the specified <see cref="IData"/> as an XMI element by dispatching to the appropriate per-type writer |
| 61 | + /// </summary> |
| 62 | + /// <param name="xmlWriter">The target <see cref="XmlWriter"/></param> |
| 63 | + /// <param name="data">The <see cref="IData"/> to write</param> |
| 64 | + /// <param name="elementName">The XML element name to use</param> |
| 65 | + /// <param name="includeDerivedProperties">Whether to include derived properties</param> |
| 66 | + /// <param name="elementOriginMap">The optional <see cref="IXmiElementOriginMap"/> for href reconstruction</param> |
| 67 | + /// <param name="currentFileUri">The <see cref="Uri"/> of the current output file for relative href computation</param> |
| 68 | + public void Write(XmlWriter xmlWriter, IData data, string elementName, bool includeDerivedProperties, IXmiElementOriginMap elementOriginMap = null, Uri currentFileUri = null) |
| 69 | + { |
| 70 | + var typeName = data.GetType().Name; |
| 71 | +
|
| 72 | + if (this.writerCache.TryGetValue(typeName, out var writer)) |
| 73 | + { |
| 74 | + writer(xmlWriter, data, elementName, includeDerivedProperties, this, elementOriginMap, currentFileUri); |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + throw new InvalidOperationException($"No writer found for type {typeName}"); |
| 79 | + } |
| 80 | + } |
| 81 | +
|
| 82 | + /// <summary> |
| 83 | + /// Writes a contained child element, checking origin map for cross-file href |
| 84 | + /// </summary> |
| 85 | + /// <param name="xmlWriter">The target <see cref="XmlWriter"/></param> |
| 86 | + /// <param name="childData">The child <see cref="IData"/> to write</param> |
| 87 | + /// <param name="elementName">The XML element name to use</param> |
| 88 | + /// <param name="includeDerivedProperties">Whether to include derived properties</param> |
| 89 | + /// <param name="elementOriginMap">The optional <see cref="IXmiElementOriginMap"/> for href reconstruction</param> |
| 90 | + /// <param name="currentFileUri">The <see cref="Uri"/> of the current output file for relative href computation</param> |
| 91 | + public void WriteContainedElement(XmlWriter xmlWriter, IData childData, string elementName, bool includeDerivedProperties, IXmiElementOriginMap elementOriginMap, Uri currentFileUri) |
| 92 | + { |
| 93 | + if (elementOriginMap != null && currentFileUri != null) |
| 94 | + { |
| 95 | + var childSourceFile = elementOriginMap.GetSourceFile(childData.Id); |
| 96 | +
|
| 97 | + if (childSourceFile != null && childSourceFile != currentFileUri) |
| 98 | + { |
| 99 | + WriteHrefElement(xmlWriter, childData, elementName, childSourceFile, currentFileUri); |
| 100 | + return; |
| 101 | + } |
| 102 | + } |
| 103 | +
|
| 104 | + this.Write(xmlWriter, childData, elementName, includeDerivedProperties, elementOriginMap, currentFileUri); |
| 105 | + } |
| 106 | +
|
| 107 | + /// <summary> |
| 108 | + /// Writes a reference child element, checking origin map for cross-file href |
| 109 | + /// </summary> |
| 110 | + /// <param name="xmlWriter">The target <see cref="XmlWriter"/></param> |
| 111 | + /// <param name="childData">The child <see cref="IData"/> to write</param> |
| 112 | + /// <param name="elementName">The XML element name to use</param> |
| 113 | + /// <param name="elementOriginMap">The optional <see cref="IXmiElementOriginMap"/> for href reconstruction</param> |
| 114 | + /// <param name="currentFileUri">The <see cref="Uri"/> of the current output file for relative href computation</param> |
| 115 | + public void WriteReferenceElement(XmlWriter xmlWriter, IData childData, string elementName, IXmiElementOriginMap elementOriginMap, Uri currentFileUri) |
| 116 | + { |
| 117 | + if (elementOriginMap != null && currentFileUri != null) |
| 118 | + { |
| 119 | + var childSourceFile = elementOriginMap.GetSourceFile(childData.Id); |
| 120 | +
|
| 121 | + if (childSourceFile != null && childSourceFile != currentFileUri) |
| 122 | + { |
| 123 | + WriteHrefElement(xmlWriter, childData, elementName, childSourceFile, currentFileUri); |
| 124 | + return; |
| 125 | + } |
| 126 | + } |
| 127 | +
|
| 128 | + xmlWriter.WriteStartElement(elementName); |
| 129 | + xmlWriter.WriteAttributeString("xmi", "idref", null, childData.Id.ToString()); |
| 130 | + xmlWriter.WriteEndElement(); |
| 131 | + } |
| 132 | +
|
| 133 | + /// <summary> |
| 134 | + /// Writes an href element for cross-file references |
| 135 | + /// </summary> |
| 136 | + private static void WriteHrefElement(XmlWriter xmlWriter, IData childData, string elementName, Uri targetFile, Uri currentFile) |
| 137 | + { |
| 138 | + var relativePath = currentFile.MakeRelativeUri(targetFile); |
| 139 | + var href = $"{Uri.UnescapeDataString(relativePath.ToString())}#{childData.Id}"; |
| 140 | +
|
| 141 | + xmlWriter.WriteStartElement(elementName); |
| 142 | + xmlWriter.WriteAttributeString("href", href); |
| 143 | + xmlWriter.WriteEndElement(); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +
|
| 148 | +// ------------------------------------------------------------------------------------------------ |
| 149 | +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- |
| 150 | +// ------------------------------------------------------------------------------------------------ |
0 commit comments