// Copyright (C) Stichting Deltares 2023. All rights reserved. // // This file is part of the Dam Engine. // // The Dam Engine is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Linq; using System.Reflection; using System.Xml.Linq; namespace Deltares.DamEngine.Calculators.KernelWrappers.Assemblers { /// /// Default implementation for the interface /// /// /// This base class inherits from this class just fixes /// the dto type to XElement /// public abstract class DtoDocumentAssembler : IDtoAssembler where TDomainInputObject : new() where TDomainOutputObject : new() { /// /// Initializes a new instance of the DtoDocumentAssembler class /// /// Sets the element name /// The text reader stream for reading in ther xsd file which is embedded as an resource public DtoDocumentAssembler(string elementName, string elementNamespace, string embeddedXsdPath) : this(elementName, elementNamespace, null, null) {} /// /// Initializes a new instance of the DtoDocumentAssembler class /// /// Sets the element name /// The text reader stream for reading in ther xsd file which is embedded as an resource public DtoDocumentAssembler(string elementName, string elementNamespace, DtoAssembler inputAssembler, DtoAssembler outputAssembler) { if (elementName == null) { throw new ArgumentNullException("elementName"); } if (elementNamespace == null) { throw new ArgumentNullException("elementNamespace"); } ElementName = elementName; ElementNamespace = elementNamespace; OutputAssembler = outputAssembler; InputAssembler = inputAssembler; //Stream xsdStream = Common.GetEmbeddedFile(Assembly.GetExecutingAssembly(), embeddedXsdPath); //this.Schema = XDocument.Load(new StreamReader(xsdStream)); } /// /// Gets the xml element name /// public string ElementName { get; } /// /// Gets or sets the namespace value for the element /// public string ElementNamespace { get; } /// /// Gets the xml schema (if any) /// public XDocument Schema { get; private set; } /// /// /// public DtoAssembler InputAssembler { get; set; } /// /// /// public DtoAssembler OutputAssembler { get; set; } public TDomainOutputObject CreateOutputObject(string fileName) { return CreateOutputObject(CreateDocument(fileName)); } public TDomainInputObject CreateInputObject(string fileName) { return CreateInputObject(CreateDocument(fileName)); } private XDocument CreateDocument(string fileName) { if (!fileName.HasValidStringValue()) { throw new ArgumentNullException("fileName"); } XDocument doc = XDocument.Load(fileName); return doc; } #region IDtoAssembler Members public virtual TDomainOutputObject CreateOutputObject(XDocument dtoObject) { if (dtoObject == null) { throw new ArgumentNullException("dtoObject"); } if (OutputAssembler != null) { // the document should contain the element only once! XElement el = (from x in dtoObject.Descendants() where x.Name.LocalName == OutputAssembler.ElementName select x).Single(); return OutputAssembler.CreateDomainObject(el); } return default; } public virtual TDomainInputObject CreateInputObject(XDocument dtoObject) { if (dtoObject == null) { throw new ArgumentNullException("dtoObject"); } if (InputAssembler != null) { // the document should contain the element only once! XElement el = (from x in dtoObject.Descendants() where x.Name.LocalName == InputAssembler.ElementName select x).Single(); return InputAssembler.CreateDomainObject(el); } return default; } public virtual XDocument CreateDataTransferObject(TDomainInputObject dtoObject) { var doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); // add the file identification to the document var fileId = new FileIdentification(Assembly.GetCallingAssembly()); var fileIdAssembler = new FileIdentificationAssembler(); XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; XNamespace tns = ElementNamespace; XNamespace tns1 = fileIdAssembler.ElementNamespace; var root = new XElement(tns + ElementName, new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName), new XAttribute(XNamespace.Xmlns + "tns", tns.NamespaceName), new XAttribute(XNamespace.Xmlns + "tns1", tns1.NamespaceName)); doc.Add(root); doc.Root.Add(fileId.ToDto(fileIdAssembler, tns)); if (InputAssembler != null) { XNamespace tns2 = InputAssembler.ElementNamespace; root.Add(new XAttribute(XNamespace.Xmlns + "tns2", tns2.NamespaceName)); doc.Root.Add(dtoObject.ToDto(InputAssembler, tns)); } return doc; } #endregion } }