diff --git a/ReqIFSharp.Tests/AttributeValueTests/ExternalObjectTestFixture.cs b/ReqIFSharp.Tests/AttributeValueTests/ExternalObjectTestFixture.cs new file mode 100644 index 0000000..26cc44d --- /dev/null +++ b/ReqIFSharp.Tests/AttributeValueTests/ExternalObjectTestFixture.cs @@ -0,0 +1,60 @@ +// ------------------------------------------------------------------------------------------------- +// +// +// Copyright 2017 RHEA System 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. +// +// +// ------------------------------------------------------------------------------------------------ + +using System.IO; + +namespace ReqIFSharp.Tests.AttributeValueTests +{ + using System; + using System.Collections.Generic; + using System.Text; + + using NUnit.Framework; + + /// + /// Suite of tests for the class. + /// + [TestFixture] + public class ExternalObjectTestFixture + { + private ExternalObject externalObject; + + [SetUp] + public void SetUp() + { + this.externalObject = new ExternalObject(null); + } + + [Test] + public void Verfify_that_expected_exceptions_are_raised_when_QueryLocalData_is_called() + { + Assert.Throws(() => this.externalObject.QueryLocalData(null)); + + var stream = new MemoryStream(); + + this.externalObject.Uri = "http"; + Assert.Throws(() => this.externalObject.QueryLocalData(stream)); + + this.externalObject.Uri = "somfile"; + this.externalObject.ReqIFFilePath = ""; + Assert.Throws(() => this.externalObject.QueryLocalData(stream)); + } + } +} diff --git a/ReqIFSharp.Tests/ReqIFDeSerializerTestFixture.cs b/ReqIFSharp.Tests/ReqIFDeSerializerTestFixture.cs index 9c62e92..29f2ca1 100644 --- a/ReqIFSharp.Tests/ReqIFDeSerializerTestFixture.cs +++ b/ReqIFSharp.Tests/ReqIFDeSerializerTestFixture.cs @@ -128,6 +128,29 @@ public void Verify_that_the_Tool_Extensions_are_deserialized_from_ProR_Traceabil Assert.That(toolExtension.InnerXml, Is.Not.Empty); } + [Test] + public void Verify_that_ExternalObjects_are_created_and_that_local_data_can_be_queried() + { + var reqifPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "requirements-and-objects.reqifz"); + var deserializer = new ReqIFDeserializer(); + + var reqIf = deserializer.Deserialize(reqifPath).First(); + + foreach (var specObject in reqIf.CoreContent.SpecObjects) + { + foreach (var attributeValueXhtml in specObject.Values.OfType()) + { + foreach (var externalObject in attributeValueXhtml.ExternalObjects) + { + var targetStream = new MemoryStream(); + externalObject.QueryLocalData(targetStream); + Assert.That(targetStream.Length, Is.GreaterThan(0)); + targetStream.Dispose(); + } + } + } + } + #if NETFRAMEWORK || NETCOREAPP3_1 [Test] public void VerifyThatAReqIFXMLDocumentCanBeDeserializedWithValidation() diff --git a/ReqIFSharp.Tests/ReqIFSharp.Tests.csproj b/ReqIFSharp.Tests/ReqIFSharp.Tests.csproj index 9f19cb9..43409e8 100644 --- a/ReqIFSharp.Tests/ReqIFSharp.Tests.csproj +++ b/ReqIFSharp.Tests/ReqIFSharp.Tests.csproj @@ -51,6 +51,9 @@ Always + + Always + Always diff --git a/ReqIFSharp.Tests/TestData/requirements-and-objects.reqifz b/ReqIFSharp.Tests/TestData/requirements-and-objects.reqifz new file mode 100644 index 0000000..206ab59 Binary files /dev/null and b/ReqIFSharp.Tests/TestData/requirements-and-objects.reqifz differ diff --git a/ReqIFSharp/AttributeValue/AttributeValueXHTML.cs b/ReqIFSharp/AttributeValue/AttributeValueXHTML.cs index 67db44a..c56183c 100644 --- a/ReqIFSharp/AttributeValue/AttributeValueXHTML.cs +++ b/ReqIFSharp/AttributeValue/AttributeValueXHTML.cs @@ -21,6 +21,7 @@ namespace ReqIFSharp { using System; + using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Xml; @@ -36,6 +37,7 @@ public class AttributeValueXHTML : AttributeValue /// public AttributeValueXHTML() { + this.ExternalObjects = new List(); } /// @@ -47,6 +49,7 @@ public AttributeValueXHTML() internal AttributeValueXHTML(SpecElementWithAttributes specElAt) : base(specElAt) { + this.ExternalObjects = new List(); } /// @@ -59,6 +62,8 @@ internal AttributeValueXHTML(SpecElementWithAttributes specElAt) internal AttributeValueXHTML(AttributeDefinitionXHTML attributeDefinition) : base(attributeDefinition) { + this.ExternalObjects = new List(); + this.OwningDefinition = attributeDefinition; } @@ -84,6 +89,11 @@ public override object ObjectValue set => this.TheValue = value.ToString(); } + /// + /// Gets the that may be present as xhtml:object in the XHTML content + /// + public List ExternalObjects { get; private set; } + /// /// Gets or sets the Reference to the attribute definition that relates the value to its data type. /// @@ -125,7 +135,7 @@ protected override void SetAttributeDefinition(AttributeDefinition attributeDefi /// Gets or sets a value indicating whether the attribute value is a simplified representation of the original value. /// public bool IsSimplified { get; set; } - + /// /// Generates a object from its XML representation. /// @@ -157,11 +167,69 @@ public override void ReadXml(XmlReader reader) if (subtree.MoveToContent() == XmlNodeType.Element && reader.LocalName == "THE-VALUE") { this.TheValue = subtree.ReadInnerXml().Trim(); + + if (this.TheValue.Contains("xhtml:object data")) + { + var externalObjects = this.CreateExternalObjects(this.TheValue); + this.ExternalObjects.AddRange(externalObjects); + } } } } } + /// + /// Creates s from provided XHTML + /// + /// + /// The XHTML that may contain + /// + /// + /// An which may be empty + /// + private IEnumerable CreateExternalObjects(string xhtml) + { + var result = new List(); + + var xmlDocument = new XmlDocument(); + xmlDocument.LoadXml(xhtml); + + var xmlNodeList = xmlDocument.GetElementsByTagName("xhtml:object").OfType() ; + + foreach (var xmlElement in xmlNodeList) + { + var mimeTypeAttribute = xmlElement.GetAttribute("type"); + var uriAttribute = xmlElement.GetAttribute("data"); + if (!string.IsNullOrEmpty(uriAttribute)) + { + uriAttribute = Uri.UnescapeDataString(uriAttribute); + } + + var heightAttribute = xmlElement.GetAttribute("height"); + var widthAttribute = xmlElement.GetAttribute("width"); + + var externalObject = new ExternalObject(this) + { + MimeType = mimeTypeAttribute, + Uri = uriAttribute + }; + + if (!string.IsNullOrEmpty(heightAttribute) && int.TryParse(heightAttribute, out int height)) + { + externalObject.Height = height; + } + + if (!string.IsNullOrEmpty(widthAttribute) && int.TryParse(widthAttribute, out int width)) + { + externalObject.Width = width; + } + + result.Add(externalObject); + } + + return result; + } + /// /// Converts a object into its XML representation. /// diff --git a/ReqIFSharp/AttributeValue/ExternalObject.cs b/ReqIFSharp/AttributeValue/ExternalObject.cs new file mode 100644 index 0000000..18e517b --- /dev/null +++ b/ReqIFSharp/AttributeValue/ExternalObject.cs @@ -0,0 +1,135 @@ +// ------------------------------------------------------------------------------------------------- +// +// +// Copyright 2017 RHEA System 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. +// +// +// ------------------------------------------------------------------------------------------------ + +namespace ReqIFSharp +{ + using System; + using System.IO; + using System.IO.Compression; + + /// + /// External objects are referenced binary objects that are referenced using the XHTML object element from the XHTML Object Module. + /// The location of an external object MUST be specified via the data attribute which contains either + /// a) a URL relative to the location of the exchange XML document, or + /// b) an absolute URL. + /// + /// + /// The specification for the XTHML object element defines several XML attributes. For ReqIF, only a subset of these attributes is relevant and used. + /// + public class ExternalObject + { + /// + /// Initializes a new instance of the class. + /// + /// + /// The owning + /// + public ExternalObject(AttributeValueXHTML attributeValueXhtml) + { + this.Owner = attributeValueXhtml; + } + + /// + /// Gets the owning + /// + public AttributeValueXHTML Owner { get; private set; } + + /// + /// Gets or sets the Uri of the , this may be a relative uri or an absolute uri + /// + public string Uri { get; set; } + + /// + /// Gets or sets the MimeType of the external object represented as a string + /// + public string MimeType { get; set; } + + /// + /// Gets or sets the height of the external object in case it is an image + /// + public int? Height { get; set; } + + /// + /// Gets or sets the width of the external object in case it is an image + /// + public int? Width { get; set; } + + /// + /// Gets or sets the path of the reqif file that contains the data from which this + /// object was created/ + /// + internal string ReqIFFilePath { get; set; } + + /// + /// Asserts whether the external object should be queried from the reqifz file or from + /// another (absolute) location such as a resource available via HTTP or HTTPS + /// + /// + public bool IsDataLocal() + { + return !this.Uri.StartsWith("http"); + } + + /// + /// Queries the local data from the reqifz file and writes the data to the provided target + /// + /// + /// The target to which the data is written + /// + /// + /// Thrown when the is null + /// + /// + /// thrown when the is an absolute uri and not a relative Uri or + /// when the reqifz file is unknown + /// + public void QueryLocalData(Stream target) + { + if (target == null) + { + throw new ArgumentNullException(nameof(target), "The target stream may not be null"); + } + + if (!this.IsDataLocal()) + { + throw new InvalidOperationException("The Uri of the External Object is not a relative Uri that can be found in the reqifz file"); + } + + if (string.IsNullOrEmpty(this.ReqIFFilePath)) + { + throw new InvalidOperationException("The local data cannot be queried, the path to the reqifz file is unknown"); + } + + using (var reader = new FileStream(this.ReqIFFilePath, FileMode.Open)) + { + using (var archive = new ZipArchive(reader, ZipArchiveMode.Read)) + { + var zipArchiveEntry = archive.GetEntry(this.Uri); + if (zipArchiveEntry != null) + { + var sourceStream = zipArchiveEntry.Open(); + sourceStream.CopyTo(target); + sourceStream.Dispose(); + } + } + } + } + } +} diff --git a/ReqIFSharp/ReqIFDeserializer.cs b/ReqIFSharp/ReqIFDeserializer.cs index f4c42b6..9a1ef61 100644 --- a/ReqIFSharp/ReqIFDeserializer.cs +++ b/ReqIFSharp/ReqIFDeserializer.cs @@ -133,7 +133,11 @@ private IEnumerable NonValidatingDeserialization(string xmlFilePath) { using (xmlReader = XmlReader.Create(zipArchiveEntry.Open())) { - reqifs.Add((ReqIF)xmlSerializer.Deserialize(xmlReader)); + var reqif = (ReqIF) xmlSerializer.Deserialize(xmlReader); + + this.UpdateExternalObjectsReqIfFilePath(reqif, xmlFilePath); + + reqifs.Add(reqif); } } @@ -193,6 +197,9 @@ private IEnumerable NonValidatingDeserialization(string xmlFilePath) using (xmlReader = XmlReader.Create(zipArchiveEntry.Open())) { var reqif = (ReqIF) xmlSerializer.Deserialize(xmlReader); + + this.UpdateExternalObjectsReqIfFilePath(reqif, xmlFilePath); + reqifs.Add(reqif); } } @@ -295,5 +302,29 @@ private IEnumerable ValidatingDeserialization(string xmlFilePath, Valida } } #endif + + /// + /// Updates the property with the path to the source reqifz file + /// that contains the reqif xml files and external objects + /// + /// + /// The object that contains the s + /// + /// + /// The path to the source reqifz file. + /// + private void UpdateExternalObjectsReqIfFilePath(ReqIF reqIf, string path) + { + foreach (var specObject in reqIf.CoreContent.SpecObjects) + { + foreach (var attributeValueXhtml in specObject.Values.OfType() ) + { + foreach (var externalObject in attributeValueXhtml.ExternalObjects) + { + externalObject.ReqIFFilePath = path; + } + } + } + } } } diff --git a/ReqIFSharp/SpecElementWithAttributes/SpecElementWithAttributes.cs b/ReqIFSharp/SpecElementWithAttributes/SpecElementWithAttributes.cs index efd3675..b90a259 100644 --- a/ReqIFSharp/SpecElementWithAttributes/SpecElementWithAttributes.cs +++ b/ReqIFSharp/SpecElementWithAttributes/SpecElementWithAttributes.cs @@ -114,7 +114,7 @@ public SpecType SpecType public override void ReadXml(XmlReader reader) { base.ReadXml(reader); - + using (var specElementWithAttributesReader = reader.ReadSubtree()) { while (specElementWithAttributesReader.Read())