Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions ReqIFSharp.Tests/AttributeValueTests/ExternalObjectTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="ExternalObjectTestFixture.cs" company="RHEA System S.A.">
//
// 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.
//
// </copyright>
// ------------------------------------------------------------------------------------------------

using System.IO;

namespace ReqIFSharp.Tests.AttributeValueTests
{
using System;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;

/// <summary>
/// Suite of tests for the <see cref="ExternalObject"/> class.
/// </summary>
[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<ArgumentNullException>(() => this.externalObject.QueryLocalData(null));

var stream = new MemoryStream();

this.externalObject.Uri = "http";
Assert.Throws<InvalidOperationException>(() => this.externalObject.QueryLocalData(stream));

this.externalObject.Uri = "somfile";
this.externalObject.ReqIFFilePath = "";
Assert.Throws<InvalidOperationException>(() => this.externalObject.QueryLocalData(stream));
}
}
}
23 changes: 23 additions & 0 deletions ReqIFSharp.Tests/ReqIFDeSerializerTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AttributeValueXHTML>())
{
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()
Expand Down
3 changes: 3 additions & 0 deletions ReqIFSharp.Tests/ReqIFSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<None Update="TestData\ProR_Traceability-Template-v1.0.reqif">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\requirements-and-objects.reqifz">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestData\sample_debug.reqif">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Binary file not shown.
70 changes: 69 additions & 1 deletion ReqIFSharp/AttributeValue/AttributeValueXHTML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace ReqIFSharp
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml;
Expand All @@ -36,6 +37,7 @@ public class AttributeValueXHTML : AttributeValue
/// </summary>
public AttributeValueXHTML()
{
this.ExternalObjects = new List<ExternalObject>();
}

/// <summary>
Expand All @@ -47,6 +49,7 @@ public AttributeValueXHTML()
internal AttributeValueXHTML(SpecElementWithAttributes specElAt)
: base(specElAt)
{
this.ExternalObjects = new List<ExternalObject>();
}

/// <summary>
Expand All @@ -59,6 +62,8 @@ internal AttributeValueXHTML(SpecElementWithAttributes specElAt)
internal AttributeValueXHTML(AttributeDefinitionXHTML attributeDefinition)
: base(attributeDefinition)
{
this.ExternalObjects = new List<ExternalObject>();

this.OwningDefinition = attributeDefinition;
}

Expand All @@ -84,6 +89,11 @@ public override object ObjectValue
set => this.TheValue = value.ToString();
}

/// <summary>
/// Gets the <see cref="List{ExternalObject}"/> that may be present as xhtml:object in the XHTML content
/// </summary>
public List<ExternalObject> ExternalObjects { get; private set; }

/// <summary>
/// Gets or sets the Reference to the attribute definition that relates the value to its data type.
/// </summary>
Expand Down Expand Up @@ -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.
/// </summary>
public bool IsSimplified { get; set; }

/// <summary>
/// Generates a <see cref="AttributeValueXHTML"/> object from its XML representation.
/// </summary>
Expand Down Expand Up @@ -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);
}
}
}
}
}

/// <summary>
/// Creates <see cref="ExternalObject"/>s from provided XHTML
/// </summary>
/// <param name="xhtml">
/// The XHTML that may contain <see cref="ExternalObject"/>
/// </param>
/// <returns>
/// An <see cref="IEnumerable{ExternalObject}"/> which may be empty
/// </returns>
private IEnumerable<ExternalObject> CreateExternalObjects(string xhtml)
{
var result = new List<ExternalObject>();

var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xhtml);

var xmlNodeList = xmlDocument.GetElementsByTagName("xhtml:object").OfType<XmlElement>() ;

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;
}

/// <summary>
/// Converts a <see cref="AttributeValueXHTML"/> object into its XML representation.
/// </summary>
Expand Down
135 changes: 135 additions & 0 deletions ReqIFSharp/AttributeValue/ExternalObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="ExternalObject.cs" company="RHEA System S.A.">
//
// Copyright 2017 RHEA System S.A.
Comment thread
samatstariongroup marked this conversation as resolved.
Resolved
//
// 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>
// ------------------------------------------------------------------------------------------------

namespace ReqIFSharp
{
using System;
using System.IO;
using System.IO.Compression;

/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// The specification for the XTHML object element defines several XML attributes. For ReqIF, only a subset of these attributes is relevant and used.
/// </remarks>
public class ExternalObject
{
/// <summary>
/// Initializes a new instance of the <see cref="ExternalObject"/> class.
/// </summary>
/// <param name="attributeValueXhtml">
/// The owning <see cref="AttributeValueXHTML"/>
/// </param>
public ExternalObject(AttributeValueXHTML attributeValueXhtml)
{
this.Owner = attributeValueXhtml;
}

/// <summary>
/// Gets the owning <see cref="AttributeValueXHTML"/>
/// </summary>
public AttributeValueXHTML Owner { get; private set; }

/// <summary>
/// Gets or sets the Uri of the <see cref="ExternalObject"/>, this may be a relative uri or an absolute uri
/// </summary>
public string Uri { get; set; }

/// <summary>
/// Gets or sets the MimeType of the external object represented as a string
/// </summary>
public string MimeType { get; set; }

/// <summary>
/// Gets or sets the height of the external object in case it is an image
/// </summary>
public int? Height { get; set; }

/// <summary>
/// Gets or sets the width of the external object in case it is an image
/// </summary>
public int? Width { get; set; }

/// <summary>
/// Gets or sets the path of the reqif file that contains the data from which this <see cref="AttributeValueXHTML"/>
/// object was created/
/// </summary>
internal string ReqIFFilePath { get; set; }

/// <summary>
/// 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
/// </summary>
/// <returns></returns>
public bool IsDataLocal()
{
return !this.Uri.StartsWith("http");
}

/// <summary>
/// Queries the local data from the reqifz file and writes the data to the provided target <see cref="Stream"/>
/// </summary>
/// <param name="target">
/// The target <see cref="Stream"/> to which the data is written
/// </param>
/// <exception cref="ArgumentNullException">
/// Thrown when the <paramref name="target"/> is null
/// </exception>
/// <exception cref="InvalidOperationException">
/// thrown when the <see cref="Uri"/> is an absolute uri and not a relative Uri or
/// when the reqifz file is unknown
/// </exception>
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))
Comment thread
samatstariongroup marked this conversation as resolved.
Resolved
{
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();
}
}
}
}
}
}
Loading